1 | var path = require('path');
|
2 | var fs = require('fs');
|
3 | var util = require("./util");
|
4 |
|
5 | exports = module.exports;
|
6 |
|
7 | exports.determineHostForSocket = function(socket)
|
8 | {
|
9 | var resolvedVirtual = false;
|
10 |
|
11 | var host = process.env.CLOUDCMS_VIRTUAL_HOST;
|
12 | if (!host)
|
13 | {
|
14 |
|
15 | var virtualHostDomain = process.env.CLOUDCMS_VIRTUAL_HOST_DOMAIN;
|
16 | if (virtualHostDomain)
|
17 | {
|
18 |
|
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 |
|
29 | if (host)
|
30 | {
|
31 |
|
32 | if (host.indexOf(",") > -1)
|
33 | {
|
34 | var candidates = host.split(",");
|
35 |
|
36 |
|
37 | for (var x = 0; x < candidates.length; x++)
|
38 | {
|
39 |
|
40 | if (candidates[x].toLowerCase().indexOf(virtualHostDomain) > -1)
|
41 | {
|
42 | host = candidates[x];
|
43 | break;
|
44 | }
|
45 | }
|
46 | }
|
47 |
|
48 |
|
49 | host = util.trim(host);
|
50 |
|
51 |
|
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 |
|