Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: tell the parser about CONNECT responses #6198

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ function socketOnData(d) {
// client
function parserOnIncomingClient(res, shouldKeepAlive) {
var socket = this.socket;
var parser = socket.parser;
var req = socket._httpMessage;


Expand All @@ -432,6 +433,7 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
// Responses to CONNECT request is handled as Upgrade.
if (req.method === 'CONNECT') {
res.upgrade = true;
parser.upgrade = true;
return true; // skip body
}

Expand Down
25 changes: 25 additions & 0 deletions src/node_http_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,26 @@ class Parser : public AsyncWrap {
args.GetReturnValue().Set(ret);
}

static void GetUpgrade(Local<String> property,
const PropertyCallbackInfo<Value>& args) {
Parser* parser = Unwrap<Parser>(args.Holder());

Local<Value> ret = Boolean::New(
parser->env()->isolate(),
parser->parser_.upgrade);

args.GetReturnValue().Set(ret);
}

static void SetUpgrade(Local<String> property,
Local<Value> value,
const PropertyCallbackInfo<void>& args) {
Parser* parser = Unwrap<Parser>(args.Holder());

bool upgrade = value->BooleanValue();
parser->parser_.upgrade = upgrade;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't look correct to me. http_parser.upgrade is only supposed to be read, not written, by clients of libhttp_parser.

}

protected:
class ScopedRetainParser {
public:
Expand Down Expand Up @@ -736,6 +756,7 @@ void InitHttpParser(Local<Object> target,
void* priv) {
Environment* env = Environment::GetCurrent(context);
Local<FunctionTemplate> t = env->NewFunctionTemplate(Parser::New);

t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "HTTPParser"));

Expand Down Expand Up @@ -771,6 +792,10 @@ void InitHttpParser(Local<Object> target,
env->SetProtoMethod(t, "unconsume", Parser::Unconsume);
env->SetProtoMethod(t, "getCurrentBuffer", Parser::GetCurrentBuffer);

Local<v8::ObjectTemplate> o = t->InstanceTemplate();
o->SetAccessor(FIXED_ONE_BYTE_STRING(env->isolate(), "upgrade"),
Parser::GetUpgrade, Parser::SetUpgrade);

target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "HTTPParser"),
t->GetFunction());
}
Expand Down
72 changes: 72 additions & 0 deletions test/parallel/test-http-connect-head.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');

var server = http.createServer(function(req, res) {
assert(false);
});
server.on('connect', common.mustCall(function(req, socket, firstBodyChunk) {
assert.equal(req.method, 'CONNECT');
assert.equal(req.url, 'example.com:443');
console.error('Server got CONNECT request');

// It is legal for the server to send some data intended for the client
// along with the CONNECT response
socket.write(
'HTTP/1.1 200 Connection established\r\n' +
'Date: Tue, 15 Nov 1994 08:12:31 GMT\r\n' +
'\r\n' +
'Head'
);

var data = firstBodyChunk.toString();
socket.on('data', function(buf) {
data += buf.toString();
});
socket.on('end', function() {
socket.end(data);
});
}));
server.listen(common.PORT, common.mustCall(function() {
var req = http.request({
port: common.PORT,
method: 'CONNECT',
path: 'example.com:443'
}, function(res) {
assert(false);
});

req.on('close', common.mustCall(function() { }));

req.on('connect', common.mustCall(function(res, socket, firstBodyChunk) {
console.error('Client got CONNECT request');

// Make sure this request got removed from the pool.
var name = 'localhost:' + common.PORT;
assert(!http.globalAgent.sockets.hasOwnProperty(name));
assert(!http.globalAgent.requests.hasOwnProperty(name));

// Make sure this socket has detached.
assert(!socket.ondata);
assert(!socket.onend);
assert.equal(socket.listeners('connect').length, 0);
assert.equal(socket.listeners('data').length, 0);

var data = firstBodyChunk.toString();

// test that the firstBodyChunk was not parsed as HTTP
assert.equal(data, 'Head');

socket.on('data', function(buf) {
data += buf.toString();
});
socket.on('end', function() {
assert.equal(data, 'HeadRequestEnd');
server.close();
});
socket.end('End');
}));

req.end('Request');
}));