Skip to content

Commit 09ab6d2

Browse files
committed
http: simplify method
1 parent 6404c35 commit 09ab6d2

File tree

3 files changed

+26
-35
lines changed

3 files changed

+26
-35
lines changed

lib/_http_common.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,15 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,
117117
return parser.onIncoming(incoming, shouldKeepAlive);
118118
}
119119

120-
function parserOnBody(b, start, len) {
120+
function parserOnBody(b) {
121121
const stream = this.incoming;
122122

123123
// If the stream has already been removed, then drop it.
124124
if (stream === null)
125125
return;
126126

127127
// Pretend this was the result of a stream._read call.
128-
if (len > 0 && !stream._dumped) {
128+
if (!stream._dumped) {
129129
const ret = stream.push(b);
130130
if (!ret)
131131
readStop(this.socket);

src/node_http_parser.cc

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -450,27 +450,20 @@ class Parser : public AsyncWrap, public StreamListener {
450450

451451

452452
int on_body(const char* at, size_t length) {
453-
EscapableHandleScope scope(env()->isolate());
453+
if (length == 0)
454+
return 0;
454455

455-
Local<Object> obj = object();
456-
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();
457460

458461
if (!cb->IsFunction())
459462
return 0;
460463

461-
// Make sure Buffer will be in parent HandleScope
462-
Local<Object> current_buffer = scope.Escape(
463-
Buffer::Copy(env()->isolate(), at, length).ToLocalChecked());
464+
Local<Value> buffer = Buffer::Copy(env, at, length).ToLocalChecked();
464465

465-
Local<Value> argv[3] = {
466-
current_buffer,
467-
Integer::NewFromUnsigned(
468-
env()->isolate(), 0),
469-
Integer::NewFromUnsigned(env()->isolate(), length)};
470-
471-
MaybeLocal<Value> r = MakeCallback(cb.As<Function>(),
472-
arraysize(argv),
473-
argv);
466+
MaybeLocal<Value> r = MakeCallback(cb.As<Function>(), 1, &buffer);
474467

475468
if (r.IsEmpty()) {
476469
got_exception_ = true;

test/parallel/test-http-parser.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ function newParser(type) {
6262

6363

6464
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);
6767
assert.strictEqual(body, expected);
6868
});
6969
}
@@ -126,8 +126,8 @@ function expectBody(expected) {
126126
assert.strictEqual(statusMessage, 'OK');
127127
};
128128

129-
const onBody = (buf, start, len) => {
130-
const body = String(buf.slice(start, start + len));
129+
const onBody = (buf) => {
130+
const body = String(buf);
131131
assert.strictEqual(body, 'pong');
132132
};
133133

@@ -195,8 +195,8 @@ function expectBody(expected) {
195195
parser[kOnHeaders] = mustCall(onHeaders);
196196
};
197197

198-
const onBody = (buf, start, len) => {
199-
const body = String(buf.slice(start, start + len));
198+
const onBody = (buf) => {
199+
const body = String(buf);
200200
assert.strictEqual(body, 'ping');
201201
seen_body = true;
202202
};
@@ -291,8 +291,8 @@ function expectBody(expected) {
291291
assert.strictEqual(versionMinor, 1);
292292
};
293293

294-
const onBody = (buf, start, len) => {
295-
const body = String(buf.slice(start, start + len));
294+
const onBody = (buf) => {
295+
const body = String(buf);
296296
assert.strictEqual(body, 'foo=42&bar=1337');
297297
};
298298

@@ -332,8 +332,8 @@ function expectBody(expected) {
332332
let body_part = 0;
333333
const body_parts = ['123', '123456', '1234567890'];
334334

335-
const onBody = (buf, start, len) => {
336-
const body = String(buf.slice(start, start + len));
335+
const onBody = (buf) => {
336+
const body = String(buf);
337337
assert.strictEqual(body, body_parts[body_part++]);
338338
};
339339

@@ -371,8 +371,8 @@ function expectBody(expected) {
371371
const body_parts =
372372
['123', '123456', '123456789', '123456789ABC', '123456789ABCDEF'];
373373

374-
const onBody = (buf, start, len) => {
375-
const body = String(buf.slice(start, start + len));
374+
const onBody = (buf) => {
375+
const body = String(buf);
376376
assert.strictEqual(body, body_parts[body_part++]);
377377
};
378378

@@ -428,8 +428,8 @@ function expectBody(expected) {
428428

429429
let expected_body = '123123456123456789123456789ABC123456789ABCDEF';
430430

431-
const onBody = (buf, start, len) => {
432-
const chunk = String(buf.slice(start, start + len));
431+
const onBody = (buf) => {
432+
const chunk = String(buf);
433433
assert.strictEqual(expected_body.indexOf(chunk), 0);
434434
expected_body = expected_body.slice(chunk.length);
435435
};
@@ -445,9 +445,7 @@ function expectBody(expected) {
445445

446446
for (let i = 1; i < request.length - 1; ++i) {
447447
const a = request.slice(0, i);
448-
console.error(`request.slice(0, ${i}) = ${JSON.stringify(a.toString())}`);
449448
const b = request.slice(i);
450-
console.error(`request.slice(${i}) = ${JSON.stringify(b.toString())}`);
451449
test(a, b);
452450
}
453451
}
@@ -488,8 +486,8 @@ function expectBody(expected) {
488486

489487
let expected_body = '123123456123456789123456789ABC123456789ABCDEF';
490488

491-
const onBody = (buf, start, len) => {
492-
const chunk = String(buf.slice(start, start + len));
489+
const onBody = (buf) => {
490+
const chunk = String(buf);
493491
assert.strictEqual(expected_body.indexOf(chunk), 0);
494492
expected_body = expected_body.slice(chunk.length);
495493
};

0 commit comments

Comments
 (0)