lib/cors-anywhere.js
/**
 * This method modifies the response headers of the proxied response.
 * If a redirect is detected, the response is not sent to the client,
 * and a new request is initiated.
 *
 * client (req) -> CORS Anywhere -> (proxyReq) -> other server
 * client (res) <- CORS Anywhere <- (proxyRes) <- other server
 *
 * @param proxy {HttpProxy}
 * @param proxyReq {ClientRequest} The outgoing request to the other server.
 * @param proxyRes {ServerResponse} The response from the other server.
 * @param req {IncomingMessage} Incoming HTTP request, augmented with property corsAnywhereRequestState
 * @param req.corsAnywhereRequestState {object}
 * @param req.corsAnywhereRequestState.location {object} See parseURL
 * @param req.corsAnywhereRequestState.getProxyForUrl {function} See proxyRequest
 * @param req.corsAnywhereRequestState.proxyBaseUrl {string} Base URL of the CORS API endpoint
 * @param req.corsAnywhereRequestState.maxRedirects {number} Maximum number of redirects
 * @param req.corsAnywhereRequestState.redirectCount_ {number} Internally used to count redirects
 * @param res {ServerResponse} Outgoing response to the client that wanted to proxy the HTTP request.
 *
 * @returns {boolean} true if http-proxy should continue to pipe proxyRes to res.
 */
This method modifies the response headers of the proxied response.
If a redirect is detected, the response is not sent to the client,
and a new request is initiated.



@param proxy {HttpProxy}
@param proxyReq {ClientRequest} The outgoing request to the other server.
@param proxyRes {ServerResponse} The response from the other server.

