Compare commits
No commits in common. "80bc3e4a119aa876e638a7602cb0bf7f126e8bdf" and "514ba20c41a68e5d6a6c609859eda7b4ef3651d1" have entirely different histories.
80bc3e4a11
...
514ba20c41
48
Main.gd
48
Main.gd
@ -1,6 +1,15 @@
|
|||||||
extends Control
|
extends Control
|
||||||
|
|
||||||
var mime_types := MimeTypeHelper.generate_db()
|
const mime_types: Dictionary = {
|
||||||
|
"html": "text/html",
|
||||||
|
"htm": "text/html",
|
||||||
|
"md": "text/plain",
|
||||||
|
"css": "text/css",
|
||||||
|
"txt": "text/plain",
|
||||||
|
"png": "image/png",
|
||||||
|
"jpg": "image/jpeg",
|
||||||
|
"jpeg": "image/jpeg",
|
||||||
|
}
|
||||||
|
|
||||||
var _server: HTTPServer = null
|
var _server: HTTPServer = null
|
||||||
|
|
||||||
@ -18,9 +27,7 @@ func _start_server(port: int = 3001) -> void:
|
|||||||
_server = HTTPServer.new()
|
_server = HTTPServer.new()
|
||||||
var dir := Directory.new()
|
var dir := Directory.new()
|
||||||
if dir.open("res://server_files") == OK:
|
if dir.open("res://server_files") == OK:
|
||||||
if dir.list_dir_begin() != OK:
|
dir.list_dir_begin()
|
||||||
# TODO: show error to user here
|
|
||||||
return
|
|
||||||
var file_name := dir.get_next()
|
var file_name := dir.get_next()
|
||||||
while file_name != "":
|
while file_name != "":
|
||||||
if !dir.current_is_dir():
|
if !dir.current_is_dir():
|
||||||
@ -37,9 +44,7 @@ func _start_server(port: int = 3001) -> void:
|
|||||||
_server.endpoint(HTTPServer.Method.GET, "/", funcref(self, "_serve_file"), ["index.html"])
|
_server.endpoint(HTTPServer.Method.GET, "/", funcref(self, "_serve_file"), ["index.html"])
|
||||||
|
|
||||||
|
|
||||||
if _server.listen(port) != OK:
|
_server.listen(port)
|
||||||
# TODO: show error to user here
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
func _stop_server() -> void:
|
func _stop_server() -> void:
|
||||||
@ -51,29 +56,26 @@ func _process(_delta: float) -> void:
|
|||||||
if _server == null:
|
if _server == null:
|
||||||
return
|
return
|
||||||
|
|
||||||
if not _server.take_connection():
|
_server.take_connection()
|
||||||
# TODO: show error to user here
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
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)
|
print(file_name)
|
||||||
|
|
||||||
var f := File.new()
|
var f = File.new()
|
||||||
var success = f.open("res://server_files/%s" % file_name, File.READ)
|
f.open("res://server_files/%s" % file_name, File.READ)
|
||||||
|
|
||||||
if success == OK:
|
var mime = get_mime_type(file_name)
|
||||||
|
|
||||||
var mime := mime_types.get(file_name)
|
response.header("content-type", get_mime_type(file_name))
|
||||||
response.header("content-type", mime.full_type)
|
|
||||||
|
|
||||||
# variant
|
response.data(f.get_as_text())
|
||||||
# warning-ignore:incompatible_ternary
|
# else:
|
||||||
var data = f.get_as_text() if mime.type == 'text' else f.get_buffer(f.get_len())
|
# response.header("content-type", "text/plain")
|
||||||
|
# response.data("500 - Read Error")
|
||||||
|
|
||||||
response.data(data)
|
|
||||||
|
|
||||||
else:
|
func get_mime_type(file_name: String) -> String:
|
||||||
response.header("content-type", "text/plain")
|
var ext := file_name.get_extension().to_lower()
|
||||||
response.data("500 - Read Error")
|
return mime_types[ext] if ext in mime_types else "application/octet-stream"
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -13,15 +13,9 @@ _global_script_classes=[ {
|
|||||||
"class": "HTTPServer",
|
"class": "HTTPServer",
|
||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
"path": "res://addons/http_server/http_server.gd"
|
"path": "res://addons/http_server/http_server.gd"
|
||||||
}, {
|
|
||||||
"base": "Reference",
|
|
||||||
"class": "MimeTypeHelper",
|
|
||||||
"language": "GDScript",
|
|
||||||
"path": "res://addons/http_server/mimetypes.gd"
|
|
||||||
} ]
|
} ]
|
||||||
_global_script_class_icons={
|
_global_script_class_icons={
|
||||||
"HTTPServer": "",
|
"HTTPServer": ""
|
||||||
"MimeTypeHelper": ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[application]
|
[application]
|
||||||
|
Loading…
Reference in New Issue
Block a user