Skip to content

Commit

Permalink
Merge pull request #47 from pzerelles/master
Browse files Browse the repository at this point in the history
fix: preserve body in request copy
  • Loading branch information
gornostay25 authored Feb 20, 2024
2 parents 9dc9f63 + fb17caf commit a051191
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
],
"scripts": {
"build": "rm -fr files && bun run build.js",
"prepare": "bun run build",
"lint": "prettier --check .",
"format": "prettier --write ."
},
Expand Down
31 changes: 22 additions & 9 deletions src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ const server = new Server(manifest);
await server.init({ env: (Bun || process).env });

const xff_depth = parseInt(env("XFF_DEPTH", build_options.xff_depth ?? 1));
const origin = env('ORIGIN', undefined);
const origin = env("ORIGIN", undefined);

const address_header = env("ADDRESS_HEADER", "").toLowerCase();
const protocol_header = env("PROTOCOL_HEADER", "").toLowerCase();
const host_header = env("HOST_HEADER", "").toLowerCase();
const host_header = env("HOST_HEADER", "host").toLowerCase();

/** @param {boolean} assets */
export default function (assets) {
Expand Down Expand Up @@ -92,13 +92,26 @@ function serve(path, client = false) {
);
}

/**@param {Request} req */
function ssr(req) {
let request = req;
let url = req.url;
let path = url.slice(url.split("/", 3).join("/").length);
let base = origin || get_origin(req.headers);
request = new Request(base + path, req);
/**@param {Request} request */
function ssr(request) {
if (origin) {
const requestOrigin = get_origin(request.headers);
if (origin !== requestOrigin) {
const url = request.url.slice(request.url.split("/", 3).join("/").length);
request = new Request(origin + url, {
method: request.method,
headers: request.headers,
body: request.body,
referrer: request.referrer,
referrerPolicy: request.referrerPolicy,
mode: request.mode,
credentials: request.credentials,
cache: request.cache,
redirect: request.redirect,
integrity: request.integrity,
});
}
}

if (address_header && !request.headers.has(address_header)) {
throw new Error(
Expand Down

0 comments on commit a051191

Please sign in to comment.