@returns {boolean} true if http-proxy should continue to pipe proxyRes to res.
This method modifies the response headers of the proxied response . If a redirect is detected the response is not sent to the client and a new request is initiated . @param proxy { HttpProxy } @param proxyReq { ClientRequest } The outgoing request to the other server . @param proxyRes { ServerResponse } The response from the other server . @returns { boolean } true if http - proxy should continue to pipe proxyRes to res .
function onProxyResponse(proxy, proxyReq, proxyRes, req, res) {
  var requestState = req.corsAnywhereRequestState;
  var statusCode = proxyRes.statusCode;
  if (!requestState.redirectCount_) {
    res.setHeader('x-request-url', requestState.location.href);
  }
  if (statusCode === 301 || statusCode === 302 || statusCode === 303 || statusCode === 307 || statusCode === 308) {
    var locationHeader = proxyRes.headers.location;
    var parsedLocation;
    if (locationHeader) {
      locationHeader = url.resolve(requestState.location.href, locationHeader);
      parsedLocation = parseURL(locationHeader);
    }
    if (parsedLocation) {
      if (statusCode === 301 || statusCode === 302 || statusCode === 303) {
        requestState.redirectCount_ = requestState.redirectCount_ + 1 || 1;
        if (requestState.redirectCount_ <= requestState.maxRedirects) {
           within the server, because some clients (e.g. Android Stock Browser)
          res.setHeader('X-CORS-Redirect-' + requestState.redirectCount_, statusCode + ' ' + locationHeader);
          req.method = 'GET';
          req.headers['content-length'] = '0';
          delete req.headers['content-type'];
          requestState.location = parsedLocation;
          req.removeAllListeners();
          proxyReq.removeAllListeners('error');
          proxyReq.once('error', function catchAndIgnoreError() {});
          proxyReq.abort();
          proxyRequest(req, res, proxy);
          return false;
        }
      }
      proxyRes.headers.location = requestState.proxyBaseUrl + '/' + locationHeader;
    }
  }
  if(true){
    var pathRegex = /path="((\/|\w|\?|\=|\:|\%|\.|\&|\#)*?)"/gm
    function updateCookies(cookies){
      var cookiePath = pathRegex.test(cookies)[0] || '/';
      return cookies.replace(pathRegex, "path="+ proxyReq.host + cookiePath +'"')
    }
    if(typeof proxyRes.headers['set-cookie'] === "object"){
      proxyRes.headers['Given'] = proxyRes.headers['set-cookie'];
    }
    if(typeof proxyRes.headers['set-cookie2'] === "object"){
      proxyRes.headers['Given2'] = proxyRes.headers['set-cookie2'];
    }
    else{
    }
  }else{
    delete proxyRes.headers['set-cookie'];
    delete proxyRes.headers['set-cookie2'];
  }
  proxyRes.headers['x-final-url'] = requestState.location.href;
  withCORS(proxyRes.headers, req);
  return true;
}
function onProxyResponse ( proxy , proxyReq , proxyRes , req , res ) { var requestState = req . corsAnywhereRequestState ; var statusCode = proxyRes . statusCode ; if ( ! requestState . redirectCount_ ) { res . setHeader ( 'x-request-url' , requestState . location . href ) ; } if ( statusCode === 301 || statusCode === 302 || statusCode === 303 || statusCode === 307 || statusCode === 308 ) { var locationHeader = proxyRes . headers . location ; var parsedLocation ; if ( locationHeader ) { locationHeader = url . resolve ( requestState . location . href , locationHeader ) ; parsedLocation = parseURL ( locationHeader ) ; } if ( parsedLocation ) { if ( statusCode === 301 || statusCode === 302 || statusCode === 303 ) { requestState . redirectCount_ = requestState . redirectCount_ + 1 || 1 ; if ( requestState . redirectCount_ <= requestState . maxRedirects ) { res . setHeader ( 'X-CORS-Redirect-' + requestState . redirectCount_ , statusCode + ' ' + locationHeader ) ; req . method = 'GET' ; req . headers [ 'content-length' ] = '0' ; delete req . headers [ 'content-type' ] ; requestState . location = parsedLocation ; req . removeAllListeners ( ) ; proxyReq . removeAllListeners ( 'error' ) ; proxyReq . once ( 'error' , function catchAndIgnoreError ( ) { } ) ; proxyReq . abort ( ) ; proxyRequest ( req , res , proxy ) ; return false ; } } proxyRes . headers . location = requestState . proxyBaseUrl + '/' + locationHeader ; } } if ( true ) { var pathRegex = / path="((\/|\w|\?|\=|\:|\%|\.|\&|\#)*?)" / gm function updateCookies ( cookies ) { var cookiePath = pathRegex . test ( cookies ) [ 0 ] || '/' ; return cookies . replace ( pathRegex , "path=" + proxyReq . host + cookiePath + '"' ) } if ( typeof proxyRes . headers [ 'set-cookie' ] === "object" ) { proxyRes . headers [ 'Given' ] = proxyRes . headers [ 'set-cookie' ] ; } if ( typeof proxyRes . headers [ 'set-cookie2' ] === "object" ) { proxyRes . headers [ 'Given2' ] = proxyRes . headers [ 'set-cookie2' ] ; } else { } } else { delete proxyRes . headers [ 'set-cookie' ] ; delete proxyRes . headers [ 'set-cookie2' ] ; } proxyRes . headers [ 'x-final-url' ] = requestState . location . href ; withCORS ( proxyRes . headers , req ) ; return true ; }
This method modifies the response headers of the proxied response.
This method modifies the response headers of the proxied response .
// Handle redirects // Exclude 307 & 308, because they are rare, and require preserving the method + request body // Handle redirects within the server, because some clients (e.g. Android Stock Browser) // cancel redirects. // Set header for debugging purposes. Do not try to parse it! // Remove all listeners (=reset events to initial state) // Remove the error listener so that the ECONNRESET "error" that // may occur after aborting a request does not propagate to res. // https://github.com/nodejitsu/node-http-proxy/blob/v1.11.1/lib/http-proxy/passes/web-incoming.js#L134 // Initiate a new proxy request. // Strip or update cookies //the line below must be changed. //once i figure out how to send enableCookies as true to this server //req.corsAnywhereRequestState.enableCookies //change to be made here //check what is the type of cookie -- string or object //if it's an object, leave it as it is //if it's a string, perform the instructions below //the line below has been commented out because, the site i will be trying to access //returns a json/object type cookie //the line below parses the cookie as a string //perhaps what can be done in future is to change the updateCookies function as described @the function //proxyRes.headers['set-cookie'] = updateCookies(proxyRes.headers['set-cookie']) //proxyRes.headers['set-cookie2'] = updateCookies(proxyRes.headers['set-cookie2']) //in the code above, i have rename assigned new headers, Given and Given2, to the relevant set cookie values //because the when the cookies are returned as set-cookie, they get intercepted bby the browsers, //preventing me from accessing them. so what i've done is simply returning the cookies as a different header //i also had to add these 2 new headers to the access-control-expose-headers header in the withCors function. //no cookies are available