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

Support http2. #1399

Merged
merged 7 commits into from
Aug 3, 2018
Merged

Support http2. #1399

merged 7 commits into from
Aug 3, 2018

Conversation

sogaani
Copy link
Contributor

@sogaani sogaani commented Jul 31, 2018

Hi.

This PR enable http/2, if EXPOSE_HTTP2 env is true.

This PR still not support ALPN(as @kornelski mentioned in #980).
I'd prefer to add ALPN functionality after merging this PR and express http/2 PR.

@kornelski
Copy link
Contributor

Thank you! That's a very useful functionality.

@kornelski
Copy link
Contributor

Does EXPOSE_HTTP2 affect ability to make HTTP/1 requests? Is Node going to fall back automatically? I wonder if we need to give more runtime control over this.

this._headers = {};

const session = http2.connect(`${protocol}://${host}:${port}`, sessionOptions);
this.setHeader('host', `${host}:${port}`)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it necessary here? In normal implementation we allow options.host to be different than host header, because it's useful to do request.get('http://[ip address]').host('example.com') to force request to connect to a particular IP address.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think current implementation supports request.get('http://[ip address]').host('example.com'). I will check later 👍 .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Still not send header here and we can change Host header by set method. I add test for this.

case 'http':
return net.connect(options.socketPath);
case 'https':
options.ALPNProtocols = ['h2'];
Copy link
Contributor

Choose a reason for hiding this comment

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

AFAIK browsers also send http/1 here. Do we support a fallback?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Current implement does not support fall back. I can implement fallback, I plan to take fallback after this PR. Or Should I integrate with this PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. If there's no fallback, then that's OK.

switch (key) {
case HTTP2_HEADER_HOST:
key = HTTP2_HEADER_AUTHORITY;
value = value.startsWith('http') ? parse(value).host : value;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is such conditional parsing needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is add for compatibility with Http/1. HTTP2_HEADER_AUTHORITY(alternative Host in http/2) only support domain string, otherwise it raise error. However, I met Host header with schema(like Host: http://example.com) in some express tests.

Copy link
Contributor

Choose a reason for hiding this comment

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

So it would be better to improve the check, otherwise host like httparchive.org would be misparsed :)

/^https?:\/\//.test(value)

@@ -12,13 +17,19 @@ app.get("/", (req, res) => {
});

app.post("/", (req, res) => {
res.send(req.body);
if (process.env.EXPOSE_HTTP2){
res.set('content-type', 'application/json');
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

http/2 module have not full compatibility with http/1 module. http/2 imcommingHttpRequest does not have body property.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Above was my misunderstanding.
Http2 request does not append content-length header if use writableStream. And body-perser does not parse body without content-length. res.send method exported by express automatically add content-type: **json header with object input. However I cannot got body object by body-parser. Therefor I added content-type manually.

@@ -94,6 +105,7 @@ describe("[unix-sockets] https", () => {
.get(`${base}/request/path`)
.ca(cert)
.end((err, res) => {
console.log(err);
Copy link
Contributor

Choose a reason for hiding this comment

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

👀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My misstake! I will fix 👍

@@ -6,7 +6,7 @@ const base = setup.uri;

describe("req.get()", () => {
it("should set a default user-agent", () =>
request.get(`${base}/ua`).then(res => {
request.put(`${base}/ua`).then(res => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

http/2 client module cannot send body with head or get Method. But, it seems to not need here. I check this again later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I test locally and noticed that this change is not needed. I will revert this change.

if(process.env.EXPOSE_HTTP2){
Object.keys(headers).forEach(function(name) {
if(isPseudoHeader(name)){
delete headers[name];
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this should translate the header to HTTP/1 header, or allow both in the test using it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

HTTP/2 use some headers to control protocol(e.g. :method, :authority, :path ...) and it can not set by user. I just delete those headers here.

@sogaani
Copy link
Contributor Author

sogaani commented Jul 31, 2018

Does EXPOSE_HTTP2 affect ability to make HTTP/1 requests? Is Node going to fall back automatically? I wonder if we need to give more runtime control over this.

Yes. We cannot use HTTP/1 requests, if EXPOSE_HTTP2 is true. Node runtime does not support fall back. Control by env is good for that existing http/1 tests divert to http/2 tests like this PR.
Other runtime control like following might be useful.

request.get('/').http2().end()

@kornelski
Copy link
Contributor

kornelski commented Jul 31, 2018

@focusaurus do you have a preference how HTTP/1 vs 2 API should look like?

@focusaurus
Copy link
Contributor

Hmm. Am I interpreting the discussion correctly? So if a user sets EXPOSE_HTTP2=true they can only connect to http2 servers from that entire node process and connections to http/1 servers will fail? That sounds untenable. I think I'd prefer leaving HTTP/1 as the default and exposing an API to use http/2 on a per-request basis. For that I think the .http2() method name is a good API.

@sogaani
Copy link
Contributor Author

sogaani commented Jul 31, 2018

@focusaurus Thank you for feedback!.

I think I'd prefer leaving HTTP/1 as the default and exposing an API to use http/2 on a per-request basis. For that I think the .http2() method name is a good API.

It is reasonable. I change this soon. 👍

@sogaani
Copy link
Contributor Author

sogaani commented Aug 1, 2018

I addressed review and change the API to exposing an HTTP/2 API.

Superagent can be enabled http2 by http2 field:

    const request = require('superagent');
    request.http2 =true;
    request
      .get('http://example.com/search')
      .then(res => {

      });

A request can be enabled http2 by http2 method:

    const request = require('superagent');
    request
      .get('http://example.com/search')
      .http2()
      .then(res => {

      });

@sogaani
Copy link
Contributor Author

sogaani commented Aug 3, 2018

ping @kornelski @focusaurus

@kornelski kornelski merged commit 70bc5ed into ladjs:master Aug 3, 2018
@kornelski
Copy link
Contributor

Thanks

@focusaurus
Copy link
Contributor

Awesome contribution @sogaani !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants