@@ -661,52 +661,49 @@ where
661661 }
662662 }
663663 }
664- HttpTask :: Body ( data, end_stream) => match data {
665- Some ( d) => {
666- if session. cache . enabled ( ) {
667- // TODO: do this async
668- // fail if writing the body would exceed the max_file_size_bytes
669- let body_size_allowed =
670- session. cache . track_body_bytes_for_max_file_size ( d. len ( ) ) ;
671- if !body_size_allowed {
672- debug ! ( "chunked response exceeded max cache size, remembering that it is uncacheable" ) ;
673- session
674- . cache
675- . response_became_uncacheable ( NoCacheReason :: ResponseTooLarge ) ;
676-
677- return Error :: e_explain (
678- ERR_RESPONSE_TOO_LARGE ,
679- format ! (
680- "writing data of size {} bytes would exceed max file size of {} bytes" ,
681- d. len( ) ,
682- session. cache. max_file_size_bytes( ) . expect( "max file size bytes must be set to exceed size" )
683- ) ,
684- ) ;
685- }
664+ HttpTask :: Body ( data, end_stream) | HttpTask :: UpgradedBody ( data, end_stream) => {
665+ // It is not normally advisable to cache upgraded responses
666+ // e.g. they are essentially close-delimited, so they are easily truncated
667+ // but the framework still allows for it
668+ match data {
669+ Some ( d) => {
670+ if session. cache . enabled ( ) {
671+ // TODO: do this async
672+ // fail if writing the body would exceed the max_file_size_bytes
673+ let body_size_allowed =
674+ session. cache . track_body_bytes_for_max_file_size ( d. len ( ) ) ;
675+ if !body_size_allowed {
676+ debug ! ( "chunked response exceeded max cache size, remembering that it is uncacheable" ) ;
677+ session
678+ . cache
679+ . response_became_uncacheable ( NoCacheReason :: ResponseTooLarge ) ;
680+
681+ return Error :: e_explain (
682+ ERR_RESPONSE_TOO_LARGE ,
683+ format ! (
684+ "writing data of size {} bytes would exceed max file size of {} bytes" ,
685+ d. len( ) ,
686+ session. cache. max_file_size_bytes( ) . expect( "max file size bytes must be set to exceed size" )
687+ ) ,
688+ ) ;
689+ }
686690
687- // this will panic if more data is sent after we see end_stream
688- // but should be impossible in real world
689- let miss_handler = session. cache . miss_handler ( ) . unwrap ( ) ;
691+ // this will panic if more data is sent after we see end_stream
692+ // but should be impossible in real world
693+ let miss_handler = session. cache . miss_handler ( ) . unwrap ( ) ;
690694
691- miss_handler. write_body ( d. clone ( ) , * end_stream) . await ?;
692- if * end_stream {
693- session. cache . finish_miss_handler ( ) . await ?;
695+ miss_handler. write_body ( d. clone ( ) , * end_stream) . await ?;
696+ if * end_stream {
697+ session. cache . finish_miss_handler ( ) . await ?;
698+ }
694699 }
695700 }
696- }
697- None => {
698- if session. cache . enabled ( ) && * end_stream {
699- session . cache . finish_miss_handler ( ) . await ? ;
701+ None => {
702+ if session . cache . enabled ( ) && * end_stream {
703+ session. cache . finish_miss_handler ( ) . await ? ;
704+ }
700705 }
701706 }
702- } ,
703- HttpTask :: UpgradedBody ( ..) => {
704- // caching upgraded bodies isn't supported with and doesn't make sense with the HttpCache
705- // (caller of cache http task will disable cache in the session)
706- return Error :: e_explain (
707- InternalError ,
708- "Unexpected UpgradedBody task while caching" ,
709- ) ;
710707 }
711708 HttpTask :: Trailer ( _) => { } // h1 trailer is not supported yet
712709 HttpTask :: Done => {
@@ -2285,7 +2282,16 @@ impl ServeFromCache {
22852282 & mut self ,
22862283 cache : & mut HttpCache ,
22872284 range : & mut RangeBodyFilter ,
2285+ upgraded : bool ,
22882286 ) -> Result < HttpTask > {
2287+ fn body_task ( data : Bytes , upgraded : bool ) -> HttpTask {
2288+ if upgraded {
2289+ HttpTask :: UpgradedBody ( Some ( data) , false )
2290+ } else {
2291+ HttpTask :: Body ( Some ( data) , false )
2292+ }
2293+ }
2294+
22892295 if !cache. enabled ( ) {
22902296 // Cache is disabled due to internal error
22912297 // TODO: if nothing is sent to eyeball yet, figure out a way to recovery by
@@ -2317,7 +2323,7 @@ impl ServeFromCache {
23172323 }
23182324 loop {
23192325 if let Some ( b) = cache. hit_handler ( ) . read_body ( ) . await ? {
2320- return Ok ( HttpTask :: Body ( Some ( b ) , false ) ) ; // false for now
2326+ return Ok ( body_task ( b , upgraded ) ) ;
23212327 }
23222328 // EOF from hit handler for body requested
23232329 // if multipart, then seek again
@@ -2336,7 +2342,7 @@ impl ServeFromCache {
23362342 // safety: caller of enable_miss() call it only if the async_body_reader exist
23372343 loop {
23382344 if let Some ( b) = cache. miss_body_reader ( ) . unwrap ( ) . read_body ( ) . await ? {
2339- return Ok ( HttpTask :: Body ( Some ( b ) , false ) ) ; // false for now
2345+ return Ok ( body_task ( b , upgraded ) ) ;
23402346 } else {
23412347 // EOF from hit handler for body requested
23422348 // if multipart, then seek again
0 commit comments