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: func _ready() -> void:
request() request()
request_completed.connect(func(resp: HTTPResponse) -> void: 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]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("Body:")
print(resp.body.get_string_from_utf8()) print(resp.body.get_string_from_utf8())
) )
@ -47,7 +47,7 @@ func request(path: String = "") -> void:
query = path query = path
if port == 443: if port == 443:
ssl = true 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 = StreamPeerTCP.new()
client.connect_to_host(host, port) client.connect_to_host(host, port)
state = State.CONNECTING state = State.CONNECTING
@ -70,12 +70,12 @@ func _process(_delta: float) -> void:
if ssl: if ssl:
_handle_ssl() _handle_ssl()
return 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 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) _send_request(client)
elif client.get_status() == StreamPeerTCP.Status.STATUS_CONNECTED and state == State.REQUESTING: 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 state = State.RESPONSE
_handle_response(sclient if ssl else client) _handle_response(sclient if ssl else client)
elif client.get_status() == StreamPeerTCP.Status.STATUS_CONNECTED and state == State.RESPONSE: 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: func _send_request(peer: StreamPeer) -> void:
var request_str := "GET %s://%s:%d/%s HTTP/1.1\r\n" % [ var proto = "http"
"https" if ssl else "http", if ssl:
host, port, query 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): if (ssl and port == 443) or (not ssl and port == 80):
request_str += "Host: %s\r\n" % host request_str += "Host: %s\r\n" % host
else: else:
@ -101,11 +102,13 @@ func _send_request(peer: StreamPeer) -> void:
ver["major"], ver["minor"], ver["patch"], ver["major"], ver["minor"], ver["patch"],
ver["status"], ver["build"] ver["status"], ver["build"]
] ]
request_str += "User-Agent: GodotEngine/%s (%s)" % [ request_str += "User-Agent: GodotEngine/%s (%s)\r\n" % [
version, version,
OS.get_name() OS.get_name()
] ]
request_str += "Accept: %s\r\n\r\n" % accept 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()) peer.put_data(request_str.to_utf8_buffer())
func _handle_response(peer: StreamPeer) -> void: func _handle_response(peer: StreamPeer) -> void:
@ -152,7 +155,7 @@ func _handle_error(error: Variant) -> void:
func _handle_ssl() -> void: func _handle_ssl() -> void:
if sclient == null: 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 = StreamPeerTLS.new()
sclient.connect_to_stream(client, host) sclient.connect_to_stream(client, host)