From e76ac95062fbd1f1031a667cc3153f3ee5c02382 Mon Sep 17 00:00:00 2001 From: Jun Kurihara Date: Tue, 2 Aug 2022 22:35:07 +0900 Subject: [PATCH] cleartext backend request will be http1.1 --- config-example.toml | 4 ++-- src/backend/upstream_opts.rs | 8 ++++---- src/config/parse.rs | 4 ++-- src/handler/handler_main.rs | 9 ++++++--- src/handler/utils_request.rs | 4 ++-- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/config-example.toml b/config-example.toml index a735289..d8a9ecd 100644 --- a/config-example.toml +++ b/config-example.toml @@ -51,7 +51,7 @@ upstream = [ { location = 'www.yahoo.com', tls = true }, { location = 'www.yahoo.co.jp', tls = true }, ] -upstream_options = ["override_host", "convert_to_http2"] +upstream_options = ["override_host", "convert_https_to_2"] # Non-default destination in "localhost" app, which is routed by "path" [[apps.localhost.reverse_proxy]] @@ -69,7 +69,7 @@ upstream = [ upstream_options = [ "override_host", "upgrade_insecure_requests", - "convert_to_http11", + "convert_https_to_11", ] ###################################################################### diff --git a/src/backend/upstream_opts.rs b/src/backend/upstream_opts.rs index 71885b8..1cdb2a5 100644 --- a/src/backend/upstream_opts.rs +++ b/src/backend/upstream_opts.rs @@ -4,8 +4,8 @@ use crate::error::*; pub enum UpstreamOption { OverrideHost, UpgradeInsecureRequests, - ConvertToHttp11, - ConvertToHttp2, + ConvertHttpsTo11, + ConvertHttpsTo2, // TODO: Adds more options for heder override } impl TryFrom<&str> for UpstreamOption { @@ -14,8 +14,8 @@ impl TryFrom<&str> for UpstreamOption { match val { "override_host" => Ok(Self::OverrideHost), "upgrade_insecure_requests" => Ok(Self::UpgradeInsecureRequests), - "convert_to_http11" => Ok(Self::ConvertToHttp11), - "convert_to_http2" => Ok(Self::ConvertToHttp2), + "convert_https_to_11" => Ok(Self::ConvertHttpsTo11), + "convert_https_to_2" => Ok(Self::ConvertHttpsTo2), _ => Err(RpxyError::Other(anyhow!("Unsupported header option"))), } } diff --git a/src/config/parse.rs b/src/config/parse.rs index b355fdb..8efc839 100644 --- a/src/config/parse.rs +++ b/src/config/parse.rs @@ -227,8 +227,8 @@ fn get_reverse_proxy(rp_settings: &[ReverseProxyOption]) -> std::result::Result< ensure!( upstream .iter() - .all(|(_, elem)| !(elem.opts.contains(&UpstreamOption::ConvertToHttp11) - && elem.opts.contains(&UpstreamOption::ConvertToHttp2))), + .all(|(_, elem)| !(elem.opts.contains(&UpstreamOption::ConvertHttpsTo11) + && elem.opts.contains(&UpstreamOption::ConvertHttpsTo2))), "either one of force_http11 or force_http2 can be enabled" ); diff --git a/src/handler/handler_main.rs b/src/handler/handler_main.rs index d22fe8b..b6d0146 100644 --- a/src/handler/handler_main.rs +++ b/src/handler/handler_main.rs @@ -4,6 +4,7 @@ use crate::{backend::UpstreamGroup, error::*, globals::Globals, log::*, utils::S use hyper::{ client::connect::Connect, header::{self, HeaderValue}, + http::uri::Scheme, Body, Client, Request, Response, StatusCode, Uri, Version, }; use std::{env, net::SocketAddr, sync::Arc}; @@ -289,10 +290,12 @@ where .insert(header::CONNECTION, HeaderValue::from_str("upgrade")?); } + // If not specified (force_httpXX_upstream) and https, version is preserved except for http/3 apply_upstream_options_to_request_line(req, upstream_group)?; - - // if not specified (force_httpXX_upstream), version is preserved except for http/3 - if req.version() == Version::HTTP_3 { + // Maybe workaround: Change version to http/1.1 when destination scheme is http + if req.version() != Version::HTTP_11 && upstream_chosen.uri.scheme() == Some(&Scheme::HTTP) { + *req.version_mut() = Version::HTTP_11; + } else if req.version() == Version::HTTP_3 { debug!("HTTP/3 is currently unsupported for request to upstream. Use HTTP/2."); *req.version_mut() = Version::HTTP_2; } diff --git a/src/handler/utils_request.rs b/src/handler/utils_request.rs index eb70f03..6bb4a19 100644 --- a/src/handler/utils_request.rs +++ b/src/handler/utils_request.rs @@ -10,8 +10,8 @@ use hyper::{header, Request}; pub(super) fn apply_upstream_options_to_request_line(req: &mut Request, upstream: &UpstreamGroup) -> Result<()> { for opt in upstream.opts.iter() { match opt { - UpstreamOption::ConvertToHttp11 => *req.version_mut() = hyper::Version::HTTP_11, - UpstreamOption::ConvertToHttp2 => *req.version_mut() = hyper::Version::HTTP_2, + UpstreamOption::ConvertHttpsTo11 => *req.version_mut() = hyper::Version::HTTP_11, + UpstreamOption::ConvertHttpsTo2 => *req.version_mut() = hyper::Version::HTTP_2, _ => (), } }