- | Enc_normal, Enc_normal | Enc_gzipped, Enc_gzipped -> ()
- | Enc_normal, Enc_gzipped -> gunzip ~keep:true (basename ^ ".gz")
- | Enc_gzipped, Enc_normal -> gzip ~keep:true basename
- end; (* now resource is in cache *)
- (* invariant: file to be sent back to client is available on disk in the
- format the client likes *)
- (match enc with (* send file to client *)
- | Enc_normal ->
- return_file ~fname:basename ~contype:"text/xml" ~patch_fun outchan
- | Enc_gzipped ->
- return_file
- ~fname:(basename ^ ".gz") ~contype:"text/xml" ~contenc:"x-gzip"
- ~patch_fun outchan);
- match (enc, Http_getter_env.cache_mode) with (* remove temp files *)
- | Enc_normal, Enc_normal | Enc_gzipped, Enc_gzipped -> ()
- | Enc_normal, Enc_gzipped -> Sys.remove basename
- | Enc_gzipped, Enc_normal -> Sys.remove (basename ^ ".gz")
+ | Enc_normal, Enc_normal | Enc_gzipped, Enc_gzipped ->
+ (* resource in cache is already in the required format *)
+ (match enc with
+ | Enc_normal ->
+ debug_print "No format mangling required (encoding = normal)";
+ return_file ~fname:basename ~contype ~patch_fun outchan
+ | Enc_gzipped ->
+ debug_print "No format mangling required (encoding = gzipped)";
+ return_file
+ ~fname:(basename ^ ".gz") ~contype ~contenc:"x-gzip"
+ ~patch_fun ~gunzip:true
+ outchan)
+ | Enc_normal, Enc_gzipped | Enc_gzipped, Enc_normal ->
+ (match tmp_short_circuit with
+ | None -> (* no short circuit possible, use cache *)
+ debug_print "No short circuit available, use cache";
+ let tmp = tempfile () in
+ (match enc with
+ | Enc_normal ->
+ (* required format is normal, cached version is gzipped *)
+ gunzip (* gunzip to tmp *)
+ ~output:tmp ~keep:true (basename ^ ".gz");
+ return_file ~fname:tmp ~contype ~patch_fun outchan;
+ | Enc_gzipped ->
+ (* required format is gzipped, cached version is normal *)
+ gzip ~output:tmp ~keep:true basename; (* gzip to tmp *)
+ return_file
+ ~fname:tmp ~contype ~contenc:"x-gzip"
+ ~patch_fun ~gunzip:true
+ outchan);
+ Sys.remove tmp
+ | Some (fname, Enc_normal) -> (* short circuit available, use it! *)
+ debug_print "Using short circuit (encoding = normal)";
+ return_file ~fname ~contype ~patch_fun outchan;
+ Sys.remove fname
+ | Some (fname, Enc_gzipped) -> (* short circuit available, use it! *)
+ debug_print "Using short circuit (encoding = gzipped)";
+ return_file
+ ~fname ~contype ~contenc:"x-gzip" ~patch_fun ~gunzip:true outchan;
+ Sys.remove fname)
+ ))
+;;