Compare commits
No commits in common. "8cbfffba94daca682affa70d0c9328340aff3fbe" and "a20397215786a1de73dd9e4af2648d4473218eb1" have entirely different histories.
8cbfffba94
...
a203972157
12
Main.gd
12
Main.gd
@ -29,7 +29,7 @@ func _start_server(port: int = 3001) -> void:
|
|||||||
file_name = dir.get_next()
|
file_name = dir.get_next()
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# print(file_name)
|
print(file_name)
|
||||||
|
|
||||||
_server.endpoint(HTTPServer.Method.GET, "/%s" % file_name, funcref(self, "_serve_file"), [file_name])
|
_server.endpoint(HTTPServer.Method.GET, "/%s" % file_name, funcref(self, "_serve_file"), [file_name])
|
||||||
|
|
||||||
@ -64,15 +64,19 @@ func _process(_delta: float) -> void:
|
|||||||
|
|
||||||
func _serve_file(_request: HTTPServer.Request, response: HTTPServer.Response, binds: Array) -> void:
|
func _serve_file(_request: HTTPServer.Request, response: HTTPServer.Response, binds: Array) -> void:
|
||||||
var file_name: String = binds[0] as String
|
var file_name: String = binds[0] as String
|
||||||
|
print(file_name)
|
||||||
|
|
||||||
var f := File.new()
|
var f := File.new()
|
||||||
var success = f.open("res://server_files/%s" % file_name, File.READ)
|
var success = f.open("res://server_files/%s" % file_name, File.READ)
|
||||||
|
|
||||||
if success == OK:
|
if success == OK:
|
||||||
var mime := mime_types.get(file_name)
|
|
||||||
response.type(mime)
|
|
||||||
|
|
||||||
var data = f.get_buffer(f.get_len())
|
var mime := mime_types.get(file_name)
|
||||||
|
response.header("content-type", mime.full_type)
|
||||||
|
|
||||||
|
# variant
|
||||||
|
# warning-ignore:incompatible_ternary
|
||||||
|
var data = f.get_as_text() if mime.type == 'text' else f.get_buffer(f.get_len())
|
||||||
|
|
||||||
response.data(data)
|
response.data(data)
|
||||||
|
|
||||||
|
@ -106,7 +106,7 @@ func __process_connection(connection: StreamPeerTCP) -> void:
|
|||||||
body = PoolStringArray(body_parts).join("\r\n")
|
body = PoolStringArray(body_parts).join("\r\n")
|
||||||
|
|
||||||
var response: Response = __process_request(method, endpoint, headers, body)
|
var response: Response = __process_request(method, endpoint, headers, body)
|
||||||
connection.put_data(response.get_data())
|
connection.put_data(response.to_utf8())
|
||||||
|
|
||||||
|
|
||||||
func __process_request(method: String, endpoint: String, headers: Dictionary, body: String) -> Response:
|
func __process_request(method: String, endpoint: String, headers: Dictionary, body: String) -> Response:
|
||||||
|
@ -11,7 +11,6 @@ var __headers: Dictionary = {
|
|||||||
# value: Variant, header value
|
# value: Variant, header value
|
||||||
}
|
}
|
||||||
var __status: int = 200
|
var __status: int = 200
|
||||||
var __type: MimeTypeHelper.MimeType
|
|
||||||
|
|
||||||
|
|
||||||
# Public methods
|
# Public methods
|
||||||
@ -33,10 +32,6 @@ func status(status: int) -> void:
|
|||||||
__status = status
|
__status = status
|
||||||
|
|
||||||
|
|
||||||
func type(type: MimeTypeHelper.MimeType) -> void:
|
|
||||||
__type = type
|
|
||||||
|
|
||||||
|
|
||||||
func to_utf8() -> PoolByteArray:
|
func to_utf8() -> PoolByteArray:
|
||||||
var content = PoolStringArray()
|
var content = PoolStringArray()
|
||||||
|
|
||||||
@ -50,7 +45,6 @@ func to_utf8() -> PoolByteArray:
|
|||||||
data = JSON.print(data)
|
data = JSON.print(data)
|
||||||
|
|
||||||
__headers['content-length'] = len(data)
|
__headers['content-length'] = len(data)
|
||||||
__headers["content-type"] = "application/octet-stream" if !__type else __type.full_type
|
|
||||||
|
|
||||||
for header in __headers:
|
for header in __headers:
|
||||||
content.append("%s: %s" % [header, String(__headers[header])])
|
content.append("%s: %s" % [header, String(__headers[header])])
|
||||||
@ -61,48 +55,3 @@ func to_utf8() -> PoolByteArray:
|
|||||||
content.append(data)
|
content.append(data)
|
||||||
|
|
||||||
return content.join("\r\n").to_utf8()
|
return content.join("\r\n").to_utf8()
|
||||||
|
|
||||||
|
|
||||||
func get_data() -> PoolByteArray:
|
|
||||||
var res = __response_headers()
|
|
||||||
|
|
||||||
var data = __data
|
|
||||||
if !data:
|
|
||||||
return res
|
|
||||||
|
|
||||||
var type: MimeTypeHelper.MimeType = __type
|
|
||||||
if !type:
|
|
||||||
type = MimeTypeHelper.MimeType.new()
|
|
||||||
|
|
||||||
if data is String: # else, assume data is PoolByteArray
|
|
||||||
data = data.to_utf8()
|
|
||||||
|
|
||||||
res.append_array(data)
|
|
||||||
|
|
||||||
return res
|
|
||||||
|
|
||||||
|
|
||||||
# Private methods
|
|
||||||
|
|
||||||
func __response_headers() -> PoolByteArray:
|
|
||||||
var res = PoolStringArray()
|
|
||||||
|
|
||||||
res.append(Status.code_to_status_line(__status))
|
|
||||||
|
|
||||||
var data = __data
|
|
||||||
if !data:
|
|
||||||
data = Status.code_to_description(__status)
|
|
||||||
|
|
||||||
__headers["content-length"] = len(data)
|
|
||||||
|
|
||||||
__headers["content-type"] = "application/octet-stream" if !__type else __type.full_type
|
|
||||||
|
|
||||||
for header in __headers:
|
|
||||||
res.append("%s: %s" % [header, String(__headers[header])])
|
|
||||||
|
|
||||||
res.append("")
|
|
||||||
|
|
||||||
var s = res.join("\r\n")
|
|
||||||
s = s + "\r\n"
|
|
||||||
|
|
||||||
return s.to_utf8()
|
|
||||||
|
Loading…
Reference in New Issue
Block a user