Skip to content

Commit 4c91142

Browse files
Jake ChampionJakeChampion
authored andcommitted
feat: add event.client.tlsProtocol
1 parent 49b0c99 commit 4c91142

File tree

6 files changed

+45
-6
lines changed

6 files changed

+45
-6
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,24 @@ bool ClientInfo::tls_cipher_openssl_name_get(JSContext *cx, unsigned argc, JS::V
125125
args.rval().setString(result);
126126
return true;
127127
}
128+
129+
130+
bool ClientInfo::tls_protocol_get(JSContext *cx, unsigned argc, JS::Value *vp) {
131+
METHOD_HEADER(0);
132+
133+
auto res = HttpReq::http_req_downstream_tls_protocol();
134+
if (auto *err = res.to_err()) {
135+
HANDLE_ERROR(cx, *err);
136+
return false;
137+
}
138+
139+
HostString protocol = std::move(res.unwrap());
140+
JS::RootedString result(cx, JS_NewStringCopyN(cx, protocol.ptr.get(), protocol.len));
141+
142+
args.rval().setString(result);
143+
return true;
144+
}
145+
128146
const JSFunctionSpec ClientInfo::static_methods[] = {
129147
JS_FS_END,
130148
};
@@ -141,6 +159,7 @@ const JSPropertySpec ClientInfo::properties[] = {
141159
JS_PSG("address", address_get, JSPROP_ENUMERATE),
142160
JS_PSG("geo", geo_get, JSPROP_ENUMERATE),
143161
JS_PSG("tlsCipherOpensslName", tls_cipher_openssl_name_get, JSPROP_ENUMERATE),
162+
JS_PSG("tlsProtocol", tls_protocol_get, JSPROP_ENUMERATE),
144163
JS_PS_END,
145164
};
146165

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class ClientInfo final : public BuiltinNoConstructor<ClientInfo> {
99
static bool address_get(JSContext *cx, unsigned argc, JS::Value *vp);
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);
12+
static bool tls_protocol_get(JSContext *cx, unsigned argc, JS::Value *vp);
1213

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

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ bool fastly_http_req_downstream_tls_cipher_openssl_name(fastly_world_string_t *r
194194
err);
195195
}
196196

197+
bool fastly_http_req_downstream_tls_protocol(fastly_world_string_t *ret, fastly_error_t *err) {
198+
ret->ptr = static_cast<char *>(cabi_malloc(32, 1));
199+
return convert_result(
200+
fastly::req_downstream_tls_protocol(reinterpret_cast<char *>(ret->ptr), 32, &ret->len), err);
201+
}
202+
197203
bool fastly_http_req_new(fastly_request_handle_t *ret, fastly_error_t *err) {
198204
return convert_result(fastly::req_new(ret), err);
199205
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,8 @@ int req_downstream_client_ip_addr_get(char *octets, size_t *nwritten);
152152
WASM_IMPORT("fastly_http_req", "downstream_tls_cipher_openssl_name")
153153
int req_downstream_tls_cipher_openssl_name(char *ret, size_t ret_len, size_t *nwritten);
154154

155-
// (@interface func (export "downstream_tls_cipher_openssl_name")
156-
// (param $cipher_out (@witx pointer char8))
157-
// (param $cipher_max_len (@witx usize))
158-
// (param $nwritten_out (@witx pointer (@witx usize)))
159-
// (result $err $fastly_status)
160-
// )
155+
WASM_IMPORT("fastly_http_req", "downstream_tls_protocol")
156+
int req_downstream_tls_protocol(char *ret, size_t ret_len, size_t *nwritten);
161157

162158
// (@interface func (export "downstream_tls_protocol")
163159
// (param $protocol_out (@witx pointer char8))

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

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

517517
return res;
518518
}
519+
520+
// http-req-downstream-tls-protocol: func() -> result<string, error>
521+
Result<HostString> HttpReq::http_req_downstream_tls_protocol() {
522+
Result<HostString> res;
523+
524+
fastly_error_t err;
525+
fastly_world_string_t ret;
526+
if (!fastly_http_req_downstream_tls_protocol(&ret, &err)) {
527+
res.emplace_err(err);
528+
} else {
529+
res.emplace(ret);
530+
}
531+
532+
return res;
533+
}
519534
bool HttpReq::is_valid() const { return this->handle != HttpReq::invalid; }
520535

521536
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
@@ -257,6 +257,8 @@ class HttpReq final : public HttpBase {
257257

258258
static Result<HostString> http_req_downstream_tls_cipher_openssl_name();
259259

260+
static Result<HostString> http_req_downstream_tls_protocol();
261+
260262
/// Send this request synchronously, and wait for the response.
261263
Result<Response> send(HttpBody body, std::string_view backend);
262264

0 commit comments

Comments
 (0)