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