11'use strict' ;
2+ // Flags: --expose_internals
23
34const assert = require ( 'assert' ) ;
45const common = require ( '../common' ) ;
56const http = require ( 'http' ) ;
67const net = require ( 'net' ) ;
7- const MAX = 8 * 1024 ; // 8KB
8+ const MAX = + ( process . argv [ 2 ] || 8 * 1024 ) ; // Command line option, or 8KB.
9+
10+ assert ( process . binding ( 'config' ) . maxHTTPHeaderSize ,
11+ 'The option should exist on process.binding(\'config\')' ) ;
12+
13+ console . log ( 'pid is' , process . pid ) ;
14+ console . log ( 'max header size is' , process . binding ( 'config' ) . maxHTTPHeaderSize ) ;
815
916// Verify that we cannot receive more than 8KB of headers.
1017
@@ -28,19 +35,15 @@ function fillHeaders(headers, currentSize, valid = false) {
2835 headers += 'a' . repeat ( MAX - headers . length - 3 ) ;
2936 // Generate valid headers
3037 if ( valid ) {
31- // TODO(mcollina): understand why -9 is needed instead of -1
32- headers = headers . slice ( 0 , - 9 ) ;
38+ // TODO(mcollina): understand why -32 is needed instead of -1
39+ headers = headers . slice ( 0 , - 32 ) ;
3340 }
3441 return headers + '\r\n\r\n' ;
3542}
3643
37- const timeout = common . platformTimeout ( 10 ) ;
38-
3944function writeHeaders ( socket , headers ) {
4045 const array = [ ] ;
41-
42- // this is off from 1024 so that \r\n does not get split
43- const chunkSize = 1000 ;
46+ const chunkSize = 100 ;
4447 let last = 0 ;
4548
4649 for ( let i = 0 ; i < headers . length / chunkSize ; i ++ ) {
@@ -55,19 +58,25 @@ function writeHeaders(socket, headers) {
5558 next ( ) ;
5659
5760 function next ( ) {
58- if ( socket . write ( array . shift ( ) ) ) {
59- if ( array . length === 0 ) {
60- socket . end ( ) ;
61- } else {
62- setTimeout ( next , timeout ) ;
63- }
61+ if ( socket . destroyed ) {
62+ console . log ( 'socket was destroyed early, data left to write:' ,
63+ array . join ( '' ) . length ) ;
64+ return ;
65+ }
66+
67+ const chunk = array . shift ( ) ;
68+
69+ if ( chunk ) {
70+ console . log ( 'writing chunk of size' , chunk . length ) ;
71+ socket . write ( chunk , next ) ;
6472 } else {
65- socket . once ( 'drain' , next ) ;
73+ socket . end ( ) ;
6674 }
6775 }
6876}
6977
7078function test1 ( ) {
79+ console . log ( 'test1' ) ;
7180 let headers =
7281 'HTTP/1.1 200 OK\r\n' +
7382 'Content-Length: 0\r\n' +
@@ -82,6 +91,9 @@ function test1() {
8291 writeHeaders ( sock , headers ) ;
8392 sock . resume ( ) ;
8493 } ) ;
94+
95+ // The socket might error but that's ok
96+ sock . on ( 'error' , ( ) => { } ) ;
8597 } ) ;
8698
8799 server . listen ( 0 , common . mustCall ( ( ) => {
@@ -90,17 +102,17 @@ function test1() {
90102
91103 client . on ( 'error' , common . mustCall ( ( err ) => {
92104 assert . strictEqual ( err . code , 'HPE_HEADER_OVERFLOW' ) ;
93- server . close ( ) ;
94- setImmediate ( test2 ) ;
105+ server . close ( test2 ) ;
95106 } ) ) ;
96107 } ) ) ;
97108}
98109
99110const test2 = common . mustCall ( ( ) => {
111+ console . log ( 'test2' ) ;
100112 let headers =
101113 'GET / HTTP/1.1\r\n' +
102114 'Host: localhost\r\n' +
103- 'Agent: node \r\n' +
115+ 'Agent: nod2 \r\n' +
104116 'X-CRASH: ' ;
105117
106118 // /, Host, localhost, Agent, node, X-CRASH, a...
@@ -109,7 +121,7 @@ const test2 = common.mustCall(() => {
109121
110122 const server = http . createServer ( common . mustNotCall ( ) ) ;
111123
112- server . on ( 'clientError' , common . mustCall ( ( err ) => {
124+ server . once ( 'clientError' , common . mustCall ( ( err ) => {
113125 assert . strictEqual ( err . code , 'HPE_HEADER_OVERFLOW' ) ;
114126 } ) ) ;
115127
@@ -121,34 +133,46 @@ const test2 = common.mustCall(() => {
121133 } ) ;
122134
123135 finished ( client , common . mustCall ( ( err ) => {
124- server . close ( ) ;
125- setImmediate ( test3 ) ;
136+ server . close ( test3 ) ;
126137 } ) ) ;
127138 } ) ) ;
128139} ) ;
129140
130141const test3 = common . mustCall ( ( ) => {
142+ console . log ( 'test3' ) ;
131143 let headers =
132144 'GET / HTTP/1.1\r\n' +
133145 'Host: localhost\r\n' +
134- 'Agent: node \r\n' +
146+ 'Agent: nod3 \r\n' +
135147 'X-CRASH: ' ;
136148
137149 // /, Host, localhost, Agent, node, X-CRASH, a...
138150 const currentSize = 1 + 4 + 9 + 5 + 4 + 7 ;
139151 headers = fillHeaders ( headers , currentSize , true ) ;
140152
153+ console . log ( 'writing' , headers . length ) ;
154+
141155 const server = http . createServer ( common . mustCall ( ( req , res ) => {
142- res . end ( 'hello world ' ) ;
143- setImmediate ( server . close . bind ( server ) ) ;
156+ res . end ( 'hello from test3 server ' ) ;
157+ server . close ( ) ;
144158 } ) ) ;
145159
160+ server . on ( 'clientError' , ( err ) => {
161+ console . log ( err . code ) ;
162+ if ( err . code === 'HPE_HEADER_OVERFLOW' ) {
163+ console . log ( err . rawPacket . toString ( 'hex' ) ) ;
164+ }
165+ } ) ;
166+ server . on ( 'clientError' , common . mustNotCall ( ) ) ;
167+
146168 server . listen ( 0 , common . mustCall ( ( ) => {
147169 const client = net . connect ( server . address ( ) . port ) ;
148170 client . on ( 'connect' , ( ) => {
149171 writeHeaders ( client , headers ) ;
150172 client . resume ( ) ;
151173 } ) ;
174+
175+ client . pipe ( process . stdout ) ;
152176 } ) ) ;
153177} ) ;
154178
0 commit comments