GeezerVison is showing github.com — http2: throw from mapToHeaders · nodejs/node@cd7df56
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/http2/core.js +6-14Lines changed: 6 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1484,8 +1484,6 @@ class ClientHttp2Session extends Http2Session { | 1484 | 1484 | } | 1485 | 1485 |
| 1486 | 1486 | const headersList = mapToHeaders(headers); | 1487 | | - if (!Array.isArray(headersList)) | 1488 | | - throw headersList; | 1489 | 1487 |
| 1490 | 1488 | const stream = new ClientHttp2Stream(this, undefined, undefined, {}); | 1491 | 1489 | stream[kSentHeaders] = headers; | @@ -1890,8 +1888,6 @@ class Http2Stream extends Duplex { | 1890 | 1888 | this[kUpdateTimer](); | 1891 | 1889 |
| 1892 | 1890 | const headersList = mapToHeaders(headers, assertValidPseudoHeaderTrailer); | 1893 | | - if (!Array.isArray(headersList)) | 1894 | | - throw headersList; | 1895 | 1891 | this[kSentTrailers] = headers; | 1896 | 1892 |
| 1897 | 1893 | // Send the trailers in setImmediate so we don't do it on nghttp2 stack. | @@ -2058,12 +2054,14 @@ function processRespondWithFD(self, fd, headers, offset = 0, length = -1, | 2058 | 2054 | const state = self[kState]; | 2059 | 2055 | state.flags |= STREAM_FLAGS_HEADERS_SENT; | 2060 | 2056 |
| 2061 | | - const headersList = mapToHeaders(headers, assertValidPseudoHeaderResponse); | 2062 | | - self[kSentHeaders] = headers; | 2063 | | - if (!Array.isArray(headersList)) { | 2064 | | - self.destroy(headersList); | | 2057 | + let headersList; | | 2058 | + try { | | 2059 | + headersList = mapToHeaders(headers, assertValidPseudoHeaderResponse); | | 2060 | + } catch (err) { | | 2061 | + self.destroy(err); | 2065 | 2062 | return; | 2066 | 2063 | } | | 2064 | + self[kSentHeaders] = headers; | 2067 | 2065 |
| 2068 | 2066 | // Close the writable side of the stream, but only as far as the writable | 2069 | 2067 | // stream implementation is concerned. | @@ -2287,8 +2285,6 @@ class ServerHttp2Stream extends Http2Stream { | 2287 | 2285 | options.readable = false; | 2288 | 2286 |
| 2289 | 2287 | const headersList = mapToHeaders(headers); | 2290 | | - if (!Array.isArray(headersList)) | 2291 | | - throw headersList; | 2292 | 2288 |
| 2293 | 2289 | const streamOptions = options.endStream ? STREAM_OPTION_EMPTY_PAYLOAD : 0; | 2294 | 2290 |
| @@ -2365,8 +2361,6 @@ class ServerHttp2Stream extends Http2Stream { | 2365 | 2361 | } | 2366 | 2362 |
| 2367 | 2363 | const headersList = mapToHeaders(headers, assertValidPseudoHeaderResponse); | 2368 | | - if (!Array.isArray(headersList)) | 2369 | | - throw headersList; | 2370 | 2364 | this[kSentHeaders] = headers; | 2371 | 2365 |
| 2372 | 2366 | state.flags |= STREAM_FLAGS_HEADERS_SENT; | @@ -2527,8 +2521,6 @@ class ServerHttp2Stream extends Http2Stream { | 2527 | 2521 | this[kUpdateTimer](); | 2528 | 2522 |
| 2529 | 2523 | const headersList = mapToHeaders(headers, assertValidPseudoHeaderResponse); | 2530 | | - if (!Array.isArray(headersList)) | 2531 | | - throw headersList; | 2532 | 2524 | if (!this[kInfoHeaders]) | 2533 | 2525 | this[kInfoHeaders] = [headers]; | 2534 | 2526 | else | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/http2/util.js +7-7Lines changed: 7 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -406,22 +406,22 @@ function assertValidPseudoHeader(key) { | 406 | 406 | if (!kValidPseudoHeaders.has(key)) { | 407 | 407 | const err = new ERR_HTTP2_INVALID_PSEUDOHEADER(key); | 408 | 408 | Error.captureStackTrace(err, assertValidPseudoHeader); | 409 | | - return err; | | 409 | + throw err; | 410 | 410 | } | 411 | 411 | } | 412 | 412 |
| 413 | 413 | function assertValidPseudoHeaderResponse(key) { | 414 | 414 | if (key !== ':status') { | 415 | 415 | const err = new ERR_HTTP2_INVALID_PSEUDOHEADER(key); | 416 | 416 | Error.captureStackTrace(err, assertValidPseudoHeaderResponse); | 417 | | - return err; | | 417 | + throw err; | 418 | 418 | } | 419 | 419 | } | 420 | 420 |
| 421 | 421 | function assertValidPseudoHeaderTrailer(key) { | 422 | 422 | const err = new ERR_HTTP2_INVALID_PSEUDOHEADER(key); | 423 | 423 | Error.captureStackTrace(err, assertValidPseudoHeaderTrailer); | 424 | | - return err; | | 424 | + throw err; | 425 | 425 | } | 426 | 426 |
| 427 | 427 | function mapToHeaders(map, | @@ -454,26 +454,26 @@ function mapToHeaders(map, | 454 | 454 | break; | 455 | 455 | default: | 456 | 456 | if (isSingleValueHeader) | 457 | | - return new ERR_HTTP2_HEADER_SINGLE_VALUE(key); | | 457 | + throw new ERR_HTTP2_HEADER_SINGLE_VALUE(key); | 458 | 458 | } | 459 | 459 | } else { | 460 | 460 | value = String(value); | 461 | 461 | } | 462 | 462 | if (isSingleValueHeader) { | 463 | 463 | if (singles.has(key)) | 464 | | - return new ERR_HTTP2_HEADER_SINGLE_VALUE(key); | | 464 | + throw new ERR_HTTP2_HEADER_SINGLE_VALUE(key); | 465 | 465 | singles.add(key); | 466 | 466 | } | 467 | 467 | if (key[0] === ':') { | 468 | 468 | err = assertValuePseudoHeader(key); | 469 | 469 | if (err !== undefined) | 470 | | - return err; | | 470 | + throw err; | 471 | 471 | ret = `${key}\0${value}\0${ret}`; | 472 | 472 | count++; | 473 | 473 | continue; | 474 | 474 | } | 475 | 475 | if (isIllegalConnectionSpecificHeader(key, value)) { | 476 | | - return new ERR_HTTP2_INVALID_CONNECTION_HEADERS(key); | | 476 | + throw new ERR_HTTP2_INVALID_CONNECTION_HEADERS(key); | 477 | 477 | } | 478 | 478 | if (isArray) { | 479 | 479 | for (var k = 0; k < value.length; k++) { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-http2-util-assert-valid-pseudoheader.js +11-19Lines changed: 11 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,24 +8,16 @@ const common = require('../common'); | 8 | 8 | // have to test it through mapToHeaders | 9 | 9 |
| 10 | 10 | const { mapToHeaders } = require('internal/http2/util'); | 11 | | -const assert = require('assert'); | 12 | 11 |
| 13 | | -function isNotError(val) { | 14 | | - assert(!(val instanceof Error)); | 15 | | -} | | 12 | +// These should not throw | | 13 | +mapToHeaders({ ':status': 'a' }); | | 14 | +mapToHeaders({ ':path': 'a' }); | | 15 | +mapToHeaders({ ':authority': 'a' }); | | 16 | +mapToHeaders({ ':scheme': 'a' }); | | 17 | +mapToHeaders({ ':method': 'a' }); | 16 | 18 |
| 17 | | -function isError(val) { | 18 | | - common.expectsError({ | 19 | | - code: 'ERR_HTTP2_INVALID_PSEUDOHEADER', | 20 | | - type: TypeError, | 21 | | - message: '":foo" is an invalid pseudoheader or is used incorrectly' | 22 | | - })(val); | 23 | | -} | 24 | | - | 25 | | -isNotError(mapToHeaders({ ':status': 'a' })); | 26 | | -isNotError(mapToHeaders({ ':path': 'a' })); | 27 | | -isNotError(mapToHeaders({ ':authority': 'a' })); | 28 | | -isNotError(mapToHeaders({ ':scheme': 'a' })); | 29 | | -isNotError(mapToHeaders({ ':method': 'a' })); | 30 | | - | 31 | | -isError(mapToHeaders({ ':foo': 'a' })); | | 19 | +common.expectsError(() => mapToHeaders({ ':foo': 'a' }), { | | 20 | + code: 'ERR_HTTP2_INVALID_PSEUDOHEADER', | | 21 | + type: TypeError, | | 22 | + message: '":foo" is an invalid pseudoheader or is used incorrectly' | | 23 | +}); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-http2-util-headers-list.js +18-16Lines changed: 18 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -175,11 +175,11 @@ const { | 175 | 175 | ':statuS': 204, | 176 | 176 | }; | 177 | 177 |
| 178 | | - common.expectsError({ | | 178 | + common.expectsError(() => mapToHeaders(headers), { | 179 | 179 | code: 'ERR_HTTP2_HEADER_SINGLE_VALUE', | 180 | 180 | type: TypeError, | 181 | 181 | message: 'Header field ":status" must only have a single value' | 182 | | - })(mapToHeaders(headers)); | | 182 | + }); | 183 | 183 | } | 184 | 184 |
| 185 | 185 | // The following are not allowed to have multiple values | @@ -224,10 +224,10 @@ const { | 224 | 224 | HTTP2_HEADER_X_CONTENT_TYPE_OPTIONS | 225 | 225 | ].forEach((name) => { | 226 | 226 | const msg = `Header field "${name}" must only have a single value`; | 227 | | - common.expectsError({ | | 227 | + common.expectsError(() => mapToHeaders({ [name]: [1, 2, 3] }), { | 228 | 228 | code: 'ERR_HTTP2_HEADER_SINGLE_VALUE', | 229 | 229 | message: msg | 230 | | - })(mapToHeaders({ [name]: [1, 2, 3] })); | | 230 | + }); | 231 | 231 | }); | 232 | 232 |
| 233 | 233 | [ | @@ -281,30 +281,32 @@ const { | 281 | 281 | 'Proxy-Connection', | 282 | 282 | 'Keep-Alive' | 283 | 283 | ].forEach((name) => { | 284 | | - common.expectsError({ | | 284 | + common.expectsError(() => mapToHeaders({ [name]: 'abc' }), { | 285 | 285 | code: 'ERR_HTTP2_INVALID_CONNECTION_HEADERS', | 286 | 286 | name: 'TypeError [ERR_HTTP2_INVALID_CONNECTION_HEADERS]', | 287 | 287 | message: 'HTTP/1 Connection specific headers are forbidden: ' + | 288 | 288 | `"${name.toLowerCase()}"` | 289 | | - })(mapToHeaders({ [name]: 'abc' })); | | 289 | + }); | 290 | 290 | }); | 291 | 291 |
| 292 | | -common.expectsError({ | | 292 | +common.expectsError(() => mapToHeaders({ [HTTP2_HEADER_TE]: ['abc'] }), { | 293 | 293 | code: 'ERR_HTTP2_INVALID_CONNECTION_HEADERS', | 294 | 294 | name: 'TypeError [ERR_HTTP2_INVALID_CONNECTION_HEADERS]', | 295 | 295 | message: 'HTTP/1 Connection specific headers are forbidden: ' + | 296 | 296 | `"${HTTP2_HEADER_TE}"` | 297 | | -})(mapToHeaders({ [HTTP2_HEADER_TE]: ['abc'] })); | | 297 | +}); | 298 | 298 |
| 299 | | -common.expectsError({ | 300 | | - code: 'ERR_HTTP2_INVALID_CONNECTION_HEADERS', | 301 | | - name: 'TypeError [ERR_HTTP2_INVALID_CONNECTION_HEADERS]', | 302 | | - message: 'HTTP/1 Connection specific headers are forbidden: ' + | 303 | | - `"${HTTP2_HEADER_TE}"` | 304 | | -})(mapToHeaders({ [HTTP2_HEADER_TE]: ['abc', 'trailers'] })); | | 299 | +common.expectsError( | | 300 | + () => mapToHeaders({ [HTTP2_HEADER_TE]: ['abc', 'trailers'] }), { | | 301 | + code: 'ERR_HTTP2_INVALID_CONNECTION_HEADERS', | | 302 | + name: 'TypeError [ERR_HTTP2_INVALID_CONNECTION_HEADERS]', | | 303 | + message: 'HTTP/1 Connection specific headers are forbidden: ' + | | 304 | + `"${HTTP2_HEADER_TE}"` | | 305 | + }); | 305 | 306 |
| 306 | | -assert(!(mapToHeaders({ te: 'trailers' }) instanceof Error)); | 307 | | -assert(!(mapToHeaders({ te: ['trailers'] }) instanceof Error)); | | 307 | +// These should not throw | | 308 | +mapToHeaders({ te: 'trailers' }); | | 309 | +mapToHeaders({ te: ['trailers'] }); | 308 | 310 |
| 309 | 311 |
| 310 | 312 | { | |
|