forked from xkxx/node-spdy-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
191 lines (182 loc) · 5.9 KB
/
parser.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* Node HTTPS Proxy
* HTTP Header digester
* @author xkx
* @modified rankjie
* @version 2.0.024.2013.02.05
* @copyright 2013
* @licence GPL 3.0
*/
//version
exports.version = '2.0.024.2013.02.05';
var common = require('./common.js'),
util = require('util'),
url = require('url');
//regExps
var regexps = {
hostname : /([\w\d\._-]+):([\d]+)/,
fullPath : /[\w]+:\/\/[\S]+/,
shortPath : /\/[\S]*/,
httpVersion : /HTTP\/\d.\d/,
port : /:[\d]+/,
parse : function(regexp, string) {
var match = string.match(regexps[regexp]);
return (match) ? null : match.toString();
}
};
// header digester
// the url can be one of the following:
// server:port
// scheme://server:port/path
// /path
exports.headDigester = function(request) {
var index, urlObj, path = request.url;
if (request.method == "CONNECT") {
var match = path.match(regexps.hostname);
if(!match)
return {illegalConnectURL: true};
return {host: match[1], port: match[2]};
}
var host = request.headers.host;
if (host) { //optimization
index = host.indexOf(':');
if(index == -1)
urlObj = {host: host, port: 80};
else
urlObj = {host: host.slice(0, index), port: host.slice(index+1)};
index = request.url.indexOf('//');
if(index != -1) {
path = request.url.slice(index+2);
}
urlObj.path = path.slice(path.indexOf('/'));
return urlObj;
}
urlObj = url.parse(request.url); //TODO: optimize by writing reg maybe?
url.port = url.port || 80;
return urlObj;
};
//adapt head string for remote server
exports.createRequest = function(request, url) {
var requestObj = url;
requestObj.headers = request.headers;
requestObj.method = request.method;
var connection = requestObj.headers['proxy-connection'];
if(connection) {
delete requestObj.headers['proxy-connection'];
requestObj.headers.connection = connection;
}
delete requestObj.headers['proxy-authorization'];
return requestObj;
};
//create Response for net; Not Used
var httpHead = 'HTTP/1.1 ', CRLF = '\r\n', br = '<br/>';
exports.httpCreateResponse = function(type) {
// options: {noDefaultHeaders: bool}
var content = responses[type] || responses['default'];
var headers = content.headers || {};
if (!content.options || !content.options.noDefaultHeaders) {
headers.server = headers.server || 'Server: Nginx/1.1.0';
if (content.html) {
headers['content-length'] = content.html.length;
headers['content-type'] = 'text/html';
}
}
this.writeHead(content.statusCode, content.reasonPhrase, headers);
if(content.html) this.write(content.html);
if(content.isFinal) this.end();
};
exports.netCreateResponse = function(type) {
var content = responses[type] || responses['default'];
var result = httpHead + content.statusCode + ' ' + content.reasonPhrase + CRLF;
if (!content.noDefaultHeaders) {
result += 'Date: ' + new Date().toUTCString() + CRLF;
if (!content.headers.Server) result += 'Server: Nginx/1.1.0' + CRLF;
if (content.html) {
result += 'Content-Length: ' + content.html.length + CRLF;
result += 'Content-Type: text/html' + CRLF;
}
}
if (content.headers) {
for (var i in content.headers)
{result += i + ': ' + content.headers[i] + CRLF;}
}
result += CRLF;
result += content.html || '';
return result;
};
// very lightweight and poor-functionality HTML5 generator; useful?
var createHTML = function(title, body, additionals) {
var exportStg = '<!DOCTYPE html><html><head><title>' + title + '</title><meta charset="utf-8">', i = 0;
if (additionals) {
if (additionals.css) {
var cssItem;
for (i = 0; i < additionals.css.length; i++) {
cssItem = additionals.css[i];
exportStg += '<link rel="stylesheet" href="';
if (typeof cssItem == 'string') exportStg += cssItem + '" ';
else {
exportStg += cssItem.href + '" ' || '';
exportStg += 'media="' + cssItem.mediaType + '" ' || '';
}
exportStg += '/>';
}
}
if (additionals.js) {
for (i = 0; i < additionals.js.length; i++)
{exportStg += '<script src="' + additionals.js[i] + '"></script>';}
}
}
exportStg += '<body>' + body + '</body></html>';
return exportStg;
};
var statusCat = function(statusCode, statusText) {
return '<div style="text-align:center;"><img src="http://httpcats.herokuapp.com/' + statusCode +
'.jpg" alt="404" height="500"/><div>' + statusText + '</div></div>';
};
var responses = {
'https-established': {
statusCode: 200,
reasonPhrase: 'Connection established',
headers: {'Connection': 'Keep-Alive'},
options: {noDefaultHeaders: true}
},
'decline-http': {
statusCode: 501,
reasonPhrase: 'Not Implemented',
html: createHTML('501 Not Implemented', "This mass relay doesn't accept Cerberus payloads.")
},
'/': {
statusCode: 418,
reasonPhrase: "I'm a teapot",
html: createHTML('Nodejs HTTP/HTTPS Proxy Server', common.about.replace(/\n/g, '<br/>') + "<hr/>")
},
'request-timeout': {
statusCode: 504,
reasonPhrase: 'Gateway Timeout',
html: createHTML('504 Gateway Timeout', 'Your Request has timeout. You may try again or get a life.')
},
'illegal-response': {
statusCode: 502,
reasonPhrase: 'Bad Gateway',
html: createHTML('502 Bad Gateway', statusCat(502, "We received an invalid response from the server. Maybe it's running off a Raspberry PI."))
},
'ECONNREFUSED': {
statusCode: 502,
reasonPhrase: 'Bad Gateway',
html: createHTML('502 Bad Gateway', statusCat(502, "ECONNREFUSED. GLaDOS said no."))
},
'ENOTFOUND': {
statusCode: 400,
reasonPhrase: 'Bad Request',
html: createHTML('400 Bad Request', statusCat(400, "ENOTFOUND. Chuck Norris doesn't call the wrong number. You answer the wrong phone."))
},
'ECONNRESET': {
statusCode: 502,
reasonPhrase: 'Bad Gateway',
html: createHTML('502 Bad Gateway', statusCat(502, "ECONNRESET. Me suppose this is what you get for supporting SOPA/PIPA."))
},
'default': {
statusCode: 404,
reasonPhrase: 'Not Found',
html: createHTML('404 File Not Found', statusCat(404, 'The Creepers have taken over the Internet. Sssssssssssorry about that.'))
}
};