Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 var http2 = require('../node-http2');
6 var fs = require('fs');
7 var url = require('url');
8 var crypto = require('crypto');
10 // Hook into the decompression code to log the decompressed name-value pairs
11 var http2_compression = require('../node-http2/node_modules/http2-protocol/lib/compressor');
12 var HeaderSetDecompressor = http2_compression.HeaderSetDecompressor;
13 var originalRead = HeaderSetDecompressor.prototype.read;
14 var lastDecompressor;
15 var decompressedPairs;
16 HeaderSetDecompressor.prototype.read = function() {
17 if (this != lastDecompressor) {
18 lastDecompressor = this;
19 decompressedPairs = [];
20 }
21 var pair = originalRead.apply(this, arguments);
22 if (pair) {
23 decompressedPairs.push(pair);
24 }
25 return pair;
26 }
28 function getHttpContent(path) {
29 var content = '<!doctype html>' +
30 '<html>' +
31 '<head><title>HOORAY!</title></head>' +
32 '<body>You Win! (by requesting' + path + ')</body>' +
33 '</html>';
34 return content;
35 }
37 function generateContent(size) {
38 var content = '';
39 for (var i = 0; i < size; i++) {
40 content += '0';
41 }
42 return content;
43 }
45 /* This takes care of responding to the multiplexed request for us */
46 var m = {
47 mp1res: null,
48 mp2res: null,
49 buf: null,
50 mp1start: 0,
51 mp2start: 0,
53 checkReady: function() {
54 if (this.mp1res != null && this.mp2res != null) {
55 this.buf = generateContent(30*1024);
56 this.mp1start = 0;
57 this.mp2start = 0;
58 this.send(this.mp1res, 0);
59 setTimeout(this.send.bind(this, this.mp2res, 0), 5);
60 }
61 },
63 send: function(res, start) {
64 var end = Math.min(start + 1024, this.buf.length);
65 var content = this.buf.substring(start, end);
66 res.write(content);
67 if (end < this.buf.length) {
68 setTimeout(this.send.bind(this, res, end), 10);
69 } else {
70 res.end();
71 }
72 }
73 };
75 function handleRequest(req, res) {
76 var u = url.parse(req.url);
77 var content = getHttpContent(u.pathname);
78 var push;
80 if (req.httpVersionMajor === 2) {
81 res.setHeader('X-Connection-Http2', 'yes');
82 res.setHeader('X-Http2-StreamId', '' + req.stream.id);
83 } else {
84 res.setHeader('X-Connection-Http2', 'no');
85 }
87 if (u.pathname === '/exit') {
88 res.setHeader('Content-Type', 'text/plain');
89 res.writeHead(200);
90 res.end('ok');
91 process.exit();
92 }
94 else if ((u.pathname === '/multiplex1') && (req.httpVersionMajor === 2)) {
95 res.setHeader('Content-Type', 'text/plain');
96 res.writeHead(200);
97 m.mp1res = res;
98 m.checkReady();
99 return;
100 }
102 else if ((u.pathname === '/multiplex2') && (req.httpVersionMajor === 2)) {
103 res.setHeader('Content-Type', 'text/plain');
104 res.writeHead(200);
105 m.mp2res = res;
106 m.checkReady();
107 return;
108 }
110 else if (u.pathname === "/header") {
111 var val = req.headers["x-test-header"];
112 if (val) {
113 res.setHeader("X-Received-Test-Header", val);
114 }
115 }
117 else if (u.pathname === "/cookie_crumbling") {
118 res.setHeader("X-Received-Header-Pairs", JSON.stringify(decompressedPairs));
119 }
121 else if (u.pathname === "/push") {
122 push = res.push('/push.js');
123 push.writeHead(200, {
124 'content-type': 'application/javascript',
125 'pushed' : 'yes',
126 'content-length' : 11,
127 'X-Connection-Http2': 'yes'
128 });
129 push.end('// comments');
130 content = '<head> <script src="push.js"/></head>body text';
131 }
133 else if (u.pathname === "/push2") {
134 push = res.push('/push2.js');
135 push.writeHead(200, {
136 'content-type': 'application/javascript',
137 'pushed' : 'yes',
138 // no content-length
139 'X-Connection-Http2': 'yes'
140 });
141 push.end('// comments');
142 content = '<head> <script src="push2.js"/></head>body text';
143 }
145 else if (u.pathname === "/big") {
146 content = generateContent(128 * 1024);
147 var hash = crypto.createHash('md5');
148 hash.update(content);
149 var md5 = hash.digest('hex');
150 res.setHeader("X-Expected-MD5", md5);
151 }
153 else if (u.pathname === "/post") {
154 if (req.method != "POST") {
155 res.writeHead(405);
156 res.end('Unexpected method: ' + req.method);
157 return;
158 }
160 var post_hash = crypto.createHash('md5');
161 req.on('data', function receivePostData(chunk) {
162 post_hash.update(chunk.toString());
163 });
164 req.on('end', function finishPost() {
165 var md5 = post_hash.digest('hex');
166 res.setHeader('X-Calculated-MD5', md5);
167 res.writeHead(200);
168 res.end(content);
169 });
171 return;
172 }
174 res.setHeader('Content-Type', 'text/html');
175 res.writeHead(200);
176 res.end(content);
177 }
179 // Set up the SSL certs for our server
180 var options = {
181 key: fs.readFileSync(__dirname + '/../moz-spdy/spdy-key.pem'),
182 cert: fs.readFileSync(__dirname + '/../moz-spdy/spdy-cert.pem'),
183 ca: fs.readFileSync(__dirname + '/../moz-spdy/spdy-ca.pem'),
184 //, log: require('../node-http2/test/util').createLogger('server')
185 };
187 var server = http2.createServer(options, handleRequest);
188 server.on('connection', function(socket) {
189 socket.on('error', function() {
190 // Ignoring SSL socket errors, since they usually represent a connection that was tore down
191 // by the browser because of an untrusted certificate. And this happens at least once, when
192 // the first test case if done.
193 });
194 });
195 server.listen(6944);
196 console.log('HTTP2 server listening on port 6944');