Skip to content

Commit

Permalink
Add x-middleman-passthrough Header (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
A-Helberg authored Mar 5, 2024
1 parent 9b77c87 commit 65e3555
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "middleman"
version = "0.1.3"
version = "0.1.4"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ Starts a reverse proxy to <UPSTREAM>, listens on <BIND>:<PORT>.
Records upstream responses to <TAPES> directory.
Returns recorded response if url matches (does not call upstream in this case).
The optional header `x-middleman-passthrough` can be specified in http requests to middleman to pass a request through to the <UPSTREAM>.
Any value other than the exact string "false" will be considered Truthy.
The `--replay-only` config flag takes precedence over the `x-middleman-passthrough` header.
Usage: middleman [OPTIONS]
Options:
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static DEFAULT_CONFIG_FILENAME: &str = "middleman.toml";
#[command(
author,
version,
about = "Starts a reverse proxy to <UPSTREAM>, listens on <BIND>:<PORT>.\nRecords upstream responses to <TAPES> directory.\nReturns recorded response if url matches (does not call upstream in this case)."
about = "Starts a reverse proxy to <UPSTREAM>, listens on <BIND>:<PORT>.\nRecords upstream responses to <TAPES> directory.\nReturns recorded response if url matches (does not call upstream in this case).\n\nThe optional header `x-middleman-passthrough` can be specified in http requests to middleman to pass a request through to the <UPSTREAM>.\nAny value other than the exact string \"false\" will be considered Truthy.\nThe `--replay-only` config flag takes precedence over the `x-middleman-passthrough` header."
)]
pub struct CliArgs {
/// the port to listen on
Expand Down
11 changes: 11 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::tokiort::TokioIo;
use std::net::{IpAddr, SocketAddr};
use std::str::FromStr;

use crate::clone::clone_incoming_response;
use hyper::upgrade::Upgraded;
use hyper::Method;

Expand Down Expand Up @@ -83,6 +84,16 @@ async fn proxy_handler(
if config.replay_only {
proxy::replay(config, req).await
} else {
let passthrough = req.headers().contains_key("x-middleman-passthrough")
&& req.headers().get("x-middleman-passthrough").unwrap() != "false";

if passthrough == true {
let (req, _) = clone::clone_incoming_request(req).await?;
let resp = proxy::make_request(req).await?;
let (_, resp) = clone_incoming_response(resp).await?;
return Ok(resp);
}

if proxy::recording_exists(&proxy::recording_name(&config.tapes, &req)) {
return proxy::replay(config, req).await;
}
Expand Down

0 comments on commit 65e3555

Please sign in to comment.