Skip to content
This repository was archived by the owner on Jun 25, 2022. It is now read-only.

Commit 23ef2e7

Browse files
authored
Merge pull request #109 from tjvr/refactor
Refactor GET and POST into one method
2 parents cd14ace + f8a313a commit 23ef2e7

File tree

1 file changed

+15
-64
lines changed

1 file changed

+15
-64
lines changed

modules/proxy.js

Lines changed: 15 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,19 @@ var sizeLimit = 2e6; //TODO: change this to something different depending on tie
2020
/*
2121
get handler handles standard GET reqs as well as streams
2222
*/
23-
function get (req, res, next) {
23+
const proxy = method => (req, res, next) => {
2424

2525
res.header('Access-Control-Allow-Origin', '*'); // Actually do the CORS thing! :)
2626

27-
var url = req.url.substr(1);
27+
let url;
28+
switch (method) {
29+
case 'GET':
30+
url = req.url.substr(1);
31+
break;
32+
case 'POST':
33+
url = req.params[0];
34+
break;
35+
}
2836

2937
// disallow blocked phrases
3038
if (url.match(blockedPhrases)) {
@@ -56,8 +64,7 @@ function get (req, res, next) {
5664
headers['X-Fowarded-For'] = (forwardedFor ? forwardedFor + ',' : '') + req.connection.remoteAddress;
5765

5866
var data = 0; // This variable contains the size of the data (for limiting file size)
59-
request
60-
.get(url, {headers}) // GET the document that the user specified
67+
request(url, {method, headers}) // request the document that the user specified
6168
.on('response', function (page) {
6269
// Check content length - if it's larger than the size limit, end the request with a 413 error.
6370
if (Number(page.headers['content-length']) > sizeLimit) {
@@ -91,66 +98,7 @@ function get (req, res, next) {
9198
})
9299
.pipe(res); // Stream requested url to response
93100
next();
94-
}
95-
96-
function post (req, res, next) {
97-
res.header('Access-Control-Allow-Origin', '*'); // Actually do the CORS thing! :)
98-
99-
var url = req.params[0];
100-
var data = 0;
101-
102-
// require Origin header
103-
if (!requireHeader.some(header => req.headers[header])) {
104-
res.statusCode = 403;
105-
return res.end('Origin: header is required');
106-
}
107-
108-
// TODO redirect same origin
109-
/* from cors-anywhere: boolean redirectSameOrigin - If true, requests to
110-
* URLs from the same origin will not be proxied but redirected. The
111-
* primary purpose for this option is to save server resources by
112-
* delegating the request to the client (since same-origin requests should
113-
* always succeed, even without proxying). */
114-
115-
// forward client headers to server
116-
117-
var headers = {};
118-
for (var header in req.headers) {
119-
if (!clientHeadersBlacklist.has(header.toLowerCase())) {
120-
headers[header] = req.headers[header];
121-
}
122-
}
123-
124-
var forwardedFor = req.headers['X-Fowarded-For'];
125-
headers['X-Fowarded-For'] = (forwardedFor ? forwardedFor + ',' : '') + req.connection.remoteAddress;
126-
req.pipe(
127-
request
128-
.post(url, {headers})
129-
.on('data', function (chunk) {
130-
data += chunk.length;
131-
if (data > sizeLimit){
132-
res.abort(); // kills response and request cleanly
133-
}
134-
})
135-
.on('response', function (page) {
136-
res.statusCode = page.statusCode;
137-
138-
// if the page already supports cors, redirect to the URL directly
139-
if (page.headers['access-control-allow-origin'] === '*') { // TODO is this best?
140-
res.redirect(url, next);
141-
}
142-
143-
// include only desired headers
144-
for (var header in page.headers) {
145-
if (!serverHeadersBlacklist.has(header)) {
146-
res.header(header, page.headers[header]);
147-
}
148-
}
149-
// must flush here -- otherwise pipe() will include the headers anyway!
150-
res.flushHeaders();
151-
}));
152-
next();
153-
}
101+
};
154102

155103
/*
156104
opts handler allows us to use our own CORS preflight settings
@@ -164,4 +112,7 @@ function opts (req, res, next) { // Couple of lines taken from http://stackoverf
164112
next();
165113
}
166114

115+
const get = proxy('GET');
116+
const post = proxy('POST');
117+
167118
module.exports = {get, post, opts};

0 commit comments

Comments
 (0)