11use arc_swap:: ArcSwap ;
22use cached:: proc_macro:: cached;
3+ use bytes:: Bytes ;
34use futures_lite:: future:: block_on;
4- use futures_lite:: { future:: Boxed , FutureExt } ;
5- use http_body_util:: BodyExt ;
5+ use futures_lite:: { future:: Boxed , FutureExt , StreamExt } ;
6+ use http_body_util:: { BodyExt , StreamBody } ;
7+ use hyper:: body:: Frame ;
68use hyper:: { header, Method , Request , Response } ;
79use log:: { error, info, trace, warn} ;
810use percent_encoding:: { percent_encode, CONTROLS } ;
@@ -18,7 +20,7 @@ use std::result::Result;
1820
1921use crate :: dbg_msg;
2022use crate :: oauth:: { force_refresh_token, token_daemon, Oauth , OauthBackendImpl } ;
21- use crate :: server:: RequestExt ;
23+ use crate :: server:: { Body , RequestExt } ;
2224use crate :: utils:: { format_url, Post } ;
2325
2426const REDDIT_URL_BASE : & str = "https://oauth.reddit.com" ;
@@ -69,8 +71,6 @@ pub fn build_client() -> WreqClient {
6971 . expect ( "Should always be able to build a client" )
7072}
7173
72- use crate :: server:: { full, Body } ;
73-
7474/// Convert a wreq Response into a hyper Response<Body>.
7575/// wreq and hyper now both use http v1.x, so header types are directly compatible.
7676trait IntoHyperResponse {
@@ -93,15 +93,18 @@ impl IntoHyperResponse for wreq::Response {
9393 }
9494 }
9595
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.
96+ // wreq already decompresses; drop content-length since the final
97+ // size is unknown without buffering, relying on chunked encoding.
9998 if let Some ( h) = builder. headers_mut ( ) {
10099 h. remove ( header:: CONTENT_ENCODING ) ;
101- h. insert ( header:: CONTENT_LENGTH , bytes . len ( ) . into ( ) ) ;
100+ h. remove ( header:: CONTENT_LENGTH ) ;
102101 }
103102
104- builder. body ( full ( bytes) ) . map_err ( |e| e. to_string ( ) )
103+ let stream = self
104+ . bytes_stream ( )
105+ . map ( |r| r. map ( Frame :: data) . map_err ( |e| e. to_string ( ) ) ) ;
106+
107+ builder. body ( BodyExt :: boxed ( StreamBody :: new ( stream) ) ) . map_err ( |e| e. to_string ( ) )
105108 }
106109}
107110
0 commit comments