| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -117,17 +117,16 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method, | |||
| 117 | 117 | return parser.onIncoming(incoming, shouldKeepAlive); | |
| 118 | 118 | } | |
| 119 | 119 | ||
| 120 | - function parserOnBody(b, start, len) { | ||
| 120 | + function parserOnBody(b) { | ||
| 121 | 121 | const stream = this.incoming; | |
| 122 | 122 | ||
| 123 | 123 | // If the stream has already been removed, then drop it. | |
| 124 | 124 | if (stream === null) | |
| 125 | 125 | return; | |
| 126 | 126 | ||
| 127 | 127 | // Pretend this was the result of a stream._read call. | |
| 128 | - if (len > 0 && !stream._dumped) { | ||
| 129 | - const slice = b.slice(start, start + len); | ||
| 130 | - const ret = stream.push(slice); | ||
| 128 | + if (!stream._dumped) { | ||
| 129 | + const ret = stream.push(b); | ||
| 131 | 130 | if (!ret) | |
| 132 | 131 | readStop(this.socket); | |
| 133 | 132 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -248,11 +248,7 @@ class Parser : public AsyncWrap, public StreamListener { | |||
| 248 | 248 | binding_data_(binding_data) { | |
| 249 | 249 | } | |
| 250 | 250 | ||
| 251 | - | ||
| 252 | - void MemoryInfo(MemoryTracker* tracker) const override { | ||
| 253 | - tracker->TrackField("current_buffer", current_buffer_); | ||
| 254 | - } | ||
| 255 | - | ||
| 251 | + SET_NO_MEMORY_INFO() | ||
| 256 | 252 | SET_MEMORY_INFO_NAME(Parser) | |
| 257 | 253 | SET_SELF_SIZE(Parser) | |
| 258 | 254 | ||
@@ -454,32 +450,20 @@ class Parser : public AsyncWrap, public StreamListener { | |||
| 454 | 450 | ||
| 455 | 451 | ||
| 456 | 452 | int on_body(const char* at, size_t length) { | |
| 457 | - EscapableHandleScope scope(env()->isolate()); | ||
| 453 | + if (length == 0) | ||
| 454 | + return 0; | ||
| 458 | 455 | ||
| 459 | - Local<Object> obj = object(); | ||
| 460 | - Local<Value> cb = obj->Get(env()->context(), kOnBody).ToLocalChecked(); | ||
| 456 | + Environment* env = this->env(); | ||
| 457 | + HandleScope handle_scope(env->isolate()); | ||
| 458 | + | ||
| 459 | + Local<Value> cb = object()->Get(env->context(), kOnBody).ToLocalChecked(); | ||
| 461 | 460 | ||
| 462 | 461 | if (!cb->IsFunction()) | |
| 463 | 462 | return 0; | |
| 464 | 463 | ||
| 465 | - // We came from consumed stream | ||
| 466 | - if (current_buffer_.IsEmpty()) { | ||
| 467 | - // Make sure Buffer will be in parent HandleScope | ||
| 468 | - current_buffer_ = scope.Escape(Buffer::Copy( | ||
| 469 | - env()->isolate(), | ||
| 470 | - current_buffer_data_, | ||
| 471 | - current_buffer_len_).ToLocalChecked()); | ||
| 472 | - } | ||
| 464 | + Local<Value> buffer = Buffer::Copy(env, at, length).ToLocalChecked(); | ||
| 473 | 465 | ||
| 474 | - Local<Value> argv[3] = { | ||
| 475 | - current_buffer_, | ||
| 476 | - Integer::NewFromUnsigned( | ||
| 477 | - env()->isolate(), static_cast<uint32_t>(at - current_buffer_data_)), | ||
| 478 | - Integer::NewFromUnsigned(env()->isolate(), length)}; | ||
| 479 | - | ||
| 480 | - MaybeLocal<Value> r = MakeCallback(cb.As<Function>(), | ||
| 481 | - arraysize(argv), | ||
| 482 | - argv); | ||
| 466 | + MaybeLocal<Value> r = MakeCallback(cb.As<Function>(), 1, &buffer); | ||
| 483 | 467 | ||
| 484 | 468 | if (r.IsEmpty()) { | |
| 485 | 469 | got_exception_ = true; | |
@@ -593,17 +577,9 @@ class Parser : public AsyncWrap, public StreamListener { | |||
| 593 | 577 | static void Execute(const FunctionCallbackInfo<Value>& args) { | |
| 594 | 578 | Parser* parser; | |
| 595 | 579 | ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder()); | |
| 596 | - CHECK(parser->current_buffer_.IsEmpty()); | ||
| 597 | - CHECK_EQ(parser->current_buffer_len_, 0); | ||
| 598 | - CHECK_NULL(parser->current_buffer_data_); | ||
| 599 | 580 | ||
| 600 | 581 | ArrayBufferViewContents<char> buffer(args[0]); | |
| 601 | 582 | ||
| 602 | - // This is a hack to get the current_buffer to the callbacks with the least | ||
| 603 | - // amount of overhead. Nothing else will run while http_parser_execute() | ||
| 604 | - // runs, therefore this pointer can be set and used for the execution. | ||
| 605 | - parser->current_buffer_ = args[0].As<Object>(); | ||
| 606 | - | ||
| 607 | 583 | Local<Value> ret = parser->Execute(buffer.data(), buffer.length()); | |
| 608 | 584 | ||
| 609 | 585 | if (!ret.IsEmpty()) | |
@@ -615,7 +591,6 @@ class Parser : public AsyncWrap, public StreamListener { | |||
| 615 | 591 | Parser* parser; | |
| 616 | 592 | ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder()); | |
| 617 | 593 | ||
| 618 | - CHECK(parser->current_buffer_.IsEmpty()); | ||
| 619 | 594 | Local<Value> ret = parser->Execute(nullptr, 0); | |
| 620 | 595 | ||
| 621 | 596 | if (!ret.IsEmpty()) | |
@@ -695,11 +670,6 @@ class Parser : public AsyncWrap, public StreamListener { | |||
| 695 | 670 | // Should always be called from the same context. | |
| 696 | 671 | CHECK_EQ(env, parser->env()); | |
| 697 | 672 | ||
| 698 | - if (parser->execute_depth_) { | ||
| 699 | - parser->pending_pause_ = should_pause; | ||
| 700 | - return; | ||
| 701 | - } | ||
| 702 | - | ||
| 703 | 673 | if (should_pause) { | |
| 704 | 674 | llhttp_pause(&parser->parser_); | |
| 705 | 675 | } else { | |
@@ -801,7 +771,6 @@ class Parser : public AsyncWrap, public StreamListener { | |||
| 801 | 771 | if (nread == 0) | |
| 802 | 772 | return; | |
| 803 | 773 | ||
| 804 | - current_buffer_.Clear(); | ||
| 805 | 774 | Local<Value> ret = Execute(buf.base, nread); | |
| 806 | 775 | ||
| 807 | 776 | // Exception | |
@@ -834,17 +803,12 @@ class Parser : public AsyncWrap, public StreamListener { | |||
| 834 | 803 | ||
| 835 | 804 | llhttp_errno_t err; | |
| 836 | 805 | ||
| 837 | - // Do not allow re-entering `http_parser_execute()` | ||
| 838 | - CHECK_EQ(execute_depth_, 0); | ||
| 839 | - | ||
| 840 | - execute_depth_++; | ||
| 841 | 806 | if (data == nullptr) { | |
| 842 | 807 | err = llhttp_finish(&parser_); | |
| 843 | 808 | } else { | |
| 844 | 809 | err = llhttp_execute(&parser_, data, len); | |
| 845 | 810 | Save(); | |
| 846 | 811 | } | |
| 847 | - execute_depth_--; | ||
| 848 | 812 | ||
| 849 | 813 | // Calculate bytes read and resume after Upgrade/CONNECT pause | |
| 850 | 814 | size_t nread = len; | |
@@ -864,8 +828,6 @@ class Parser : public AsyncWrap, public StreamListener { | |||
| 864 | 828 | llhttp_pause(&parser_); | |
| 865 | 829 | } | |
| 866 | 830 | ||
| 867 | - // Unassign the 'buffer_' variable | ||
| 868 | - current_buffer_.Clear(); | ||
| 869 | 831 | current_buffer_len_ = 0; | |
| 870 | 832 | current_buffer_data_ = nullptr; | |
| 871 | 833 | ||
@@ -989,8 +951,6 @@ class Parser : public AsyncWrap, public StreamListener { | |||
| 989 | 951 | ||
| 990 | 952 | ||
| 991 | 953 | int MaybePause() { | |
| 992 | - CHECK_NE(execute_depth_, 0); | ||
| 993 | - | ||
| 994 | 954 | if (!pending_pause_) { | |
| 995 | 955 | return 0; | |
| 996 | 956 | } | |
@@ -1018,10 +978,8 @@ class Parser : public AsyncWrap, public StreamListener { | |||
| 1018 | 978 | size_t num_values_; | |
| 1019 | 979 | bool have_flushed_; | |
| 1020 | 980 | bool got_exception_; | |
| 1021 | - Local<Object> current_buffer_; | ||
| 1022 | 981 | size_t current_buffer_len_; | |
| 1023 | 982 | const char* current_buffer_data_; | |
| 1024 | - unsigned int execute_depth_ = 0; | ||
| 1025 | 983 | bool headers_completed_ = false; | |
| 1026 | 984 | bool pending_pause_ = false; | |
| 1027 | 985 | uint64_t header_nread_ = 0; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,37 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const { request } = require('http'); | ||
| 6 | + const { Duplex } = require('stream'); | ||
| 7 | + | ||
| 8 | + let socket; | ||
| 9 | + | ||
| 10 | + function createConnection(...args) { | ||
| 11 | + socket = new Duplex({ | ||
| 12 | + read() {}, | ||
| 13 | + write(chunk, encoding, callback) { | ||
| 14 | + if (chunk.toString().includes('\r\n\r\n')) { | ||
| 15 | + this.push('HTTP/1.1 100 Continue\r\n\r\n'); | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + callback(); | ||
| 19 | + } | ||
| 20 | + }); | ||
| 21 | + | ||
| 22 | + return socket; | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + const req = request('http://localhost:8080', { createConnection }); | ||
| 26 | + | ||
| 27 | + req.on('information', common.mustCall(({ statusCode }) => { | ||
| 28 | + assert.strictEqual(statusCode, 100); | ||
| 29 | + socket.push('HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n'); | ||
| 30 | + socket.push(null); | ||
| 31 | + })); | ||
| 32 | + | ||
| 33 | + req.on('response', common.mustCall(({ statusCode }) => { | ||
| 34 | + assert.strictEqual(statusCode, 200); | ||
| 35 | + })); | ||
| 36 | + | ||
| 37 | + req.end(); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,8 +62,8 @@ function newParser(type) { | |||
| 62 | 62 | ||
| 63 | 63 | ||
| 64 | 64 | function expectBody(expected) { | |
| 65 | - return mustCall(function(buf, start, len) { | ||
| 66 | - const body = String(buf.slice(start, start + len)); | ||
| 65 | + return mustCall(function(buf) { | ||
| 66 | + const body = String(buf); | ||
| 67 | 67 | assert.strictEqual(body, expected); | |
| 68 | 68 | }); | |
| 69 | 69 | } | |
@@ -126,8 +126,8 @@ function expectBody(expected) { | |||
| 126 | 126 | assert.strictEqual(statusMessage, 'OK'); | |
| 127 | 127 | }; | |
| 128 | 128 | ||
| 129 | - const onBody = (buf, start, len) => { | ||
| 130 | - const body = String(buf.slice(start, start + len)); | ||
| 129 | + const onBody = (buf) => { | ||
| 130 | + const body = String(buf); | ||
| 131 | 131 | assert.strictEqual(body, 'pong'); | |
| 132 | 132 | }; | |
| 133 | 133 | ||
@@ -195,8 +195,8 @@ function expectBody(expected) { | |||
| 195 | 195 | parser[kOnHeaders] = mustCall(onHeaders); | |
| 196 | 196 | }; | |
| 197 | 197 | ||
| 198 | - const onBody = (buf, start, len) => { | ||
| 199 | - const body = String(buf.slice(start, start + len)); | ||
| 198 | + const onBody = (buf) => { | ||
| 199 | + const body = String(buf); | ||
| 200 | 200 | assert.strictEqual(body, 'ping'); | |
| 201 | 201 | seen_body = true; | |
| 202 | 202 | }; | |
@@ -291,8 +291,8 @@ function expectBody(expected) { | |||
| 291 | 291 | assert.strictEqual(versionMinor, 1); | |
| 292 | 292 | }; | |
| 293 | 293 | ||
| 294 | - const onBody = (buf, start, len) => { | ||
| 295 | - const body = String(buf.slice(start, start + len)); | ||
| 294 | + const onBody = (buf) => { | ||
| 295 | + const body = String(buf); | ||
| 296 | 296 | assert.strictEqual(body, 'foo=42&bar=1337'); | |
| 297 | 297 | }; | |
| 298 | 298 | ||
@@ -332,8 +332,8 @@ function expectBody(expected) { | |||
| 332 | 332 | let body_part = 0; | |
| 333 | 333 | const body_parts = ['123', '123456', '1234567890']; | |
| 334 | 334 | ||
| 335 | - const onBody = (buf, start, len) => { | ||
| 336 | - const body = String(buf.slice(start, start + len)); | ||
| 335 | + const onBody = (buf) => { | ||
| 336 | + const body = String(buf); | ||
| 337 | 337 | assert.strictEqual(body, body_parts[body_part++]); | |
| 338 | 338 | }; | |
| 339 | 339 | ||
@@ -371,8 +371,8 @@ function expectBody(expected) { | |||
| 371 | 371 | const body_parts = | |
| 372 | 372 | ['123', '123456', '123456789', '123456789ABC', '123456789ABCDEF']; | |
| 373 | 373 | ||
| 374 | - const onBody = (buf, start, len) => { | ||
| 375 | - const body = String(buf.slice(start, start + len)); | ||
| 374 | + const onBody = (buf) => { | ||
| 375 | + const body = String(buf); | ||
| 376 | 376 | assert.strictEqual(body, body_parts[body_part++]); | |
| 377 | 377 | }; | |
| 378 | 378 | ||
@@ -428,8 +428,8 @@ function expectBody(expected) { | |||
| 428 | 428 | ||
| 429 | 429 | let expected_body = '123123456123456789123456789ABC123456789ABCDEF'; | |
| 430 | 430 | ||
| 431 | - const onBody = (buf, start, len) => { | ||
| 432 | - const chunk = String(buf.slice(start, start + len)); | ||
| 431 | + const onBody = (buf) => { | ||
| 432 | + const chunk = String(buf); | ||
| 433 | 433 | assert.strictEqual(expected_body.indexOf(chunk), 0); | |
| 434 | 434 | expected_body = expected_body.slice(chunk.length); | |
| 435 | 435 | }; | |
@@ -445,9 +445,7 @@ function expectBody(expected) { | |||
| 445 | 445 | ||
| 446 | 446 | for (let i = 1; i < request.length - 1; ++i) { | |
| 447 | 447 | const a = request.slice(0, i); | |
| 448 | - console.error(`request.slice(0, ${i}) = ${JSON.stringify(a.toString())}`); | ||
| 449 | 448 | const b = request.slice(i); | |
| 450 | - console.error(`request.slice(${i}) = ${JSON.stringify(b.toString())}`); | ||
| 451 | 449 | test(a, b); | |
| 452 | 450 | } | |
| 453 | 451 | } | |
@@ -488,8 +486,8 @@ function expectBody(expected) { | |||
| 488 | 486 | ||
| 489 | 487 | let expected_body = '123123456123456789123456789ABC123456789ABCDEF'; | |
| 490 | 488 | ||
| 491 | - const onBody = (buf, start, len) => { | ||
| 492 | - const chunk = String(buf.slice(start, start + len)); | ||
| 489 | + const onBody = (buf) => { | ||
| 490 | + const chunk = String(buf); | ||
| 493 | 491 | assert.strictEqual(expected_body.indexOf(chunk), 0); | |
| 494 | 492 | expected_body = expected_body.slice(chunk.length); | |
| 495 | 493 | }; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments