diff options
Diffstat (limited to 'ext')
-rw-r--r-- | ext/http/01_http.js | 47 | ||||
-rw-r--r-- | ext/http/lib.rs | 6 | ||||
-rw-r--r-- | ext/net/ops_tls.rs | 2 |
3 files changed, 48 insertions, 7 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); diff --git a/ext/http/lib.rs b/ext/http/lib.rs index 535f52a6c..48a58067e 100644 --- a/ext/http/lib.rs +++ b/ext/http/lib.rs @@ -289,9 +289,9 @@ impl HttpAcceptor { } /// A resource representing a single HTTP request/response stream. -struct HttpStreamResource { +pub struct HttpStreamResource { conn: Rc<HttpConnResource>, - rd: AsyncRefCell<HttpRequestReader>, + pub rd: AsyncRefCell<HttpRequestReader>, wr: AsyncRefCell<HttpResponseWriter>, accept_encoding: RefCell<Encoding>, cancel_handle: CancelHandle, @@ -324,7 +324,7 @@ impl Resource for HttpStreamResource { } /// The read half of an HTTP stream. -enum HttpRequestReader { +pub enum HttpRequestReader { Headers(Request<Body>), Body(Peekable<Body>), Closed, diff --git a/ext/net/ops_tls.rs b/ext/net/ops_tls.rs index 05e007176..74301292b 100644 --- a/ext/net/ops_tls.rs +++ b/ext/net/ops_tls.rs @@ -127,7 +127,7 @@ impl TlsStream { Self::new(tcp, Connection::Server(tls)) } - fn into_split(self) -> (ReadHalf, WriteHalf) { + pub fn into_split(self) -> (ReadHalf, WriteHalf) { let shared = Shared::new(self); let rd = ReadHalf { shared: shared.clone(), |