UNPKG

2.26 kBJavaScriptView Raw
1var path = require('path');
2var fs = require('fs');
3var util = require("./util");
4
5exports = module.exports;
6
7exports.determineHostForSocket = function(socket)
8{
9 var resolvedVirtual = false;
10
11 var host = process.env.CLOUDCMS_VIRTUAL_HOST;
12 if (!host)
13 {
14 // see if we are configured for virtual hosting
15 var virtualHostDomain = process.env.CLOUDCMS_VIRTUAL_HOST_DOMAIN;
16 if (virtualHostDomain)
17 {
18 // check headers
19 if (socket.handshake && socket.handshake.headers)
20 {
21 host = socket.handshake.headers.host;
22 if (socket.handshake.headers["x-forwarded-host"])
23 {
24 host = socket.handshake.headers["x-forwarded-host"];
25 }
26 }
27
28 // if we have a host, process a bit further
29 if (host)
30 {
31 // if multiple hosts come our way
32 if (host.indexOf(",") > -1)
33 {
34 var candidates = host.split(",");
35
36 // find the one that is for our virtualHostDomain
37 for (var x = 0; x < candidates.length; x++)
38 {
39 // keep only those that are subdomains of our intended parent virtualHostDomain (i.e. "cloudcms.net")
40 if (candidates[x].toLowerCase().indexOf(virtualHostDomain) > -1)
41 {
42 host = candidates[x];
43 break;
44 }
45 }
46 }
47
48 // trim host for safe measure
49 host = util.trim(host);
50
51 // strip out port if it's on there
52 var x = host.indexOf(":");
53 if (x > -1)
54 {
55 host = host.substring(0, x);
56 }
57
58 if (host)
59 {
60 resolvedVirtual = true;
61 }
62 }
63 }
64 }
65
66 if (!host)
67 {
68 if (socket.handshake && socket.handshake.headers)
69 {
70 host = socket.handshake.headers.host;
71 }
72 }
73
74 if (!host) {
75 host = "localhost";
76 }
77
78 return host;
79};
80