forked from alediator/sir-geoexplorer-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.js
162 lines (152 loc) · 4.83 KB
/
proxy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
var clientRequest = require("ringo/httpclient").request;
var Headers = require("ringo/utils/http").Headers;
var MemoryStream = require("io").MemoryStream;
var objects = require("ringo/utils/objects");
var proxyGeoserver = require("./proxyGeoserver");
var URL = java.net.URL;
var app = exports.app = function(request) {
var response;
var url = request.queryParams.url;
if (url) {
console.log("url");
// Geoserver proxy #74477
if(proxyGeoserver.isGeoServerURL(request)){
response = proxyGeoserver.handleGeoserverRequest(request);
}else{
response = proxyPass({
request: request,
url: url
});
}
} else {
response = responseForStatus(400, "Request must contain url parameter.");
}
return response;
};
var pass = exports.pass = function(config) {
console.log("pass");
if (typeof config == "string") {
config = {url: config};
}
return function(request) {
var query = request.queryString;
var path = request.pathInfo && request.pathInfo.substring(1) || "";
var newUrl = config.url + path + (query ? "?" + query : "");
return proxyPass(objects.merge({
request: request,
url: newUrl
}, config));
};
};
var getUrlProps = exports.getUrlProps = function(url) {
console.log("getUrlProps");
var o, props;
try {
o = new URL(url);
} catch(err) {
// pass
}
if (o) {
var username, password;
var userInfo = o.getUserInfo();
if (userInfo) {
// this could potentially be removed if the following ticket is closed
// https://github.com/ringo/ringojs/issues/issue/121
// but, it could make sense to keep it here as well
[username, password] = userInfo.split(":");
url = url.replace(userInfo + "@", "");
}
var port = o.getPort();
if (port < 0) {
port = null;
}
props = {
url: url,
scheme: o.getProtocol(),
username: username || null,
password: password || null,
host: o.getHost(),
port: port,
path: o.getPath() || "/",
query: o.getQuery(),
hash: o.getRef()
};
}
return props;
};
var createProxyRequestProps = exports.createProxyRequestProps = function(config) {
console.log("createProxyRequestProps");
var props;
var request = config.request;
var url = config.url;
var urlProps = getUrlProps(url);
if (urlProps) {
var headers = new Headers(objects.clone(request.headers));
if (!config.preserveHost) {
headers.set("Host", urlProps.host + (urlProps.port ? ":" + urlProps.port : ""));
}
if (!config.allowAuth) {
// strip authorization and cookie headers
headers.unset("Authorization");
headers.unset("Cookie");
}
var data;
var method = request.method;
if (method == "PUT" || method == "POST") {
if (request.headers.get("content-length")) {
data = request.input;
}
}
props = {
url: urlProps.url,
method: request.method,
scheme: urlProps.scheme,
username: urlProps.username,
password: urlProps.password,
headers: headers,
data: data
};
}
return props;
};
function proxyPass(config) {
console.log("proxyPass");
var response;
var outgoing = createProxyRequestProps(config);
var incoming = config.request;
if (!outgoing || outgoing.scheme !== incoming.scheme) {
response = responseForStatus(400, "The url parameter value must be absolute url with same scheme as request.");
} else {
// re-issue request
var exchange = clientRequest({
url: outgoing.url,
method: outgoing.method,
username: outgoing.username,
password: outgoing.password,
headers: outgoing.headers,
data: outgoing.data,
async: false
});
}
exchange.wait();
var headers = new Headers(objects.clone(exchange.headers));
if (!config.allowAuth) {
// strip out authorization and cookie headers
headers.unset("WWW-Authenticate");
headers.unset("Set-Cookie");
}
return {
status: exchange.status,
headers: headers,
body: new MemoryStream(exchange.contentBytes)
};
}
var showParams = exports.showParams = function (keyValues){
for(var key in keyValues){
if(!!key
&& !!keyValues[key]){
//data[key] = config.request.queryParams[key];
console.log(key+"="+keyValues[key]);
}
}
}