| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0b43c08 commit 32ac376
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -466,6 +466,7 @@ function tickOnSocket(req, socket) { | |||
| 466 | 466 | parser.reinitialize(HTTPParser.RESPONSE); | |
| 467 | 467 | parser.socket = socket; | |
| 468 | 468 | parser.incoming = null; | |
| 469 | + parser.outgoing = req; | ||
| 469 | 470 | req.parser = parser; | |
| 470 | 471 | ||
| 471 | 472 | socket.parser = parser; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -77,6 +77,20 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method, | |||
| 77 | 77 | parser.incoming.statusMessage = statusMessage; | |
| 78 | 78 | } | |
| 79 | 79 | ||
| 80 | + // The client made non-upgrade request, and server is just advertising | ||
| 81 | + // supported protocols. | ||
| 82 | + // | ||
| 83 | + // See RFC7230 Section 6.7 | ||
| 84 | + // | ||
| 85 | + // NOTE: RegExp below matches `upgrade` in `Connection: abc, upgrade, def` | ||
| 86 | + // header. | ||
| 87 | + if (upgrade && | ||
| 88 | + parser.outgoing !== null && | ||
| 89 | + (parser.outgoing._headers.upgrade === undefined || | ||
| 90 | + !/(^|\W)upgrade(\W|$)/i.test(parser.outgoing._headers.connection))) { | ||
| 91 | + upgrade = false; | ||
| 92 | + } | ||
| 93 | + | ||
| 80 | 94 | parser.incoming.upgrade = upgrade; | |
| 81 | 95 | ||
| 82 | 96 | var skipBody = false; // response to HEAD or CONNECT | |
@@ -142,6 +156,10 @@ var parsers = new FreeList('parsers', 1000, function() { | |||
| 142 | 156 | parser._url = ''; | |
| 143 | 157 | parser._consumed = false; | |
| 144 | 158 | ||
| 159 | + parser.socket = null; | ||
| 160 | + parser.incoming = null; | ||
| 161 | + parser.outgoing = null; | ||
| 162 | + | ||
| 145 | 163 | // Only called in the slow case where slow means | |
| 146 | 164 | // that the request headers were either fragmented | |
| 147 | 165 | // across multiple TCP packets or too large to be | |
@@ -175,6 +193,7 @@ function freeParser(parser, req, socket) { | |||
| 175 | 193 | parser.socket.parser = null; | |
| 176 | 194 | parser.socket = null; | |
| 177 | 195 | parser.incoming = null; | |
| 196 | + parser.outgoing = null; | ||
| 178 | 197 | if (parsers.free(parser) === false) | |
| 179 | 198 | parser.close(); | |
| 180 | 199 | parser = null; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,54 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const http = require('http'); | ||
| 6 | + | ||
| 7 | + const tests = [ | ||
| 8 | + { headers: {}, expected: 'regular' }, | ||
| 9 | + { headers: { upgrade: 'h2c' }, expected: 'regular' }, | ||
| 10 | + { headers: { connection: 'upgrade' }, expected: 'regular' }, | ||
| 11 | + { headers: { connection: 'upgrade', upgrade: 'h2c' }, expected: 'upgrade' } | ||
| 12 | + ]; | ||
| 13 | + | ||
| 14 | + function fire() { | ||
| 15 | + if (tests.length === 0) | ||
| 16 | + return server.close(); | ||
| 17 | + | ||
| 18 | + const test = tests.shift(); | ||
| 19 | + | ||
| 20 | + const done = common.mustCall(function done(result) { | ||
| 21 | + assert.equal(result, test.expected); | ||
| 22 | + | ||
| 23 | + fire(); | ||
| 24 | + }); | ||
| 25 | + | ||
| 26 | + const req = http.request({ | ||
| 27 | + port: common.PORT, | ||
| 28 | + path: '/', | ||
| 29 | + headers: test.headers | ||
| 30 | + }, function onResponse(res) { | ||
| 31 | + res.resume(); | ||
| 32 | + done('regular'); | ||
| 33 | + }); | ||
| 34 | + | ||
| 35 | + req.on('upgrade', function onUpgrade(res, socket) { | ||
| 36 | + socket.destroy(); | ||
| 37 | + done('upgrade'); | ||
| 38 | + }); | ||
| 39 | + | ||
| 40 | + req.end(); | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + const server = http.createServer(function(req, res) { | ||
| 44 | + res.writeHead(200, { | ||
| 45 | + Connection: 'upgrade, keep-alive', | ||
| 46 | + Upgrade: 'h2c' | ||
| 47 | + }); | ||
| 48 | + res.end('hello world'); | ||
| 49 | + }).on('upgrade', function(req, socket) { | ||
| 50 | + socket.end('HTTP/1.1 101 Switching protocols\r\n' + | ||
| 51 | + 'Connection: upgrade\r\n' + | ||
| 52 | + 'Upgrade: h2c\r\n\r\n' + | ||
| 53 | + 'ohai'); | ||
| 54 | + }).listen(common.PORT, fire); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,6 +36,7 @@ srv.listen(common.PORT, '127.0.0.1', function() { | |||
| 36 | 36 | port: common.PORT, | |
| 37 | 37 | host: '127.0.0.1', | |
| 38 | 38 | headers: { | |
| 39 | + 'connection': 'upgrade', | ||
| 39 | 40 | 'upgrade': 'websocket' | |
| 40 | 41 | } | |
| 41 | 42 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,7 +32,13 @@ var gotUpgrade = false; | |||
| 32 | 32 | ||
| 33 | 33 | srv.listen(common.PORT, '127.0.0.1', function() { | |
| 34 | 34 | ||
| 35 | - var req = http.get({ port: common.PORT }); | ||
| 35 | + var req = http.get({ | ||
| 36 | + port: common.PORT, | ||
| 37 | + headers: { | ||
| 38 | + connection: 'upgrade', | ||
| 39 | + upgrade: 'websocket' | ||
| 40 | + } | ||
| 41 | + }); | ||
| 36 | 42 | req.on('upgrade', function(res, socket, upgradeHead) { | |
| 37 | 43 | // XXX: This test isn't fantastic, as it assumes that the entire response | |
| 38 | 44 | // from the server will arrive in a single data callback | |
| Back | FazBrowse Home | New Git URL |
0 commit comments