From 3d2fd37191ab837b4312f2f1a7bf3add2b679288 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Sun, 3 Jul 2022 23:37:47 +0300 Subject: [PATCH 1/3] make the endpoints at request time --- Main.gd | 31 +++++++------------------------ addons/http_server/http_server.gd | 5 +---- 2 files changed, 8 insertions(+), 28 deletions(-) diff --git a/Main.gd b/Main.gd index f697e15..4a0277b 100644 --- a/Main.gd +++ b/Main.gd @@ -17,26 +17,8 @@ func _start_server(port: int = 3001) -> void: return _server = HTTPServer.new() - var dir := Directory.new() - if dir.open(server_dir) == OK: - if dir.list_dir_begin() != OK: - # TODO: show error to user here - return - var file_name := dir.get_next() - while file_name != "": - if !dir.current_is_dir(): - if file_name.get_extension() == "import": - file_name = dir.get_next() - continue - -# print(file_name) - - _server.endpoint(HTTPServer.Method.GET, "/%s" % file_name, funcref(self, "_serve_file"), [file_name]) - - file_name = dir.get_next() - - _server.endpoint(HTTPServer.Method.GET, "/", funcref(self, "_serve_file"), ["index.html"]) - + _server.endpoint(HTTPServer.Method.GET, "/", funcref(self, "_serve_file"), ["index.html"]) # this is still needed to make 'localhost:port/' to point to index.html + _server.fallback(funcref(self, "_serve_file")) if _server.listen(port) != OK: # TODO: show error to user here @@ -64,11 +46,11 @@ func _process(_delta: float) -> void: func _serve_file(_request: HTTPServer.Request, response: HTTPServer.Response, binds: Array) -> void: var file_name: String = binds[0] as String - +# TODO: deprecate binds in favor of using the Request.endpoint prop, as for this app's purposes it would be the same as the file name var f := File.new() var success = f.open(server_dir.plus_file(file_name), File.READ) - if success == OK: + if success == OK: # TODO: handle other errors like file not found var mime := mime_types.get(file_name) response.type(mime) @@ -77,8 +59,9 @@ func _serve_file(_request: HTTPServer.Request, response: HTTPServer.Response, bi response.data(data) else: - response.header("content-type", "text/plain") - response.data("500 - Read Error") + response.type(mime_types.get("txt")) + response.status(500) + response.data("Internal Server Error") func _on_ServerUI_start_server_button_pressed(port: int, new_dir: String) -> void: diff --git a/addons/http_server/http_server.gd b/addons/http_server/http_server.gd index 23f3bf5..ef8cf74 100644 --- a/addons/http_server/http_server.gd +++ b/addons/http_server/http_server.gd @@ -160,10 +160,7 @@ func __process_request(method: String, endpoint: String, headers: Dictionary, bo "[INF] Recieved request method: %s, endpoint: %s" % [method, endpoint] ) - if !binds: - endpoint_func.call_func(request, response) - else: - endpoint_func.call_func(request, response, binds) + endpoint_func.call_func(request, response, binds if binds else [request.endpoint()]) return response From e44cac165a20b8914b75093d62bb71254223e883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Mon, 4 Jul 2022 06:44:00 +0300 Subject: [PATCH 2/3] sync http-server with upstream and properly check for endpoints --- Main.gd | 10 +++++----- addons/http_server/http_server.gd | 27 ++++++++++----------------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/Main.gd b/Main.gd index 4a0277b..de2bd3c 100644 --- a/Main.gd +++ b/Main.gd @@ -17,8 +17,7 @@ func _start_server(port: int = 3001) -> void: return _server = HTTPServer.new() - _server.endpoint(HTTPServer.Method.GET, "/", funcref(self, "_serve_file"), ["index.html"]) # this is still needed to make 'localhost:port/' to point to index.html - _server.fallback(funcref(self, "_serve_file")) + _server.endpoint(HTTPServer.Method.GET, "/", funcref(self, "_serve_file")) if _server.listen(port) != OK: # TODO: show error to user here @@ -44,9 +43,10 @@ func _process(_delta: float) -> void: return -func _serve_file(_request: HTTPServer.Request, response: HTTPServer.Response, binds: Array) -> void: - var file_name: String = binds[0] as String -# TODO: deprecate binds in favor of using the Request.endpoint prop, as for this app's purposes it would be the same as the file name +func _serve_file(request: HTTPServer.Request, response: HTTPServer.Response) -> void: + var file_name: String = request.endpoint() + if file_name == "/": # if the request is for root, serve index + file_name = "index.html" var f := File.new() var success = f.open(server_dir.plus_file(file_name), File.READ) diff --git a/addons/http_server/http_server.gd b/addons/http_server/http_server.gd index ef8cf74..9acd653 100644 --- a/addons/http_server/http_server.gd +++ b/addons/http_server/http_server.gd @@ -13,7 +13,7 @@ const Status = preload("res://addons/http_server/status.gd") var __endpoints: Dictionary = { # key: [Int, String], array with 0 index representing method, 1 index representing endpoint - # value: [FuncRef, Array], index 0 = reference to function to call, index 1 = binds to pass to func + # value: FuncRef, reference to function to call } var __fallback: FuncRef = null var __server: TCP_Server = null @@ -32,7 +32,7 @@ func endpoint(type: int, endpoint: String, function: FuncRef, binds: Array = []) ) return - __endpoints[endpoint_hash] = [function, binds] + __endpoints[endpoint_hash] = function func fallback(function: FuncRef) -> void: @@ -121,22 +121,15 @@ func __process_request(method: String, endpoint: String, headers: Dictionary, bo var endpoint_func: FuncRef = null var endpoint_parts: PoolStringArray = endpoint.split("/", false) - var binds - # special case for if endpoint is just root - if endpoint == "/": - var endpoint_hash: Array = [type, "/"] + while !endpoint_func: + var endpoint_hash: Array = [type, "/" + endpoint_parts.join("/")] if __endpoints.has(endpoint_hash): - endpoint_func = __endpoints[endpoint_hash][0] - binds = __endpoints[endpoint_hash][1] - else: - while (!endpoint_func && !endpoint_parts.empty()): - var endpoint_hash: Array = [type, "/" + endpoint_parts.join("/")] - if __endpoints.has(endpoint_hash): - endpoint_func = __endpoints[endpoint_hash][0] - binds = __endpoints[endpoint_hash][1] - else: - endpoint_parts.remove(endpoint_parts.size() - 1) + endpoint_func = __endpoints[endpoint_hash] + elif endpoint_parts.empty(): + break + else: + endpoint_parts.remove(endpoint_parts.size() - 1) if !endpoint_func: @@ -160,7 +153,7 @@ func __process_request(method: String, endpoint: String, headers: Dictionary, bo "[INF] Recieved request method: %s, endpoint: %s" % [method, endpoint] ) - endpoint_func.call_func(request, response, binds if binds else [request.endpoint()]) + endpoint_func.call_func(request, response) return response From c0759f0cbe55d7055d47024e25fa7cefa6d6366a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Tue, 23 Aug 2022 11:47:34 +0300 Subject: [PATCH 3/3] add vscode folder to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 4f48ad7..08ca32e 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,6 @@ export_presets.cfg # Mono-specific ignores .mono/ data_*/ + +# VSCode config folder +.vscode/