2023-05-09 15:03:30 +00:00
|
|
|
# Public Constants
|
|
|
|
|
|
|
|
const Status = preload("res://addons/http_server/status.gd")
|
|
|
|
|
|
|
|
|
|
|
|
# Private variables
|
|
|
|
|
2023-05-09 15:15:14 +00:00
|
|
|
var __data: Variant = "" # variant
|
2023-05-09 15:03:30 +00:00
|
|
|
var __headers: Dictionary = {
|
|
|
|
# key: String, header name
|
|
|
|
# value: Variant, header value
|
|
|
|
}
|
|
|
|
var __status: int = 200
|
2023-05-09 15:15:14 +00:00
|
|
|
var __type: MimeTypeHelper.MimeType
|
2023-05-09 15:03:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Public methods
|
|
|
|
|
|
|
|
func data(data) -> void: # data: Variant
|
|
|
|
__data = data
|
|
|
|
|
|
|
|
|
2023-05-09 15:15:14 +00:00
|
|
|
func header(name: String, value: Variant) -> void:
|
2023-05-09 15:03:30 +00:00
|
|
|
__headers[name.to_lower()] = value
|
|
|
|
|
|
|
|
|
2023-05-09 15:15:14 +00:00
|
|
|
func json(data: Variant) -> void:
|
2023-05-09 15:03:30 +00:00
|
|
|
header("content-type", "application/json")
|
|
|
|
__data = data
|
|
|
|
|
|
|
|
|
|
|
|
func status(status: int) -> void:
|
|
|
|
__status = status
|
|
|
|
|
|
|
|
|
2023-05-09 15:15:14 +00:00
|
|
|
func type(type: MimeTypeHelper.MimeType) -> void:
|
|
|
|
__type = type
|
|
|
|
|
|
|
|
|
2023-05-09 15:03:30 +00:00
|
|
|
func to_utf8_buffer() -> PackedByteArray:
|
|
|
|
var content = PackedStringArray()
|
|
|
|
|
|
|
|
content.append(Status.code_to_status_line(__status))
|
|
|
|
|
|
|
|
var data = __data
|
|
|
|
if data.is_empty():
|
|
|
|
data = Status.code_to_description(__status)
|
|
|
|
|
|
|
|
if __headers.get("content-type", "") == "application/json":
|
|
|
|
data = JSON.stringify(data)
|
|
|
|
|
|
|
|
__headers['content-length'] = len(data)
|
|
|
|
|
|
|
|
for header in __headers:
|
|
|
|
content.append("%s: %s" % [header, str(__headers[header])])
|
|
|
|
|
|
|
|
content.append("")
|
|
|
|
|
|
|
|
if data:
|
|
|
|
content.append(data)
|
|
|
|
|
|
|
|
return "\r\n".join(content).to_utf8_buffer()
|
2023-05-09 15:15:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
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()
|