-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Support http2. #1399
Conversation
Thank you! That's a very useful functionality. |
Does |
lib/node/http2wrapper.js
Outdated
this._headers = {}; | ||
|
||
const session = http2.connect(`${protocol}://${host}:${port}`, sessionOptions); | ||
this.setHeader('host', `${host}:${port}`) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 👍 .
There was a problem hiding this comment.
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.
lib/node/http2wrapper.js
Outdated
case 'http': | ||
return net.connect(options.socketPath); | ||
case 'https': | ||
options.ALPNProtocols = ['h2']; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
lib/node/http2wrapper.js
Outdated
switch (key) { | ||
case HTTP2_HEADER_HOST: | ||
key = HTTP2_HEADER_AUTHORITY; | ||
value = value.startsWith('http') ? parse(value).host : value; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
test/node/pipe.js
Outdated
@@ -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'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this needed?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
test/node/unix-sockets.js
Outdated
@@ -94,6 +105,7 @@ describe("[unix-sockets] https", () => { | |||
.get(`${base}/request/path`) | |||
.ca(cert) | |||
.end((err, res) => { | |||
console.log(err); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👀
There was a problem hiding this comment.
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 👍
test/node/user-agent.js
Outdated
@@ -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 => { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
test/support/server.js
Outdated
if(process.env.EXPOSE_HTTP2){ | ||
Object.keys(headers).forEach(function(name) { | ||
if(isPseudoHeader(name)){ | ||
delete headers[name]; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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.
|
@focusaurus do you have a preference how HTTP/1 vs 2 API should look like? |
Hmm. Am I interpreting the discussion correctly? So if a user sets |
@focusaurus Thank you for feedback!.
It is reasonable. I change this soon. 👍 |
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 => {
}); |
ping @kornelski @focusaurus |
Thanks |
Awesome contribution @sogaani ! |
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.