source: trip-planner-front/node_modules/needle/test/utils/proxy.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.6 KB
RevLine 
[6a3a178]1var http = require('http'),
2 https = require('https'),
3 url = require('url');
4
5var port = 1234,
6 log = true,
7 request_auth = false;
8
9http.createServer(function(request, response) {
10
11 console.log(request.headers);
12 console.log("Got request: " + request.url);
13 console.log("Forwarding request to " + request.headers['host']);
14
15 if (request_auth) {
16 if (!request.headers['proxy-authorization']) {
17 response.writeHead(407, {'Proxy-Authenticate': 'Basic realm="proxy.com"'})
18 return response.end('Hello.');
19 }
20 }
21
22 var remote = url.parse(request.url);
23 var protocol = remote.protocol == 'https:' ? https : http;
24
25 var opts = {
26 host: request.headers['host'],
27 port: remote.port || (remote.protocol == 'https:' ? 443 : 80),
28 method: request.method,
29 path: remote.pathname,
30 headers: request.headers
31 }
32
33 var proxy_request = protocol.request(opts, function(proxy_response){
34
35 proxy_response.on('data', function(chunk) {
36 if (log) console.log(chunk.toString());
37 response.write(chunk, 'binary');
38 });
39 proxy_response.on('end', function() {
40 response.end();
41 });
42
43 response.writeHead(proxy_response.statusCode, proxy_response.headers);
44 });
45
46 request.on('data', function(chunk) {
47 if (log) console.log(chunk.toString());
48 proxy_request.write(chunk, 'binary');
49 });
50
51 request.on('end', function() {
52 proxy_request.end();
53 });
54
55}).listen(port);
56
57process.on('uncaughtException', function(err){
58 console.log('Uncaught exception!');
59 console.log(err);
60});
61
62console.log("Proxy server listening on port " + port);
Note: See TracBrowser for help on using the repository browser.