mirror of
https://github.com/denoland/deno.git
synced 2025-01-11 08:33:43 -05:00
fix(node/http): add encrypted field to FakeSocket (#19886)
Fixes #19557
This commit is contained in:
parent
5ff040bf59
commit
da709729e3
3 changed files with 25 additions and 12 deletions
|
@ -637,7 +637,9 @@ Deno.test("[node/http] HTTPS server", async () => {
|
|||
const server = https.createServer({
|
||||
cert: Deno.readTextFileSync("cli/tests/testdata/tls/localhost.crt"),
|
||||
key: Deno.readTextFileSync("cli/tests/testdata/tls/localhost.key"),
|
||||
}, (_req, res) => {
|
||||
}, (req, res) => {
|
||||
// @ts-ignore: It exists on TLSSocket
|
||||
assert(req.socket.encrypted);
|
||||
res.end("success!");
|
||||
});
|
||||
server.listen(() => {
|
||||
|
@ -664,7 +666,9 @@ Deno.test(
|
|||
{ permissions: { net: true } },
|
||||
async () => {
|
||||
const promise = deferred();
|
||||
const server = http.createServer((_req, res) => {
|
||||
const server = http.createServer((req, res) => {
|
||||
// @ts-ignore: It exists on TLSSocket
|
||||
assert(!req.socket.encrypted);
|
||||
res.writeHead(200, { "Content-Type": "text/plain" });
|
||||
res.end("okay");
|
||||
});
|
||||
|
|
|
@ -278,6 +278,7 @@ class FakeSocket extends EventEmitter {
|
|||
super();
|
||||
this.remoteAddress = opts.hostname;
|
||||
this.remotePort = opts.port;
|
||||
this.encrypted = opts.encrypted;
|
||||
}
|
||||
|
||||
setKeepAlive() {}
|
||||
|
@ -561,7 +562,7 @@ class ClientRequest extends OutgoingMessage {
|
|||
this.onSocket(createConnection(optsWithoutSignal));
|
||||
}
|
||||
}*/
|
||||
this.onSocket(new FakeSocket());
|
||||
this.onSocket(new FakeSocket({ encrypted: this._encrypted }));
|
||||
|
||||
const url = this._createUrlStrFromOptions();
|
||||
|
||||
|
@ -1306,7 +1307,10 @@ export class ServerResponse extends NodeWritable {
|
|||
return status === 101 || status === 204 || status === 205 || status === 304;
|
||||
}
|
||||
|
||||
constructor(resolve: (value: Response | PromiseLike<Response>) => void) {
|
||||
constructor(
|
||||
resolve: (value: Response | PromiseLike<Response>) => void,
|
||||
socket: FakeSocket,
|
||||
) {
|
||||
let controller: ReadableByteStreamController;
|
||||
const readable = new ReadableStream({
|
||||
start(c) {
|
||||
|
@ -1349,7 +1353,7 @@ export class ServerResponse extends NodeWritable {
|
|||
});
|
||||
this.#readable = readable;
|
||||
this.#resolve = resolve;
|
||||
this.socket = new FakeSocket();
|
||||
this.socket = socket;
|
||||
}
|
||||
|
||||
setHeader(name: string, value: string) {
|
||||
|
@ -1438,7 +1442,7 @@ export class IncomingMessageForServer extends NodeReadable {
|
|||
// These properties are used by `npm:forwarded` for example.
|
||||
socket: { remoteAddress: string; remotePort: number };
|
||||
|
||||
constructor(req: Request, remoteAddr: { hostname: string; port: number }) {
|
||||
constructor(req: Request, socket: FakeSocket) {
|
||||
// Check if no body (GET/HEAD/OPTIONS/...)
|
||||
const reader = req.body?.getReader();
|
||||
super({
|
||||
|
@ -1465,10 +1469,7 @@ export class IncomingMessageForServer extends NodeReadable {
|
|||
// url: (new URL(request.url).pathname),
|
||||
this.url = req.url?.slice(req.url.indexOf("/", 8));
|
||||
this.method = req.method;
|
||||
this.socket = new FakeSocket({
|
||||
remoteAddress: remoteAddr.hostname,
|
||||
remotePort: remoteAddr.port,
|
||||
});
|
||||
this.socket = socket;
|
||||
this.#req = req;
|
||||
}
|
||||
|
||||
|
@ -1572,7 +1573,12 @@ export class ServerImpl extends EventEmitter {
|
|||
_serve() {
|
||||
const ac = new AbortController();
|
||||
const handler = (request: Request, info: Deno.ServeHandlerInfo) => {
|
||||
const req = new IncomingMessageForServer(request, info.remoteAddr);
|
||||
const socket = new FakeSocket({
|
||||
remoteAddress: info.remoteAddr.hostname,
|
||||
remotePort: info.remoteAddr.port,
|
||||
encrypted: this._encrypted,
|
||||
});
|
||||
const req = new IncomingMessageForServer(request, socket);
|
||||
if (req.upgrade && this.listenerCount("upgrade") > 0) {
|
||||
const { conn, response } = upgradeHttpRaw(request);
|
||||
const socket = new Socket({
|
||||
|
@ -1582,7 +1588,7 @@ export class ServerImpl extends EventEmitter {
|
|||
return response;
|
||||
} else {
|
||||
return new Promise<Response>((resolve): void => {
|
||||
const res = new ServerResponse(resolve);
|
||||
const res = new ServerResponse(resolve, socket);
|
||||
this.emit("request", req, res);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -50,6 +50,8 @@ export class Server extends HttpServer {
|
|||
: this._opts.key,
|
||||
};
|
||||
}
|
||||
|
||||
_encrypted = true;
|
||||
}
|
||||
export function createServer(opts, requestListener?: ServerHandler) {
|
||||
return new Server(opts, requestListener);
|
||||
|
@ -110,6 +112,7 @@ const globalAgent = new Agent({
|
|||
|
||||
/** HttpsClientRequest class loosely follows http.ClientRequest class API. */
|
||||
class HttpsClientRequest extends ClientRequest {
|
||||
override _encrypted: true;
|
||||
override defaultProtocol = "https:";
|
||||
override _getClient(): Deno.HttpClient | undefined {
|
||||
if (caCerts === null) {
|
||||
|
|
Loading…
Reference in a new issue