Compare commits

...

1 Commits

Author SHA1 Message Date
Don Isaac
2b7441b8b1 fix(node): parallel/test-http-server.js 2025-03-18 17:27:15 -07:00
2 changed files with 210 additions and 64 deletions

View File

@@ -121,6 +121,7 @@ const ObjectDefineProperty = Object.defineProperty;
const GlobalPromise = globalThis.Promise;
const headerCharRegex = /[^\t\x20-\x7e\x80-\xff]/;
/**
* True if val contains an invalid field-vchar
* field-value = *( field-content / obs-fold )
@@ -393,7 +394,7 @@ const NodeHTTPServerSocket = class Socket extends Duplex {
$isCallable(callback) && callback(err);
return;
}
this.#closeHandle(handle, callback);
}
@@ -608,79 +609,82 @@ function emitListeningNextTick(self, hostname, port) {
type Server = InstanceType<typeof Server>;
const Server = function Server(options, callback) {
if (!(this instanceof Server)) return new Server(options, callback);
if (typeof options === "function") {
callback = options;
options = kEmptyObject;
} else if ($isUndefinedOrNull(options)) {
options = kEmptyObject;
} else {
validateObject(options, "options");
// we store options; cloning it keeps unexpected user mutations from affecting Server.
options = { ...options };
}
EventEmitter.$call(this);
this.listening = false;
this._unref = false;
this.maxRequestsPerSocket = 0;
this[kInternalSocketData] = undefined;
this[tlsSymbol] = null;
this[optionsSymbol] = options;
if (typeof options === "function") {
callback = options;
options = {};
} else if (options == null || typeof options === "object") {
options = { ...options };
this[tlsSymbol] = null;
let key = options.key;
if (key) {
if (!isValidTLSArray(key)) {
throw new TypeError(
"key argument must be an string, Buffer, TypedArray, BunFile or an array containing string, Buffer, TypedArray or BunFile",
);
}
this[isTlsSymbol] = true;
let key = options.key;
if (key) {
if (!isValidTLSArray(key)) {
throw new TypeError(
"key argument must be an string, Buffer, TypedArray, BunFile or an array containing string, Buffer, TypedArray or BunFile",
);
}
let cert = options.cert;
if (cert) {
if (!isValidTLSArray(cert)) {
throw new TypeError(
"cert argument must be an string, Buffer, TypedArray, BunFile or an array containing string, Buffer, TypedArray or BunFile",
);
}
this[isTlsSymbol] = true;
this[isTlsSymbol] = true;
}
let cert = options.cert;
if (cert) {
if (!isValidTLSArray(cert)) {
throw new TypeError(
"cert argument must be an string, Buffer, TypedArray, BunFile or an array containing string, Buffer, TypedArray or BunFile",
);
}
let ca = options.ca;
if (ca) {
if (!isValidTLSArray(ca)) {
throw new TypeError(
"ca argument must be an string, Buffer, TypedArray, BunFile or an array containing string, Buffer, TypedArray or BunFile",
);
}
this[isTlsSymbol] = true;
}
let passphrase = options.passphrase;
if (passphrase && typeof passphrase !== "string") {
throw new TypeError("passphrase argument must be an string");
}
let serverName = options.servername;
if (serverName && typeof serverName !== "string") {
throw new TypeError("servername argument must be an string");
}
let secureOptions = options.secureOptions || 0;
if (secureOptions && typeof secureOptions !== "number") {
throw new TypeError("secureOptions argument must be an number");
}
if (this[isTlsSymbol]) {
this[tlsSymbol] = {
serverName,
key,
cert,
ca,
passphrase,
secureOptions,
};
} else {
this[tlsSymbol] = null;
}
} else {
throw new Error("bun-http-polyfill: invalid arguments");
this[isTlsSymbol] = true;
}
this[optionsSymbol] = options;
let ca = options.ca;
if (ca) {
if (!isValidTLSArray(ca)) {
throw new TypeError(
"ca argument must be an string, Buffer, TypedArray, BunFile or an array containing string, Buffer, TypedArray or BunFile",
);
}
this[isTlsSymbol] = true;
}
let passphrase = options.passphrase;
if (passphrase && typeof passphrase !== "string") {
throw new TypeError("passphrase argument must be an string");
}
let serverName = options.servername;
if (serverName && typeof serverName !== "string") {
throw new TypeError("servername argument must be an string");
}
let secureOptions = options.secureOptions || 0;
if (secureOptions && typeof secureOptions !== "number") {
throw new TypeError("secureOptions argument must be an number");
}
if (this[isTlsSymbol]) {
this[tlsSymbol] = {
serverName,
key,
cert,
ca,
passphrase,
secureOptions,
};
} else {
this[tlsSymbol] = null;
}
if (callback) this.on("request", callback);
return this;
@@ -1971,7 +1975,6 @@ const ServerResponsePrototype = {
// But we don't want it for the fetch() response version.
end(chunk, encoding, callback) {
const handle = this[kHandle];
const isFinished = this.finished || handle?.finished;
if ($isCallable(chunk)) {
callback = chunk;
chunk = undefined;

View File

@@ -0,0 +1,143 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
require('../common');
const assert = require('assert');
const net = require('net');
const http = require('http');
const url = require('url');
const qs = require('querystring');
const invalid_options = [ 'foo', 42, true, [] ];
invalid_options.forEach((option) => {
assert.throws(() => {
new http.Server(option);
}, {
code: 'ERR_INVALID_ARG_TYPE'
});
});
let request_number = 0;
let requests_sent = 0;
let server_response = '';
let client_got_eof = false;
const server = http.createServer(function(req, res) {
res.id = request_number;
req.id = request_number++;
assert.strictEqual(res.req, req);
if (req.id === 0) {
assert.strictEqual(req.method, 'GET');
assert.strictEqual(url.parse(req.url).pathname, '/hello');
assert.strictEqual(qs.parse(url.parse(req.url).query).hello, 'world');
assert.strictEqual(qs.parse(url.parse(req.url).query).foo, 'b==ar');
}
if (req.id === 1) {
assert.strictEqual(req.method, 'POST');
assert.strictEqual(url.parse(req.url).pathname, '/quit');
}
if (req.id === 2) {
assert.strictEqual(req.headers['x-x'], 'foo');
}
if (req.id === 3) {
assert.strictEqual(req.headers['x-x'], 'bar');
this.close();
}
setTimeout(function() {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write(url.parse(req.url).pathname);
res.end();
}, 1);
});
server.listen(0);
server.httpAllowHalfOpen = true;
server.on('listening', function() {
const c = net.createConnection(this.address().port);
c.setEncoding('utf8');
c.on('connect', function() {
c.write(
'GET /hello?hello=world&foo=b==ar HTTP/1.1\r\n' +
'Host: example.com\r\n\r\n');
requests_sent += 1;
});
c.on('data', function(chunk) {
server_response += chunk;
if (requests_sent === 1) {
c.write(
'POST /quit HTTP/1.1\r\n' +
'Host: example.com\r\n\r\n'
);
requests_sent += 1;
}
if (requests_sent === 2) {
c.write('GET / HTTP/1.1\r\nX-X: foo\r\nHost: example.com\r\n\r\n' +
'GET / HTTP/1.1\r\nX-X: bar\r\nHost: example.com\r\n\r\n');
// Note: we are making the connection half-closed here
// before we've gotten the response from the server. This
// is a pretty bad thing to do and not really supported
// by many http servers. Node supports it optionally if
// you set server.httpAllowHalfOpen=true, which we've done
// above.
c.end();
assert.strictEqual(c.readyState, 'readOnly');
requests_sent += 2;
}
});
c.on('end', function() {
client_got_eof = true;
});
c.on('close', function() {
assert.strictEqual(c.readyState, 'closed');
});
});
process.on('exit', function() {
assert.strictEqual(request_number, 4);
assert.strictEqual(requests_sent, 4);
const hello = new RegExp('/hello');
assert.match(server_response, hello);
const quit = new RegExp('/quit');
assert.match(server_response, quit);
assert.strictEqual(client_got_eof, true);
assert.strictEqual(server.close(), server);
});