Skip to content
This repository has been archived by the owner on Jul 6, 2018. It is now read-only.

http2: request.socket|connection #130

Closed

Conversation

sebdeckers
Copy link
Contributor

@sebdeckers sebdeckers commented May 17, 2017

As discussed in: #125 (comment)

Adds request.socket and request.connection for compatibility with documented HTTP APIs:

Use case: I found that popular packages like cookies (NPM: >500k/mo) make use of this to detect the TLS context.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

http2

mcollina

This comment was marked as off-topic.

@sebdeckers
Copy link
Contributor Author

@mcollina Do you mean just replicate that logic in our test, or to actually pull in the cookies package from NPM and use it in a test? (If the latter, what is the recommended way to add 3rd party packages to the node repo?)

@mcollina
Copy link
Member

We need a line that just does this: https://github.com/pillarjs/cookies/blob/master/index.js#L87

@sebdeckers
Copy link
Contributor Author

Okay, I'm just doing a look around for other examples.

finalhandler (NPM: 14M/mo)

req.socket.destroy()

https://github.com/pillarjs/finalhandler/blob/master/index.js#L130

express (NPM: ∞/mo)

req.connection.encrypted = true;

https://github.com/expressjs/express/blob/master/test/req.protocol.js#L41

var proto = this.connection.encrypted
  ? 'https'
  : 'http';
var trust = this.app.get('trust proxy fn');

if (!trust(this.connection.remoteAddress, 0)) {
  return proto;
}

https://github.com/expressjs/express/blob/master/lib/request.js#L307-L314

@mcollina
Copy link
Member

https://github.com/pillarjs/finalhandler/blob/master/index.js#L127-L132 cannot be supported as HTTP1 specific. In that specific case for HTTP1, we would have to destroy the stream, not the connection.

finalhandler also uses res._header  which was not documented in core. The one we should support is https://nodejs.org/api/http.html#http_response_headerssent. I will send a PR to finalhandler.

@sebdeckers
Copy link
Contributor Author

hapi

    // Setup timeout

    if (this.raw.req.socket &&
        this.route.settings.timeout.socket !== undefined) {

        this.raw.req.socket.setTimeout(this.route.settings.timeout.socket || 0);    // Value can be false or positive
    }

https://github.com/hapijs/hapi/blob/master/lib/request.js#L344-L350

@mcollina
Copy link
Member

I'm convinced. We need this :).

In the test, can you check that whatever socket  returns is correct? Just asserting its truthness is not enough.

@sebdeckers
Copy link
Contributor Author

Added a simple typeof check which should cover all the public interfaces in use today.

Should I add a test case for HTTPS too? There's not really a difference in core/compat (both are just Http2Session). And tls.TLSSocket is a subclass of net.Socket anyway.

@mcollina
Copy link
Member

that's fine as it is

mcollina

This comment was marked as off-topic.

cjihrig

This comment was marked as off-topic.

jasnell

This comment was marked as off-topic.

@jasnell
Copy link
Member

jasnell commented May 17, 2017

Regarding the hapi setTimeout example... I'm curious why it is necessary actually. In the http/2 case, the Http2Session instance itself has a setTimeout() and there is a 1-to-1 relationship between the Socket and the Session. Setting the timeout on the Http2Session should have the same basic effect, then. In fact, it may be better to accept have setTimeout() on Http2Session defer to the setTimeout() on the socket rather than it being a separate implementation.

@sebdeckers sebdeckers force-pushed the compat-request-socket-connection branch from dd06fb5 to 9f3e375 Compare May 17, 2017 16:08
@sebdeckers
Copy link
Contributor Author

@jasnell I suspect it stems from wanting to set different timeouts for specific Hapi routes. See: hapijs/hapi#2195 It would only be possible to apply the timeout once the request has been parsed, not at the initial socket connection listener. Just a guess.

@jasnell
Copy link
Member

jasnell commented May 17, 2017

Yeah, I get that, but given the multiplexing model, and the fact that individual Http2Stream objects have their own timeouts that are independent of the Http2Session and Socket, does that existing model still make sense here?

jasnell

This comment was marked as off-topic.

@sebdeckers sebdeckers mentioned this pull request May 18, 2017
4 tasks
mcollina pushed a commit that referenced this pull request May 18, 2017
Support the socket/connection getter like require('http') does.

PR-URL: #130
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
@mcollina
Copy link
Member

Landed as 3904d7c

@mcollina mcollina closed this May 18, 2017
jasnell pushed a commit that referenced this pull request May 19, 2017
Support the socket/connection getter like require('http') does.

PR-URL: #130
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
jasnell pushed a commit that referenced this pull request May 31, 2017
Support the socket/connection getter like require('http') does.

PR-URL: #130
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
@sebdeckers sebdeckers deleted the compat-request-socket-connection branch June 20, 2017 00:49
jasnell pushed a commit to jasnell/http2-1 that referenced this pull request Jun 22, 2017
Support the socket/connection getter like require('http') does.

PR-URL: nodejs#130
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
jasnell pushed a commit to jasnell/http2-1 that referenced this pull request Jul 10, 2017
Support the socket/connection getter like require('http') does.

PR-URL: nodejs#130
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
jasnell pushed a commit to jasnell/http2-1 that referenced this pull request Jul 14, 2017
Support the socket/connection getter like require('http') does.

PR-URL: nodejs#130
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants