Skip to content

Commit

Permalink
Auto update host header when forwarding. (#56)
Browse files Browse the repository at this point in the history
- Host header will auto-update
- `debug: true` can be set for forward option for extra logging.
  • Loading branch information
ballercat authored May 6, 2024
1 parent fd9d78c commit a6655cf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.1 - Automatically update host header

- fix: auto update host header during forwarding

## 0.4.0 - Support a forward array in configuration

- feat: support an array in the `forward` setting
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jambox",
"version": "0.4.0",
"version": "0.4.1",
"description": "Tool for recording and playing back HTTP requests.",
"bin": {
"jam": "./jam.mjs",
Expand Down
13 changes: 10 additions & 3 deletions src/Jambox.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,12 @@ export default class Jambox extends Emitter {
);
const useSSL =
targetURL.port === '443' || targetURL.protocol === 'https:';
const changeHosts = originalURL.host !== targetURL.host;

const httpOptions = {
ignoreHostHttpsErrors: true,
forwarding: {
targetHost: `http${useSSL ? 's' : ''}://${targetURL.host}`,
updateHostHeader: changeHosts ? originalURL.host : false,
updateHostHeader: true,
},
};

Expand Down Expand Up @@ -193,9 +192,17 @@ export default class Jambox extends Emitter {
.asPriority(101)
.thenJson(204, {}, optionsHeaders);
}
if (options.debug) {
httpOptions.beforeRequest = (req) => {
debug(`[${originalURL.host}] ${req.path} match`);
return req;
};
}

const matchers = [
new GlobMatcher(originalURL, { paths: options.paths || ['**'] }),
new GlobMatcher(originalURL, {
paths: options.paths || ['**'],
}),
];

await this.proxy.addRequestRule({
Expand Down

0 comments on commit a6655cf

Please sign in to comment.