|
| 1 | +/* |
| 2 | + * 3DCityDB-Web-Map |
| 3 | + * http://www.3dcitydb.org/ |
| 4 | + * |
| 5 | + * Copyright 2015 - 2017 |
| 6 | + * Chair of Geoinformatics |
| 7 | + * Technical University of Munich, Germany |
| 8 | + * https://www.gis.bgu.tum.de/ |
| 9 | + * |
| 10 | + * The 3DCityDB-Web-Map is jointly developed with the following |
| 11 | + * cooperation partners: |
| 12 | + * |
| 13 | + * virtualcitySYSTEMS GmbH, Berlin <http://www.virtualcitysystems.de/> |
| 14 | + * |
| 15 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 16 | + * you may not use this file except in compliance with the License. |
| 17 | + * You may obtain a copy of the License at |
| 18 | + * |
| 19 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 20 | + * |
| 21 | + * Unless required by applicable law or agreed to in writing, software |
| 22 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 23 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 24 | + * See the License for the specific language governing permissions and |
| 25 | + * limitations under the License. |
| 26 | + */ |
| 27 | + |
| 28 | +/** |
| 29 | + * A simple JavaScript based HTTP server forked from the cesium-starter-app project at https://github.com/pjcozzi/cesium-starter-app. |
| 30 | + **/ |
| 31 | + |
| 32 | +(function() { |
| 33 | + "use strict"; |
| 34 | + /*global console,require,__dirname,process*/ |
| 35 | + /*jshint es3:false*/ |
| 36 | + |
| 37 | + var express = require('express'); |
| 38 | + var compression = require('compression'); |
| 39 | + var url = require('url'); |
| 40 | + var request = require('request'); |
| 41 | + var serveIndex = require('serve-index'); |
| 42 | + |
| 43 | + var yargs = require('yargs').options({ |
| 44 | + 'port' : { |
| 45 | + 'default' : 8000, |
| 46 | + 'description' : 'Port to listen on.' |
| 47 | + }, |
| 48 | + 'public' : { |
| 49 | + 'type' : 'boolean', |
| 50 | + 'description' : 'Run a public server that listens on all interfaces.' |
| 51 | + }, |
| 52 | + 'upstream-proxy' : { |
| 53 | + 'description' : 'A standard proxy server that will be used to retrieve data. Specify a URL including port, e.g. "http://proxy:8000".' |
| 54 | + }, |
| 55 | + 'bypass-upstream-proxy-hosts' : { |
| 56 | + 'description' : 'A comma separated list of hosts that will bypass the specified upstream_proxy, e.g. "lanhost1,lanhost2"' |
| 57 | + }, |
| 58 | + 'help' : { |
| 59 | + 'alias' : 'h', |
| 60 | + 'type' : 'boolean', |
| 61 | + 'description' : 'Show this help.' |
| 62 | + } |
| 63 | + }); |
| 64 | + var argv = yargs.argv; |
| 65 | + |
| 66 | + if (argv.help) { |
| 67 | + return yargs.showHelp(); |
| 68 | + } |
| 69 | + |
| 70 | + // eventually this mime type configuration will need to change |
| 71 | + // https://github.com/visionmedia/send/commit/d2cb54658ce65948b0ed6e5fb5de69d022bef941 |
| 72 | + var mime = express.static.mime; |
| 73 | + mime.define({ |
| 74 | + 'application/json' : ['czml', 'json', 'geojson', 'topojson'], |
| 75 | + 'model/vnd.gltf+json' : ['gltf'], |
| 76 | + 'model/vnd.gltf.binary' : ['bgltf'], |
| 77 | + 'text/plain' : ['glsl'] |
| 78 | + }); |
| 79 | + |
| 80 | + var app = express(); |
| 81 | + app.use(compression()); |
| 82 | + app.use(express.static(__dirname)); |
| 83 | + app.use('/data', express.static('data'), serveIndex('data', {'icons': true, 'view': 'details'})); |
| 84 | + |
| 85 | + app.use('/examples', express.static('examples'), serveIndex('examples', {'icons': true, 'view': 'details'})); |
| 86 | + |
| 87 | + |
| 88 | + function getRemoteUrlFromParam(req) { |
| 89 | + var remoteUrl = req.params[0]; |
| 90 | + if (remoteUrl) { |
| 91 | + // add http:// to the URL if no protocol is present |
| 92 | + if (!/^https?:\/\//.test(remoteUrl)) { |
| 93 | + remoteUrl = 'http://' + remoteUrl; |
| 94 | + } |
| 95 | + remoteUrl = url.parse(remoteUrl); |
| 96 | + // copy query string |
| 97 | + remoteUrl.search = url.parse(req.url).search; |
| 98 | + } |
| 99 | + return remoteUrl; |
| 100 | + } |
| 101 | + |
| 102 | + var dontProxyHeaderRegex = /^(?:Host|Proxy-Connection|Connection|Keep-Alive|Transfer-Encoding|TE|Trailer|Proxy-Authorization|Proxy-Authenticate|Upgrade)$/i; |
| 103 | + |
| 104 | + function filterHeaders(req, headers) { |
| 105 | + var result = {}; |
| 106 | + // filter out headers that are listed in the regex above |
| 107 | + Object.keys(headers).forEach(function(name) { |
| 108 | + if (!dontProxyHeaderRegex.test(name)) { |
| 109 | + result[name] = headers[name]; |
| 110 | + } |
| 111 | + }); |
| 112 | + return result; |
| 113 | + } |
| 114 | + |
| 115 | + var upstreamProxy = argv['upstream-proxy']; |
| 116 | + var bypassUpstreamProxyHosts = {}; |
| 117 | + if (argv['bypass-upstream-proxy-hosts']) { |
| 118 | + argv['bypass-upstream-proxy-hosts'].split(',').forEach(function(host) { |
| 119 | + bypassUpstreamProxyHosts[host.toLowerCase()] = true; |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + app.get('/proxy/*', function(req, res, next) { |
| 124 | + // look for request like http://localhost:8080/proxy/http://example.com/file?query=1 |
| 125 | + var remoteUrl = getRemoteUrlFromParam(req); |
| 126 | + if (!remoteUrl) { |
| 127 | + // look for request like http://localhost:8080/proxy/?http%3A%2F%2Fexample.com%2Ffile%3Fquery%3D1 |
| 128 | + remoteUrl = Object.keys(req.query)[0]; |
| 129 | + if (remoteUrl) { |
| 130 | + remoteUrl = url.parse(remoteUrl); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if (!remoteUrl) { |
| 135 | + return res.status(400).send('No url specified.'); |
| 136 | + } |
| 137 | + |
| 138 | + if (!remoteUrl.protocol) { |
| 139 | + remoteUrl.protocol = 'http:'; |
| 140 | + } |
| 141 | + |
| 142 | + var proxy; |
| 143 | + if (upstreamProxy && !(remoteUrl.host in bypassUpstreamProxyHosts)) { |
| 144 | + proxy = upstreamProxy; |
| 145 | + } |
| 146 | + |
| 147 | + // encoding : null means "body" passed to the callback will be raw bytes |
| 148 | + |
| 149 | + request.get({ |
| 150 | + url : url.format(remoteUrl), |
| 151 | + headers : filterHeaders(req, req.headers), |
| 152 | + encoding : null, |
| 153 | + proxy : proxy |
| 154 | + }, function(error, response, body) { |
| 155 | + var code = 500; |
| 156 | + |
| 157 | + if (response) { |
| 158 | + code = response.statusCode; |
| 159 | + res.header(filterHeaders(req, response.headers)); |
| 160 | + } |
| 161 | + |
| 162 | + res.status(code).send(body); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + var server = app.listen(argv.port, argv.public ? undefined : 'localhost', function() { |
| 167 | + console.log('Cesium development server running publicly. Connect to localhost:%d/', server.address().port); |
| 168 | + }); |
| 169 | + |
| 170 | + server.on('error', function (e) { |
| 171 | + if (e.code === 'EADDRINUSE') { |
| 172 | + console.log('Error: Port %d is already in use, select a different port.', argv.port); |
| 173 | + console.log('Example: node server.js --port %d', argv.port + 1); |
| 174 | + } else if (e.code === 'EACCES') { |
| 175 | + console.log('Error: This process does not have permission to listen on port %d.', argv.port); |
| 176 | + if (argv.port < 1024) { |
| 177 | + console.log('Try a port number higher than 1024.'); |
| 178 | + } |
| 179 | + } |
| 180 | + console.log(e); |
| 181 | + process.exit(1); |
| 182 | + }); |
| 183 | + |
| 184 | + server.on('close', function() { |
| 185 | + console.log('Cesium development server stopped.'); |
| 186 | + }); |
| 187 | + |
| 188 | + var isFirstSig = true; |
| 189 | + process.on('SIGINT', function() { |
| 190 | + if (isFirstSig) { |
| 191 | + console.log('Cesium development server shutting down.'); |
| 192 | + server.close(function() { |
| 193 | + process.exit(0); |
| 194 | + }); |
| 195 | + isFirstSig = false; |
| 196 | + } else { |
| 197 | + console.log('Cesium development server force kill.'); |
| 198 | + process.exit(1); |
| 199 | + } |
| 200 | + }); |
| 201 | + |
| 202 | +})(); |
| 203 | + |
0 commit comments