Fixed bugs

This commit is contained in:
Mario Steele 2025-04-30 02:11:00 -05:00
parent 0321a1b77e
commit 03c0c804b0

View file

@ -33,9 +33,9 @@ enum State {
func _ready() -> void:
request()
request_completed.connect(func(resp: HTTPResponse) -> void:
print_rich("[color=green][b]Request Completed:[/b] Status ", resp.code, "[/color]\n")
print_rich("[color=green][b]Request Completed:[/b] Status ", resp.code, "[/color]")
print_rich("[color=cyan]Headers:\n[/color]")
print_rich("[color=cyan]", JSON.stringify(resp.headers, "\t"), "[/color]\n")
print_rich("[color=cyan]", JSON.stringify(resp.headers, "\t"), "[/color]")
print("Body:")
print(resp.body.get_string_from_utf8())
)
@ -47,7 +47,7 @@ func request(path: String = "") -> void:
query = path
if port == 443:
ssl = true
print_rich("[color=yellow]Starting connection to %s:%d[/color]\n" % [host, port])
print_rich("[color=yellow]Starting connection to %s:%d[/color]" % [host, port])
client = StreamPeerTCP.new()
client.connect_to_host(host, port)
state = State.CONNECTING
@ -70,12 +70,12 @@ func _process(_delta: float) -> void:
if ssl:
_handle_ssl()
return
print_rich("[color=green]Connected to %s:%d[/color]\n" % [host, port])
print_rich("[color=green]Connected to %s:%d[/color]" % [host, port])
state = State.REQUESTING
print_rich("[color=green]Sending Request for %s from server %s:%d[/color]\n" % [query, host, port])
print_rich("[color=green]Sending Request for %s from server %s:%d[/color]" % [query, host, port])
_send_request(client)
elif client.get_status() == StreamPeerTCP.Status.STATUS_CONNECTED and state == State.REQUESTING:
print_rich("[color=green]Reading response from %s:%d[/color]\n" % [host, port])
print_rich("[color=green]Reading response from %s:%d[/color]" % [host, port])
state = State.RESPONSE
_handle_response(sclient if ssl else client)
elif client.get_status() == StreamPeerTCP.Status.STATUS_CONNECTED and state == State.RESPONSE:
@ -86,10 +86,11 @@ func _process(_delta: float) -> void:
func _send_request(peer: StreamPeer) -> void:
var request_str := "GET %s://%s:%d/%s HTTP/1.1\r\n" % [
"https" if ssl else "http",
host, port, query
]
var proto = "http"
if ssl:
proto = "https"
var request_str := "GET %s://%s:%d%s HTTP/1.1\r\n" % [proto, host, port, query]
if (ssl and port == 443) or (not ssl and port == 80):
request_str += "Host: %s\r\n" % host
else:
@ -101,11 +102,13 @@ func _send_request(peer: StreamPeer) -> void:
ver["major"], ver["minor"], ver["patch"],
ver["status"], ver["build"]
]
request_str += "User-Agent: GodotEngine/%s (%s)" % [
request_str += "User-Agent: GodotEngine/%s (%s)\r\n" % [
version,
OS.get_name()
]
request_str += "Accept: %s\r\n\r\n" % accept
print_rich("[color=cyan]Request Headers:",request_str,"[/color]--EOH--")
peer.put_data(request_str.to_utf8_buffer())
func _handle_response(peer: StreamPeer) -> void:
@ -152,7 +155,7 @@ func _handle_error(error: Variant) -> void:
func _handle_ssl() -> void:
if sclient == null:
print_rich("[color=yellow]SSL Connection requested, negotiating SSL Connection with %s:%d[/color]\n" % [host,port])
print_rich("[color=yellow]SSL Connection requested, negotiating SSL Connection with %s:%d[/color]" % [host,port])
sclient = StreamPeerTLS.new()
sclient.connect_to_stream(client, host)