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({
|
const server = https.createServer({
|
||||||
cert: Deno.readTextFileSync("cli/tests/testdata/tls/localhost.crt"),
|
cert: Deno.readTextFileSync("cli/tests/testdata/tls/localhost.crt"),
|
||||||
key: Deno.readTextFileSync("cli/tests/testdata/tls/localhost.key"),
|
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!");
|
res.end("success!");
|
||||||
});
|
});
|
||||||
server.listen(() => {
|
server.listen(() => {
|
||||||
|
@ -664,7 +666,9 @@ Deno.test(
|
||||||
{ permissions: { net: true } },
|
{ permissions: { net: true } },
|
||||||
async () => {
|
async () => {
|
||||||
const promise = deferred();
|
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.writeHead(200, { "Content-Type": "text/plain" });
|
||||||
res.end("okay");
|
res.end("okay");
|
||||||
});
|
});
|
||||||
|
|
|
@ -278,6 +278,7 @@ class FakeSocket extends EventEmitter {
|
||||||
super();
|
super();
|
||||||
this.remoteAddress = opts.hostname;
|
this.remoteAddress = opts.hostname;
|
||||||
this.remotePort = opts.port;
|
this.remotePort = opts.port;
|
||||||
|
this.encrypted = opts.encrypted;
|
||||||
}
|
}
|
||||||
|
|
||||||
setKeepAlive() {}
|
setKeepAlive() {}
|
||||||
|
@ -561,7 +562,7 @@ class ClientRequest extends OutgoingMessage {
|
||||||
this.onSocket(createConnection(optsWithoutSignal));
|
this.onSocket(createConnection(optsWithoutSignal));
|
||||||
}
|
}
|
||||||
}*/
|
}*/
|
||||||
this.onSocket(new FakeSocket());
|
this.onSocket(new FakeSocket({ encrypted: this._encrypted }));
|
||||||
|
|
||||||
const url = this._createUrlStrFromOptions();
|
const url = this._createUrlStrFromOptions();
|
||||||
|
|
||||||
|
@ -1306,7 +1307,10 @@ export class ServerResponse extends NodeWritable {
|
||||||
return status === 101 || status === 204 || status === 205 || status === 304;
|
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;
|
let controller: ReadableByteStreamController;
|
||||||
const readable = new ReadableStream({
|
const readable = new ReadableStream({
|
||||||
start(c) {
|
start(c) {
|
||||||
|
@ -1349,7 +1353,7 @@ export class ServerResponse extends NodeWritable {
|
||||||
});
|
});
|
||||||
this.#readable = readable;
|
this.#readable = readable;
|
||||||
this.#resolve = resolve;
|
this.#resolve = resolve;
|
||||||
this.socket = new FakeSocket();
|
this.socket = socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
setHeader(name: string, value: string) {
|
setHeader(name: string, value: string) {
|
||||||
|
@ -1438,7 +1442,7 @@ export class IncomingMessageForServer extends NodeReadable {
|
||||||
// These properties are used by `npm:forwarded` for example.
|
// These properties are used by `npm:forwarded` for example.
|
||||||
socket: { remoteAddress: string; remotePort: number };
|
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/...)
|
// Check if no body (GET/HEAD/OPTIONS/...)
|
||||||
const reader = req.body?.getReader();
|
const reader = req.body?.getReader();
|
||||||
super({
|
super({
|
||||||
|
@ -1465,10 +1469,7 @@ export class IncomingMessageForServer extends NodeReadable {
|
||||||
// url: (new URL(request.url).pathname),
|
// url: (new URL(request.url).pathname),
|
||||||
this.url = req.url?.slice(req.url.indexOf("/", 8));
|
this.url = req.url?.slice(req.url.indexOf("/", 8));
|
||||||
this.method = req.method;
|
this.method = req.method;
|
||||||
this.socket = new FakeSocket({
|
this.socket = socket;
|
||||||
remoteAddress: remoteAddr.hostname,
|
|
||||||
remotePort: remoteAddr.port,
|
|
||||||
});
|
|
||||||
this.#req = req;
|
this.#req = req;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1572,7 +1573,12 @@ export class ServerImpl extends EventEmitter {
|
||||||
_serve() {
|
_serve() {
|
||||||
const ac = new AbortController();
|
const ac = new AbortController();
|
||||||
const handler = (request: Request, info: Deno.ServeHandlerInfo) => {
|
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) {
|
if (req.upgrade && this.listenerCount("upgrade") > 0) {
|
||||||
const { conn, response } = upgradeHttpRaw(request);
|
const { conn, response } = upgradeHttpRaw(request);
|
||||||
const socket = new Socket({
|
const socket = new Socket({
|
||||||
|
@ -1582,7 +1588,7 @@ export class ServerImpl extends EventEmitter {
|
||||||
return response;
|
return response;
|
||||||
} else {
|
} else {
|
||||||
return new Promise<Response>((resolve): void => {
|
return new Promise<Response>((resolve): void => {
|
||||||
const res = new ServerResponse(resolve);
|
const res = new ServerResponse(resolve, socket);
|
||||||
this.emit("request", req, res);
|
this.emit("request", req, res);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,8 @@ export class Server extends HttpServer {
|
||||||
: this._opts.key,
|
: this._opts.key,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_encrypted = true;
|
||||||
}
|
}
|
||||||
export function createServer(opts, requestListener?: ServerHandler) {
|
export function createServer(opts, requestListener?: ServerHandler) {
|
||||||
return new Server(opts, requestListener);
|
return new Server(opts, requestListener);
|
||||||
|
@ -110,6 +112,7 @@ const globalAgent = new Agent({
|
||||||
|
|
||||||
/** HttpsClientRequest class loosely follows http.ClientRequest class API. */
|
/** HttpsClientRequest class loosely follows http.ClientRequest class API. */
|
||||||
class HttpsClientRequest extends ClientRequest {
|
class HttpsClientRequest extends ClientRequest {
|
||||||
|
override _encrypted: true;
|
||||||
override defaultProtocol = "https:";
|
override defaultProtocol = "https:";
|
||||||
override _getClient(): Deno.HttpClient | undefined {
|
override _getClient(): Deno.HttpClient | undefined {
|
||||||
if (caCerts === null) {
|
if (caCerts === null) {
|
||||||
|
|
Loading…
Reference in a new issue