1+ // Flags: --expose-internals
12'use strict' ;
2-
33const assert = require ( 'assert' ) ;
44const common = require ( '../common' ) ;
55const http = require ( 'http' ) ;
66const net = require ( 'net' ) ;
7- const MAX = 8 * 1024 ; // 8KB
7+ const MAX = + ( process . argv [ 2 ] || 8 * 1024 ) ; // Command line option, or 8KB.
8+
9+ const { getOptionValue } = require ( 'internal/options' ) ;
10+
11+ console . log ( 'pid is' , process . pid ) ;
12+ console . log ( 'max header size is' , getOptionValue ( '--max-http-header-size' ) ) ;
813
914// Verify that we cannot receive more than 8KB of headers.
1015
@@ -28,19 +33,15 @@ function fillHeaders(headers, currentSize, valid = false) {
2833 headers += 'a' . repeat ( MAX - headers . length - 3 ) ;
2934 // Generate valid headers
3035 if ( valid ) {
31- // TODO(mcollina): understand why -9 is needed instead of -1
32- headers = headers . slice ( 0 , - 9 ) ;
36+ // TODO(mcollina): understand why -32 is needed instead of -1
37+ headers = headers . slice ( 0 , - 32 ) ;
3338 }
3439 return headers + '\r\n\r\n' ;
3540}
3641
37- const timeout = common . platformTimeout ( 10 ) ;
38-
3942function writeHeaders ( socket , headers ) {
4043 const array = [ ] ;
41-
42- // this is off from 1024 so that \r\n does not get split
43- const chunkSize = 1000 ;
44+ const chunkSize = 100 ;
4445 let last = 0 ;
4546
4647 for ( let i = 0 ; i < headers . length / chunkSize ; i ++ ) {
@@ -55,19 +56,25 @@ function writeHeaders(socket, headers) {
5556 next ( ) ;
5657
5758 function next ( ) {
58- if ( socket . write ( array . shift ( ) ) ) {
59- if ( array . length === 0 ) {
60- socket . end ( ) ;
61- } else {
62- setTimeout ( next , timeout ) ;
63- }
59+ if ( socket . destroyed ) {
60+ console . log ( 'socket was destroyed early, data left to write:' ,
61+ array . join ( '' ) . length ) ;
62+ return ;
63+ }
64+
65+ const chunk = array . shift ( ) ;
66+
67+ if ( chunk ) {
68+ console . log ( 'writing chunk of size' , chunk . length ) ;
69+ socket . write ( chunk , next ) ;
6470 } else {
65- socket . once ( 'drain' , next ) ;
71+ socket . end ( ) ;
6672 }
6773 }
6874}
6975
7076function test1 ( ) {
77+ console . log ( 'test1' ) ;
7178 let headers =
7279 'HTTP/1.1 200 OK\r\n' +
7380 'Content-Length: 0\r\n' +
@@ -82,6 +89,9 @@ function test1() {
8289 writeHeaders ( sock , headers ) ;
8390 sock . resume ( ) ;
8491 } ) ;
92+
93+ // The socket might error but that's ok
94+ sock . on ( 'error' , ( ) => { } ) ;
8595 } ) ;
8696
8797 server . listen ( 0 , common . mustCall ( ( ) => {
@@ -90,17 +100,17 @@ function test1() {
90100
91101 client . on ( 'error' , common . mustCall ( ( err ) => {
92102 assert . strictEqual ( err . code , 'HPE_HEADER_OVERFLOW' ) ;
93- server . close ( ) ;
94- setImmediate ( test2 ) ;
103+ server . close ( test2 ) ;
95104 } ) ) ;
96105 } ) ) ;
97106}
98107
99108const test2 = common . mustCall ( ( ) => {
109+ console . log ( 'test2' ) ;
100110 let headers =
101111 'GET / HTTP/1.1\r\n' +
102112 'Host: localhost\r\n' +
103- 'Agent: node \r\n' +
113+ 'Agent: nod2 \r\n' +
104114 'X-CRASH: ' ;
105115
106116 // /, Host, localhost, Agent, node, X-CRASH, a...
@@ -109,7 +119,7 @@ const test2 = common.mustCall(() => {
109119
110120 const server = http . createServer ( common . mustNotCall ( ) ) ;
111121
112- server . on ( 'clientError' , common . mustCall ( ( err ) => {
122+ server . once ( 'clientError' , common . mustCall ( ( err ) => {
113123 assert . strictEqual ( err . code , 'HPE_HEADER_OVERFLOW' ) ;
114124 } ) ) ;
115125
@@ -121,34 +131,46 @@ const test2 = common.mustCall(() => {
121131 } ) ;
122132
123133 finished ( client , common . mustCall ( ( err ) => {
124- server . close ( ) ;
125- setImmediate ( test3 ) ;
134+ server . close ( test3 ) ;
126135 } ) ) ;
127136 } ) ) ;
128137} ) ;
129138
130139const test3 = common . mustCall ( ( ) => {
140+ console . log ( 'test3' ) ;
131141 let headers =
132142 'GET / HTTP/1.1\r\n' +
133143 'Host: localhost\r\n' +
134- 'Agent: node \r\n' +
144+ 'Agent: nod3 \r\n' +
135145 'X-CRASH: ' ;
136146
137147 // /, Host, localhost, Agent, node, X-CRASH, a...
138148 const currentSize = 1 + 4 + 9 + 5 + 4 + 7 ;
139149 headers = fillHeaders ( headers , currentSize , true ) ;
140150
151+ console . log ( 'writing' , headers . length ) ;
152+
141153 const server = http . createServer ( common . mustCall ( ( req , res ) => {
142- res . end ( 'hello world ' ) ;
143- setImmediate ( server . close . bind ( server ) ) ;
154+ res . end ( 'hello from test3 server ' ) ;
155+ server . close ( ) ;
144156 } ) ) ;
145157
158+ server . on ( 'clientError' , ( err ) => {
159+ console . log ( err . code ) ;
160+ if ( err . code === 'HPE_HEADER_OVERFLOW' ) {
161+ console . log ( err . rawPacket . toString ( 'hex' ) ) ;
162+ }
163+ } ) ;
164+ server . on ( 'clientError' , common . mustNotCall ( ) ) ;
165+
146166 server . listen ( 0 , common . mustCall ( ( ) => {
147167 const client = net . connect ( server . address ( ) . port ) ;
148168 client . on ( 'connect' , ( ) => {
149169 writeHeaders ( client , headers ) ;
150170 client . resume ( ) ;
151171 } ) ;
172+
173+ client . pipe ( process . stdout ) ;
152174 } ) ) ;
153175} ) ;
154176
0 commit comments