| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent dac8407 commit 0aacd49
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1262,6 +1262,21 @@ class Http2Session extends EventEmitter { | |||
| 1262 | 1262 | this.on('newListener', sessionListenerAdded); | |
| 1263 | 1263 | this.on('removeListener', sessionListenerRemoved); | |
| 1264 | 1264 | ||
| 1265 | + // Process data on the next tick - a remoteSettings handler may be attached. | ||
| 1266 | + // https://github.com/nodejs/node/issues/35981 | ||
| 1267 | + process.nextTick(() => { | ||
| 1268 | + // Socket already has some buffered data - emulate receiving it | ||
| 1269 | + // https://github.com/nodejs/node/issues/35475 | ||
| 1270 | + // https://github.com/nodejs/node/issues/34532 | ||
| 1271 | + if (socket.readableLength) { | ||
| 1272 | + let buf; | ||
| 1273 | + while ((buf = socket.read()) !== null) { | ||
| 1274 | + debugSession(type, `${buf.length} bytes already in buffer`); | ||
| 1275 | + this[kHandle].receive(buf); | ||
| 1276 | + } | ||
| 1277 | + } | ||
| 1278 | + }); | ||
| 1279 | + | ||
| 1265 | 1280 | debugSession(type, 'created'); | |
| 1266 | 1281 | } | |
| 1267 | 1282 | ||
@@ -3276,21 +3291,6 @@ function connect(authority, options, listener) { | |||
| 3276 | 3291 | if (typeof listener === 'function') | |
| 3277 | 3292 | session.once('connect', listener); | |
| 3278 | 3293 | ||
| 3279 | - // Process data on the next tick - a remoteSettings handler may be attached. | ||
| 3280 | - // https://github.com/nodejs/node/issues/35981 | ||
| 3281 | - process.nextTick(() => { | ||
| 3282 | - debug('Http2Session connect', options.createConnection); | ||
| 3283 | - // Socket already has some buffered data - emulate receiving it | ||
| 3284 | - // https://github.com/nodejs/node/issues/35475 | ||
| 3285 | - if (socket && socket.readableLength) { | ||
| 3286 | - let buf; | ||
| 3287 | - while ((buf = socket.read()) !== null) { | ||
| 3288 | - debug(`Http2Session connect: ${buf.length} bytes already in buffer`); | ||
| 3289 | - session[kHandle].receive(buf); | ||
| 3290 | - } | ||
| 3291 | - } | ||
| 3292 | - }); | ||
| 3293 | - | ||
| 3294 | 3294 | return session; | |
| 3295 | 3295 | } | |
| 3296 | 3296 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,72 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + if (!common.hasCrypto) | ||
| 4 | + common.skip('missing crypto'); | ||
| 5 | + | ||
| 6 | + const assert = require('assert'); | ||
| 7 | + const net = require('net'); | ||
| 8 | + const http = require('http'); | ||
| 9 | + const http2 = require('http2'); | ||
| 10 | + | ||
| 11 | + // Example test for HTTP/1 vs HTTP/2 protocol autoselection. | ||
| 12 | + // Refs: https://github.com/nodejs/node/issues/34532 | ||
| 13 | + | ||
| 14 | + const h1Server = http.createServer(common.mustCall((req, res) => { | ||
| 15 | + res.end('HTTP/1 Response'); | ||
| 16 | + })); | ||
| 17 | + | ||
| 18 | + const h2Server = http2.createServer(common.mustCall((req, res) => { | ||
| 19 | + res.end('HTTP/2 Response'); | ||
| 20 | + })); | ||
| 21 | + | ||
| 22 | + const rawServer = net.createServer(common.mustCall(function listener(socket) { | ||
| 23 | + const data = socket.read(3); | ||
| 24 | + | ||
| 25 | + if (!data) { // Repeat until data is available | ||
| 26 | + socket.once('readable', () => listener(socket)); | ||
| 27 | + return; | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + // Put the data back, so the real server can handle it: | ||
| 31 | + socket.unshift(data); | ||
| 32 | + | ||
| 33 | + if (data.toString('ascii') === 'PRI') { // Very dumb preface check | ||
| 34 | + h2Server.emit('connection', socket); | ||
| 35 | + } else { | ||
| 36 | + h1Server.emit('connection', socket); | ||
| 37 | + } | ||
| 38 | + }, 2)); | ||
| 39 | + | ||
| 40 | + rawServer.listen(common.mustCall(() => { | ||
| 41 | + const { port } = rawServer.address(); | ||
| 42 | + | ||
| 43 | + let done = 0; | ||
| 44 | + { | ||
| 45 | + // HTTP/2 Request | ||
| 46 | + const client = http2.connect(`http://localhost:${port}`); | ||
| 47 | + const req = client.request({ ':path': '/' }); | ||
| 48 | + req.end(); | ||
| 49 | + | ||
| 50 | + let content = ''; | ||
| 51 | + req.setEncoding('utf8'); | ||
| 52 | + req.on('data', (chunk) => content += chunk); | ||
| 53 | + req.on('end', common.mustCall(() => { | ||
| 54 | + assert.strictEqual(content, 'HTTP/2 Response'); | ||
| 55 | + if (++done === 2) rawServer.close(); | ||
| 56 | + client.close(); | ||
| 57 | + })); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + { | ||
| 61 | + // HTTP/1 Request | ||
| 62 | + http.get(`http://localhost:${port}`, common.mustCall((res) => { | ||
| 63 | + let content = ''; | ||
| 64 | + res.setEncoding('utf8'); | ||
| 65 | + res.on('data', (chunk) => content += chunk); | ||
| 66 | + res.on('end', common.mustCall(() => { | ||
| 67 | + assert.strictEqual(content, 'HTTP/1 Response'); | ||
| 68 | + if (++done === 2) rawServer.close(); | ||
| 69 | + })); | ||
| 70 | + })); | ||
| 71 | + } | ||
| 72 | + })); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments