patch http_server and add mimetypes

This commit is contained in:
Lera Elvoé 2023-05-09 18:15:14 +03:00
parent c7392c4d96
commit 85cdaca6f8
Signed by: yagich
SSH Key Fingerprint: SHA256:6xjGb6uA7lAVcULa7byPEN//rQ0wPoG+UzYVMfZnbvc
2 changed files with 8748 additions and 3 deletions

File diff suppressed because it is too large Load Diff

View File

@ -5,12 +5,13 @@ const Status = preload("res://addons/http_server/status.gd")
# Private variables
var __data = "" # variant
var __data: Variant = "" # variant
var __headers: Dictionary = {
# key: String, header name
# value: Variant, header value
}
var __status: int = 200
var __type: MimeTypeHelper.MimeType
# Public methods
@ -19,11 +20,11 @@ func data(data) -> void: # data: Variant
__data = data
func header(name: String, value) -> void: # value: Variant
func header(name: String, value: Variant) -> void:
__headers[name.to_lower()] = value
func json(data) -> void: # data: Variant
func json(data: Variant) -> void:
header("content-type", "application/json")
__data = data
@ -32,6 +33,10 @@ func status(status: int) -> void:
__status = status
func type(type: MimeTypeHelper.MimeType) -> void:
__type = type
func to_utf8_buffer() -> PackedByteArray:
var content = PackedStringArray()
@ -55,3 +60,49 @@ func to_utf8_buffer() -> PackedByteArray:
content.append(data)
return "\r\n".join(content).to_utf8_buffer()
func get_data() -> PackedByteArray:
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 PackedByteArray
data = data.to_utf8()
res.append_array(data)
return res
# Private methods
func __response_headers() -> PackedByteArray:
var res = PackedStringArray()
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")
var s = ("\r\n").join(res)
s = s + "\r\n"
return s.to_utf8_buffer()