diff --git a/doc/demo16_http/http.md b/doc/demo16_http/http.md new file mode 100644 index 0000000..f246435 --- /dev/null +++ b/doc/demo16_http/http.md @@ -0,0 +1,16 @@ +![Image text](image/http1.png) +![Image text](image/http2.png) +![Image text](image/http3.png) +![Image text](image/http4.png) +![Image text](image/http5.png) +![Image text](image/http6.png) +![Image text](image/http7.png) +![Image text](image/http8.png) +![Image text](image/http9.png) +![Image text](image/http10.png) +![Image text](image/http11.png) +![Image text](image/http12.png) +![Image text](image/http13.png) +![Image text](image/http14.png) +![Image text](image/http15.png) +![Image text](image/http16.png) diff --git a/doc/demo16_http/image/http1.png b/doc/demo16_http/image/http1.png new file mode 100644 index 0000000..8e212ad Binary files /dev/null and b/doc/demo16_http/image/http1.png differ diff --git a/doc/demo16_http/image/http1.png.import b/doc/demo16_http/image/http1.png.import new file mode 100644 index 0000000..0d340e5 --- /dev/null +++ b/doc/demo16_http/image/http1.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/http1.png-c5cbc8b5618b413157e45ba9459731ec.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo16_http/image/http1.png" +dest_files=[ "res://.import/http1.png-c5cbc8b5618b413157e45ba9459731ec.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo16_http/image/http10.png b/doc/demo16_http/image/http10.png new file mode 100644 index 0000000..e213c3c Binary files /dev/null and b/doc/demo16_http/image/http10.png differ diff --git a/doc/demo16_http/image/http10.png.import b/doc/demo16_http/image/http10.png.import new file mode 100644 index 0000000..e8641ef --- /dev/null +++ b/doc/demo16_http/image/http10.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/http10.png-f9b9b17fec0eb8de939e012d9aea8cbe.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo16_http/image/http10.png" +dest_files=[ "res://.import/http10.png-f9b9b17fec0eb8de939e012d9aea8cbe.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo16_http/image/http11.png b/doc/demo16_http/image/http11.png new file mode 100644 index 0000000..75c966f Binary files /dev/null and b/doc/demo16_http/image/http11.png differ diff --git a/doc/demo16_http/image/http11.png.import b/doc/demo16_http/image/http11.png.import new file mode 100644 index 0000000..7b4350c --- /dev/null +++ b/doc/demo16_http/image/http11.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/http11.png-4c1f2edb0dc76ff1e200a8242de8290e.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo16_http/image/http11.png" +dest_files=[ "res://.import/http11.png-4c1f2edb0dc76ff1e200a8242de8290e.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo16_http/image/http12.png b/doc/demo16_http/image/http12.png new file mode 100644 index 0000000..efc97d0 Binary files /dev/null and b/doc/demo16_http/image/http12.png differ diff --git a/doc/demo16_http/image/http12.png.import b/doc/demo16_http/image/http12.png.import new file mode 100644 index 0000000..189bb42 --- /dev/null +++ b/doc/demo16_http/image/http12.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/http12.png-a7d76ca8cc02f01c49256099f01ec3a5.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo16_http/image/http12.png" +dest_files=[ "res://.import/http12.png-a7d76ca8cc02f01c49256099f01ec3a5.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo16_http/image/http13.png b/doc/demo16_http/image/http13.png new file mode 100644 index 0000000..bc28198 Binary files /dev/null and b/doc/demo16_http/image/http13.png differ diff --git a/doc/demo16_http/image/http13.png.import b/doc/demo16_http/image/http13.png.import new file mode 100644 index 0000000..da79adf --- /dev/null +++ b/doc/demo16_http/image/http13.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/http13.png-0a83f960c398d66685c318207c5e1103.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo16_http/image/http13.png" +dest_files=[ "res://.import/http13.png-0a83f960c398d66685c318207c5e1103.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo16_http/image/http14.png b/doc/demo16_http/image/http14.png new file mode 100644 index 0000000..4696395 Binary files /dev/null and b/doc/demo16_http/image/http14.png differ diff --git a/doc/demo16_http/image/http14.png.import b/doc/demo16_http/image/http14.png.import new file mode 100644 index 0000000..39b131b --- /dev/null +++ b/doc/demo16_http/image/http14.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/http14.png-b733b8af81f91429f6eaf374e52c81d3.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo16_http/image/http14.png" +dest_files=[ "res://.import/http14.png-b733b8af81f91429f6eaf374e52c81d3.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo16_http/image/http15.png b/doc/demo16_http/image/http15.png new file mode 100644 index 0000000..b4d785a Binary files /dev/null and b/doc/demo16_http/image/http15.png differ diff --git a/doc/demo16_http/image/http15.png.import b/doc/demo16_http/image/http15.png.import new file mode 100644 index 0000000..90d12ec --- /dev/null +++ b/doc/demo16_http/image/http15.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/http15.png-100d5762acc35f671d25bc86c1fffb5b.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo16_http/image/http15.png" +dest_files=[ "res://.import/http15.png-100d5762acc35f671d25bc86c1fffb5b.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo16_http/image/http16.png b/doc/demo16_http/image/http16.png new file mode 100644 index 0000000..e44c397 Binary files /dev/null and b/doc/demo16_http/image/http16.png differ diff --git a/doc/demo16_http/image/http16.png.import b/doc/demo16_http/image/http16.png.import new file mode 100644 index 0000000..502a59a --- /dev/null +++ b/doc/demo16_http/image/http16.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/http16.png-361f414d17603932b1659a1bd2592455.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo16_http/image/http16.png" +dest_files=[ "res://.import/http16.png-361f414d17603932b1659a1bd2592455.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo16_http/image/http2.png b/doc/demo16_http/image/http2.png new file mode 100644 index 0000000..836d0e3 Binary files /dev/null and b/doc/demo16_http/image/http2.png differ diff --git a/doc/demo16_http/image/http2.png.import b/doc/demo16_http/image/http2.png.import new file mode 100644 index 0000000..004e98e --- /dev/null +++ b/doc/demo16_http/image/http2.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/http2.png-db4564d91ea8fdbbccedaf537360a82f.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo16_http/image/http2.png" +dest_files=[ "res://.import/http2.png-db4564d91ea8fdbbccedaf537360a82f.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo16_http/image/http3.png b/doc/demo16_http/image/http3.png new file mode 100644 index 0000000..b0c984b Binary files /dev/null and b/doc/demo16_http/image/http3.png differ diff --git a/doc/demo16_http/image/http3.png.import b/doc/demo16_http/image/http3.png.import new file mode 100644 index 0000000..e9718a8 --- /dev/null +++ b/doc/demo16_http/image/http3.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/http3.png-ef3265fb85ce4f97b9ac95cab2a08a32.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo16_http/image/http3.png" +dest_files=[ "res://.import/http3.png-ef3265fb85ce4f97b9ac95cab2a08a32.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo16_http/image/http4.png b/doc/demo16_http/image/http4.png new file mode 100644 index 0000000..d412f5d Binary files /dev/null and b/doc/demo16_http/image/http4.png differ diff --git a/doc/demo16_http/image/http4.png.import b/doc/demo16_http/image/http4.png.import new file mode 100644 index 0000000..3a7fc59 --- /dev/null +++ b/doc/demo16_http/image/http4.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/http4.png-0cfcdabf76d023cce608c29c693f58e5.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo16_http/image/http4.png" +dest_files=[ "res://.import/http4.png-0cfcdabf76d023cce608c29c693f58e5.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo16_http/image/http5.png b/doc/demo16_http/image/http5.png new file mode 100644 index 0000000..e9ce0d1 Binary files /dev/null and b/doc/demo16_http/image/http5.png differ diff --git a/doc/demo16_http/image/http5.png.import b/doc/demo16_http/image/http5.png.import new file mode 100644 index 0000000..7ee56d0 --- /dev/null +++ b/doc/demo16_http/image/http5.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/http5.png-d117c5cde4b569ee676583a7df11a093.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo16_http/image/http5.png" +dest_files=[ "res://.import/http5.png-d117c5cde4b569ee676583a7df11a093.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo16_http/image/http6.png b/doc/demo16_http/image/http6.png new file mode 100644 index 0000000..300a6e0 Binary files /dev/null and b/doc/demo16_http/image/http6.png differ diff --git a/doc/demo16_http/image/http6.png.import b/doc/demo16_http/image/http6.png.import new file mode 100644 index 0000000..3f4b2f2 --- /dev/null +++ b/doc/demo16_http/image/http6.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/http6.png-1a4dca93d5eed46998f1cc82199442f2.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo16_http/image/http6.png" +dest_files=[ "res://.import/http6.png-1a4dca93d5eed46998f1cc82199442f2.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo16_http/image/http7.png b/doc/demo16_http/image/http7.png new file mode 100644 index 0000000..902d676 Binary files /dev/null and b/doc/demo16_http/image/http7.png differ diff --git a/doc/demo16_http/image/http7.png.import b/doc/demo16_http/image/http7.png.import new file mode 100644 index 0000000..1783a6b --- /dev/null +++ b/doc/demo16_http/image/http7.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/http7.png-43884cec2298c173597e9145af50ff9f.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo16_http/image/http7.png" +dest_files=[ "res://.import/http7.png-43884cec2298c173597e9145af50ff9f.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo16_http/image/http8.png b/doc/demo16_http/image/http8.png new file mode 100644 index 0000000..9da2687 Binary files /dev/null and b/doc/demo16_http/image/http8.png differ diff --git a/doc/demo16_http/image/http8.png.import b/doc/demo16_http/image/http8.png.import new file mode 100644 index 0000000..b9adbbb --- /dev/null +++ b/doc/demo16_http/image/http8.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/http8.png-8b32de6195c4a3243c5c2c1f1232cb2e.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo16_http/image/http8.png" +dest_files=[ "res://.import/http8.png-8b32de6195c4a3243c5c2c1f1232cb2e.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo16_http/image/http9.png b/doc/demo16_http/image/http9.png new file mode 100644 index 0000000..f011f1f Binary files /dev/null and b/doc/demo16_http/image/http9.png differ diff --git a/doc/demo16_http/image/http9.png.import b/doc/demo16_http/image/http9.png.import new file mode 100644 index 0000000..c2b89dc --- /dev/null +++ b/doc/demo16_http/image/http9.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/http9.png-9bccfb621c35397f399ba1e4cb3833a2.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo16_http/image/http9.png" +dest_files=[ "res://.import/http9.png-9bccfb621c35397f399ba1e4cb3833a2.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/scene/demo16_http/http.tscn b/scene/demo16_http/http.tscn new file mode 100644 index 0000000..de53cd8 --- /dev/null +++ b/scene/demo16_http/http.tscn @@ -0,0 +1,12 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://script/demo16_http/http_request.gd" type="Script" id=1] +[ext_resource path="res://script/demo16_http/http_utils.gd" type="Script" id=2] + +[node name="Node2D" type="Node2D"] + +[node name="HTTPRequest" type="HTTPRequest" parent="."] +script = ExtResource( 1 ) + +[node name="HttpUtils" type="Node2D" parent="."] +script = ExtResource( 2 ) diff --git a/script/demo16_http/http_request.gd b/script/demo16_http/http_request.gd new file mode 100644 index 0000000..4725363 --- /dev/null +++ b/script/demo16_http/http_request.gd @@ -0,0 +1,23 @@ +extends HTTPRequest + + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + pass + +func httpRequest(): + var headers: PoolStringArray = [] + self.set_use_threads(true) + self.connect("request_completed", self, "doSomething") + #self.request("https://swapi.dev/api/people/1/", headers, true, HTTPClient.METHOD_PUT, "name: Jane Skywalker") + self.request("https://www.taptap.com/") + + +func doSomething(result, response_code, headers, body: PoolByteArray): + + if(response_code == 200): + var data = body.get_string_from_utf8() + print(data) + else: + print('response_code: ', response_code) + print('problem on the server') diff --git a/script/demo16_http/http_utils.gd b/script/demo16_http/http_utils.gd new file mode 100644 index 0000000..69626f8 --- /dev/null +++ b/script/demo16_http/http_utils.gd @@ -0,0 +1,8 @@ +extends Node2D + +const HttpUtils = preload("res://zfoo/util/HttpUtils.gd") + +func _ready(): + var util = HttpUtils.new() + var response = util.get("https://www.zhihu.com") + print(response) diff --git a/zfoo/util/HttpUtils.gd b/zfoo/util/HttpUtils.gd new file mode 100644 index 0000000..881eebb --- /dev/null +++ b/zfoo/util/HttpUtils.gd @@ -0,0 +1,294 @@ +extends Object + +const TimeUtils = preload("res://zfoo/scheduler/TimeUtils.gd") +const StringUtils = preload("res://zfoo/util/StringUtils.gd") + +const REQUEST_TIMEOUT = 30 * TimeUtils.MILLIS_PER_SECOND + + + +const HEADERS = [ + "User-Agent: Pirulo/1.0 (Godot)", + "Accept: */*" +] + +const CONTENT_BYTESTREAM = "Content-Type: bytestream" +const CONTENT_JSON = "Content-Type: application/json" +const CONTENT_TEXT = "Content-Type: text/plain" + +var ERR_BODY = "Parse Error unsupported body" +var ERR_CONN = "Connection error, can't reach host" +var ERR_REQUEST = "Request failed, invalid params?" + +#func _init(): + # Init SSL Certificates - Is broken in master + # Add manually in Menu Project->Network->ssl + # ProjectSettings.set("network/ssl/certificates", get_script().get_path().get_base_dir()+"/ca-certificates.crt") + +func error(code): + var rs = {} + + rs["header"] = {} + rs["body"] = str(code) + rs["code"] = 404 + + print(rs) + + return rs + + +var HTTP = "http://" +var HTTPS = "https://" + +func req(verb, address, body): + if StringUtils.isEmpty(address): + return error("Invalid http request, adress is empty") + + var headerBody = toHeaderAndBody(body) + var headers = headerBody[0] + body = headerBody[1] + + var uri_dict = get_link_address_port_path(address) + + var serverAdress = uri_dict["host"] + var port = uri_dict["port"] + var path = uri_dict["path"] + var fullhost = uri_dict["fullhost"] + + var ssl = uri_dict["ssl"] + + var _http = HTTPClient.new() + _http.set_blocking_mode( true ) #wait until all data is available on response + + var err = _http.connect_to_host(serverAdress, port, ssl) + assert(err == OK) # Make sure connection is OK. + var start_time_in_ms = OS.get_ticks_msec() + while (_http.get_status()==HTTPClient.STATUS_CONNECTING) || (_http.get_status()==HTTPClient.STATUS_RESOLVING): + _http.poll() + print("Connecting...") + if ((OS.get_ticks_msec() - start_time_in_ms) >= REQUEST_TIMEOUT): # TIMEOUT + printerr(ERR_TIMEOUT) + return + + if !OS.has_feature("web"): + OS.delay_msec(500) + else: + yield(Engine.get_main_loop(), "idle_frame") + + var http_fullhost = checkServerConnection(address) + + if(TYPE_INT != typeof(_http) && _http.get_status()==HTTPClient.STATUS_CONNECTED): + err = _http.request_raw(verb, path, headers, body) + + if(!err): + return getResponse(_http) + else: + return error(ERR_REQUEST) + return error(ERR_CONN) + +#var http # sync can only have one / blocks +# TODO all requests should be async +# 1. The ui will block and multiple sync http connections are not possbile anyway +# 2. The multiple http connections are useless, and returning the http connection to close it is not possible +func get(adress): + return req(HTTPClient.METHOD_GET, adress, PoolByteArray()) + +func put(adress, body=PoolByteArray([])): + return req(HTTPClient.METHOD_PUT, adress, body) + +func post(adress, body=PoolByteArray([])): + return req(HTTPClient.METHOD_POST, adress, body) + +func delete(adress): + return req(HTTPClient.METHOD_DELETE, adress, PoolByteArray()) + +func thread(verb, adress, function, body1): + var thread = Thread.new() + var args = [verb, adress, body1, function, thread] + thread.start(self, "reqAsync", args) + return thread + +var requests = {} +func stop_async(thread): + if(requests.has(thread)): + requests[thread].close() + +func reqAsync(args): + var verb = args[0] + var adress = args[1] + var body1 = args[2] + var function = args[3] + var thread = args[4] + + function.call_func(req( verb, adress, body1)) + requests.erase(thread) + +func getAsync(adress, function): + return thread(HTTPClient.METHOD_GET, adress, function, PoolByteArray()) + +func putAsync(adress, function, body=PoolByteArray([])): + return thread(HTTPClient.METHOD_PUT, adress, function, body) + +func postAsync(adress, function, body=PoolByteArray([])): + return thread(HTTPClient.METHOD_POST, adress, function, body) + +func deleteAsync(adress, function): + return thread(HTTPClient.METHOD_DELETE, adress, function, PoolByteArray()) + +func toHeaderAndBody(body): + var headers = Array(HEADERS) + + var type = typeof(body) + if (type == TYPE_RAW_ARRAY): + if (body.size()>0): + headers.append(CONTENT_BYTESTREAM) + elif (type == TYPE_DICTIONARY): + if (!body.empty()): + headers.append(CONTENT_JSON) + body = to_json(body).to_utf8() + elif (type == TYPE_STRING): + if (body.length()>0): + headers.append(CONTENT_TEXT) + body = body.to_utf8() + + return [headers,body] + +func get_link_address_port_path(uri): + var left = "" + var link = "" + var ssl = false + + if(uri.begins_with(HTTPS)): + ssl = true + link = uri.replace(HTTPS, "") + left+=HTTPS + else: + link = uri.replace(HTTP, "") + left+=HTTP + + var hostport = link.split("/", true)[0] + + left+=hostport + + var host_port = hostport.split(":", true) + var host = host_port[0] + + var port = "80" + if(host_port.size()>1): + port = host_port[1] + if(uri.begins_with(HTTPS)):# check if https -> ssl/tls 443 + port = "443" + + var path = uri.replace(left,"") + if (StringUtils.isEmpty(path)): + path = "/" + + return { + "uri":uri, + "host":host, + "port":int(port), + "path":path, + "ssl":ssl, + "fullhost":hostport + } + +func checkServerConnection(adress): + + var uri_dict = get_link_address_port_path(adress) + + var serverAdress = uri_dict["host"] + var port = uri_dict["port"] + var path = uri_dict["path"] + var fullhost = uri_dict["fullhost"] + + var ssl = uri_dict["ssl"] + + var _http = HTTPClient.new() + _http.set_blocking_mode( true ) #wait until all data is available on response + + var err = _http.connect_to_host(serverAdress, port, ssl) + + if(!err): + var start_time_in_ms = OS.get_ticks_msec() + while( (_http.get_status()==HTTPClient.STATUS_CONNECTING or + _http.get_status()==HTTPClient.STATUS_RESOLVING) && + (OS.get_ticks_msec()-start_time_in_ms)=REQUEST_TIMEOUT): # TIMEOUT + return [HTTPClient.STATUS_CANT_CONNECT, fullhost] + else: + return [_http, fullhost, path] + else: + return [HTTPClient.STATUS_CANT_CONNECT, fullhost] + +func getResponse(_http): + + var rs = {} + + rs["header"] = {} + rs["body"] = "" + rs["code"] = 404 + + # Keep polling until the request is going on - in some cases the server will not respond I will add a timeout + var start_time_in_ms = OS.get_ticks_msec() + while ((_http.get_status() != HTTPClient.STATUS_DISCONNECTED) && + (_http.get_status() == HTTPClient.STATUS_REQUESTING) && + ((OS.get_ticks_msec()-start_time_in_ms)=REQUEST_TIMEOUT): + return error("TIMEOUT") + + rs["code"] = _http.get_response_code() + + if (_http.has_response()): + # Get response headers + var headers = _http.get_response_headers_as_dictionary() + + for key in headers: + rs["header"][key.to_lower()] = headers[key] + + # var cache = headers + + var rb = PoolByteArray() #array that will hold the data + + while(_http.get_status()==HTTPClient.STATUS_BODY): + #_http.set_read_chunk_size( _http.get_response_body_length() ) + rb += _http.read_response_body_chunk() + + if("content-length" in rs["header"] && rs["header"]["content-length"]!="0"): + rs["body"] = parse_body_to_var(rb, rs["header"]["content-type"]) + else: + rs["body"] = "" + + return rs + else: + if _http.get_status()==HTTPClient.STATUS_CONNECTION_ERROR: + print("Maybe Certificates no set in project_settings/network/ssl/") + breakpoint + return rs + +func parse_body_to_var(body, content_type): + if(content_type.find("application/json")!=-1): + body = body.get_string_from_utf8() + #print(body) + var bodyDict = parse_json( body ) + #print(bodyDict) + if( typeof(bodyDict) == TYPE_DICTIONARY): + body = bodyDict + elif( typeof(bodyDict) == TYPE_ARRAY): + body = bodyDict + elif(body=="null"): + body = {} + else: + print("Error body incompatible content_type: " + str(typeof(bodyDict))) + + elif(content_type.find("text/plain")!=-1||content_type.find("text/html")!=-1): + body = body.get_string_from_utf8() + elif(content_type.find("bytestream")!=-1): + pass #return body + return body