Skip to content

Commit

Permalink
move some code around
Browse files Browse the repository at this point in the history
  • Loading branch information
nfriedly committed Mar 26, 2021
1 parent c99cc72 commit 6b0de84
Showing 1 changed file with 54 additions and 53 deletions.
107 changes: 54 additions & 53 deletions lib/unblocker.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,59 +110,6 @@ function Unblocker(config) {

var getRealUrl = Unblocker.getRealUrl(config);

function handleRequest(clientRequest, clientResponse, next) {
// convenience methods
clientRequest.thisHost = thisHost.bind(thisHost, clientRequest);
clientRequest.thisSite = thisSite.bind(thisSite, clientRequest);
clientResponse.redirectTo = redirectTo.bind(
redirectTo,
clientRequest,
clientResponse
);

if (!next) {
next = function () {
clientResponse.redirectTo("");
};
}

var url_data = url.parse(clientRequest.url);

// only requests that start with this get proxied - the rest get
// redirected to either a url that matches this or the home page
if (url_data.pathname.indexOf(config.prefix + "http") === 0) {
var uri = url.parse(getRealUrl(clientRequest.url));

// redirect urls like /proxy/http://asdf.com to /proxy/http://asdf.com/ to make relative image paths work
// but, don't redirect in cases where routers collapsed '//' to '/' (#130)
var formatted = url.format(uri);
var raw = clientRequest.url.substr(config.prefix.length);
if (formatted !== raw && formatted.replace("://", ":/") !== raw) {
return clientResponse.redirectTo(formatted);
}

// this is how api consumers can hook into requests. The data object is passed to all requestMiddleware before the request is sent to the remote server, and it is passed through all responseMiddleware before being sent to the client.
var data = {
url: formatted,
clientRequest: clientRequest,
clientResponse: clientResponse,
headers: _.cloneDeep(clientRequest.headers),
stream: clientRequest,
};

proxy(data, next);
} else {
// any other url gets redirected to the correct proxied url if we can
// determine it based on their referrer, or passed back to express (or whatever) otherwise
var targetUrl = recoverTargetUrl(clientRequest);
if (targetUrl) {
clientResponse.redirectTo(targetUrl);
} else {
next();
}
}
}

/**
* This is what makes this server magic: if we get an unrecognized request that wasn't corrected by
* proxy's filter, this checks the referrer to determine what the path should be, and then issues a
Expand Down Expand Up @@ -256,6 +203,60 @@ function Unblocker(config) {
response.end();
}

// regular web requests
function handleRequest(clientRequest, clientResponse, next) {
// convenience methods
clientRequest.thisHost = thisHost.bind(thisHost, clientRequest);
clientRequest.thisSite = thisSite.bind(thisSite, clientRequest);
clientResponse.redirectTo = redirectTo.bind(
redirectTo,
clientRequest,
clientResponse
);

if (!next) {
next = function () {
clientResponse.redirectTo("");
};
}

var url_data = url.parse(clientRequest.url);

// only requests that start with this get proxied - the rest get
// redirected to either a url that matches this or the home page
if (url_data.pathname.indexOf(config.prefix + "http") === 0) {
var uri = url.parse(getRealUrl(clientRequest.url));

// redirect urls like /proxy/http://asdf.com to /proxy/http://asdf.com/ to make relative image paths work
// but, don't redirect in cases where routers collapsed '//' to '/' (#130)
var formatted = url.format(uri);
var raw = clientRequest.url.substr(config.prefix.length);
if (formatted !== raw && formatted.replace("://", ":/") !== raw) {
return clientResponse.redirectTo(formatted);
}

// this is how api consumers can hook into requests. The data object is passed to all requestMiddleware before the request is sent to the remote server, and it is passed through all responseMiddleware before being sent to the client.
var data = {
url: formatted,
clientRequest: clientRequest,
clientResponse: clientResponse,
headers: _.cloneDeep(clientRequest.headers),
stream: clientRequest,
};

proxy(data, next);
} else {
// any other url gets redirected to the correct proxied url if we can
// determine it based on their referrer, or passed back to express (or whatever) otherwise
var targetUrl = recoverTargetUrl(clientRequest);
if (targetUrl) {
clientResponse.redirectTo(targetUrl);
} else {
next();
}
}
}

// websocket support
// todo: dedupe
const proxyWebsocket = Unblocker.websockets(config);
Expand Down

0 comments on commit 6b0de84

Please sign in to comment.