1
+ // Flags: --expose-internals
1
2
'use strict' ;
2
-
3
3
const assert = require ( 'assert' ) ;
4
4
const common = require ( '../common' ) ;
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
+
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' ) ) ;
8
13
9
14
// Verify that we cannot receive more than 8KB of headers.
10
15
@@ -28,19 +33,15 @@ function fillHeaders(headers, currentSize, valid = false) {
28
33
headers += 'a' . repeat ( MAX - headers . length - 3 ) ;
29
34
// Generate valid headers
30
35
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 ) ;
33
38
}
34
39
return headers + '\r\n\r\n' ;
35
40
}
36
41
37
- const timeout = common . platformTimeout ( 10 ) ;
38
-
39
42
function writeHeaders ( socket , headers ) {
40
43
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 ;
44
45
let last = 0 ;
45
46
46
47
for ( let i = 0 ; i < headers . length / chunkSize ; i ++ ) {
@@ -55,19 +56,25 @@ function writeHeaders(socket, headers) {
55
56
next ( ) ;
56
57
57
58
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 ) ;
64
70
} else {
65
- socket . once ( 'drain' , next ) ;
71
+ socket . end ( ) ;
66
72
}
67
73
}
68
74
}
69
75
70
76
function test1 ( ) {
77
+ console . log ( 'test1' ) ;
71
78
let headers =
72
79
'HTTP/1.1 200 OK\r\n' +
73
80
'Content-Length: 0\r\n' +
@@ -82,6 +89,9 @@ function test1() {
82
89
writeHeaders ( sock , headers ) ;
83
90
sock . resume ( ) ;
84
91
} ) ;
92
+
93
+ // The socket might error but that's ok
94
+ sock . on ( 'error' , ( ) => { } ) ;
85
95
} ) ;
86
96
87
97
server . listen ( 0 , common . mustCall ( ( ) => {
@@ -90,17 +100,17 @@ function test1() {
90
100
91
101
client . on ( 'error' , common . mustCall ( ( err ) => {
92
102
assert . strictEqual ( err . code , 'HPE_HEADER_OVERFLOW' ) ;
93
- server . close ( ) ;
94
- setImmediate ( test2 ) ;
103
+ server . close ( test2 ) ;
95
104
} ) ) ;
96
105
} ) ) ;
97
106
}
98
107
99
108
const test2 = common . mustCall ( ( ) => {
109
+ console . log ( 'test2' ) ;
100
110
let headers =
101
111
'GET / HTTP/1.1\r\n' +
102
112
'Host: localhost\r\n' +
103
- 'Agent: node \r\n' +
113
+ 'Agent: nod2 \r\n' +
104
114
'X-CRASH: ' ;
105
115
106
116
// /, Host, localhost, Agent, node, X-CRASH, a...
@@ -109,7 +119,7 @@ const test2 = common.mustCall(() => {
109
119
110
120
const server = http . createServer ( common . mustNotCall ( ) ) ;
111
121
112
- server . on ( 'clientError' , common . mustCall ( ( err ) => {
122
+ server . once ( 'clientError' , common . mustCall ( ( err ) => {
113
123
assert . strictEqual ( err . code , 'HPE_HEADER_OVERFLOW' ) ;
114
124
} ) ) ;
115
125
@@ -121,34 +131,46 @@ const test2 = common.mustCall(() => {
121
131
} ) ;
122
132
123
133
finished ( client , common . mustCall ( ( err ) => {
124
- server . close ( ) ;
125
- setImmediate ( test3 ) ;
134
+ server . close ( test3 ) ;
126
135
} ) ) ;
127
136
} ) ) ;
128
137
} ) ;
129
138
130
139
const test3 = common . mustCall ( ( ) => {
140
+ console . log ( 'test3' ) ;
131
141
let headers =
132
142
'GET / HTTP/1.1\r\n' +
133
143
'Host: localhost\r\n' +
134
- 'Agent: node \r\n' +
144
+ 'Agent: nod3 \r\n' +
135
145
'X-CRASH: ' ;
136
146
137
147
// /, Host, localhost, Agent, node, X-CRASH, a...
138
148
const currentSize = 1 + 4 + 9 + 5 + 4 + 7 ;
139
149
headers = fillHeaders ( headers , currentSize , true ) ;
140
150
151
+ console . log ( 'writing' , headers . length ) ;
152
+
141
153
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 ( ) ;
144
156
} ) ) ;
145
157
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
+
146
166
server . listen ( 0 , common . mustCall ( ( ) => {
147
167
const client = net . connect ( server . address ( ) . port ) ;
148
168
client . on ( 'connect' , ( ) => {
149
169
writeHeaders ( client , headers ) ;
150
170
client . resume ( ) ;
151
171
} ) ;
172
+
173
+ client . pipe ( process . stdout ) ;
152
174
} ) ) ;
153
175
} ) ;
154
176
0 commit comments