sync http-server with upstream and properly check for endpoints
This commit is contained in:
		
							
								
								
									
										10
									
								
								Main.gd
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								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)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user