Commit e67b725f authored by Luke Sandberg's avatar Luke Sandberg

Upgrade turbopack-dev-server from hyper 0.14 to hyper 1.x

parent 4d5015d3
...@@ -480,7 +480,7 @@ dependencies = [ ...@@ -480,7 +480,7 @@ dependencies = [
"bytes", "bytes",
"futures-util", "futures-util",
"http 1.1.0", "http 1.1.0",
"http-body 1.0.1", "http-body",
"http-body-util", "http-body-util",
"itoa", "itoa",
"matchit", "matchit",
...@@ -506,7 +506,7 @@ dependencies = [ ...@@ -506,7 +506,7 @@ dependencies = [
"bytes", "bytes",
"futures-util", "futures-util",
"http 1.1.0", "http 1.1.0",
"http-body 1.0.1", "http-body",
"http-body-util", "http-body-util",
"mime", "mime",
"pin-project-lite", "pin-project-lite",
...@@ -531,12 +531,6 @@ dependencies = [ ...@@ -531,12 +531,6 @@ dependencies = [
"rustc-demangle", "rustc-demangle",
] ]
[[package]]
name = "base64"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]] [[package]]
name = "base64" name = "base64"
version = "0.21.4" version = "0.21.4"
...@@ -2634,25 +2628,6 @@ version = "0.2.0" ...@@ -2634,25 +2628,6 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3531d702d6c1a3ba92a5fb55a404c7b8c476c8e7ca249951077afcbe4bc807f" checksum = "c3531d702d6c1a3ba92a5fb55a404c7b8c476c8e7ca249951077afcbe4bc807f"
[[package]]
name = "h2"
version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb2c4422095b67ee78da96fbb51a4cc413b3b25883c7717ff7ca1ab31022c9c9"
dependencies = [
"bytes",
"fnv",
"futures-core",
"futures-sink",
"futures-util",
"http 0.2.11",
"indexmap 2.13.0",
"slab",
"tokio",
"tokio-util",
"tracing",
]
[[package]] [[package]]
name = "h2" name = "h2"
version = "0.4.7" version = "0.4.7"
...@@ -2887,17 +2862,6 @@ dependencies = [ ...@@ -2887,17 +2862,6 @@ dependencies = [
"itoa", "itoa",
] ]
[[package]]
name = "http-body"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1"
dependencies = [
"bytes",
"http 0.2.11",
"pin-project-lite",
]
[[package]] [[package]]
name = "http-body" name = "http-body"
version = "1.0.1" version = "1.0.1"
...@@ -2917,7 +2881,7 @@ dependencies = [ ...@@ -2917,7 +2881,7 @@ dependencies = [
"bytes", "bytes",
"futures-util", "futures-util",
"http 1.1.0", "http 1.1.0",
"http-body 1.0.1", "http-body",
"pin-project-lite", "pin-project-lite",
] ]
...@@ -2939,30 +2903,6 @@ version = "2.1.0" ...@@ -2939,30 +2903,6 @@ version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "hyper"
version = "0.14.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41dfc780fdec9373c01bae43289ea34c972e40ee3c9f6b3c8801a35f35586ce7"
dependencies = [
"bytes",
"futures-channel",
"futures-core",
"futures-util",
"h2 0.3.24",
"http 0.2.11",
"http-body 0.4.5",
"httparse",
"httpdate",
"itoa",
"pin-project-lite",
"socket2 0.5.10",
"tokio",
"tower-service",
"tracing",
"want",
]
[[package]] [[package]]
name = "hyper" name = "hyper"
version = "1.7.0" version = "1.7.0"
...@@ -2973,9 +2913,9 @@ dependencies = [ ...@@ -2973,9 +2913,9 @@ dependencies = [
"bytes", "bytes",
"futures-channel", "futures-channel",
"futures-core", "futures-core",
"h2 0.4.7", "h2",
"http 1.1.0", "http 1.1.0",
"http-body 1.0.1", "http-body",
"httparse", "httparse",
"httpdate", "httpdate",
"itoa", "itoa",
...@@ -2993,7 +2933,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" ...@@ -2993,7 +2933,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3c93eb611681b207e1fe55d5a71ecf91572ec8a6705cdb6857f7d8d5242cf58" checksum = "e3c93eb611681b207e1fe55d5a71ecf91572ec8a6705cdb6857f7d8d5242cf58"
dependencies = [ dependencies = [
"http 1.1.0", "http 1.1.0",
"hyper 1.7.0", "hyper",
"hyper-util", "hyper-util",
"rustls", "rustls",
"rustls-pki-types", "rustls-pki-types",
...@@ -3008,7 +2948,7 @@ version = "0.5.2" ...@@ -3008,7 +2948,7 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b90d566bffbce6a75bd8b09a05aa8c2cb1fabb6cb348f8840c9e4c90a0d83b0" checksum = "2b90d566bffbce6a75bd8b09a05aa8c2cb1fabb6cb348f8840c9e4c90a0d83b0"
dependencies = [ dependencies = [
"hyper 1.7.0", "hyper",
"hyper-util", "hyper-util",
"pin-project-lite", "pin-project-lite",
"tokio", "tokio",
...@@ -3023,7 +2963,7 @@ checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" ...@@ -3023,7 +2963,7 @@ checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0"
dependencies = [ dependencies = [
"bytes", "bytes",
"http-body-util", "http-body-util",
"hyper 1.7.0", "hyper",
"hyper-util", "hyper-util",
"native-tls", "native-tls",
"tokio", "tokio",
...@@ -3033,15 +2973,17 @@ dependencies = [ ...@@ -3033,15 +2973,17 @@ dependencies = [
[[package]] [[package]]
name = "hyper-tungstenite" name = "hyper-tungstenite"
version = "0.9.0" version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "880b8b1c98a5ec2a505c7c90db6d3f6f1f480af5655d9c5b55facc9382a5a5b5" checksum = "bc778da281a749ed28d2be73a9f2cd13030680a1574bc729debd1195e44f00e9"
dependencies = [ dependencies = [
"hyper 0.14.32", "http-body-util",
"pin-project", "hyper",
"hyper-util",
"pin-project-lite",
"tokio", "tokio",
"tokio-tungstenite", "tokio-tungstenite",
"tungstenite 0.18.0", "tungstenite 0.28.0",
] ]
[[package]] [[package]]
...@@ -3056,13 +2998,13 @@ dependencies = [ ...@@ -3056,13 +2998,13 @@ dependencies = [
"futures-core", "futures-core",
"futures-util", "futures-util",
"http 1.1.0", "http 1.1.0",
"http-body 1.0.1", "http-body",
"hyper 1.7.0", "hyper",
"ipnet", "ipnet",
"libc", "libc",
"percent-encoding", "percent-encoding",
"pin-project-lite", "pin-project-lite",
"socket2 0.5.10", "socket2 0.6.2",
"tokio", "tokio",
"tower-service", "tower-service",
"tracing", "tracing",
...@@ -4270,9 +4212,9 @@ dependencies = [ ...@@ -4270,9 +4212,9 @@ dependencies = [
"bytes", "bytes",
"futures-util", "futures-util",
"http 1.1.0", "http 1.1.0",
"http-body 1.0.1", "http-body",
"http-body-util", "http-body-util",
"hyper 1.7.0", "hyper",
"hyper-util", "hyper-util",
"log", "log",
"rand 0.9.0", "rand 0.9.0",
...@@ -5740,7 +5682,7 @@ dependencies = [ ...@@ -5740,7 +5682,7 @@ dependencies = [
"quinn-udp", "quinn-udp",
"rustc-hash 2.1.1", "rustc-hash 2.1.1",
"rustls", "rustls",
"socket2 0.5.10", "socket2 0.6.2",
"thiserror 2.0.12", "thiserror 2.0.12",
"tokio", "tokio",
"tracing", "tracing",
...@@ -5778,7 +5720,7 @@ dependencies = [ ...@@ -5778,7 +5720,7 @@ dependencies = [
"cfg_aliases", "cfg_aliases",
"libc", "libc",
"once_cell", "once_cell",
"socket2 0.5.10", "socket2 0.6.2",
"tracing", "tracing",
"windows-sys 0.60.2", "windows-sys 0.60.2",
] ]
...@@ -6128,9 +6070,9 @@ dependencies = [ ...@@ -6128,9 +6070,9 @@ dependencies = [
"futures-core", "futures-core",
"futures-util", "futures-util",
"http 1.1.0", "http 1.1.0",
"http-body 1.0.1", "http-body",
"http-body-util", "http-body-util",
"hyper 1.7.0", "hyper",
"hyper-rustls", "hyper-rustls",
"hyper-tls", "hyper-tls",
"hyper-util", "hyper-util",
...@@ -7035,16 +6977,6 @@ dependencies = [ ...@@ -7035,16 +6977,6 @@ dependencies = [
"managed", "managed",
] ]
[[package]]
name = "socket2"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64a4a911eed85daf18834cfaa86a79b7d266ff93ff5ba14005426219480ed662"
dependencies = [
"libc",
"winapi",
]
[[package]] [[package]]
name = "socket2" name = "socket2"
version = "0.5.10" version = "0.5.10"
...@@ -8989,14 +8921,14 @@ dependencies = [ ...@@ -8989,14 +8921,14 @@ dependencies = [
[[package]] [[package]]
name = "tokio-tungstenite" name = "tokio-tungstenite"
version = "0.18.0" version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54319c93411147bced34cb5609a80e0a8e44c5999c93903a81cd866630ec0bfd" checksum = "d25a406cddcc431a75d3d9afc6a7c0f7428d4891dd973e4d54c56b46127bf857"
dependencies = [ dependencies = [
"futures-util", "futures-util",
"log", "log",
"tokio", "tokio",
"tungstenite 0.18.0", "tungstenite 0.28.0",
] ]
[[package]] [[package]]
...@@ -9144,11 +9076,11 @@ dependencies = [ ...@@ -9144,11 +9076,11 @@ dependencies = [
"axum", "axum",
"base64 0.22.1", "base64 0.22.1",
"bytes", "bytes",
"h2 0.4.7", "h2",
"http 1.1.0", "http 1.1.0",
"http-body 1.0.1", "http-body",
"http-body-util", "http-body-util",
"hyper 1.7.0", "hyper",
"hyper-timeout", "hyper-timeout",
"hyper-util", "hyper-util",
"percent-encoding", "percent-encoding",
...@@ -9208,7 +9140,7 @@ dependencies = [ ...@@ -9208,7 +9140,7 @@ dependencies = [
"bytes", "bytes",
"futures-util", "futures-util",
"http 1.1.0", "http 1.1.0",
"http-body 1.0.1", "http-body",
"iri-string", "iri-string",
"pin-project-lite", "pin-project-lite",
"tower 0.5.2", "tower 0.5.2",
...@@ -9356,13 +9288,13 @@ dependencies = [ ...@@ -9356,13 +9288,13 @@ dependencies = [
[[package]] [[package]]
name = "tungstenite" name = "tungstenite"
version = "0.18.0" version = "0.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30ee6ab729cd4cf0fd55218530c4522ed30b7b6081752839b68fcec8d0960788" checksum = "9e3dac10fd62eaf6617d3a904ae222845979aec67c615d1c842b4002c7666fb9"
dependencies = [ dependencies = [
"base64 0.13.1",
"byteorder", "byteorder",
"bytes", "bytes",
"data-encoding",
"http 0.2.11", "http 0.2.11",
"httparse", "httparse",
"log", "log",
...@@ -9375,14 +9307,14 @@ dependencies = [ ...@@ -9375,14 +9307,14 @@ dependencies = [
[[package]] [[package]]
name = "tungstenite" name = "tungstenite"
version = "0.20.1" version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e3dac10fd62eaf6617d3a904ae222845979aec67c615d1c842b4002c7666fb9" checksum = "9ef1a641ea34f399a848dea702823bbecfb4c486f911735368f1f137cb8257e1"
dependencies = [ dependencies = [
"byteorder", "byteorder",
"bytes", "bytes",
"data-encoding", "data-encoding",
"http 0.2.11", "http 1.1.0",
"httparse", "httparse",
"log", "log",
"rand 0.8.5", "rand 0.8.5",
...@@ -9394,20 +9326,18 @@ dependencies = [ ...@@ -9394,20 +9326,18 @@ dependencies = [
[[package]] [[package]]
name = "tungstenite" name = "tungstenite"
version = "0.21.0" version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ef1a641ea34f399a848dea702823bbecfb4c486f911735368f1f137cb8257e1" checksum = "8628dcc84e5a09eb3d8423d6cb682965dea9133204e8fb3efee74c2a0c259442"
dependencies = [ dependencies = [
"byteorder",
"bytes", "bytes",
"data-encoding", "data-encoding",
"http 1.1.0", "http 1.1.0",
"httparse", "httparse",
"log", "log",
"rand 0.8.5", "rand 0.9.0",
"sha1", "sha1",
"thiserror 1.0.69", "thiserror 2.0.12",
"url",
"utf-8", "utf-8",
] ]
...@@ -10056,8 +9986,10 @@ dependencies = [ ...@@ -10056,8 +9986,10 @@ dependencies = [
"bincode 2.0.1", "bincode 2.0.1",
"flate2", "flate2",
"futures", "futures",
"hyper 0.14.32", "http-body-util",
"hyper",
"hyper-tungstenite", "hyper-tungstenite",
"hyper-util",
"mime", "mime",
"mime_guess", "mime_guess",
"parking_lot", "parking_lot",
...@@ -10066,7 +9998,7 @@ dependencies = [ ...@@ -10066,7 +9998,7 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"serde_qs", "serde_qs",
"socket2 0.4.9", "socket2 0.5.10",
"tokio", "tokio",
"tokio-stream", "tokio-stream",
"tokio-util", "tokio-util",
......
...@@ -21,8 +21,10 @@ auto-hash-map = { workspace = true } ...@@ -21,8 +21,10 @@ auto-hash-map = { workspace = true }
bincode = { workspace = true } bincode = { workspace = true }
flate2 = { workspace = true } flate2 = { workspace = true }
futures = { workspace = true } futures = { workspace = true }
hyper = { version = "0.14", features = ["full"] } http-body-util = "0.1"
hyper-tungstenite = "0.9.0" hyper = { version = "1", features = ["server", "http1"] }
hyper-tungstenite = "0.19"
hyper-util = { version = "0.1", features = ["tokio", "server-auto", "server-graceful"] }
mime = { workspace = true } mime = { workspace = true }
mime_guess = "2.0.4" mime_guess = "2.0.4"
parking_lot = { workspace = true } parking_lot = { workspace = true }
...@@ -31,7 +33,7 @@ rustc-hash = { workspace = true } ...@@ -31,7 +33,7 @@ rustc-hash = { workspace = true }
serde = { workspace = true } serde = { workspace = true }
serde_json = { workspace = true } serde_json = { workspace = true }
serde_qs = { workspace = true } serde_qs = { workspace = true }
socket2 = "0.4.9" socket2 = "0.5"
tokio = { workspace = true } tokio = { workspace = true }
tokio-stream = "0.1.9" tokio-stream = "0.1.9"
tokio-util = { workspace = true } tokio-util = { workspace = true }
......
use std::{io::Read, iter}; use std::io::Read;
use anyhow::{Result, anyhow}; use anyhow::{Result, anyhow};
use auto_hash_map::AutoSet; use auto_hash_map::AutoSet;
use flate2::{Compression, bufread::GzEncoder}; use flate2::{Compression, bufread::GzEncoder};
use futures::{StreamExt, TryStreamExt, stream}; use futures::TryStreamExt;
use http_body_util::BodyExt;
use hyper::{ use hyper::{
Request, Response, Request, Response,
header::{CONTENT_ENCODING, CONTENT_LENGTH, HeaderName}, body::Incoming,
http::HeaderValue, header::{CONTENT_ENCODING, CONTENT_LENGTH, HeaderName, HeaderValue},
}; };
use mime::Mime; use mime::Mime;
use turbo_tasks::{ use turbo_tasks::{
CollectiblesSource, OperationVc, ReadRef, ResolvedVc, TransientInstance, Vc, apply_effects, CollectiblesSource, OperationVc, ReadRef, ResolvedVc, TransientInstance, Vc, apply_effects,
util::SharedError,
}; };
use turbo_tasks_bytes::Bytes; use turbo_tasks_bytes::Bytes;
use turbo_tasks_fs::FileContent; use turbo_tasks_fs::FileContent;
...@@ -22,10 +22,13 @@ use turbopack_core::{ ...@@ -22,10 +22,13 @@ use turbopack_core::{
version::VersionedContent, version::VersionedContent,
}; };
use crate::source::{ use crate::{
Body, ContentSource, ContentSourceSideEffect, HeaderList, ProxyResult, ResponseBody, empty_body, full_body,
request::SourceRequest, source::{
resolve::{ResolveSourceRequestResult, resolve_source_request}, Body, ContentSource, ContentSourceSideEffect, HeaderList, ProxyResult,
request::SourceRequest,
resolve::{ResolveSourceRequestResult, resolve_source_request},
},
}; };
#[turbo_tasks::value(serialization = "none")] #[turbo_tasks::value(serialization = "none")]
...@@ -75,10 +78,10 @@ async fn get_from_source_operation( ...@@ -75,10 +78,10 @@ async fn get_from_source_operation(
/// response. /// response.
pub async fn process_request_with_content_source( pub async fn process_request_with_content_source(
source: OperationVc<Box<dyn ContentSource>>, source: OperationVc<Box<dyn ContentSource>>,
request: Request<hyper::Body>, request: Request<Incoming>,
issue_reporter: Vc<Box<dyn IssueReporter>>, issue_reporter: Vc<Box<dyn IssueReporter>>,
) -> Result<( ) -> Result<(
Response<hyper::Body>, Response<ResponseBody>,
AutoSet<ResolvedVc<Box<dyn ContentSourceSideEffect>>>, AutoSet<ResolvedVc<Box<dyn ContentSourceSideEffect>>>,
)> { )> {
let original_path = request.uri().path().to_string(); let original_path = request.uri().path().to_string();
...@@ -111,14 +114,14 @@ pub async fn process_request_with_content_source( ...@@ -111,14 +114,14 @@ pub async fn process_request_with_content_source(
for (header_name, header_value) in headers { for (header_name, header_value) in headers {
header_map.append( header_map.append(
HeaderName::try_from(header_name.as_str())?, HeaderName::try_from(header_name.as_str())?,
hyper::header::HeaderValue::try_from(header_value.as_str())?, HeaderValue::try_from(header_value.as_str())?,
); );
} }
for (header_name, header_value) in header_overwrites.iter() { for (header_name, header_value) in header_overwrites.iter() {
header_map.insert( header_map.insert(
HeaderName::try_from(header_name.as_str())?, HeaderName::try_from(header_name.as_str())?,
hyper::header::HeaderValue::try_from(header_value.as_str())?, HeaderValue::try_from(header_value.as_str())?,
); );
} }
...@@ -141,7 +144,7 @@ pub async fn process_request_with_content_source( ...@@ -141,7 +144,7 @@ pub async fn process_request_with_content_source(
if let Some(content_type) = file.content_type() { if let Some(content_type) = file.content_type() {
header_map.append( header_map.append(
"content-type", "content-type",
hyper::header::HeaderValue::try_from(content_type.to_string())?, HeaderValue::try_from(content_type.to_string())?,
); );
should_compress = should_compress_predicate(content_type); should_compress = should_compress_predicate(content_type);
...@@ -152,7 +155,7 @@ pub async fn process_request_with_content_source( ...@@ -152,7 +155,7 @@ pub async fn process_request_with_content_source(
// If a text type, application/javascript, or application/json was // If a text type, application/javascript, or application/json was
// guessed, use a utf-8 charset as we most likely generated it as // guessed, use a utf-8 charset as we most likely generated it as
// such. // such.
entry.insert(hyper::header::HeaderValue::try_from( entry.insert(HeaderValue::try_from(
if (guess.type_() == mime::TEXT if (guess.type_() == mime::TEXT
|| guess.subtype() == mime::JAVASCRIPT || guess.subtype() == mime::JAVASCRIPT
|| guess.subtype() == mime::JSON) || guess.subtype() == mime::JSON)
...@@ -167,40 +170,28 @@ pub async fn process_request_with_content_source( ...@@ -167,40 +170,28 @@ pub async fn process_request_with_content_source(
if !header_map.contains_key("cache-control") { if !header_map.contains_key("cache-control") {
// The dev server contents might change at any time, we can't cache them. // The dev server contents might change at any time, we can't cache them.
header_map.append( header_map.append("cache-control", HeaderValue::try_from("must-revalidate")?);
"cache-control",
hyper::header::HeaderValue::try_from("must-revalidate")?,
);
} }
let content = file.content(); let content = file.content();
let response = if should_compress { let response = if should_compress {
header_map.insert(CONTENT_ENCODING, HeaderValue::from_static("gzip")); header_map.insert(CONTENT_ENCODING, HeaderValue::from_static("gzip"));
// Hyper requires an owned reader... We could do this with streaming by cloning
// each `Bytes` and implementing `BufRead` for `Iterator<bytes::Bytes>`, but
// it's not really worth it, just compressing the whole thing up-front is fine.
//
// Use fast compression, since we're likely just tranferring data over
// localhost.
let mut gz_bytes = Vec::new(); let mut gz_bytes = Vec::new();
GzEncoder::new(content.read(), Compression::fast()) GzEncoder::new(content.read(), Compression::fast())
.read_to_end(&mut gz_bytes) .read_to_end(&mut gz_bytes)
.expect("read of Rope should never fail"); .expect("read of Rope should never fail");
response.body(hyper::Body::wrap_stream(stream::iter(iter::once( response.body(full_body(gz_bytes))?
hyper::Result::Ok(gz_bytes),
))))?
} else { } else {
// hyper requires an owned stream, so we must clone the iterator items let mut all_bytes = Vec::new();
// this is relatively cheap: each chunk is a `Bytes`, so `Clone` updates a for chunk in content.read() {
// refcount all_bytes.extend_from_slice(chunk);
let owned_chunks: Vec<_> = }
content.read().cloned().map(hyper::Result::Ok).collect();
header_map.insert( header_map.insert(
CONTENT_LENGTH, CONTENT_LENGTH,
hyper::header::HeaderValue::try_from(content.len().to_string())?, HeaderValue::try_from(all_bytes.len().to_string())?,
); );
response.body(hyper::Body::wrap_stream(stream::iter(owned_chunks)))? response.body(full_body(all_bytes))?
}; };
return Ok((response, side_effects)); return Ok((response, side_effects));
...@@ -213,40 +204,42 @@ pub async fn process_request_with_content_source( ...@@ -213,40 +204,42 @@ pub async fn process_request_with_content_source(
for (name, value) in &proxy_result.headers { for (name, value) in &proxy_result.headers {
headers.append( headers.append(
HeaderName::from_bytes(name.as_bytes())?, HeaderName::from_bytes(name.as_bytes())?,
hyper::header::HeaderValue::from_str(value)?, HeaderValue::from_str(value)?,
); );
} }
return Ok(( // Collect proxy body from async stream into bytes
response.body(hyper::Body::wrap_stream(proxy_result.body.read()))?, let mut body_bytes = Vec::new();
side_effects, let mut body_stream = proxy_result.body.read();
)); while let Some(chunk) = body_stream.try_next().await? {
body_bytes.extend_from_slice(&chunk);
}
return Ok((response.body(full_body(body_bytes))?, side_effects));
} }
GetFromSourceResult::NotFound => {} GetFromSourceResult::NotFound => {}
} }
Ok(( Ok((
Response::builder().status(404).body(hyper::Body::empty())?, Response::builder().status(404).body(empty_body())?,
side_effects, side_effects,
)) ))
} }
async fn http_request_to_source_request(request: Request<hyper::Body>) -> Result<SourceRequest> { async fn http_request_to_source_request(request: Request<Incoming>) -> Result<SourceRequest> {
let (parts, body) = request.into_parts(); let (parts, body) = request.into_parts();
// For simplicity, we fully consume the body now and early return if there were // Collect the entire body
// any errors. let collected = body
let bytes: Vec<_> = body .collect()
.map(|bytes| { .await
bytes.map_or_else( .map_err(|e| anyhow!("failed to collect request body: {e}"))?;
|e| Err(SharedError::new(anyhow!(e))), let body_bytes = collected.to_bytes();
// The outer Ok is consumed by try_collect, but the Body type requires a Result, so let bytes = if body_bytes.is_empty() {
// we need to double wrap. vec![]
|b| Ok(Ok(Bytes::from(b))), } else {
) vec![Ok(Bytes::from(body_bytes))]
}) };
.try_collect::<Vec<_>>()
.await?;
Ok(SourceRequest { Ok(SourceRequest {
method: parts.method.to_string(), method: parts.method.to_string(),
......
...@@ -8,6 +8,7 @@ use anyhow::{Context as _, Error, Result}; ...@@ -8,6 +8,7 @@ use anyhow::{Context as _, Error, Result};
use futures::{SinkExt, prelude::*, ready, stream::FusedStream}; use futures::{SinkExt, prelude::*, ready, stream::FusedStream};
use hyper::{HeaderMap, Uri, upgrade::Upgraded}; use hyper::{HeaderMap, Uri, upgrade::Upgraded};
use hyper_tungstenite::{HyperWebsocket, WebSocketStream, tungstenite::Message}; use hyper_tungstenite::{HyperWebsocket, WebSocketStream, tungstenite::Message};
use hyper_util::rt::TokioIo;
use pin_project_lite::pin_project; use pin_project_lite::pin_project;
use tokio::select; use tokio::select;
use tokio_stream::StreamMap; use tokio_stream::StreamMap;
...@@ -239,7 +240,7 @@ fn resource_to_request(resource: &ResourceIdentifier) -> Result<SourceRequest> { ...@@ -239,7 +240,7 @@ fn resource_to_request(resource: &ResourceIdentifier) -> Result<SourceRequest> {
pin_project! { pin_project! {
struct UpdateClient { struct UpdateClient {
#[pin] #[pin]
ws: WebSocketStream<Upgraded>, ws: WebSocketStream<TokioIo<Upgraded>>,
ended: bool, ended: bool,
} }
} }
...@@ -332,8 +333,8 @@ impl<'a> Sink<ClientUpdateInstruction<'a>> for UpdateClient { ...@@ -332,8 +333,8 @@ impl<'a> Sink<ClientUpdateInstruction<'a>> for UpdateClient {
} }
} }
impl From<WebSocketStream<Upgraded>> for UpdateClient { impl From<WebSocketStream<TokioIo<Upgraded>>> for UpdateClient {
fn from(ws: WebSocketStream<Upgraded>) -> Self { fn from(ws: WebSocketStream<TokioIo<Upgraded>>) -> Self {
Self { ws, ended: false } Self { ws, ended: false }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment