Skip to content

Commit

Permalink
Proxy to market - Add HOST header because of Cloudflare
Browse files Browse the repository at this point in the history
  • Loading branch information
elpiel committed Dec 18, 2020
1 parent f063d03 commit f7e089f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 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.

21 changes: 20 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::net::SocketAddr;
use std::sync::Arc;
use thiserror::Error;

use http::{StatusCode, Uri};
use http::{header::HOST, StatusCode, Uri};
use slog::{error, info, Logger};

pub mod cache;
Expand Down Expand Up @@ -141,6 +141,25 @@ async fn handle<C: cache::Client>(

*req.uri_mut() = uri.parse::<Uri>()?;

// for Cloudflare we need to add a HOST header
let market_host_header = {
let url = market.market_url.to_url();
let host = url
.host_str()
.expect("MarketUrl always has a host")
.to_string();

match url.port() {
Some(port) => format!("{}:{}", host, port),
None => host,
}
};

let host = market_host_header
.parse()
.expect("The MarketUrl should be valid HOST header");
req.headers_mut().insert(HOST, host);

let proxy_response = match client.request(req).await {
Ok(response) => {
info!(&logger, "Proxied request to market"; "uri" => uri, "method" => %method);
Expand Down

0 comments on commit f7e089f

Please sign in to comment.