11use arc_swap:: ArcSwap ;
22use cached:: proc_macro:: cached;
33use futures_lite:: future:: block_on;
4- use futures_lite:: { future:: Boxed , FutureExt } ;
5- use http_body_util:: BodyExt ;
4+ use futures_lite:: { future:: Boxed , FutureExt , StreamExt } ;
5+ use http_body_util:: { BodyExt , StreamBody } ;
6+ use hyper:: body:: Frame ;
67use hyper:: { header, Method , Request , Response } ;
78use log:: { error, info, trace, warn} ;
89use percent_encoding:: { percent_encode, CONTROLS } ;
@@ -18,7 +19,7 @@ use std::result::Result;
1819
1920use crate :: dbg_msg;
2021use crate :: oauth:: { force_refresh_token, token_daemon, Oauth , OauthBackendImpl } ;
21- use crate :: server:: RequestExt ;
22+ use crate :: server:: { Body , RequestExt } ;
2223use crate :: utils:: { format_url, Post } ;
2324
2425const REDDIT_URL_BASE : & str = "https://oauth.reddit.com" ;
@@ -69,8 +70,6 @@ pub fn build_client() -> WreqClient {
6970 . expect ( "Should always be able to build a client" )
7071}
7172
72- use crate :: server:: { full, Body } ;
73-
7473/// Convert a wreq Response into a hyper Response<Body>.
7574/// wreq and hyper now both use http v1.x, so header types are directly compatible.
7675trait IntoHyperResponse {
@@ -93,15 +92,18 @@ impl IntoHyperResponse for wreq::Response {
9392 }
9493 }
9594
96- let bytes = self . bytes ( ) . await . map_err ( |e| e. to_string ( ) ) ?;
97- // wreq already decompresses; remove the content-encoding header so
98- // downstream code doesn't try to decompress again.
95+ // wreq already decompresses; drop content-length since the final
96+ // size is unknown without buffering, relying on chunked encoding.
9997 if let Some ( h) = builder. headers_mut ( ) {
10098 h. remove ( header:: CONTENT_ENCODING ) ;
101- h. insert ( header:: CONTENT_LENGTH , bytes . len ( ) . into ( ) ) ;
99+ h. remove ( header:: CONTENT_LENGTH ) ;
102100 }
103101
104- builder. body ( full ( bytes) ) . map_err ( |e| e. to_string ( ) )
102+ let stream = self
103+ . bytes_stream ( )
104+ . map ( |r| r. map ( Frame :: data) . map_err ( |e| e. to_string ( ) ) ) ;
105+
106+ builder. body ( BodyExt :: boxed ( StreamBody :: new ( stream) ) ) . map_err ( |e| e. to_string ( ) )
105107 }
106108}
107109
0 commit comments