@@ -117,7 +117,7 @@ function IncomingMessage (connection) {
117117
118118 this . connection = connection ;
119119 this . httpVersion = null ;
120- this . headers = [ ] ;
120+ this . headers = { } ;
121121
122122 // request (server) only
123123 this . uri = "" ;
@@ -142,6 +142,15 @@ IncomingMessage.prototype.resume = function () {
142142 this . connection . readResume ( ) ;
143143} ;
144144
145+ IncomingMessage . prototype . _addHeaderLine = function ( field , value ) {
146+ if ( field in this . headers ) {
147+ // TODO Certain headers like 'Content-Type' should not be concatinated.
148+ // See https://www.google.com/reader/view/?tab=my#overview-page
149+ this . headers [ field ] += ", " + value ;
150+ } else {
151+ this . headers [ field ] = value ;
152+ }
153+ } ;
145154
146155function OutgoingMessage ( ) {
147156 node . EventEmitter . call ( this ) ;
@@ -162,22 +171,26 @@ OutgoingMessage.prototype.send = function (data, encoding) {
162171 this . output . push ( data ) ;
163172} ;
164173
165- OutgoingMessage . prototype . sendHeaderLines = function ( first_line , header_lines ) {
174+ OutgoingMessage . prototype . sendHeaderLines = function ( first_line , headers ) {
166175 var sent_connection_header = false ;
167176 var sent_content_length_header = false ;
168177 var sent_transfer_encoding_header = false ;
169178
170- header_lines = header_lines || [ ] ;
171-
172179 // first_line in the case of request is: "GET /index.html HTTP/1.1\r\n"
173180 // in the case of response it is: "HTTP/1.1 200 OK\r\n"
174- var header = first_line ;
175-
176- for ( var i = 0 ; i < header_lines . length ; i ++ ) {
177- var field = header_lines [ i ] [ 0 ] ;
178- var value = header_lines [ i ] [ 1 ] ;
181+ var message_header = first_line ;
182+ var field , value ;
183+ for ( var i in headers ) {
184+ if ( headers instanceof Array ) {
185+ field = headers [ i ] [ 0 ] ;
186+ value = headers [ i ] [ 1 ] ;
187+ } else {
188+ if ( ! headers . hasOwnProperty ( i ) ) continue ;
189+ field = i ;
190+ value = headers [ i ] ;
191+ }
179192
180- header += field + ": " + value + CRLF ;
193+ message_header += field + ": " + value + CRLF ;
181194
182195 if ( connection_expression . exec ( field ) ) {
183196 sent_connection_header = true ;
@@ -196,23 +209,23 @@ OutgoingMessage.prototype.sendHeaderLines = function (first_line, header_lines)
196209 // keep-alive logic
197210 if ( sent_connection_header == false ) {
198211 if ( this . should_keep_alive ) {
199- header += "Connection: keep-alive\r\n" ;
212+ message_header += "Connection: keep-alive\r\n" ;
200213 } else {
201214 this . closeOnFinish = true ;
202- header += "Connection: close\r\n" ;
215+ message_header += "Connection: close\r\n" ;
203216 }
204217 }
205218
206219 if ( sent_content_length_header == false && sent_transfer_encoding_header == false ) {
207220 if ( this . use_chunked_encoding_by_default ) {
208- header += "Transfer-Encoding: chunked\r\n" ;
221+ message_header += "Transfer-Encoding: chunked\r\n" ;
209222 this . chunked_encoding = true ;
210223 }
211224 }
212225
213- header += CRLF ;
226+ message_header += CRLF ;
214227
215- this . send ( header ) ;
228+ this . send ( message_header ) ;
216229 // wait until the first body chunk, or finish(), is sent to flush.
217230} ;
218231
@@ -255,7 +268,7 @@ ServerResponse.prototype.sendHeader = function (statusCode, headers) {
255268} ;
256269
257270
258- function ClientRequest ( method , uri , header_lines ) {
271+ function ClientRequest ( method , uri , headers ) {
259272 OutgoingMessage . call ( this ) ;
260273
261274 this . should_keep_alive = false ;
@@ -266,7 +279,7 @@ function ClientRequest (method, uri, header_lines) {
266279 }
267280 this . closeOnFinish = true ;
268281
269- this . sendHeaderLines ( method + " " + uri + " HTTP/1.1\r\n" , header_lines ) ;
282+ this . sendHeaderLines ( method + " " + uri + " HTTP/1.1\r\n" , headers ) ;
270283}
271284node . inherits ( ClientRequest , OutgoingMessage ) ;
272285
@@ -282,7 +295,7 @@ function createIncomingMessageStream (connection, incoming_listener) {
282295 stream . addListener ( "incoming" , incoming_listener ) ;
283296
284297 var incoming ;
285- var last_header_was_a_value = false ;
298+ var field = null , value = null ;
286299
287300 connection . addListener ( "message_begin" , function ( ) {
288301 incoming = new IncomingMessage ( connection ) ;
@@ -294,25 +307,31 @@ function createIncomingMessageStream (connection, incoming_listener) {
294307 } ) ;
295308
296309 connection . addListener ( "header_field" , function ( data ) {
297- if ( incoming . headers . length > 0 && last_header_was_a_value == false ) {
298- incoming . headers [ incoming . headers . length - 1 ] [ 0 ] += data ;
310+ if ( value ) {
311+ incoming . _addHeaderLine ( field , value ) ;
312+ field = null ;
313+ value = null ;
314+ }
315+ if ( field ) {
316+ field += data ;
299317 } else {
300- incoming . headers . push ( [ data ] ) ;
318+ field = data ;
301319 }
302- last_header_was_a_value = false ;
303320 } ) ;
304321
305322 connection . addListener ( "header_value" , function ( data ) {
306- var last_pair = incoming . headers [ incoming . headers . length - 1 ] ;
307- if ( last_pair . length == 1 ) {
308- last_pair [ 1 ] = data ;
309- } else {
310- last_pair [ 1 ] += data ;
311- }
312- last_header_was_a_value = true ;
323+ if ( value ) {
324+ value += data ;
325+ } else {
326+ value = data ;
327+ }
313328 } ) ;
314329
315330 connection . addListener ( "headers_complete" , function ( info ) {
331+ if ( field && value ) {
332+ incoming . _addHeaderLine ( field , value ) ;
333+ }
334+
316335 incoming . httpVersion = info . httpVersion ;
317336
318337 if ( info . method ) {
0 commit comments