Skip to content

Commit 7f50da5

Browse files
committed
dev: use Connection: close for http requests by default
1 parent 8143e57 commit 7f50da5

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/puter-js/src/lib/http.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,14 @@ export const make_http_api = ({ Socket, DEFAULT_PORT }) => {
6464
throw new Error('Chunked transfer encoding not implemented');
6565
}
6666
};
67+
const TRANSFER_NO_KEEPALIVE = {
68+
data: data => {
69+
res.emit('data', decoder.decode(data));
70+
}
71+
};
6772
let transfer = null;
6873

74+
let keepalive = false;
6975
const STATE_HEADERS = {
7076
data: data => {
7177
data = decoder.decode(data);
@@ -95,7 +101,9 @@ export const make_http_api = ({ Socket, DEFAULT_PORT }) => {
95101
}
96102

97103

98-
if ( res.headers['transfer-encoding'] === 'chunked' ) {
104+
if ( ! keepalive ) {
105+
transfer = TRANSFER_NO_KEEPALIVE;
106+
} else if ( res.headers['transfer-encoding'] === 'chunked' ) {
99107
transfer = TRANSFER_CHUNKED;
100108
} else if ( res.headers['transfer-encoding'] ) {
101109
throw new Error('Unsupported transfer encoding');
@@ -133,6 +141,13 @@ export const make_http_api = ({ Socket, DEFAULT_PORT }) => {
133141
const path = options.path || '/';
134142
const headers = options.headers || {};
135143
headers['Host'] = options.hostname;
144+
if ( ! headers['Connection'] ) {
145+
headers['Connection'] = 'close';
146+
} else {
147+
if ( headers['Connection'] !== 'close' ) {
148+
keepalive = true;
149+
}
150+
}
136151

137152
let requestString = `${method} ${path} HTTP/1.1\r\n`;
138153
for (const [key, value] of Object.entries(headers)) {

0 commit comments

Comments
 (0)