@@ -64,8 +64,14 @@ export const make_http_api = ({ Socket, DEFAULT_PORT }) => {
64
64
throw new Error ( 'Chunked transfer encoding not implemented' ) ;
65
65
}
66
66
} ;
67
+ const TRANSFER_NO_KEEPALIVE = {
68
+ data : data => {
69
+ res . emit ( 'data' , decoder . decode ( data ) ) ;
70
+ }
71
+ } ;
67
72
let transfer = null ;
68
73
74
+ let keepalive = false ;
69
75
const STATE_HEADERS = {
70
76
data : data => {
71
77
data = decoder . decode ( data ) ;
@@ -95,7 +101,9 @@ export const make_http_api = ({ Socket, DEFAULT_PORT }) => {
95
101
}
96
102
97
103
98
- if ( res . headers [ 'transfer-encoding' ] === 'chunked' ) {
104
+ if ( ! keepalive ) {
105
+ transfer = TRANSFER_NO_KEEPALIVE ;
106
+ } else if ( res . headers [ 'transfer-encoding' ] === 'chunked' ) {
99
107
transfer = TRANSFER_CHUNKED ;
100
108
} else if ( res . headers [ 'transfer-encoding' ] ) {
101
109
throw new Error ( 'Unsupported transfer encoding' ) ;
@@ -133,6 +141,13 @@ export const make_http_api = ({ Socket, DEFAULT_PORT }) => {
133
141
const path = options . path || '/' ;
134
142
const headers = options . headers || { } ;
135
143
headers [ 'Host' ] = options . hostname ;
144
+ if ( ! headers [ 'Connection' ] ) {
145
+ headers [ 'Connection' ] = 'close' ;
146
+ } else {
147
+ if ( headers [ 'Connection' ] !== 'close' ) {
148
+ keepalive = true ;
149
+ }
150
+ }
136
151
137
152
let requestString = `${ method } ${ path } HTTP/1.1\r\n` ;
138
153
for ( const [ key , value ] of Object . entries ( headers ) ) {
0 commit comments