@@ -20,11 +20,19 @@ var sizeLimit = 2e6; //TODO: change this to something different depending on tie
20
20
/*
21
21
get handler handles standard GET reqs as well as streams
22
22
*/
23
- function get ( req , res , next ) {
23
+ const proxy = method => ( req , res , next ) => {
24
24
25
25
res . header ( 'Access-Control-Allow-Origin' , '*' ) ; // Actually do the CORS thing! :)
26
26
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
+ }
28
36
29
37
// disallow blocked phrases
30
38
if ( url . match ( blockedPhrases ) ) {
@@ -56,8 +64,7 @@ function get (req, res, next) {
56
64
headers [ 'X-Fowarded-For' ] = ( forwardedFor ? forwardedFor + ',' : '' ) + req . connection . remoteAddress ;
57
65
58
66
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
61
68
. on ( 'response' , function ( page ) {
62
69
// Check content length - if it's larger than the size limit, end the request with a 413 error.
63
70
if ( Number ( page . headers [ 'content-length' ] ) > sizeLimit ) {
@@ -91,66 +98,7 @@ function get (req, res, next) {
91
98
} )
92
99
. pipe ( res ) ; // Stream requested url to response
93
100
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
+ } ;
154
102
155
103
/*
156
104
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
164
112
next ( ) ;
165
113
}
166
114
115
+ const get = proxy ( 'GET' ) ;
116
+ const post = proxy ( 'POST' ) ;
117
+
167
118
module . exports = { get, post, opts} ;
0 commit comments