GeezerVison is showing github.com — http: batch body spans per parser execute · nodejs/node@6b8a90d
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 clipboard +55Lines changed: 55 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +// Measures parsing a chunked request body as the chunk size shrinks. | | 4 | +// The wire buffer is fed to the parser in 64 KiB slices, like socket reads. | | 5 | + | | 6 | +const common = require('../common'); | | 7 | + | | 8 | +const bench = common.createBenchmark(main, { | | 9 | + chunkSize: [1, 16, 256, 4096], | | 10 | + len: [64 * 1024], | | 11 | + n: [500], | | 12 | +}, { | | 13 | + flags: ['--expose-internals', '--no-warnings'], | | 14 | +}); | | 15 | + | | 16 | +function main({ chunkSize, len, n }) { | | 17 | + const { HTTPParser } = common.binding('http_parser'); | | 18 | + const REQUEST = HTTPParser.REQUEST; | | 19 | + const kOnHeaders = HTTPParser.kOnHeaders | 0; | | 20 | + const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0; | | 21 | + const kOnBody = HTTPParser.kOnBody | 0; | | 22 | + const kOnMessageComplete = HTTPParser.kOnMessageComplete | 0; | | 23 | + const kMaxHeaderPairs = 2000; | | 24 | + const kReadSize = 64 * 1024; | | 25 | + | | 26 | + const chunk = `${chunkSize.toString(16)}\r\n${'x'.repeat(chunkSize)}\r\n`; | | 27 | + const wire = Buffer.from( | | 28 | + 'POST / HTTP/1.1\r\nHost: localhost\r\nTransfer-Encoding: chunked\r\n\r\n' + | | 29 | + chunk.repeat(Math.ceil(len / chunkSize)) + | | 30 | + '0\r\n\r\n'); | | 31 | + | | 32 | + const reads = []; | | 33 | + for (let i = 0; i < wire.length; i += kReadSize) | | 34 | + reads.push(wire.subarray(i, i + kReadSize)); | | 35 | + | | 36 | + let bodyBytes = 0; | | 37 | + const parser = new HTTPParser(); | | 38 | + parser.initialize(REQUEST, {}, 0, 0, undefined, kMaxHeaderPairs); | | 39 | + parser[kOnHeaders] = function() {}; | | 40 | + parser[kOnHeadersComplete] = function() {}; | | 41 | + parser[kOnBody] = function(b) { bodyBytes += b.length; }; | | 42 | + parser[kOnMessageComplete] = function() {}; | | 43 | + | | 44 | + bench.start(); | | 45 | + for (let i = 0; i < n; i++) { | | 46 | + for (const read of reads) | | 47 | + parser.execute(read); | | 48 | + parser.initialize(REQUEST, {}, 0, 0, undefined, kMaxHeaderPairs); | | 49 | + } | | 50 | + bench.end(n); | | 51 | + | | 52 | + const expected = Math.ceil(len / chunkSize) * chunkSize * n; | | 53 | + if (bodyBytes !== expected) | | 54 | + throw new Error(`Expected ${expected} body bytes, got ${bodyBytes}`); | | 55 | +} |
Collapse file Copy file name to clipboardExpand all lines: src/node_http_parser.cc +50-13Lines changed: 50 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 34 | 34 | #include <algorithm> | 35 | 35 | #include <cstdlib> // free() | 36 | 36 | #include <cstring> // strdup(), strchr() | 37 | | - | | 37 | +#include <vector> | 38 | 38 |
| 39 | 39 | // This is a binding to llhttp (https://github.com/nodejs/llhttp) | 40 | 40 | // The goal is to decouple sockets from parsing for more javascript-level | @@ -85,6 +85,7 @@ const uint32_t kOnTimeout = 6; | 85 | 85 | const size_t kMaxHeaderFieldsCount = 32; | 86 | 86 | // Maximum size of chunk extensions | 87 | 87 | const size_t kMaxChunkExtensionsSize = 16384; | | 88 | +const size_t kMaxBufferedBodySize = 64 * 1024; | 88 | 89 |
| 89 | 90 | const uint32_t kLenientNone = 0; | 90 | 91 | const uint32_t kLenientHeaders = 1 << 0; | @@ -518,36 +519,66 @@ class Parser : public AsyncWrap, public StreamListener { | 518 | 519 | return static_cast<int>(val); | 519 | 520 | } | 520 | 521 |
| 521 | | - | 522 | 522 | int on_body(const char* at, size_t length) { | 523 | 523 | if (length == 0) | 524 | 524 | return 0; | 525 | 525 |
| | 526 | + if (pending_body_len_ + length > kMaxBufferedBodySize && !FlushBody()) { | | 527 | + llhttp_set_error_reason(&parser_, "HPE_JS_EXCEPTION:JS Exception"); | | 528 | + return HPE_USER; | | 529 | + } | | 530 | + | | 531 | + if (pending_body_len_ == 0) { | | 532 | + pending_body_ = at; | | 533 | + } else { | | 534 | + if (body_buf_.empty()) { | | 535 | + body_buf_.reserve(kMaxBufferedBodySize); | | 536 | + body_buf_.assign(pending_body_, pending_body_ + pending_body_len_); | | 537 | + } | | 538 | + body_buf_.insert(body_buf_.end(), at, at + length); | | 539 | + } | | 540 | + pending_body_len_ += length; | | 541 | + | | 542 | + return 0; | | 543 | + } | | 544 | + | | 545 | + bool FlushBody() { | | 546 | + if (pending_body_len_ == 0) return true; | | 547 | + | | 548 | + const char* data = body_buf_.empty() ? pending_body_ : body_buf_.data(); | | 549 | + size_t length = pending_body_len_; | | 550 | + pending_body_len_ = 0; | | 551 | + | | 552 | + if (got_exception_) { | | 553 | + body_buf_.clear(); | | 554 | + return true; | | 555 | + } | | 556 | + | 526 | 557 | Environment* env = this->env(); | 527 | 558 | HandleScope handle_scope(env->isolate()); | 528 | 559 |
| 529 | 560 | Local<Value> cb = CachedCallback(kOnBody, kOnBodyCallback); | | 561 | + if (!cb->IsFunction()) { | | 562 | + body_buf_.clear(); | | 563 | + return true; | | 564 | + } | 530 | 565 |
| 531 | | - if (!cb->IsFunction()) | 532 | | - return 0; | 533 | | - | 534 | | - Local<Value> buffer = Buffer::Copy(env, at, length).ToLocalChecked(); | 535 | | - | 536 | | - MaybeLocal<Value> r = MakeCallback(cb.As<Function>(), 1, &buffer); | | 566 | + Local<Value> buffer = Buffer::Copy(env, data, length).ToLocalChecked(); | | 567 | + body_buf_.clear(); | 537 | 568 |
| 538 | | - if (r.IsEmpty()) { | | 569 | + if (MakeCallback(cb.As<Function>(), 1, &buffer).IsEmpty()) { | 539 | 570 | got_exception_ = true; | 540 | | - llhttp_set_error_reason(&parser_, "HPE_JS_EXCEPTION:JS Exception"); | 541 | | - return HPE_USER; | | 571 | + return false; | 542 | 572 | } | 543 | 573 |
| 544 | | - return 0; | | 574 | + return true; | 545 | 575 | } | 546 | 576 |
| 547 | | - | 548 | 577 | int on_message_complete() { | 549 | 578 | HandleScope scope(env()->isolate()); | 550 | 579 |
| | 580 | + if (!FlushBody()) return -1; | | 581 | + | 551 | 582 | if (connectionsList_ != nullptr) { | 552 | 583 | connectionsList_->PopActive(this); | 553 | 584 | } | @@ -883,6 +914,9 @@ class Parser : public AsyncWrap, public StreamListener { | 883 | 914 | Save(); | 884 | 915 | } | 885 | 916 |
| | 917 | + // Deliver buffered body spans while `data` is still alive. | | 918 | + FlushBody(); | | 919 | + | 886 | 920 | // Calculate bytes read and resume after Upgrade/CONNECT pause | 887 | 921 | size_t nread = len; | 888 | 922 | if (err != HPE_OK) { | @@ -1118,6 +1152,9 @@ class Parser : public AsyncWrap, public StreamListener { | 1118 | 1152 | bool is_being_freed_ = false; | 1119 | 1153 | size_t current_buffer_len_; | 1120 | 1154 | const char* current_buffer_data_; | | 1155 | + const char* pending_body_ = nullptr; | | 1156 | + size_t pending_body_len_ = 0; | | 1157 | + std::vector<char> body_buf_; | 1121 | 1158 | bool headers_completed_ = false; | 1122 | 1159 | size_t header_pairs_ = 0; | 1123 | 1160 | size_t max_header_pairs_ = 0; | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-http-parser.js +19-2Lines changed: 19 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -331,8 +331,9 @@ function expectBody(expected) { | 331 | 331 | assert.strictEqual(versionMinor, 1); | 332 | 332 | }); | 333 | 333 |
| | 334 | + // Chunks parsed in the same execute() call are delivered as one buffer. | 334 | 335 | let body_part = 0; | 335 | | - const body_parts = ['123', '123456', '1234567890']; | | 336 | + const body_parts = ['1231234561234567890']; | 336 | 337 |
| 337 | 338 | const onBody = mustCallAtLeast((buf) => { | 338 | 339 | const body = String(buf); | @@ -369,9 +370,10 @@ function expectBody(expected) { | 369 | 370 | assert.strictEqual(versionMinor, 1); | 370 | 371 | }); | 371 | 372 |
| | 373 | + // One buffer per execute() call, holding all the chunks it parsed. | 372 | 374 | let body_part = 0; | 373 | 375 | const body_parts = | 374 | | - ['123', '123456', '123456789', '123456789ABC', '123456789ABCDEF']; | | 376 | + ['123123456', '123456789123456789ABC123456789ABCDEF']; | 375 | 377 |
| 376 | 378 | const onBody = mustCallAtLeast((buf) => { | 377 | 379 | const body = String(buf); | @@ -562,6 +564,21 @@ function expectBody(expected) { | 562 | 564 | parser.execute(req2, 0, req2.length); | 563 | 565 | } | 564 | 566 |
| | 567 | +// Buffered body spans are flushed once they would exceed 64 KiB. | | 568 | +{ | | 569 | + const chunk = `400\r\n${'x'.repeat(1024)}\r\n`; | | 570 | + const request = Buffer.from( | | 571 | + 'POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n' + | | 572 | + chunk.repeat(100) + '0\r\n\r\n'); | | 573 | + | | 574 | + const sizes = []; | | 575 | + const parser = newParser(REQUEST); | | 576 | + parser[kOnBody] = (buf) => sizes.push(buf.length); | | 577 | + parser.execute(request, 0, request.length); | | 578 | + | | 579 | + assert.deepStrictEqual(sizes, [64 * 1024, 36 * 1024]); | | 580 | +} | | 581 | + | 565 | 582 | // Test parser 'this' safety | 566 | 583 | // https://github.com/joyent/node/issues/6690 | 567 | 584 | assert.throws(function() { | |
|