| 424 | } |
| 425 | |
| 426 | fn build_upstream_request( |
| 427 | req: Request<Body>, |
| 428 | target_port: u16, |
| 429 | preserve_upgrade_headers: bool, |
| 430 | ) -> Result<Request<Body>, ServiceRouteError> { |
| 431 | let (parts, body) = req.into_parts(); |
| 432 | let path = parts.uri.path_and_query().map_or("/", |path| path.as_str()); |
| 433 | let uri = path |
| 434 | .parse::<http::Uri>() |
| 435 | .map_err(|_| ServiceRouteError::invalid_request())?; |
| 436 | |
| 437 | let mut builder = Request::builder() |
| 438 | .method(parts.method) |
| 439 | .uri(uri) |
| 440 | .version(http::Version::HTTP_11); |
| 441 | |
| 442 | let headers = builder |
| 443 | .headers_mut() |
| 444 | .ok_or_else(ServiceRouteError::internal_error)?; |
| 445 | for (name, value) in &parts.headers { |
| 446 | if (is_hop_by_hop_header(name) |
| 447 | && !(preserve_upgrade_headers && is_websocket_hop_by_hop_header(name))) |
| 448 | || is_gateway_auth_header(name) |
| 449 | { |
| 450 | continue; |
| 451 | } |
| 452 | if name == header::COOKIE { |
| 453 | if let Some(cookie) = sanitize_cookie_header(value) { |
| 454 | headers.append(name, cookie); |
| 455 | } |
| 456 | continue; |
| 457 | } |
| 458 | headers.append(name, value.clone()); |
| 459 | } |
| 460 | headers.insert( |
| 461 | header::HOST, |
| 462 | format!("127.0.0.1:{target_port}").parse().unwrap(), |
| 463 | ); |
| 464 | |
| 465 | builder |
| 466 | .body(body) |
| 467 | .map_err(|_| ServiceRouteError::internal_error()) |
| 468 | } |
| 469 | |
| 470 | fn upstream_uri_path(req: &Request<Body>) -> Result<&str, ServiceRouteError> { |
| 471 | let path = req.uri().path_and_query().map_or("/", |path| path.as_str()); |