Skip to content

Downgrade HTTP/2 to HTTP1 over HTTP(non-TLS) #26795

@nwtgck

Description

@nwtgck

Hi, developers. Thank you very much for your wonderful project! I've been using Node.js a lot.

I have a question about HTTP/2 support. Does http2.createServer() allow downgrade HTTP/2 to HTTP/1 when clients access by HTTP/1 with non-TLS? ({allowHTTP1: true} is specified)

Here is a simple script to test my question above.

  • Server: HTTP/2 (http2.createServer({allowHTTP1: true}))
  • Client1: HTTP/2 GET
  • Client2: HTTP/1 GET
// [This is a portable .js file containing server and client.]

const http2 = require("http2");
const http = require("http");

const port = 3000;
const options = {
  // HTTP/2 will be downgraded to HTTP/1.x 
  allowHTTP1: true,
};
const server = http2.createServer(options);

server.on('stream', (stream, headers) => {
  stream.respond({
    'content-type': 'text/plain',
    ':status': 200
  });
  stream.end('hello, world from server!');
});

server.listen(port, ()=>{
  // server is ready
  console.log(`Server listening on ${port}!`);

  // Choose true/false!
  const useHTTP2 = true;

  if (useHTTP2) {
    // HTTP/2 GET
    http2.connect(`http://localhost:${port}`)
      .request({":path": "/"})
      // Print body
      .pipe(process.stdout)
  } else {
    // HTTP/1 GET
    http.get(`http://localhost:${port}`, (res)=>{
      // Print body
      res.pipe(process.stdout);
    });
  }
});

HTTP/2 client is OK

When const useHTTP2 = true in the code above, the output is as follows without error.

Server listening on 3000!
hello, world from server!

HTTP/1 client has error

When const useHTTP2 = false, the output has error as follows.

Server listening on 3000!
events.js:167
      throw er; // Unhandled 'error' event
      ^

Error: Parse Error
    at Socket.socketOnData (_http_client.js:442:20)
    at Socket.emit (events.js:182:13)
    at addChunk (_stream_readable.js:283:12)
    at readableAddChunk (_stream_readable.js:264:11)
    at Socket.Readable.push (_stream_readable.js:219:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:94:17)
Emitted 'error' event at:
    at Socket.socketOnData (_http_client.js:448:9)
    at Socket.emit (events.js:182:13)
    [... lines matching original stack trace ...]
    at TCP.onStreamRead (internal/stream_base_commons.js:94:17)

Could you tell me whether HTTP/1 client is allowed?

node version

Here is my node version.

$ node -v
v10.15.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.http2Issues or PRs related to the http2 subsystem.questionIssues that look for answers.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions