xbintsc 0.6.22 → 0.6.24
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/driver/build-key.js +2 -1
- package/dist/src/driver/build-key.js.map +1 -1
- package/dist/src/driver/bundler/concatenate.d.ts +23 -0
- package/dist/src/driver/bundler/concatenate.js +210 -0
- package/dist/src/driver/bundler/concatenate.js.map +1 -0
- package/dist/src/driver/bundler/merge.d.ts +4 -4
- package/dist/src/driver/bundler/merge.js +24 -207
- package/dist/src/driver/bundler/merge.js.map +1 -1
- package/dist/src/driver/bundler/{namespace-import.d.ts → namespace-object.d.ts} +5 -1
- package/dist/src/driver/bundler/{namespace-import.js → namespace-object.js} +6 -2
- package/dist/src/driver/bundler/namespace-object.js.map +1 -0
- package/dist/src/driver/bundler/{reexport-fixup.d.ts → reexport-resolution.d.ts} +9 -3
- package/dist/src/driver/bundler/{reexport-fixup.js → reexport-resolution.js} +16 -6
- package/dist/src/driver/bundler/reexport-resolution.js.map +1 -0
- 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/link-features.js +36 -11
- package/dist/src/driver/link-features.js.map +1 -1
- package/dist/src/driver/modules.d.ts +12 -5
- package/dist/src/driver/modules.js +12 -5
- package/dist/src/driver/modules.js.map +1 -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/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/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.js +2 -1
- package/dist/src/extensions/registry.js.map +1 -1
- package/dist/src/extensions/storage.js +4 -3
- package/dist/src/extensions/storage.js.map +1 -1
- 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/deferred-reexport.test.js +25 -0
- package/dist/tests/driver/deferred-reexport.test.js.map +1 -1
- package/dist/tests/driver/link-features.test.js +9 -0
- package/dist/tests/driver/link-features.test.js.map +1 -1
- 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/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/storage.test.js +3 -2
- package/dist/tests/extensions/storage.test.js.map +1 -1
- package/doc/ai/contributing.md +14 -0
- package/doc/ai/extensions.md +5 -3
- package/doc/ai/language-support.md +1 -1
- package/doc/ai/zh-CN/contributing.md +12 -0
- package/doc/ai/zh-CN/extensions.md +4 -2
- package/doc/ai/zh-CN/language-support.md +1 -1
- package/doc/gui/api.md +1 -1
- package/doc/gui/build.md +5 -3
- package/doc/gui/rendering.md +1 -1
- package/doc/gui.md +1 -1
- package/doc/implemented/async.md +1 -1
- package/doc/implemented/extensions.md +1 -0
- 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 +2 -1
- package/doc/implemented.md +2 -2
- package/doc/unimplemented/node/modules.md +6 -6
- package/doc/unimplemented/toolchain.md +1 -1
- package/doc/zh-CN/gui/api.md +1 -1
- package/doc/zh-CN/gui/build.md +4 -2
- package/doc/zh-CN/gui/rendering.md +1 -1
- package/doc/zh-CN/gui.md +1 -1
- package/doc/zh-CN/implemented/async.md +1 -1
- package/doc/zh-CN/implemented/extensions.md +1 -0
- 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 +2 -1
- package/doc/zh-CN/implemented.md +2 -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_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/storage_internal.h +3 -3
- 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 +8 -7
- package/scripts/build-tls.ts +248 -0
- package/scripts/check-file-length.ts +4 -25
- package/scripts/fetch-vendored.ts +244 -0
- package/scripts/package-release.ts +13 -3
- package/src/driver/build-key.ts +7 -1
- package/src/driver/bundler/README.md +55 -0
- package/src/driver/bundler/concatenate.ts +244 -0
- package/src/driver/bundler/merge.ts +24 -215
- package/src/driver/bundler/{namespace-import.ts → namespace-object.ts} +5 -1
- package/src/driver/bundler/{reexport-fixup.ts → reexport-resolution.ts} +15 -5
- package/src/driver/fingerprint.ts +24 -0
- package/src/driver/link-features.ts +33 -11
- package/src/driver/modules.ts +12 -5
- package/src/driver/paths.ts +20 -0
- package/src/driver/runtime-objects.ts +25 -6
- package/src/driver/toolchain.ts +11 -2
- package/src/driver/version.ts +1 -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 +2 -1
- package/src/extensions/storage.ts +4 -3
- package/src/extensions/tls.ts +52 -0
- package/{runtime/ext_gui → vendor/stb}/stb_image.h +17 -1
- package/dist/src/driver/bundler/namespace-import.js.map +0 -1
- package/dist/src/driver/bundler/reexport-fixup.js.map +0 -1
- /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/xt_storage → vendor}/sqlite3/sqlite3.c +0 -0
- /package/{runtime/xt_storage → vendor}/sqlite3/sqlite3.h +0 -0
- /package/{runtime/ext_gui → vendor/stb}/stb_truetype.h +0 -0
|
@@ -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
|
+
}
|