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+ const { getOptionValue } = require ( 'internal/options' ) ;
11+
12+ console . log ( 'pid is' , process . pid ) ;
13+ console . log ( 'max header size is' , getOptionValue ( '--max-http-header-size' ) ) ;
814
915// Verify that we cannot receive more than 8KB of headers.
1016
@@ -28,19 +34,15 @@ function fillHeaders(headers, currentSize, valid = false) {
2834 headers += 'a' . repeat ( MAX - headers . length - 3 ) ;
2935 // Generate valid headers
3036 if ( valid ) {
31- // TODO(mcollina): understand why -9 is needed instead of -1
32- headers = headers . slice ( 0 , - 9 ) ;
37+ // TODO(mcollina): understand why -32 is needed instead of -1
38+ headers = headers . slice ( 0 , - 32 ) ;
3339 }
3440 return headers + '\r\n\r\n' ;
3541}
3642
37- const timeout = common . platformTimeout ( 10 ) ;
38-
3943function writeHeaders ( socket , headers ) {
4044 const array = [ ] ;
41-
42- // this is off from 1024 so that \r\n does not get split
43- const chunkSize = 1000 ;
45+ const chunkSize = 100 ;
4446 let last = 0 ;
4547
4648 for ( let i = 0 ; i < headers . length / chunkSize ; i ++ ) {
@@ -55,19 +57,25 @@ function writeHeaders(socket, headers) {
5557 next ( ) ;
5658
5759 function next ( ) {
58- if ( socket . write ( array . shift ( ) ) ) {
59- if ( array . length === 0 ) {
60- socket . end ( ) ;
61- } else {
62- setTimeout ( next , timeout ) ;
63- }
60+ if ( socket . destroyed ) {
61+ console . log ( 'socket was destroyed early, data left to write:' ,
62+ array . join ( '' ) . length ) ;
63+ return ;
64+ }
65+
66+ const chunk = array . shift ( ) ;
67+
68+ if ( chunk ) {
69+ console . log ( 'writing chunk of size' , chunk . length ) ;
70+ socket . write ( chunk , next ) ;
6471 } else {
65- socket . once ( 'drain' , next ) ;
72+ socket . end ( ) ;
6673 }
6774 }
6875}
6976
7077function test1 ( ) {
78+ console . log ( 'test1' ) ;
7179 let headers =
7280 'HTTP/1.1 200 OK\r\n' +
7381 'Content-Length: 0\r\n' +
@@ -82,6 +90,9 @@ function test1() {
8290 writeHeaders ( sock , headers ) ;
8391 sock . resume ( ) ;
8492 } ) ;
93+
94+ // The socket might error but that's ok
95+ sock . on ( 'error' , ( ) => { } ) ;
8596 } ) ;
8697
8798 server . listen ( 0 , common . mustCall ( ( ) => {
@@ -90,17 +101,17 @@ function test1() {
90101
91102 client . on ( 'error' , common . mustCall ( ( err ) => {
92103 assert . strictEqual ( err . code , 'HPE_HEADER_OVERFLOW' ) ;
93- server . close ( ) ;
94- setImmediate ( test2 ) ;
104+ server . close ( test2 ) ;
95105 } ) ) ;
96106 } ) ) ;
97107}
98108
99109const test2 = common . mustCall ( ( ) => {
110+ console . log ( 'test2' ) ;
100111 let headers =
101112 'GET / HTTP/1.1\r\n' +
102113 'Host: localhost\r\n' +
103- 'Agent: node \r\n' +
114+ 'Agent: nod2 \r\n' +
104115 'X-CRASH: ' ;
105116
106117 // /, Host, localhost, Agent, node, X-CRASH, a...
@@ -109,7 +120,7 @@ const test2 = common.mustCall(() => {
109120
110121 const server = http . createServer ( common . mustNotCall ( ) ) ;
111122
112- server . on ( 'clientError' , common . mustCall ( ( err ) => {
123+ server . once ( 'clientError' , common . mustCall ( ( err ) => {
113124 assert . strictEqual ( err . code , 'HPE_HEADER_OVERFLOW' ) ;
114125 } ) ) ;
115126
@@ -121,34 +132,46 @@ const test2 = common.mustCall(() => {
121132 } ) ;
122133
123134 finished ( client , common . mustCall ( ( err ) => {
124- server . close ( ) ;
125- setImmediate ( test3 ) ;
135+ server . close ( test3 ) ;
126136 } ) ) ;
127137 } ) ) ;
128138} ) ;
129139
130140const test3 = common . mustCall ( ( ) => {
141+ console . log ( 'test3' ) ;
131142 let headers =
132143 'GET / HTTP/1.1\r\n' +
133144 'Host: localhost\r\n' +
134- 'Agent: node \r\n' +
145+ 'Agent: nod3 \r\n' +
135146 'X-CRASH: ' ;
136147
137148 // /, Host, localhost, Agent, node, X-CRASH, a...
138149 const currentSize = 1 + 4 + 9 + 5 + 4 + 7 ;
139150 headers = fillHeaders ( headers , currentSize , true ) ;
140151
152+ console . log ( 'writing' , headers . length ) ;
153+
141154 const server = http . createServer ( common . mustCall ( ( req , res ) => {
142- res . end ( 'hello world ' ) ;
143- setImmediate ( server . close . bind ( server ) ) ;
155+ res . end ( 'hello from test3 server ' ) ;
156+ server . close ( ) ;
144157 } ) ) ;
145158
159+ server . on ( 'clientError' , ( err ) => {
160+ console . log ( err . code ) ;
161+ if ( err . code === 'HPE_HEADER_OVERFLOW' ) {
162+ console . log ( err . rawPacket . toString ( 'hex' ) ) ;
163+ }
164+ } ) ;
165+ server . on ( 'clientError' , common . mustNotCall ( ) ) ;
166+
146167 server . listen ( 0 , common . mustCall ( ( ) => {
147168 const client = net . connect ( server . address ( ) . port ) ;
148169 client . on ( 'connect' , ( ) => {
149170 writeHeaders ( client , headers ) ;
150171 client . resume ( ) ;
151172 } ) ;
173+
174+ client . pipe ( process . stdout ) ;
152175 } ) ) ;
153176} ) ;
154177
0 commit comments