@@ -4,10 +4,14 @@ const common = require('../common');
4
4
const assert = require ( 'assert' ) ;
5
5
const http = require ( 'http' ) ;
6
6
const net = require ( 'net' ) ;
7
- const MAX = 8 * 1024 ; // 8KB
7
+ const MAX = + ( process . argv [ 2 ] || 8 * 1024 ) ; // Command line option, or 8KB.
8
8
9
9
const { getOptionValue } = require ( 'internal/options' ) ;
10
10
11
+ console . log ( 'pid is' , process . pid ) ;
12
+ console . log ( 'max header size is' , getOptionValue ( '--max-http-header-size' ) ) ;
13
+ console . log ( 'current http parser is' , getOptionValue ( '--http-parser' ) ) ;
14
+
11
15
// Verify that we cannot receive more than 8KB of headers.
12
16
13
17
function once ( cb ) {
@@ -38,19 +42,15 @@ function fillHeaders(headers, currentSize, valid = false) {
38
42
39
43
// Generate valid headers
40
44
if ( valid ) {
41
- // TODO(mcollina): understand why -9 is needed instead of -1
42
- headers = headers . slice ( 0 , - 9 ) ;
45
+ // TODO(mcollina): understand why -32 is needed instead of -1
46
+ headers = headers . slice ( 0 , - 32 ) ;
43
47
}
44
48
return headers + '\r\n\r\n' ;
45
49
}
46
50
47
- const timeout = common . platformTimeout ( 10 ) ;
48
-
49
51
function writeHeaders ( socket , headers ) {
50
52
const array = [ ] ;
51
-
52
- // this is off from 1024 so that \r\n does not get split
53
- const chunkSize = 1000 ;
53
+ const chunkSize = 100 ;
54
54
let last = 0 ;
55
55
56
56
for ( let i = 0 ; i < headers . length / chunkSize ; i ++ ) {
@@ -65,19 +65,25 @@ function writeHeaders(socket, headers) {
65
65
next ( ) ;
66
66
67
67
function next ( ) {
68
- if ( socket . write ( array . shift ( ) ) ) {
69
- if ( array . length === 0 ) {
70
- socket . end ( ) ;
71
- } else {
72
- setTimeout ( next , timeout ) ;
73
- }
68
+ if ( socket . destroyed ) {
69
+ console . log ( 'socket was destroyed early, data left to write:' ,
70
+ array . join ( '' ) . length ) ;
71
+ return ;
72
+ }
73
+
74
+ const chunk = array . shift ( ) ;
75
+
76
+ if ( chunk ) {
77
+ console . log ( 'writing chunk of size' , chunk . length ) ;
78
+ socket . write ( chunk , next ) ;
74
79
} else {
75
- socket . once ( 'drain' , next ) ;
80
+ socket . end ( ) ;
76
81
}
77
82
}
78
83
}
79
84
80
85
function test1 ( ) {
86
+ console . log ( 'test1' ) ;
81
87
let headers =
82
88
'HTTP/1.1 200 OK\r\n' +
83
89
'Content-Length: 0\r\n' +
@@ -92,6 +98,9 @@ function test1() {
92
98
writeHeaders ( sock , headers ) ;
93
99
sock . resume ( ) ;
94
100
} ) ;
101
+
102
+ // The socket might error but that's ok
103
+ sock . on ( 'error' , ( ) => { } ) ;
95
104
} ) ;
96
105
97
106
server . listen ( 0 , common . mustCall ( ( ) => {
@@ -100,17 +109,17 @@ function test1() {
100
109
101
110
client . on ( 'error' , common . mustCall ( ( err ) => {
102
111
assert . strictEqual ( err . code , 'HPE_HEADER_OVERFLOW' ) ;
103
- server . close ( ) ;
104
- setImmediate ( test2 ) ;
112
+ server . close ( test2 ) ;
105
113
} ) ) ;
106
114
} ) ) ;
107
115
}
108
116
109
117
const test2 = common . mustCall ( ( ) => {
118
+ console . log ( 'test2' ) ;
110
119
let headers =
111
120
'GET / HTTP/1.1\r\n' +
112
121
'Host: localhost\r\n' +
113
- 'Agent: node \r\n' +
122
+ 'Agent: nod2 \r\n' +
114
123
'X-CRASH: ' ;
115
124
116
125
// /, Host, localhost, Agent, node, X-CRASH, a...
@@ -119,7 +128,7 @@ const test2 = common.mustCall(() => {
119
128
120
129
const server = http . createServer ( common . mustNotCall ( ) ) ;
121
130
122
- server . on ( 'clientError' , common . mustCall ( ( err ) => {
131
+ server . once ( 'clientError' , common . mustCall ( ( err ) => {
123
132
assert . strictEqual ( err . code , 'HPE_HEADER_OVERFLOW' ) ;
124
133
} ) ) ;
125
134
@@ -131,34 +140,46 @@ const test2 = common.mustCall(() => {
131
140
} ) ;
132
141
133
142
finished ( client , common . mustCall ( ( err ) => {
134
- server . close ( ) ;
135
- setImmediate ( test3 ) ;
143
+ server . close ( test3 ) ;
136
144
} ) ) ;
137
145
} ) ) ;
138
146
} ) ;
139
147
140
148
const test3 = common . mustCall ( ( ) => {
149
+ console . log ( 'test3' ) ;
141
150
let headers =
142
151
'GET / HTTP/1.1\r\n' +
143
152
'Host: localhost\r\n' +
144
- 'Agent: node \r\n' +
153
+ 'Agent: nod3 \r\n' +
145
154
'X-CRASH: ' ;
146
155
147
156
// /, Host, localhost, Agent, node, X-CRASH, a...
148
157
const currentSize = 1 + 4 + 9 + 5 + 4 + 7 ;
149
158
headers = fillHeaders ( headers , currentSize , true ) ;
150
159
160
+ console . log ( 'writing' , headers . length ) ;
161
+
151
162
const server = http . createServer ( common . mustCall ( ( req , res ) => {
152
- res . end ( 'hello world ' ) ;
153
- setImmediate ( server . close . bind ( server ) ) ;
163
+ res . end ( 'hello from test3 server ' ) ;
164
+ server . close ( ) ;
154
165
} ) ) ;
155
166
167
+ server . on ( 'clientError' , ( err ) => {
168
+ console . log ( err . code ) ;
169
+ if ( err . code === 'HPE_HEADER_OVERFLOW' ) {
170
+ console . log ( err . rawPacket . toString ( 'hex' ) ) ;
171
+ }
172
+ } ) ;
173
+ server . on ( 'clientError' , common . mustNotCall ( ) ) ;
174
+
156
175
server . listen ( 0 , common . mustCall ( ( ) => {
157
176
const client = net . connect ( server . address ( ) . port ) ;
158
177
client . on ( 'connect' , ( ) => {
159
178
writeHeaders ( client , headers ) ;
160
179
client . resume ( ) ;
161
180
} ) ;
181
+
182
+ client . pipe ( process . stdout ) ;
162
183
} ) ) ;
163
184
} ) ;
164
185
0 commit comments