Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / http-proxy / bin / node-http-proxy
1 #!/usr/bin/env node
2
3 var path = require('path'),
4     fs = require('fs'),
5     util  = require('util'),
6     argv = require('optimist').argv,
7     httpProxy = require('../lib/node-http-proxy');
8
9 var help = [
10     "usage: node-http-proxy [options] ",
11     "",
12     "Starts a node-http-proxy server using the specified command-line options",
13     "",
14     "options:",
15     "  --port   PORT       Port that the proxy server should run on",
16     "  --host   HOST       Host that the proxy server should run on",
17     "  --target HOST:PORT  Location of the server the proxy will target",
18     "  --config OUTFILE    Location of the configuration file for the proxy server",
19     "  --silent            Silence the log output from the proxy server",
20     "  --user   USER       User to drop privileges to once server socket is bound",
21     "  -h, --help          You're staring at it"
22 ].join('\n');
23
24 if (argv.h || argv.help || Object.keys(argv).length === 2) {
25   return util.puts(help);
26 }
27
28 var location, config = {},
29     port = argv.port || 80, 
30     host = argv.host || undefined, 
31     target = argv.target;
32     user = argv.user;
33
34 //
35 // If we were passed a config, parse it
36 //
37 if (argv.config) {
38   try {
39     var data = fs.readFileSync(argv.config);
40     config = JSON.parse(data.toString());
41   } catch (ex) {
42     util.puts('Error starting node-http-proxy: ' + ex);
43     process.exit(1);
44   }
45 }
46
47 //
48 // If `config.https` is set, then load the required file contents into the config options.
49 //
50 if (config.https) {
51   Object.keys(config.https).forEach(function (key) {
52     // If CA certs are specified, load those too.
53     if (key === "ca") {
54       for (var i=0; i < config.https.ca.length; i++) {
55         if (config.https.ca === undefined) {
56           config.https.ca = [];
57         }
58         config.https.ca[i] = fs.readFileSync(config.https[key][i], 'utf8');
59       }
60     } else {
61       config.https[key] = fs.readFileSync(config.https[key], 'utf8');
62     }
63   });
64 }
65
66 //
67 // Check to see if we should silence the logs
68 //
69 config.silent = typeof argv.silent !== 'undefined' ? argv.silent : config.silent;
70
71 //
72 // If we were passed a target, parse the url string
73 //
74 if (typeof target === 'string') location = target.split(':');
75
76 //
77 // Create the server with the specified options
78 //
79 var server;
80 if (location) {
81   var targetPort = location.length === 1 ? 80 : parseInt(location[1]);
82   server = httpProxy.createServer(targetPort, location[0], config);
83 }
84 else if (config.router) {
85   server = httpProxy.createServer(config);
86 }
87 else {
88   return util.puts(help);
89 }
90
91 //
92 // Start the server
93 //
94 if (host) {
95   server.listen(port, host);
96 } else {
97   server.listen(port);
98 }
99
100
101 //
102 // Drop privileges if requested
103 //
104 if (typeof user === 'string') {
105     process.setuid(user);
106 }
107
108 //
109 // Notify that the server is started
110 //
111 if (!config.silent) {
112   util.puts('node-http-proxy server now listening on port: ' + port);
113 }