Skip to content

Commit

Permalink
feat: add event.client.tlsProtocol
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeChampion committed Jul 7, 2023
1 parent 49b0c99 commit 4c91142
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
19 changes: 19 additions & 0 deletions runtime/js-compute-runtime/builtins/client-info.cpp
Expand Up @@ -125,6 +125,24 @@ bool ClientInfo::tls_cipher_openssl_name_get(JSContext *cx, unsigned argc, JS::V
args.rval().setString(result);
return true;
}


bool ClientInfo::tls_protocol_get(JSContext *cx, unsigned argc, JS::Value *vp) {
METHOD_HEADER(0);

auto res = HttpReq::http_req_downstream_tls_protocol();
if (auto *err = res.to_err()) {
HANDLE_ERROR(cx, *err);
return false;
}

HostString protocol = std::move(res.unwrap());
JS::RootedString result(cx, JS_NewStringCopyN(cx, protocol.ptr.get(), protocol.len));

args.rval().setString(result);
return true;
}

const JSFunctionSpec ClientInfo::static_methods[] = {
JS_FS_END,
};
Expand All @@ -141,6 +159,7 @@ const JSPropertySpec ClientInfo::properties[] = {
JS_PSG("address", address_get, JSPROP_ENUMERATE),
JS_PSG("geo", geo_get, JSPROP_ENUMERATE),
JS_PSG("tlsCipherOpensslName", tls_cipher_openssl_name_get, JSPROP_ENUMERATE),
JS_PSG("tlsProtocol", tls_protocol_get, JSPROP_ENUMERATE),
JS_PS_END,
};

Expand Down
1 change: 1 addition & 0 deletions runtime/js-compute-runtime/builtins/client-info.h
Expand Up @@ -9,6 +9,7 @@ class ClientInfo final : public BuiltinNoConstructor<ClientInfo> {
static bool address_get(JSContext *cx, unsigned argc, JS::Value *vp);
static bool geo_get(JSContext *cx, unsigned argc, JS::Value *vp);
static bool tls_cipher_openssl_name_get(JSContext *cx, unsigned argc, JS::Value *vp);
static bool tls_protocol_get(JSContext *cx, unsigned argc, JS::Value *vp);

public:
static constexpr const char *class_name = "FetchEvent";
Expand Down
Expand Up @@ -194,6 +194,12 @@ bool fastly_http_req_downstream_tls_cipher_openssl_name(fastly_world_string_t *r
err);
}

bool fastly_http_req_downstream_tls_protocol(fastly_world_string_t *ret, fastly_error_t *err) {
ret->ptr = static_cast<char *>(cabi_malloc(32, 1));
return convert_result(
fastly::req_downstream_tls_protocol(reinterpret_cast<char *>(ret->ptr), 32, &ret->len), err);
}

bool fastly_http_req_new(fastly_request_handle_t *ret, fastly_error_t *err) {
return convert_result(fastly::req_new(ret), err);
}
Expand Down
8 changes: 2 additions & 6 deletions runtime/js-compute-runtime/host_interface/fastly.h
Expand Up @@ -152,12 +152,8 @@ int req_downstream_client_ip_addr_get(char *octets, size_t *nwritten);
WASM_IMPORT("fastly_http_req", "downstream_tls_cipher_openssl_name")
int req_downstream_tls_cipher_openssl_name(char *ret, size_t ret_len, size_t *nwritten);

// (@interface func (export "downstream_tls_cipher_openssl_name")
// (param $cipher_out (@witx pointer char8))
// (param $cipher_max_len (@witx usize))
// (param $nwritten_out (@witx pointer (@witx usize)))
// (result $err $fastly_status)
// )
WASM_IMPORT("fastly_http_req", "downstream_tls_protocol")
int req_downstream_tls_protocol(char *ret, size_t ret_len, size_t *nwritten);

// (@interface func (export "downstream_tls_protocol")
// (param $protocol_out (@witx pointer char8))
Expand Down
15 changes: 15 additions & 0 deletions runtime/js-compute-runtime/host_interface/host_api.cpp
Expand Up @@ -516,6 +516,21 @@ Result<HostString> HttpReq::http_req_downstream_tls_cipher_openssl_name() {

return res;
}

// http-req-downstream-tls-protocol: func() -> result<string, error>
Result<HostString> HttpReq::http_req_downstream_tls_protocol() {
Result<HostString> res;

fastly_error_t err;
fastly_world_string_t ret;
if (!fastly_http_req_downstream_tls_protocol(&ret, &err)) {
res.emplace_err(err);
} else {
res.emplace(ret);
}

return res;
}
bool HttpReq::is_valid() const { return this->handle != HttpReq::invalid; }

Result<fastly_http_version_t> HttpReq::get_version() const {
Expand Down
2 changes: 2 additions & 0 deletions runtime/js-compute-runtime/host_interface/host_api.h
Expand Up @@ -257,6 +257,8 @@ class HttpReq final : public HttpBase {

static Result<HostString> http_req_downstream_tls_cipher_openssl_name();

static Result<HostString> http_req_downstream_tls_protocol();

/// Send this request synchronously, and wait for the response.
Result<Response> send(HttpBody body, std::string_view backend);

Expand Down

0 comments on commit 4c91142

Please sign in to comment.