Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 73 additions & 32 deletions src/bun.js/bindings/NodeHTTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1070,55 +1070,96 @@ static void NodeHTTPServer__writeHead(
}
response->writeStatus(std::string_view(statusMessage, statusMessageLength));

bool hasConnectionHeader = false;

if (headersObject) {
if (auto* fetchHeaders = jsDynamicCast<WebCore::JSFetchHeaders*>(headersObject)) {
writeFetchHeadersToUWSResponse<isSSL>(fetchHeaders->wrapped(), response);
return;
}

if (headersObject->hasNonReifiedStaticProperties()) [[unlikely]] {
headersObject->reifyAllStaticProperties(globalObject);
RETURN_IF_EXCEPTION(scope, void());
}
// Check if user provided Connection header in FetchHeaders
auto& internalHeaders = fetchHeaders->wrapped().internalHeaders();
for (const auto& header : internalHeaders.commonHeaders()) {
if (header.key == WebCore::HTTPHeaderName::Connection) {
hasConnectionHeader = true;
break;
}
}
if (!hasConnectionHeader) {
for (auto& header : internalHeaders.uncommonHeaders()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not necessary

if (WTF::equalIgnoringASCIICase(header.key, "connection"_s)) {
hasConnectionHeader = true;
break;
}
}
}
} else {
if (headersObject->hasNonReifiedStaticProperties()) [[unlikely]] {
headersObject->reifyAllStaticProperties(globalObject);
RETURN_IF_EXCEPTION(scope, void());
}

auto* structure = headersObject->structure();

auto* structure = headersObject->structure();
if (structure->canPerformFastPropertyEnumeration()) {
structure->forEachProperty(vm, [&](const auto& entry) {
JSValue headerValue = headersObject->getDirect(entry.offset());
if (!headerValue.isString()) {

if (structure->canPerformFastPropertyEnumeration()) {
structure->forEachProperty(vm, [&](const auto& entry) {
JSValue headerValue = headersObject->getDirect(entry.offset());
if (!headerValue.isString()) {
return true;
}

String key = entry.key();
String value = headerValue.toWTFString(globalObject);
RETURN_IF_EXCEPTION(scope, false);

// Check if this is a Connection header
if (WTF::equalIgnoringASCIICase(key, "connection"_s)) {
hasConnectionHeader = true;
}

writeResponseHeader<isSSL>(response, key, value);

return true;
}
});
} else {
PropertyNameArray propertyNames(vm, PropertyNameMode::Strings, PrivateSymbolMode::Exclude);
headersObject->getOwnPropertyNames(headersObject, globalObject, propertyNames, DontEnumPropertiesMode::Exclude);
RETURN_IF_EXCEPTION(scope, void());

String key = entry.key();
String value = headerValue.toWTFString(globalObject);
RETURN_IF_EXCEPTION(scope, false);
for (unsigned i = 0; i < propertyNames.size(); ++i) {
JSValue headerValue = headersObject->getIfPropertyExists(globalObject, propertyNames[i]);
RETURN_IF_EXCEPTION(scope, );
if (!headerValue.isString()) {
continue;
}

writeResponseHeader<isSSL>(response, key, value);
String key = propertyNames[i].string();
String value = headerValue.toWTFString(globalObject);
RETURN_IF_EXCEPTION(scope, void());

return true;
});
} else {
PropertyNameArray propertyNames(vm, PropertyNameMode::Strings, PrivateSymbolMode::Exclude);
headersObject->getOwnPropertyNames(headersObject, globalObject, propertyNames, DontEnumPropertiesMode::Exclude);
RETURN_IF_EXCEPTION(scope, void());
// Check if this is a Connection header
if (WTF::equalIgnoringASCIICase(key, "connection"_s)) {
hasConnectionHeader = true;
}

for (unsigned i = 0; i < propertyNames.size(); ++i) {
JSValue headerValue = headersObject->getIfPropertyExists(globalObject, propertyNames[i]);
RETURN_IF_EXCEPTION(scope, );
if (!headerValue.isString()) {
continue;
writeResponseHeader<isSSL>(response, key, value);
}

String key = propertyNames[i].string();
String value = headerValue.toWTFString(globalObject);
RETURN_IF_EXCEPTION(scope, void());
writeResponseHeader<isSSL>(response, key, value);
}
}
}

// Automatically add Connection header if not already set by user and headers were provided
if (!hasConnectionHeader && headersObject) {
auto* data = response->getHttpResponseData();
bool shouldClose = data->state & uWS::HttpResponseData<isSSL>::HTTP_CONNECTION_CLOSE;

if (!shouldClose) {
writeResponseHeader<isSSL>(response, "connection"_s, "keep-alive"_s);
} else {
writeResponseHeader<isSSL>(response, "connection"_s, "close"_s);
}
}

RELEASE_AND_RETURN(scope, void());
}

Expand Down
31 changes: 31 additions & 0 deletions test/js/node/test/parallel/test-http-automatic-headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');

const server = http.createServer(common.mustCall((req, res) => {
res.setHeader('X-Date', 'foo');
res.setHeader('X-Connection', 'bar');
res.setHeader('X-Content-Length', 'baz');
res.end();
}));
server.listen(0);

server.on('listening', common.mustCall(() => {
const agent = new http.Agent({ port: server.address().port, maxSockets: 1 });
http.get({
port: server.address().port,
path: '/hello',
agent: agent
}, common.mustCall((res) => {
assert.strictEqual(res.statusCode, 200);
assert.strictEqual(res.headers['x-date'], 'foo');
assert.strictEqual(res.headers['x-connection'], 'bar');
assert.strictEqual(res.headers['x-content-length'], 'baz');
assert(res.headers.date);
assert.strictEqual(res.headers.connection, 'keep-alive');
assert.strictEqual(res.headers['content-length'], '0');
server.close();
agent.destroy();
}));
}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';
const common = require('../common');
const assert = require('assert');

const net = require('net');
const http = require('http');

const server = http.createServer(function(request, response) {
response.removeHeader('connection');

if (request.httpVersion === '1.0') {
const socket = request.socket;
response.on('finish', common.mustCall(function() {
assert.ok(socket.writableEnded);
}));
}

response.end('beep boop\n');
});

const agent = new http.Agent({ keepAlive: true });

function makeHttp11Request(cb) {
http.get({
port: server.address().port,
agent
}, function(res) {
const socket = res.socket;

assert.strictEqual(res.statusCode, 200);
assert.strictEqual(res.headers.connection, undefined);

res.setEncoding('ascii');
let response = '';
res.on('data', function(chunk) {
response += chunk;
});
res.on('end', function() {
assert.strictEqual(response, 'beep boop\n');

process.nextTick(function() {
cb(socket);
});
});
});
}

function makeHttp10Request(cb) {
const socket = net.connect({ port: server.address().port }, function() {
socket.write('GET / HTTP/1.0\r\n' +
'Host: localhost:' + server.address().port + '\r\n' +
'\r\n');
socket.resume();

socket.on('close', cb);
});
}

server.listen(0, function() {
makeHttp11Request(function(firstSocket) {
makeHttp11Request(function(secondSocket) {
assert.strictEqual(firstSocket, secondSocket);

makeHttp10Request(function() {
server.close();
});
});
});
});
Loading