xbintsc 0.6.20 → 0.6.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +5 -0
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/cli/main.js +1 -0
- package/dist/src/cli/main.js.map +1 -1
- package/dist/src/driver/build-key.d.ts +3 -0
- package/dist/src/driver/build-key.js +5 -2
- package/dist/src/driver/build-key.js.map +1 -1
- package/dist/src/driver/compiler.js +11 -0
- package/dist/src/driver/compiler.js.map +1 -1
- package/dist/src/driver/config.d.ts +12 -0
- package/dist/src/driver/config.js +22 -0
- package/dist/src/driver/config.js.map +1 -1
- package/dist/src/driver/fingerprint.d.ts +11 -0
- package/dist/src/driver/fingerprint.js +25 -0
- package/dist/src/driver/fingerprint.js.map +1 -1
- package/dist/src/driver/index.d.ts +2 -1
- package/dist/src/driver/index.js +1 -0
- package/dist/src/driver/index.js.map +1 -1
- package/dist/src/driver/link-features.js +36 -11
- package/dist/src/driver/link-features.js.map +1 -1
- package/dist/src/driver/options.d.ts +8 -1
- package/dist/src/driver/paths.d.ts +10 -0
- package/dist/src/driver/paths.js +18 -0
- package/dist/src/driver/paths.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +22 -6
- package/dist/src/driver/runtime-objects.js.map +1 -1
- package/dist/src/driver/storage.d.ts +34 -0
- package/dist/src/driver/storage.js +67 -0
- package/dist/src/driver/storage.js.map +1 -0
- package/dist/src/driver/toolchain.d.ts +1 -1
- package/dist/src/driver/toolchain.js +10 -1
- package/dist/src/driver/toolchain.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/gui/features.d.ts +1 -1
- package/dist/src/extensions/gui/features.js +2 -2
- package/dist/src/extensions/gui/features.js.map +1 -1
- package/dist/src/extensions/gui/index.js +3 -1
- package/dist/src/extensions/gui/index.js.map +1 -1
- package/dist/src/extensions/node/http2/feature.d.ts +14 -0
- package/dist/src/extensions/node/http2/feature.js +28 -0
- package/dist/src/extensions/node/http2/feature.js.map +1 -0
- package/dist/src/extensions/node/http2/index.d.ts +5 -1
- package/dist/src/extensions/node/http2/index.js +6 -1
- package/dist/src/extensions/node/http2/index.js.map +1 -1
- package/dist/src/extensions/node/index.js +3 -1
- package/dist/src/extensions/node/index.js.map +1 -1
- package/dist/src/extensions/node/tls/feature.d.ts +12 -0
- package/dist/src/extensions/node/tls/feature.js +24 -0
- package/dist/src/extensions/node/tls/feature.js.map +1 -0
- package/dist/src/extensions/node/tls/index.d.ts +5 -1
- package/dist/src/extensions/node/tls/index.js +5 -1
- package/dist/src/extensions/node/tls/index.js.map +1 -1
- package/dist/src/extensions/node/zlib/compression.js +11 -6
- package/dist/src/extensions/node/zlib/compression.js.map +1 -1
- package/dist/src/extensions/registry.d.ts +4 -1
- package/dist/src/extensions/registry.js +9 -2
- package/dist/src/extensions/registry.js.map +1 -1
- package/dist/src/extensions/storage.d.ts +22 -0
- package/dist/src/extensions/storage.js +51 -0
- package/dist/src/extensions/storage.js.map +1 -0
- package/dist/src/extensions/tls.d.ts +26 -0
- package/dist/src/extensions/tls.js +46 -0
- package/dist/src/extensions/tls.js.map +1 -0
- package/dist/tests/driver/config.test.js +31 -0
- package/dist/tests/driver/config.test.js.map +1 -1
- package/dist/tests/driver/link-features.test.js +13 -2
- package/dist/tests/driver/link-features.test.js.map +1 -1
- package/dist/tests/driver/storage.test.d.ts +1 -0
- package/dist/tests/driver/storage.test.js +29 -0
- package/dist/tests/driver/storage.test.js.map +1 -0
- package/dist/tests/e2e/fetch.test.js +3 -1
- package/dist/tests/e2e/fetch.test.js.map +1 -1
- package/dist/tests/e2e/http2.test.d.ts +7 -0
- package/dist/tests/e2e/http2.test.js +201 -0
- package/dist/tests/e2e/http2.test.js.map +1 -0
- package/dist/tests/e2e/net-helpers.d.ts +28 -0
- package/dist/tests/e2e/net-helpers.js +59 -0
- package/dist/tests/e2e/net-helpers.js.map +1 -0
- package/dist/tests/e2e/node-modules.test.js +3 -5
- package/dist/tests/e2e/node-modules.test.js.map +1 -1
- package/dist/tests/e2e/storage.test.d.ts +7 -0
- package/dist/tests/e2e/storage.test.js +86 -0
- package/dist/tests/e2e/storage.test.js.map +1 -0
- package/dist/tests/e2e/tls.test.d.ts +10 -0
- package/dist/tests/e2e/tls.test.js +197 -0
- package/dist/tests/e2e/tls.test.js.map +1 -0
- package/dist/tests/extensions/gui-features.test.js +1 -1
- package/dist/tests/extensions/gui-features.test.js.map +1 -1
- package/dist/tests/extensions/storage.test.d.ts +1 -0
- package/dist/tests/extensions/storage.test.js +51 -0
- package/dist/tests/extensions/storage.test.js.map +1 -0
- package/doc/ai/cli.md +2 -0
- package/doc/ai/contributing.md +14 -0
- package/doc/ai/extensions.md +31 -3
- package/doc/ai/language-support.md +1 -1
- package/doc/ai/zh-CN/cli.md +2 -0
- package/doc/ai/zh-CN/contributing.md +12 -0
- package/doc/ai/zh-CN/extensions.md +27 -2
- package/doc/ai/zh-CN/language-support.md +1 -1
- package/doc/gui/api.md +1 -1
- package/doc/gui/build.md +7 -5
- package/doc/gui/media.md +4 -4
- package/doc/gui/rendering.md +1 -1
- package/doc/gui.md +3 -3
- package/doc/implemented/async.md +1 -1
- package/doc/implemented/extensions.md +2 -1
- package/doc/implemented/fetch.md +19 -4
- package/doc/implemented/node/http2.md +51 -0
- package/doc/implemented/node/tls.md +56 -0
- package/doc/implemented/node.md +8 -0
- package/doc/implemented/storage.md +65 -0
- package/doc/implemented.md +4 -2
- package/doc/unimplemented/node/modules.md +6 -6
- package/doc/unimplemented/toolchain.md +1 -1
- package/doc/xbintsc.config.schema.json +16 -1
- package/doc/zh-CN/gui/api.md +1 -1
- package/doc/zh-CN/gui/build.md +6 -4
- package/doc/zh-CN/gui/media.md +3 -3
- package/doc/zh-CN/gui/rendering.md +1 -1
- package/doc/zh-CN/gui.md +3 -3
- package/doc/zh-CN/implemented/async.md +1 -1
- package/doc/zh-CN/implemented/extensions.md +2 -1
- package/doc/zh-CN/implemented/fetch.md +15 -3
- package/doc/zh-CN/implemented/node/http2.md +44 -0
- package/doc/zh-CN/implemented/node/tls.md +47 -0
- package/doc/zh-CN/implemented/node.md +7 -0
- package/doc/zh-CN/implemented/storage.md +52 -0
- package/doc/zh-CN/implemented.md +4 -2
- package/doc/zh-CN/unimplemented/node/modules.md +3 -3
- package/doc/zh-CN/unimplemented/toolchain.md +1 -1
- package/package.json +16 -8
- package/runtime/ext_gui/dom_api_form.cpp +10 -0
- package/runtime/ext_gui/gui_media.cpp +1 -1
- package/runtime/ext_gui/media.cpp +1 -1
- package/runtime/ext_gui/media_api.h +2 -2
- package/runtime/ext_node/http2/http2.c +25 -11
- package/runtime/ext_node/http2/http2_backend.c +48 -0
- package/runtime/ext_node/http2/http2_bridge.c +25 -0
- package/runtime/ext_node/http2/parts/api.inc +40 -0
- package/runtime/ext_node/http2/parts/client.inc +215 -0
- package/runtime/ext_node/http2/parts/common.inc +330 -0
- package/runtime/ext_node/http2/parts/events.inc +139 -0
- package/runtime/ext_node/http2/parts/server.inc +301 -0
- package/runtime/ext_node/http2/parts/streams.inc +254 -0
- package/runtime/ext_node/tls/parts/api.inc +38 -0
- package/runtime/ext_node/tls/parts/connect.inc +107 -0
- package/runtime/ext_node/tls/parts/helpers.inc +121 -0
- package/runtime/ext_node/tls/parts/server.inc +101 -0
- package/runtime/ext_node/tls/parts/socket.inc +171 -0
- package/runtime/ext_node/tls/tls.c +21 -13
- package/runtime/ext_node/tls/tls_backend.c +50 -0
- package/runtime/ext_node/zlib/{miniz/compression_bridge.c → compression_bridge.c} +1 -1
- package/runtime/xt_fetch/async.inc +19 -12
- package/runtime/xt_fetch/socket.inc +55 -0
- package/runtime/xt_fetch.c +3 -0
- package/runtime/xt_http2/nghttp2_compat.h +19 -0
- package/runtime/xt_http2/nghttp2_static.h +18 -0
- package/runtime/xt_http2_api.h +38 -0
- package/runtime/xt_node_tls_api.h +37 -0
- package/runtime/xt_protocol_registry.c +22 -0
- package/runtime/xt_storage/indexed_db.c +408 -0
- package/runtime/xt_storage/indexed_db_internal.h +26 -0
- package/runtime/xt_storage/indexed_db_store.c +305 -0
- package/runtime/xt_storage/storage.c +209 -0
- package/runtime/xt_storage/storage_internal.h +66 -0
- package/runtime/xt_tls/parts/api.inc +21 -0
- package/runtime/xt_tls/parts/client.inc +112 -0
- package/runtime/xt_tls/parts/io.inc +157 -0
- package/runtime/xt_tls/parts/server.inc +102 -0
- package/runtime/xt_tls/parts/setup.inc +146 -0
- package/runtime/xt_tls/tls_bridge.c +27 -0
- package/runtime/xt_tls/tls_core.c +39 -0
- package/runtime/xt_tls/tls_internal.h +73 -0
- package/runtime/xt_tls_api.h +73 -0
- package/scripts/build-gui.ts +9 -8
- package/scripts/build-media.ts +2 -2
- package/scripts/build-tls.ts +248 -0
- package/scripts/check-file-length.ts +4 -21
- package/scripts/fetch-vendored.ts +244 -0
- package/scripts/package-release.ts +13 -3
- package/src/cli/main.ts +1 -0
- package/src/driver/build-key.ts +13 -2
- package/src/driver/compiler.ts +13 -0
- package/src/driver/config.ts +35 -0
- package/src/driver/fingerprint.ts +24 -0
- package/src/driver/index.ts +8 -0
- package/src/driver/link-features.ts +33 -11
- package/src/driver/options.ts +8 -1
- package/src/driver/paths.ts +20 -0
- package/src/driver/runtime-objects.ts +25 -6
- package/src/driver/storage.ts +84 -0
- package/src/driver/toolchain.ts +11 -2
- package/src/driver/version.ts +1 -1
- package/src/extensions/gui/features.ts +2 -2
- package/src/extensions/gui/index.ts +3 -1
- package/src/extensions/node/http2/feature.ts +33 -0
- package/src/extensions/node/http2/index.ts +6 -1
- package/src/extensions/node/index.ts +3 -1
- package/src/extensions/node/tls/feature.ts +29 -0
- package/src/extensions/node/tls/index.ts +5 -1
- package/src/extensions/node/zlib/compression.ts +11 -6
- package/src/extensions/registry.ts +9 -2
- package/src/extensions/storage.ts +58 -0
- package/src/extensions/tls.ts +52 -0
- package/vendor/sqlite3/sqlite3.c +271131 -0
- package/vendor/sqlite3/sqlite3.h +14447 -0
- package/{runtime/ext_gui → vendor/stb}/stb_image.h +17 -1
- /package/runtime/{ext_media → xt_media}/decode_audio.cpp +0 -0
- /package/runtime/{ext_media → xt_media}/media.cpp +0 -0
- /package/runtime/{ext_media → xt_media}/media_bridge.c +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz.c +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_common.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_export.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tdef.c +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tdef.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tinfl.c +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tinfl.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_zip.h +0 -0
- /package/{runtime/ext_gui → vendor/nanosvg}/nanosvg.h +0 -0
- /package/{runtime/ext_gui → vendor/nanosvg}/nanosvgrast.h +0 -0
- /package/{runtime/ext_gui → vendor/stb}/stb_truetype.h +0 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/* -- client: connect and request ------------------------------------------ */
|
|
2
|
+
|
|
3
|
+
static int xt_h2_tcp_connect(const char *host, int port) {
|
|
4
|
+
char service[16];
|
|
5
|
+
snprintf(service, sizeof(service), "%d", port);
|
|
6
|
+
struct addrinfo hints;
|
|
7
|
+
memset(&hints, 0, sizeof(hints));
|
|
8
|
+
hints.ai_family = AF_UNSPEC;
|
|
9
|
+
hints.ai_socktype = SOCK_STREAM;
|
|
10
|
+
struct addrinfo *result = NULL;
|
|
11
|
+
if (getaddrinfo(host, service, &hints, &result) != 0) return -1;
|
|
12
|
+
int fd = -1;
|
|
13
|
+
for (struct addrinfo *info = result; info != NULL; info = info->ai_next) {
|
|
14
|
+
int candidate = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
|
|
15
|
+
if (candidate < 0) continue;
|
|
16
|
+
if (connect(candidate, info->ai_addr, (xt_h2_socklen_t)info->ai_addrlen) == 0) {
|
|
17
|
+
fd = candidate;
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
xt_h2_closesocket(candidate);
|
|
21
|
+
}
|
|
22
|
+
freeaddrinfo(result);
|
|
23
|
+
return fd;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static void xt_h2_install_callbacks(nghttp2_session_callbacks *callbacks) {
|
|
27
|
+
nghttp2_session_callbacks_set_on_begin_headers_callback(callbacks, xt_h2_on_begin_headers);
|
|
28
|
+
nghttp2_session_callbacks_set_on_header_callback(callbacks, xt_h2_on_header);
|
|
29
|
+
nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks, xt_h2_on_frame_recv);
|
|
30
|
+
nghttp2_session_callbacks_set_on_data_chunk_recv_callback(callbacks, xt_h2_on_data_chunk_recv);
|
|
31
|
+
nghttp2_session_callbacks_set_on_stream_close_callback(callbacks, xt_h2_on_stream_close);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static xt_value xt_h2_session_request(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
35
|
+
(void)env;
|
|
36
|
+
xt_h2_conn *conn = xt_h2_conn_of(thisValue);
|
|
37
|
+
if (conn == NULL || conn->closed) {
|
|
38
|
+
xt_throw(xt_h2_error("the HTTP/2 session is closed"));
|
|
39
|
+
return XT_UNDEFINED;
|
|
40
|
+
}
|
|
41
|
+
xt_value headers = xt_arg(argc, argv, 0);
|
|
42
|
+
if (!XT_IS_OBJECT(headers)) {
|
|
43
|
+
xt_throw(xt_h2_error("http2 request headers must be an object"));
|
|
44
|
+
return XT_UNDEFINED;
|
|
45
|
+
}
|
|
46
|
+
const char *method = xt_string_data(xt_object_get_cstr(headers, ":method"));
|
|
47
|
+
const char *path = xt_string_data(xt_object_get_cstr(headers, ":path"));
|
|
48
|
+
const char *scheme = xt_string_data(xt_object_get_cstr(headers, ":scheme"));
|
|
49
|
+
const char *authority = xt_string_data(xt_object_get_cstr(headers, ":authority"));
|
|
50
|
+
if (method == NULL) method = "GET";
|
|
51
|
+
if (path == NULL) path = "/";
|
|
52
|
+
if (scheme == NULL) scheme = conn->scheme;
|
|
53
|
+
if (authority == NULL) authority = conn->authority;
|
|
54
|
+
|
|
55
|
+
nghttp2_nv nva[XT_H2_MAX_HEADERS];
|
|
56
|
+
size_t count = 0;
|
|
57
|
+
count = xt_h2_add_nv(nva, count, XT_H2_MAX_HEADERS, ":method", method);
|
|
58
|
+
count = xt_h2_add_nv(nva, count, XT_H2_MAX_HEADERS, ":path", path);
|
|
59
|
+
count = xt_h2_add_nv(nva, count, XT_H2_MAX_HEADERS, ":scheme", scheme);
|
|
60
|
+
count = xt_h2_add_nv(nva, count, XT_H2_MAX_HEADERS, ":authority", authority);
|
|
61
|
+
count = xt_h2_add_headers(nva, count, XT_H2_MAX_HEADERS, headers);
|
|
62
|
+
|
|
63
|
+
xt_h2_body *body = xt_h2_body_new();
|
|
64
|
+
nghttp2_data_provider2 provider;
|
|
65
|
+
provider.source.ptr = body;
|
|
66
|
+
provider.read_callback = xt_h2_read_body;
|
|
67
|
+
int32_t id = nghttp2_submit_request2(conn->session, NULL, nva, count, &provider, NULL);
|
|
68
|
+
xt_h2_free_nv(nva, count);
|
|
69
|
+
if (id < 0) {
|
|
70
|
+
xt_h2_body_free(body);
|
|
71
|
+
xt_throw(xt_h2_error("cannot submit the HTTP/2 request"));
|
|
72
|
+
return XT_UNDEFINED;
|
|
73
|
+
}
|
|
74
|
+
xt_value stream = xt_h2_new_stream(conn, id, body);
|
|
75
|
+
xt_h2_flush(conn);
|
|
76
|
+
return stream;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
xt_value xt_h2_session_close(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
80
|
+
(void)env;
|
|
81
|
+
(void)argc;
|
|
82
|
+
(void)argv;
|
|
83
|
+
xt_h2_conn *conn = xt_h2_conn_of(thisValue);
|
|
84
|
+
if (conn == NULL || conn->closed) return thisValue;
|
|
85
|
+
nghttp2_session_terminate_session(conn->session, NGHTTP2_NO_ERROR);
|
|
86
|
+
xt_h2_flush(conn);
|
|
87
|
+
xt_h2_finish(conn, NULL);
|
|
88
|
+
return thisValue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
xt_value xt_h2_stream_write(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
92
|
+
(void)env;
|
|
93
|
+
if (argc > 0) xt_h2_stream_append(thisValue, argv[0]);
|
|
94
|
+
xt_h2_conn *conn = xt_h2_conn_of(thisValue);
|
|
95
|
+
if (conn == NULL || conn->closed) return xt_bool(0);
|
|
96
|
+
int32_t id = (int32_t)xt_to_number(xt_object_get_cstr(thisValue, "__id"));
|
|
97
|
+
nghttp2_session_resume_data(conn->session, id);
|
|
98
|
+
xt_h2_flush(conn);
|
|
99
|
+
return xt_bool(1);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
xt_value xt_h2_stream_end(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
103
|
+
(void)env;
|
|
104
|
+
if (argc > 0 && !xt_truthy(xt_is_nullish(argv[0]))) xt_h2_stream_append(thisValue, argv[0]);
|
|
105
|
+
xt_h2_body *body = xt_h2_body_of(thisValue);
|
|
106
|
+
if (body != NULL) body->ended = 1;
|
|
107
|
+
xt_h2_conn *conn = xt_h2_conn_of(thisValue);
|
|
108
|
+
if (conn == NULL || conn->closed) return thisValue;
|
|
109
|
+
int32_t id = (int32_t)xt_to_number(xt_object_get_cstr(thisValue, "__id"));
|
|
110
|
+
nghttp2_session_resume_data(conn->session, id);
|
|
111
|
+
xt_h2_flush(conn);
|
|
112
|
+
return thisValue;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
xt_value xt_h2_stream_close(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
116
|
+
(void)env;
|
|
117
|
+
(void)argc;
|
|
118
|
+
(void)argv;
|
|
119
|
+
xt_h2_conn *conn = xt_h2_conn_of(thisValue);
|
|
120
|
+
if (conn == NULL || conn->closed) return thisValue;
|
|
121
|
+
int32_t id = (int32_t)xt_to_number(xt_object_get_cstr(thisValue, "__id"));
|
|
122
|
+
nghttp2_submit_rst_stream(conn->session, NGHTTP2_FLAG_NONE, id, NGHTTP2_CANCEL);
|
|
123
|
+
xt_h2_flush(conn);
|
|
124
|
+
return thisValue;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
xt_value xt_h2_stream_set_encoding(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
128
|
+
(void)env;
|
|
129
|
+
xt_node_set(thisValue, "__encoding", xt_arg(argc, argv, 0));
|
|
130
|
+
return thisValue;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/* `http2.connect(authority[, options][, listener])` */
|
|
134
|
+
xt_value xt_h2_connect(int32_t argc, xt_value *argv) {
|
|
135
|
+
const char *authority = xt_string_data(xt_to_string(xt_arg(argc, argv, 0)));
|
|
136
|
+
if (authority == NULL) {
|
|
137
|
+
xt_throw(xt_h2_error("http2.connect requires an authority URL"));
|
|
138
|
+
return XT_UNDEFINED;
|
|
139
|
+
}
|
|
140
|
+
char scheme[8];
|
|
141
|
+
char host[256];
|
|
142
|
+
char authorityCopy[512];
|
|
143
|
+
int port = 0;
|
|
144
|
+
int secure = xt_h2_parse_authority(authority, scheme, sizeof(scheme), host, sizeof(host), &port,
|
|
145
|
+
authorityCopy, sizeof(authorityCopy));
|
|
146
|
+
if (secure < 0) {
|
|
147
|
+
xt_throw(xt_h2_error("http2.connect requires an http:// or https:// URL"));
|
|
148
|
+
return XT_UNDEFINED;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
xt_value options = xt_undefined();
|
|
152
|
+
xt_value listener = xt_undefined();
|
|
153
|
+
for (int32_t i = 1; i < argc; i++) {
|
|
154
|
+
if (XT_IS_OBJECT(argv[i]) && options == xt_undefined()) options = argv[i];
|
|
155
|
+
else if (XT_IS_FUNCTION(argv[i])) listener = argv[i];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
xt_h2_conn *conn = (xt_h2_conn *)calloc(1, sizeof(xt_h2_conn));
|
|
159
|
+
if (conn == NULL) {
|
|
160
|
+
xt_throw(xt_h2_error("out of memory"));
|
|
161
|
+
return XT_UNDEFINED;
|
|
162
|
+
}
|
|
163
|
+
conn->fd = -1;
|
|
164
|
+
snprintf(conn->scheme, sizeof(conn->scheme), "%s", scheme);
|
|
165
|
+
snprintf(conn->authority, sizeof(conn->authority), "%s", authorityCopy);
|
|
166
|
+
|
|
167
|
+
if (secure) {
|
|
168
|
+
if (xt_tls_core_api == NULL) {
|
|
169
|
+
free(conn);
|
|
170
|
+
xt_throw(xt_h2_error("https requires the tls link feature"));
|
|
171
|
+
return XT_UNDEFINED;
|
|
172
|
+
}
|
|
173
|
+
char error[256];
|
|
174
|
+
error[0] = '\0';
|
|
175
|
+
int verify = 1;
|
|
176
|
+
if (XT_IS_OBJECT(options)) {
|
|
177
|
+
xt_value reject = xt_object_get_cstr(options, "rejectUnauthorized");
|
|
178
|
+
if (!xt_truthy(xt_is_nullish(reject))) verify = xt_truthy(reject) ? 1 : 0;
|
|
179
|
+
}
|
|
180
|
+
xt_value caValue = XT_IS_OBJECT(options) ? xt_object_get_cstr(options, "ca") : xt_undefined();
|
|
181
|
+
const char *ca = XT_IS_STRING(caValue) ? xt_string_data(caValue) : NULL;
|
|
182
|
+
conn->tls = xt_tls_core_api->client_connect(host, port, host, verify, ca, NULL, NULL, "h2",
|
|
183
|
+
error, sizeof(error));
|
|
184
|
+
if (conn->tls == NULL) {
|
|
185
|
+
free(conn);
|
|
186
|
+
xt_throw(xt_h2_error(error));
|
|
187
|
+
return XT_UNDEFINED;
|
|
188
|
+
}
|
|
189
|
+
conn->fd = xt_tls_core_api->fd(conn->tls);
|
|
190
|
+
} else {
|
|
191
|
+
conn->fd = xt_h2_tcp_connect(host, port);
|
|
192
|
+
if (conn->fd < 0) {
|
|
193
|
+
free(conn);
|
|
194
|
+
xt_throw(xt_h2_error("http2.connect could not reach the server"));
|
|
195
|
+
return XT_UNDEFINED;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
nghttp2_session_callbacks *callbacks = NULL;
|
|
200
|
+
nghttp2_session_callbacks_new(&callbacks);
|
|
201
|
+
xt_h2_install_callbacks(callbacks);
|
|
202
|
+
nghttp2_session_client_new(&conn->session, callbacks, conn);
|
|
203
|
+
nghttp2_session_callbacks_del(callbacks);
|
|
204
|
+
nghttp2_submit_settings(conn->session, NGHTTP2_FLAG_NONE, NULL, 0);
|
|
205
|
+
|
|
206
|
+
conn->is_server = 0;
|
|
207
|
+
conn->session_value = xt_h2_session_object(conn);
|
|
208
|
+
xt_value session = conn->session_value;
|
|
209
|
+
xt_h2_attach(conn, session);
|
|
210
|
+
if (XT_IS_FUNCTION(listener)) {
|
|
211
|
+
xt_array_push(xt_node_listeners(session, "connect", 1), listener);
|
|
212
|
+
}
|
|
213
|
+
xt_node_emit0(session, "connect");
|
|
214
|
+
return session;
|
|
215
|
+
}
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/* -- shared HTTP/2 connection state --------------------------------------- */
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* nghttp2 owns the protocol state machine (framing, HPACK, flow control) and
|
|
5
|
+
* calls back into this file for every header/data/close event. One
|
|
6
|
+
* `xt_h2_conn` bridges a socket (plain or TLS) to one nghttp2 session and to
|
|
7
|
+
* the JavaScript session object, which holds the stream table so callbacks can
|
|
8
|
+
* find the object a stream id belongs to.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
#include "../../../xt_http2/nghttp2_static.h"
|
|
12
|
+
|
|
13
|
+
#include <stdint.h>
|
|
14
|
+
#include <string.h>
|
|
15
|
+
|
|
16
|
+
#if defined(_WIN32)
|
|
17
|
+
#include <winsock2.h>
|
|
18
|
+
#define xt_h2_closesocket closesocket
|
|
19
|
+
#else
|
|
20
|
+
#include <sys/socket.h>
|
|
21
|
+
#include <unistd.h>
|
|
22
|
+
#define xt_h2_closesocket close
|
|
23
|
+
#endif
|
|
24
|
+
|
|
25
|
+
typedef struct xt_h2_conn xt_h2_conn;
|
|
26
|
+
|
|
27
|
+
/* Outgoing request/response body; nghttp2 pulls from it as flow-control credit
|
|
28
|
+
* becomes available. `source.ptr` in the data provider points here. */
|
|
29
|
+
typedef struct {
|
|
30
|
+
unsigned char *data;
|
|
31
|
+
size_t length;
|
|
32
|
+
size_t sent;
|
|
33
|
+
int ended;
|
|
34
|
+
} xt_h2_body;
|
|
35
|
+
|
|
36
|
+
struct xt_h2_conn {
|
|
37
|
+
nghttp2_session *session;
|
|
38
|
+
int fd;
|
|
39
|
+
xt_tls_conn *tls;
|
|
40
|
+
xt_value session_value;
|
|
41
|
+
xt_value owner;
|
|
42
|
+
int is_server;
|
|
43
|
+
int closed;
|
|
44
|
+
int in_recv;
|
|
45
|
+
int in_send;
|
|
46
|
+
int flush_pending;
|
|
47
|
+
int finish_requested;
|
|
48
|
+
char scheme[8];
|
|
49
|
+
char authority[512];
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/* Byte helpers exported by buffer.c (linked through the node extension). */
|
|
53
|
+
xt_value xt_buffer_ctor(int32_t argc, xt_value *argv);
|
|
54
|
+
void xt_node_buffer_set(xt_value value, uint32_t index, unsigned char byte);
|
|
55
|
+
|
|
56
|
+
static xt_h2_conn *xt_h2_conn_of(xt_value object) {
|
|
57
|
+
return (xt_h2_conn *)(uintptr_t)xt_to_number(xt_object_get_cstr(object, "__h2"));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
static xt_value xt_h2_error(const char *message) {
|
|
61
|
+
return xt_error_from_message(message != NULL && message[0] != '\0' ? message : "HTTP/2 error");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
static xt_value xt_h2_buffer(const unsigned char *data, size_t length) {
|
|
65
|
+
xt_value size = xt_number((double)length);
|
|
66
|
+
xt_value buffer = xt_buffer_ctor(1, &size);
|
|
67
|
+
for (size_t i = 0; i < length; i++) xt_node_buffer_set(buffer, (uint32_t)i, data[i]);
|
|
68
|
+
return buffer;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static xt_h2_body *xt_h2_body_new(void) {
|
|
72
|
+
xt_h2_body *body = (xt_h2_body *)calloc(1, sizeof(xt_h2_body));
|
|
73
|
+
return body;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
static void xt_h2_body_append(xt_h2_body *body, const unsigned char *data, size_t length) {
|
|
77
|
+
if (body == NULL || length == 0) return;
|
|
78
|
+
unsigned char *grown = (unsigned char *)realloc(body->data, body->length + length);
|
|
79
|
+
if (grown == NULL) return;
|
|
80
|
+
body->data = grown;
|
|
81
|
+
memcpy(body->data + body->length, data, length);
|
|
82
|
+
body->length += length;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The body pointer stored on a JS object, or NULL for "no body". */
|
|
86
|
+
static xt_h2_body *xt_h2_body_of(xt_value object) {
|
|
87
|
+
xt_value value = xt_object_get_cstr(object, "__body");
|
|
88
|
+
if (!XT_IS_NUMBER(value)) return NULL;
|
|
89
|
+
double raw = xt_to_number(value);
|
|
90
|
+
if (!(raw > 0)) return NULL;
|
|
91
|
+
return (xt_h2_body *)(uintptr_t)raw;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
static void xt_h2_body_free(xt_h2_body *body) {
|
|
95
|
+
if (body == NULL) return;
|
|
96
|
+
free(body->data);
|
|
97
|
+
free(body);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
static int xt_h2_write_all(xt_h2_conn *conn, const unsigned char *data, size_t length) {
|
|
101
|
+
if (conn->tls != NULL) return xt_tls_core_api->write(conn->tls, data, length);
|
|
102
|
+
size_t sent = 0;
|
|
103
|
+
while (sent < length) {
|
|
104
|
+
#if defined(_WIN32)
|
|
105
|
+
int written = send(conn->fd, (const char *)data + sent, (int)(length - sent), 0);
|
|
106
|
+
#else
|
|
107
|
+
ssize_t written = send(conn->fd, data + sent, length - sent, 0);
|
|
108
|
+
#endif
|
|
109
|
+
if (written <= 0) return -1;
|
|
110
|
+
sent += (size_t)written;
|
|
111
|
+
}
|
|
112
|
+
return 0;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
static int xt_h2_recv_some(xt_h2_conn *conn, unsigned char *buffer, size_t capacity) {
|
|
116
|
+
if (conn->tls != NULL) return xt_tls_core_api->read(conn->tls, buffer, capacity);
|
|
117
|
+
#if defined(_WIN32)
|
|
118
|
+
return recv(conn->fd, (char *)buffer, (int)capacity, 0);
|
|
119
|
+
#else
|
|
120
|
+
ssize_t count = recv(conn->fd, buffer, capacity, 0);
|
|
121
|
+
return count > 2147483647 ? 2147483647 : (int)count;
|
|
122
|
+
#endif
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Free every request/response body once nghttp2 can no longer reference them. */
|
|
126
|
+
static void xt_h2_release_bodies(xt_h2_conn *conn) {
|
|
127
|
+
xt_value streams = xt_object_get_cstr(conn->session_value, "__streams");
|
|
128
|
+
if (!XT_IS_ARRAY(streams)) return;
|
|
129
|
+
int32_t count = (int32_t)xt_to_number(xt_array_length(streams));
|
|
130
|
+
for (int32_t i = 0; i < count; i++) {
|
|
131
|
+
xt_value stream = xt_array_get(streams, xt_number((double)i));
|
|
132
|
+
if (!XT_IS_OBJECT(stream)) continue;
|
|
133
|
+
xt_h2_body *body = xt_h2_body_of(stream);
|
|
134
|
+
if (body != NULL) xt_h2_body_free(body);
|
|
135
|
+
xt_node_set(stream, "__body", xt_number(0));
|
|
136
|
+
xt_value response = xt_object_get_cstr(stream, "__res");
|
|
137
|
+
if (XT_IS_OBJECT(response)) {
|
|
138
|
+
xt_h2_body *responseBody = xt_h2_body_of(response);
|
|
139
|
+
if (responseBody != NULL) xt_h2_body_free(responseBody);
|
|
140
|
+
xt_node_set(response, "__body", xt_number(0));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
static void xt_h2_finish(xt_h2_conn *conn, const char *error) {
|
|
146
|
+
if (conn == NULL || conn->closed) return;
|
|
147
|
+
/* A JavaScript listener can call `session.close()` from a nghttp2 callback
|
|
148
|
+
* (`end`/`close` fire there). Deleting the session while `mem_recv2`/
|
|
149
|
+
* `mem_send2` is still running would free its state under the library, so the
|
|
150
|
+
* teardown is deferred until the current call returns. */
|
|
151
|
+
if (conn->in_recv || conn->in_send) {
|
|
152
|
+
conn->finish_requested = 1;
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
conn->closed = 1;
|
|
156
|
+
if (conn->fd >= 0) xt_loop_remove(conn->fd);
|
|
157
|
+
if (conn->session != NULL) {
|
|
158
|
+
nghttp2_session_del(conn->session);
|
|
159
|
+
conn->session = NULL;
|
|
160
|
+
}
|
|
161
|
+
/* Only now is it safe: nghttp2's data providers referenced these buffers. */
|
|
162
|
+
xt_h2_release_bodies(conn);
|
|
163
|
+
if (conn->tls != NULL) {
|
|
164
|
+
xt_tls_core_api->close(conn->tls);
|
|
165
|
+
conn->tls = NULL;
|
|
166
|
+
} else if (conn->fd >= 0) {
|
|
167
|
+
xt_h2_closesocket(conn->fd);
|
|
168
|
+
}
|
|
169
|
+
conn->fd = -1;
|
|
170
|
+
xt_node_set(conn->session_value, "__closed", xt_bool(1));
|
|
171
|
+
if (error != NULL) xt_node_emit1(conn->session_value, "error", xt_h2_error(error));
|
|
172
|
+
xt_node_emit0(conn->session_value, "close");
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
static int xt_h2_flush(xt_h2_conn *conn) {
|
|
176
|
+
if (conn == NULL || conn->session == NULL || conn->closed) return -1;
|
|
177
|
+
if (conn->in_recv) {
|
|
178
|
+
conn->flush_pending = 1;
|
|
179
|
+
return 0;
|
|
180
|
+
}
|
|
181
|
+
for (;;) {
|
|
182
|
+
if (conn->finish_requested) break;
|
|
183
|
+
const uint8_t *data = NULL;
|
|
184
|
+
conn->in_send = 1;
|
|
185
|
+
nghttp2_ssize length = nghttp2_session_mem_send2(conn->session, &data);
|
|
186
|
+
conn->in_send = 0;
|
|
187
|
+
if (length < 0) {
|
|
188
|
+
xt_h2_finish(conn, "HTTP/2 send failed");
|
|
189
|
+
return -1;
|
|
190
|
+
}
|
|
191
|
+
if (length == 0) break;
|
|
192
|
+
if (xt_h2_write_all(conn, data, (size_t)length) != 0) {
|
|
193
|
+
xt_h2_finish(conn, "HTTP/2 connection lost");
|
|
194
|
+
return -1;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (conn->finish_requested) {
|
|
198
|
+
xt_h2_finish(conn, NULL);
|
|
199
|
+
return -1;
|
|
200
|
+
}
|
|
201
|
+
if (!nghttp2_session_want_read(conn->session) && !nghttp2_session_want_write(conn->session)) {
|
|
202
|
+
xt_h2_finish(conn, NULL);
|
|
203
|
+
return -1;
|
|
204
|
+
}
|
|
205
|
+
return 0;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
static void xt_h2_on_readable(void *userdata, int events) {
|
|
209
|
+
(void)events;
|
|
210
|
+
xt_h2_conn *conn = (xt_h2_conn *)userdata;
|
|
211
|
+
if (conn == NULL || conn->closed) return;
|
|
212
|
+
unsigned char buffer[16384];
|
|
213
|
+
int count = xt_h2_recv_some(conn, buffer, sizeof(buffer));
|
|
214
|
+
if (count > 0) {
|
|
215
|
+
conn->in_recv = 1;
|
|
216
|
+
nghttp2_ssize consumed = nghttp2_session_mem_recv2(conn->session, buffer, (size_t)count);
|
|
217
|
+
conn->in_recv = 0;
|
|
218
|
+
if (conn->finish_requested) {
|
|
219
|
+
xt_h2_finish(conn, NULL);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
if (consumed < 0) {
|
|
223
|
+
xt_h2_finish(conn, "HTTP/2 protocol error");
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (conn->flush_pending) {
|
|
227
|
+
conn->flush_pending = 0;
|
|
228
|
+
xt_h2_flush(conn);
|
|
229
|
+
}
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
if (count == 0) {
|
|
233
|
+
xt_h2_finish(conn, NULL);
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
xt_h2_finish(conn, "HTTP/2 connection lost");
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
static void xt_h2_attach(xt_h2_conn *conn, xt_value session) {
|
|
240
|
+
conn->session_value = session;
|
|
241
|
+
/* The session object is the anchor for the stream table and the event target
|
|
242
|
+
* for callbacks; keep the C-side slot alive even if JavaScript drops its last
|
|
243
|
+
* reference while I/O is pending. The connection box is never freed, so the
|
|
244
|
+
* root slot stays valid for the process lifetime. */
|
|
245
|
+
xt_gc_add_root(&conn->session_value);
|
|
246
|
+
if (conn->fd >= 0) xt_loop_add(conn->fd, XT_IO_READ, xt_h2_on_readable, conn);
|
|
247
|
+
xt_h2_flush(conn);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/* -- stream table --------------------------------------------------------- */
|
|
251
|
+
|
|
252
|
+
static xt_value xt_h2_stream_at(xt_h2_conn *conn, int32_t id) {
|
|
253
|
+
xt_value streams = xt_object_get_cstr(conn->session_value, "__streams");
|
|
254
|
+
if (!XT_IS_ARRAY(streams)) return XT_UNDEFINED;
|
|
255
|
+
return xt_array_get(streams, xt_number((double)id));
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
static void xt_h2_stream_put(xt_h2_conn *conn, int32_t id, xt_value stream) {
|
|
259
|
+
xt_value streams = xt_object_get_cstr(conn->session_value, "__streams");
|
|
260
|
+
if (XT_IS_ARRAY(streams)) xt_array_set(streams, xt_number((double)id), stream);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/* -- header object -------------------------------------------------------- */
|
|
264
|
+
|
|
265
|
+
static void xt_h2_header_add(xt_value headers, const uint8_t *name, size_t nameLength,
|
|
266
|
+
const uint8_t *value, size_t valueLength) {
|
|
267
|
+
char key[256];
|
|
268
|
+
size_t keyLength = nameLength < sizeof(key) - 1 ? nameLength : sizeof(key) - 1;
|
|
269
|
+
memcpy(key, name, keyLength);
|
|
270
|
+
key[keyLength] = '\0';
|
|
271
|
+
xt_value keyValue = xt_string_from_cstr(key);
|
|
272
|
+
xt_value text = xt_string_new((const char *)value, valueLength);
|
|
273
|
+
xt_value existing = xt_object_get(headers, keyValue);
|
|
274
|
+
if (XT_IS_STRING(existing)) {
|
|
275
|
+
const char *previous = xt_string_data(existing);
|
|
276
|
+
char *combined = (char *)malloc(strlen(previous) + valueLength + 3);
|
|
277
|
+
if (combined != NULL) {
|
|
278
|
+
sprintf(combined, "%s, ", previous);
|
|
279
|
+
memcpy(combined + strlen(previous) + 2, value, valueLength);
|
|
280
|
+
combined[strlen(previous) + 2 + valueLength] = '\0';
|
|
281
|
+
xt_value merged = xt_string_from_cstr(combined);
|
|
282
|
+
free(combined);
|
|
283
|
+
xt_object_set(headers, keyValue, merged);
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
xt_object_set(headers, keyValue, text);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/* -- URL / authority ------------------------------------------------------ */
|
|
291
|
+
|
|
292
|
+
static int xt_h2_parse_authority(const char *url, char *scheme, size_t schemeSize, char *host,
|
|
293
|
+
size_t hostSize, int *port, char *authority, size_t authoritySize) {
|
|
294
|
+
if (url == NULL) return -1;
|
|
295
|
+
const char *cursor = url;
|
|
296
|
+
int secure = 0;
|
|
297
|
+
if (strncmp(cursor, "https://", 8) == 0) {
|
|
298
|
+
secure = 1;
|
|
299
|
+
cursor += 8;
|
|
300
|
+
snprintf(scheme, schemeSize, "https");
|
|
301
|
+
*port = 443;
|
|
302
|
+
} else if (strncmp(cursor, "http://", 7) == 0) {
|
|
303
|
+
cursor += 7;
|
|
304
|
+
snprintf(scheme, schemeSize, "http");
|
|
305
|
+
*port = 80;
|
|
306
|
+
} else {
|
|
307
|
+
return -1;
|
|
308
|
+
}
|
|
309
|
+
const char *end = cursor;
|
|
310
|
+
while (*end != '\0' && *end != '/' && *end != '?' && *end != '#') end++;
|
|
311
|
+
const char *hostStart = cursor;
|
|
312
|
+
for (const char *p = cursor; p < end; p++) {
|
|
313
|
+
if (*p == '@') hostStart = p + 1;
|
|
314
|
+
}
|
|
315
|
+
const char *hostEnd = end;
|
|
316
|
+
const char *colon = memchr(hostStart, ':', (size_t)(end - hostStart));
|
|
317
|
+
if (colon != NULL) {
|
|
318
|
+
hostEnd = colon;
|
|
319
|
+
*port = atoi(colon + 1);
|
|
320
|
+
}
|
|
321
|
+
size_t hostLength = (size_t)(hostEnd - hostStart);
|
|
322
|
+
if (hostLength == 0 || hostLength >= hostSize) return -1;
|
|
323
|
+
memcpy(host, hostStart, hostLength);
|
|
324
|
+
host[hostLength] = '\0';
|
|
325
|
+
size_t authorityLength = (size_t)(end - cursor);
|
|
326
|
+
if (authorityLength >= authoritySize) return -1;
|
|
327
|
+
memcpy(authority, cursor, authorityLength);
|
|
328
|
+
authority[authorityLength] = '\0';
|
|
329
|
+
return secure;
|
|
330
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/* -- nghttp2 callbacks ---------------------------------------------------- */
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Header values stay in nghttp2-owned buffers for the duration of the callback,
|
|
5
|
+
* so each one is copied into the JS header object immediately. Pseudo headers
|
|
6
|
+
* are mirrored onto the request/stream (`method`, `url`, `scheme`,
|
|
7
|
+
* `authority`, `status`) because that is how Node's `Http2Stream` exposes
|
|
8
|
+
* them.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
static int xt_h2_on_begin_headers(nghttp2_session *session, const nghttp2_frame *frame,
|
|
12
|
+
void *user_data) {
|
|
13
|
+
(void)session;
|
|
14
|
+
xt_h2_conn *conn = (xt_h2_conn *)user_data;
|
|
15
|
+
if (!conn->is_server || frame->hd.type != NGHTTP2_HEADERS) return 0;
|
|
16
|
+
if (frame->headers.cat != NGHTTP2_HCAT_REQUEST) return 0;
|
|
17
|
+
int32_t id = frame->hd.stream_id;
|
|
18
|
+
xt_value request = xt_object_new_with_proto(xt_h2_request_proto());
|
|
19
|
+
xt_node_set(request, "__h2", xt_number((double)(uintptr_t)conn));
|
|
20
|
+
xt_node_set(request, "__id", xt_number((double)id));
|
|
21
|
+
xt_node_set(request, "__headers", xt_object_new());
|
|
22
|
+
xt_node_set(request, "__encoding", xt_undefined());
|
|
23
|
+
xt_node_set(request, "__ended", xt_bool(0));
|
|
24
|
+
xt_node_set(request, "__closed", xt_bool(0));
|
|
25
|
+
/* Server requests carry no outgoing body; the response owns one. */
|
|
26
|
+
xt_node_set(request, "__body", xt_number(0));
|
|
27
|
+
xt_node_set(request, "__res", xt_undefined());
|
|
28
|
+
xt_node_set(request, "httpVersion", xt_string_from_cstr("2.0"));
|
|
29
|
+
xt_node_set(request, "stream", request);
|
|
30
|
+
xt_h2_stream_put(conn, id, request);
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static int xt_h2_on_header(nghttp2_session *session, const nghttp2_frame *frame,
|
|
35
|
+
const uint8_t *name, size_t namelen, const uint8_t *value,
|
|
36
|
+
size_t valuelen, uint8_t flags, void *user_data) {
|
|
37
|
+
(void)session;
|
|
38
|
+
(void)flags;
|
|
39
|
+
xt_h2_conn *conn = (xt_h2_conn *)user_data;
|
|
40
|
+
xt_value stream = xt_h2_stream_at(conn, frame->hd.stream_id);
|
|
41
|
+
if (!XT_IS_OBJECT(stream)) return 0;
|
|
42
|
+
xt_value headers = xt_object_get_cstr(stream, "__headers");
|
|
43
|
+
xt_h2_header_add(headers, name, namelen, value, valuelen);
|
|
44
|
+
if (namelen > 0 && name[0] == ':') {
|
|
45
|
+
xt_value text = xt_string_new((const char *)value, valuelen);
|
|
46
|
+
if (namelen == 7 && memcmp(name, ":method", 7) == 0) xt_node_set(stream, "method", text);
|
|
47
|
+
else if (namelen == 5 && memcmp(name, ":path", 5) == 0) xt_node_set(stream, "url", text);
|
|
48
|
+
else if (namelen == 7 && memcmp(name, ":scheme", 7) == 0) xt_node_set(stream, "scheme", text);
|
|
49
|
+
else if (namelen == 10 && memcmp(name, ":authority", 10) == 0) xt_node_set(stream, "authority", text);
|
|
50
|
+
else if (namelen == 7 && memcmp(name, ":status", 7) == 0) {
|
|
51
|
+
xt_node_set(stream, "status", xt_number(xt_to_number(text)));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return 0;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static int xt_h2_on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame,
|
|
58
|
+
void *user_data) {
|
|
59
|
+
(void)session;
|
|
60
|
+
xt_h2_conn *conn = (xt_h2_conn *)user_data;
|
|
61
|
+
int32_t id = frame->hd.stream_id;
|
|
62
|
+
if (frame->hd.type == NGHTTP2_HEADERS) {
|
|
63
|
+
xt_value stream = xt_h2_stream_at(conn, id);
|
|
64
|
+
if (!XT_IS_OBJECT(stream)) return 0;
|
|
65
|
+
if (conn->is_server) {
|
|
66
|
+
if (frame->headers.cat != NGHTTP2_HCAT_REQUEST) return 0;
|
|
67
|
+
xt_value response = xt_object_new_with_proto(xt_h2_response_proto());
|
|
68
|
+
xt_node_set(response, "__h2", xt_number((double)(uintptr_t)conn));
|
|
69
|
+
xt_node_set(response, "__id", xt_number((double)id));
|
|
70
|
+
xt_node_set(response, "__headers", xt_object_new());
|
|
71
|
+
xt_node_set(response, "__status", xt_number(200));
|
|
72
|
+
xt_node_set(response, "__body", xt_number((double)(uintptr_t)xt_h2_body_new()));
|
|
73
|
+
xt_node_set(response, "__submitted", xt_bool(0));
|
|
74
|
+
xt_node_set(response, "__closed", xt_bool(0));
|
|
75
|
+
xt_node_set(response, "stream", stream);
|
|
76
|
+
xt_node_set(stream, "__res", response);
|
|
77
|
+
/* Node's `stream` event is `(stream, headers)`, and the stream itself
|
|
78
|
+
* carries the response API. */
|
|
79
|
+
xt_value streamArgs[2] = {stream, xt_object_get_cstr(stream, "__headers")};
|
|
80
|
+
if (XT_IS_OBJECT(conn->owner)) xt_node_emit(conn->owner, "stream", 2, streamArgs);
|
|
81
|
+
xt_node_emit(conn->session_value, "stream", 2, streamArgs);
|
|
82
|
+
} else {
|
|
83
|
+
xt_value flags = xt_object_new();
|
|
84
|
+
xt_node_set(flags, "endStream",
|
|
85
|
+
xt_bool((frame->hd.flags & NGHTTP2_FLAG_END_STREAM) != 0));
|
|
86
|
+
xt_value responseArgs[2] = {xt_object_get_cstr(stream, "__headers"), flags};
|
|
87
|
+
xt_node_emit(stream, "response", 2, responseArgs);
|
|
88
|
+
}
|
|
89
|
+
} else if (frame->hd.type == NGHTTP2_DATA) {
|
|
90
|
+
if ((frame->hd.flags & NGHTTP2_FLAG_END_STREAM) == 0) return 0;
|
|
91
|
+
xt_value stream = xt_h2_stream_at(conn, id);
|
|
92
|
+
if (!XT_IS_OBJECT(stream)) return 0;
|
|
93
|
+
if (!xt_truthy(xt_object_get_cstr(stream, "__ended"))) {
|
|
94
|
+
xt_node_set(stream, "__ended", xt_bool(1));
|
|
95
|
+
xt_node_emit0(stream, "end");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return 0;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
static int xt_h2_on_data_chunk_recv(nghttp2_session *session, uint8_t flags, int32_t stream_id,
|
|
102
|
+
const uint8_t *data, size_t len, void *user_data) {
|
|
103
|
+
(void)session;
|
|
104
|
+
(void)flags;
|
|
105
|
+
xt_h2_conn *conn = (xt_h2_conn *)user_data;
|
|
106
|
+
xt_value stream = xt_h2_stream_at(conn, stream_id);
|
|
107
|
+
if (!XT_IS_OBJECT(stream)) return 0;
|
|
108
|
+
xt_value encoding = xt_object_get_cstr(stream, "__encoding");
|
|
109
|
+
if (XT_IS_STRING(encoding)) {
|
|
110
|
+
const char *name = xt_string_data(encoding);
|
|
111
|
+
xt_node_emit1(stream, "data", xt_node_encode(data, len, name));
|
|
112
|
+
} else {
|
|
113
|
+
xt_node_emit1(stream, "data", xt_h2_buffer(data, len));
|
|
114
|
+
}
|
|
115
|
+
return 0;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
static int xt_h2_on_stream_close(nghttp2_session *session, int32_t stream_id,
|
|
119
|
+
uint32_t error_code, void *user_data) {
|
|
120
|
+
(void)session;
|
|
121
|
+
(void)error_code;
|
|
122
|
+
xt_h2_conn *conn = (xt_h2_conn *)user_data;
|
|
123
|
+
xt_value stream = xt_h2_stream_at(conn, stream_id);
|
|
124
|
+
if (!XT_IS_OBJECT(stream)) return 0;
|
|
125
|
+
xt_value response = xt_object_get_cstr(stream, "__res");
|
|
126
|
+
if (!xt_truthy(xt_object_get_cstr(stream, "__ended"))) {
|
|
127
|
+
xt_node_set(stream, "__ended", xt_bool(1));
|
|
128
|
+
xt_node_emit0(stream, "end");
|
|
129
|
+
}
|
|
130
|
+
xt_node_set(stream, "__closed", xt_bool(1));
|
|
131
|
+
xt_node_emit0(stream, "close");
|
|
132
|
+
/* Bodies are released when the session is torn down: nghttp2 may still hold
|
|
133
|
+
* the data provider it was given while it reports the close. */
|
|
134
|
+
if (XT_IS_OBJECT(response)) {
|
|
135
|
+
xt_node_set(response, "__closed", xt_bool(1));
|
|
136
|
+
xt_node_emit0(response, "close");
|
|
137
|
+
}
|
|
138
|
+
return 0;
|
|
139
|
+
}
|