|
@@ -48,6 +48,16 @@ impl ProxyClient {
|
|
pub fn add_proxy(mut router: Router, proxy: &WebProxyConfig) -> Result<Router> {
|
|
pub fn add_proxy(mut router: Router, proxy: &WebProxyConfig) -> Result<Router> {
|
|
let url: Uri = proxy.backend.parse()?;
|
|
let url: Uri = proxy.backend.parse()?;
|
|
let path = url.path().to_string();
|
|
let path = url.path().to_string();
|
|
|
|
+ let trimmed_path = path.trim_end_matches('/');
|
|
|
|
+
|
|
|
|
+ if trimmed_path.is_empty() {
|
|
|
|
+ return Err(crate::Error::ProxySetupError(format!(
|
|
|
|
+ "Proxy backend URL must have a non-empty path, e.g. {}/api instead of {}",
|
|
|
|
+ proxy.backend.trim_end_matches('/'),
|
|
|
|
+ proxy.backend
|
|
|
|
+ )));
|
|
|
|
+ }
|
|
|
|
+
|
|
let client = ProxyClient::new(url);
|
|
let client = ProxyClient::new(url);
|
|
|
|
|
|
// We also match everything after the path using a wildcard matcher.
|
|
// We also match everything after the path using a wildcard matcher.
|
|
@@ -56,7 +66,7 @@ pub fn add_proxy(mut router: Router, proxy: &WebProxyConfig) -> Result<Router> {
|
|
router = router.route(
|
|
router = router.route(
|
|
// Always remove trailing /'s so that the exact route
|
|
// Always remove trailing /'s so that the exact route
|
|
// matches.
|
|
// matches.
|
|
- path.trim_end_matches('/'),
|
|
|
|
|
|
+ trimmed_path,
|
|
any(move |req| async move {
|
|
any(move |req| async move {
|
|
client
|
|
client
|
|
.send(req)
|
|
.send(req)
|
|
@@ -68,7 +78,7 @@ pub fn add_proxy(mut router: Router, proxy: &WebProxyConfig) -> Result<Router> {
|
|
// Wildcard match anything else _after_ the backend URL's path.
|
|
// Wildcard match anything else _after_ the backend URL's path.
|
|
// Note that we know `path` ends with a trailing `/` in this branch,
|
|
// Note that we know `path` ends with a trailing `/` in this branch,
|
|
// so `wildcard` will look like `http://localhost/api/*proxywildcard`.
|
|
// so `wildcard` will look like `http://localhost/api/*proxywildcard`.
|
|
- let wildcard = format!("{}/*proxywildcard", path.trim_end_matches('/'));
|
|
|
|
|
|
+ let wildcard = format!("{}/*proxywildcard", trimmed_path);
|
|
router = router.route(
|
|
router = router.route(
|
|
&wildcard,
|
|
&wildcard,
|
|
any(move |req| async move {
|
|
any(move |req| async move {
|
|
@@ -168,4 +178,21 @@ mod test {
|
|
async fn add_proxy_trailing_slash() {
|
|
async fn add_proxy_trailing_slash() {
|
|
test_proxy_requests("/api/".to_string()).await;
|
|
test_proxy_requests("/api/".to_string()).await;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ #[test]
|
|
|
|
+ fn add_proxy_empty_path() {
|
|
|
|
+ let config = WebProxyConfig {
|
|
|
|
+ backend: "http://localhost:8000".to_string(),
|
|
|
|
+ };
|
|
|
|
+ let router = super::add_proxy(Router::new(), &config);
|
|
|
|
+ match router.unwrap_err() {
|
|
|
|
+ crate::Error::ProxySetupError(e) => {
|
|
|
|
+ assert_eq!(
|
|
|
|
+ e,
|
|
|
|
+ "Proxy backend URL must have a non-empty path, e.g. http://localhost:8000/api instead of http://localhost:8000"
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+ e => panic!("Unexpected error type: {}", e),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|