Skip to content

Commit 3d87cb2

Browse files
Jake ChampionJakeChampion
authored andcommitted
feat: add event.client.tlsClientHello
1 parent 4c91142 commit 3d87cb2

File tree

6 files changed

+55
-13
lines changed

6 files changed

+55
-13
lines changed

runtime/js-compute-runtime/builtins/client-info.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,29 @@ bool ClientInfo::tls_cipher_openssl_name_get(JSContext *cx, unsigned argc, JS::V
126126
return true;
127127
}
128128

129+
bool ClientInfo::tls_client_hello_get(JSContext *cx, unsigned argc, JS::Value *vp) {
130+
METHOD_HEADER(0);
131+
132+
auto res = HttpReq::http_req_downstream_tls_client_hello();
133+
if (auto *err = res.to_err()) {
134+
HANDLE_ERROR(cx, *err);
135+
return false;
136+
}
137+
138+
HostBytes hello = std::move(res.unwrap());
139+
JS::RootedObject buffer(cx, JS::NewArrayBufferWithContents(cx, hello.len, hello.ptr.get()));
140+
if (!buffer) {
141+
// We can be here if the array buffer was too large -- if that was the case then a
142+
// JSMSG_BAD_ARRAY_LENGTH will have been created.
143+
return false;
144+
}
145+
146+
// `hello` is now owned by `buffer`
147+
static_cast<void>(hello.ptr.release());
148+
149+
args.rval().setObject(*buffer);
150+
return true;
151+
}
129152

130153
bool ClientInfo::tls_protocol_get(JSContext *cx, unsigned argc, JS::Value *vp) {
131154
METHOD_HEADER(0);
@@ -160,6 +183,7 @@ const JSPropertySpec ClientInfo::properties[] = {
160183
JS_PSG("geo", geo_get, JSPROP_ENUMERATE),
161184
JS_PSG("tlsCipherOpensslName", tls_cipher_openssl_name_get, JSPROP_ENUMERATE),
162185
JS_PSG("tlsProtocol", tls_protocol_get, JSPROP_ENUMERATE),
186+
JS_PSG("tlsClientHello", tls_client_hello_get, JSPROP_ENUMERATE),
163187
JS_PS_END,
164188
};
165189

runtime/js-compute-runtime/builtins/client-info.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class ClientInfo final : public BuiltinNoConstructor<ClientInfo> {
1010
static bool geo_get(JSContext *cx, unsigned argc, JS::Value *vp);
1111
static bool tls_cipher_openssl_name_get(JSContext *cx, unsigned argc, JS::Value *vp);
1212
static bool tls_protocol_get(JSContext *cx, unsigned argc, JS::Value *vp);
13+
static bool tls_client_hello_get(JSContext *cx, unsigned argc, JS::Value *vp);
1314

1415
public:
1516
static constexpr const char *class_name = "FetchEvent";

runtime/js-compute-runtime/fastly-world/fastly_world_adapter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,17 @@ bool fastly_http_req_downstream_tls_protocol(fastly_world_string_t *ret, fastly_
200200
fastly::req_downstream_tls_protocol(reinterpret_cast<char *>(ret->ptr), 32, &ret->len), err);
201201
}
202202

203+
bool fastly_http_req_downstream_tls_client_hello(fastly_world_list_u8_t *ret, fastly_error_t *err) {
204+
auto default_size = 512;
205+
ret->ptr = static_cast<uint8_t *>(cabi_malloc(default_size, 4));
206+
auto status = fastly::req_downstream_tls_client_hello(reinterpret_cast<char *>(ret->ptr), default_size, &ret->len);
207+
if (status == FASTLY_ERROR_BUFFER_LEN) {
208+
cabi_realloc(ret->ptr, default_size, 4, ret->len);
209+
status = fastly::req_downstream_tls_client_hello(reinterpret_cast<char *>(ret->ptr), ret->len, &ret->len);
210+
}
211+
return convert_result(status, err);
212+
}
213+
203214
bool fastly_http_req_new(fastly_request_handle_t *ret, fastly_error_t *err) {
204215
return convert_result(fastly::req_new(ret), err);
205216
}

runtime/js-compute-runtime/host_interface/fastly.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,8 @@ int req_downstream_tls_cipher_openssl_name(char *ret, size_t ret_len, size_t *nw
155155
WASM_IMPORT("fastly_http_req", "downstream_tls_protocol")
156156
int req_downstream_tls_protocol(char *ret, size_t ret_len, size_t *nwritten);
157157

158-
// (@interface func (export "downstream_tls_protocol")
159-
// (param $protocol_out (@witx pointer char8))
160-
// (param $protocol_max_len (@witx usize))
161-
// (param $nwritten_out (@witx pointer (@witx usize)))
162-
// (result $err $fastly_status)
163-
// )
164-
165-
// (@interface func (export "downstream_tls_client_hello")
166-
// (param $chello_out (@witx pointer char8))
167-
// (param $chello_max_len (@witx usize))
168-
// (param $nwritten_out (@witx pointer (@witx usize)))
169-
// (result $err $fastly_status)
170-
// )
158+
WASM_IMPORT("fastly_http_req", "downstream_tls_client_hello")
159+
int req_downstream_tls_client_hello(char *ret, size_t ret_len, size_t *nwritten);
171160

172161
WASM_IMPORT("fastly_http_req", "new")
173162
int req_new(fastly_request_handle_t *req_handle_out);

runtime/js-compute-runtime/host_interface/host_api.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,21 @@ Result<HostString> HttpReq::http_req_downstream_tls_protocol() {
531531

532532
return res;
533533
}
534+
535+
// http-req-downstream-tls-client-hello: func() -> result<list<u8>, error>
536+
Result<HostBytes> HttpReq::http_req_downstream_tls_client_hello() {
537+
Result<HostBytes> res;
538+
539+
fastly_world_list_u8_t ret;
540+
fastly_error_t err;
541+
if (!fastly_http_req_downstream_tls_client_hello(&ret, &err)) {
542+
res.emplace_err(err);
543+
} else {
544+
res.emplace(ret);
545+
}
546+
547+
return res;
548+
}
534549
bool HttpReq::is_valid() const { return this->handle != HttpReq::invalid; }
535550

536551
Result<fastly_http_version_t> HttpReq::get_version() const {

runtime/js-compute-runtime/host_interface/host_api.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ class HttpReq final : public HttpBase {
259259

260260
static Result<HostString> http_req_downstream_tls_protocol();
261261

262+
static Result<HostBytes> http_req_downstream_tls_client_hello();
263+
262264
/// Send this request synchronously, and wait for the response.
263265
Result<Response> send(HttpBody body, std::string_view backend);
264266

0 commit comments

Comments
 (0)