diff options
Diffstat (limited to 'ext/http/01_http.js')
-rw-r--r-- | ext/http/01_http.js | 47 |
1 files changed, 44 insertions, 3 deletions
diff --git a/ext/http/01_http.js b/ext/http/01_http.js index eae742990..ad39ce257 100644 --- a/ext/http/01_http.js +++ b/ext/http/01_http.js @@ -30,10 +30,14 @@ _idleTimeoutTimeout, _serverHandleIdleTimeout, } = window.__bootstrap.webSocket; + const { TcpConn } = window.__bootstrap.net; + const { TlsConn } = window.__bootstrap.tls; + const { Deferred } = window.__bootstrap.streams; const { ArrayPrototypeIncludes, ArrayPrototypePush, ArrayPrototypeSome, + Error, ObjectPrototypeIsPrototypeOf, PromisePrototype, Set, @@ -53,10 +57,13 @@ } = window.__bootstrap.primordials; const connErrorSymbol = Symbol("connError"); + const _deferred = Symbol("upgradeHttpDeferred"); class HttpConn { #rid = 0; #closed = false; + #remoteAddr; + #localAddr; // This set holds resource ids of resources // that were created during lifecycle of this request. @@ -64,8 +71,10 @@ // as well. managedResources = new Set(); - constructor(rid) { + constructor(rid, remoteAddr, localAddr) { this.#rid = rid; + this.#remoteAddr = remoteAddr; + this.#localAddr = localAddr; } /** @returns {number} */ @@ -125,7 +134,13 @@ const signal = abortSignal.newSignal(); const request = fromInnerRequest(innerRequest, signal, "immutable"); - const respondWith = createRespondWith(this, streamRid); + const respondWith = createRespondWith( + this, + streamRid, + request, + this.#remoteAddr, + this.#localAddr, + ); return { request, respondWith }; } @@ -159,7 +174,13 @@ return core.opAsync("op_http_read", streamRid, buf); } - function createRespondWith(httpConn, streamRid) { + function createRespondWith( + httpConn, + streamRid, + request, + remoteAddr, + localAddr, + ) { return async function respondWith(resp) { try { if (ObjectPrototypeIsPrototypeOf(PromisePrototype, resp)) { @@ -282,6 +303,20 @@ } } + const deferred = request[_deferred]; + if (deferred) { + const res = await core.opAsync("op_http_upgrade", streamRid); + let conn; + if (res.connType === "tcp") { + conn = new TcpConn(res.connRid, remoteAddr, localAddr); + } else if (res.connType === "tls") { + conn = new TlsConn(res.connRid, remoteAddr, localAddr); + } else { + throw new Error("unreachable"); + } + + deferred.resolve([conn, res.readBuf]); + } const ws = resp[_ws]; if (ws) { const wsRid = await core.opAsync( @@ -425,8 +460,14 @@ return { response, socket }; } + function upgradeHttp(req) { + req[_deferred] = new Deferred(); + return req[_deferred].promise; + } + window.__bootstrap.http = { HttpConn, upgradeWebSocket, + upgradeHttp, }; })(this); |