Skip to content

Commit

Permalink
fix slash in request
Browse files Browse the repository at this point in the history
  • Loading branch information
arunaruljothi committed Mar 6, 2023
1 parent e73939b commit 8362a9a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
18 changes: 9 additions & 9 deletions dist/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ const server = (0, http_1.createServer)((req, res) => {
const targetHostname = (new URL(fullTarget)).hostname;
const target = `${protocol}://${targetHostname}:${proxiedPort}`;
// set origin
const originUrl = req.headers.origin ? new URL(req.headers.origin) : undefined;
const originHostname = originUrl ? originUrl.hostname : "";
const origin = req.headers.origin;
const originHostname = req.headers.origin ? (new URL(req.headers.origin)).hostname : "";
// if we previously proxied this result end it now
if ("x-proxy-by" in req.headers) {
log(`RE_PROXY:: ${originUrl?.href} -> ${target}`);
log(`RE_PROXY:: ${origin} -> ${target}`);
res.writeHead(400, "ATTEMPT AT RE PROXY");
res.end();
return;
}
// redirect on same origin or origin required and was not provided
if ((requiresOrigin && (originUrl == undefined)) || (originHostname == targetHostname)) {
log(`REDIRECT:: ${originUrl?.href} -> ${target}`);
if ((requiresOrigin && (origin == undefined)) || (originHostname == targetHostname)) {
log(`REDIRECT:: ${origin} -> ${target}`);
res.writeHead(302, {
'Location': fullTarget
});
Expand All @@ -59,12 +59,12 @@ const server = (0, http_1.createServer)((req, res) => {
if ('access-control-request-headers' in req.headers) {
res.setHeader('access-control-allow-headers', allowedHeaders);
}
res.setHeader('Access-Control-Allow-Origin', originUrl?.href || "*");
res.setHeader('Access-Control-Allow-Origin', origin || "*");
res.setHeader('Access-Control-Allow-Credentials', "true");
log(`CORS:: ${originUrl?.href} -> ${fullTarget}`);
log(`CORS:: ${origin} -> ${fullTarget}`);
}
else {
log(`NO_CORS:: ${originUrl?.href} -> ${fullTarget}`);
log(`NO_CORS:: ${origin} -> ${fullTarget}`);
}
// mark the request has having been proxied
res.setHeader("x-proxy-by", name);
Expand All @@ -76,7 +76,7 @@ const server = (0, http_1.createServer)((req, res) => {
const healthPort = (parseInt(port) + 1).toString();
const healthServer = (0, http_1.createServer)((req, res) => {
res.writeHead(200);
res.write("BAD");
res.write("OK");
res.end();
});
// launch servers
Expand Down
16 changes: 8 additions & 8 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ const server: Server = createServer((req: IncomingMessage, res: ServerResponse)
const target = `${protocol}://${targetHostname}:${proxiedPort}`

// set origin
const originUrl: URL | undefined = req.headers.origin ? new URL(req.headers.origin) : undefined
const originHostname = originUrl ? originUrl.hostname : ""
const origin: string | undefined = req.headers.origin
const originHostname = req.headers.origin ? (new URL(req.headers.origin)).hostname : ""

// if we previously proxied this result end it now
if ("x-proxy-by" in req.headers){
log (`RE_PROXY:: ${originUrl?.href} -> ${target}`)
log (`RE_PROXY:: ${origin} -> ${target}`)
res.writeHead(400, "ATTEMPT AT RE PROXY")
res.end()
return
}

// redirect on same origin or origin required and was not provided
if ((requiresOrigin && (originUrl == undefined)) || (originHostname == targetHostname)){
log(`REDIRECT:: ${originUrl?.href} -> ${target}`)
if ((requiresOrigin && (origin == undefined)) || (originHostname == targetHostname)){
log(`REDIRECT:: ${origin} -> ${target}`)
res.writeHead(302, {
'Location': fullTarget
})
Expand All @@ -66,11 +66,11 @@ const server: Server = createServer((req: IncomingMessage, res: ServerResponse)
if ('access-control-request-headers' in req.headers){
res.setHeader('access-control-allow-headers',allowedHeaders )
}
res.setHeader('Access-Control-Allow-Origin', originUrl?.href || "*")
res.setHeader('Access-Control-Allow-Origin', origin || "*")
res.setHeader('Access-Control-Allow-Credentials', "true")
log(`CORS:: ${originUrl?.href} -> ${fullTarget}`)
log(`CORS:: ${origin} -> ${fullTarget}`)
}else{
log(`NO_CORS:: ${originUrl?.href} -> ${fullTarget}`)
log(`NO_CORS:: ${origin} -> ${fullTarget}`)
}

// mark the request has having been proxied
Expand Down

0 comments on commit 8362a9a

Please sign in to comment.