Skip to content

Commit 286ee13

Browse files
committed
Updated "Transfer-Encoding" header when sending gzip responses via a 3rd party server (e.g. tomcat)
1 parent 4657df4 commit 286ee13

1 file changed

Lines changed: 35 additions & 23 deletions

File tree

src/javaxt/http/servlet/HttpServletResponse.java

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -602,16 +602,7 @@ public void write(byte[] bytes, boolean compressOutput) throws IOException {
602602

603603
}
604604
catch(ClassCastException e){ //special case for javaxt-express
605-
606-
try (OutputStream out = response.getOutputStream()){
607-
try(GZIPOutputStream gz = new GZIPOutputStream(out, bufferSize)){
608-
int x;
609-
while ( (x = inputStream.read(b)) != -1) {
610-
gz.write(b,0,x);
611-
}
612-
}
613-
}
614-
605+
gzipFallback(inputStream, b);
615606
}
616607
finally{
617608
inputStream.close();
@@ -810,19 +801,7 @@ public void write(java.io.File file, String fileName, String contentType, boolea
810801

811802
}
812803
catch(ClassCastException e){ //special case for javaxt-express
813-
814-
try (OutputStream out = response.getOutputStream()){
815-
try(GZIPOutputStream gz = new GZIPOutputStream(out, bufferSize)){
816-
817-
int x;
818-
while ( (x = inputStream.read(b)) != -1) {
819-
gz.write(b,0,x);
820-
}
821-
822-
gz.finish();
823-
}
824-
}
825-
804+
gzipFallback(inputStream, b);
826805
}
827806
finally{
828807
inputStream.close();
@@ -856,6 +835,39 @@ public void write(java.io.File file, String fileName, String contentType, boolea
856835
}
857836
}
858837

838+
839+
//**************************************************************************
840+
//** gzipFallback
841+
//**************************************************************************
842+
/** Fallback for non-Jetty servers. Compress and send file using the
843+
* abstracted servlet output stream provided by the underlying server.
844+
* This is method is intended to support javaxt-express when deployed in
845+
* another server (e.g. Tomcat).
846+
*/
847+
private void gzipFallback(java.io.InputStream inputStream, byte[] b)
848+
throws IOException {
849+
850+
851+
//Update "Transfer-Encoding". Some servers send invalid responses when
852+
//"Transfer-Encoding" is set to "chunked" (e.g. spring boot / tomcat)
853+
setHeader("Transfer-Encoding", "gzip");
854+
855+
856+
//Compress and write data to the servlet output stream
857+
try (OutputStream out = response.getOutputStream()){
858+
try(GZIPOutputStream gz = new GZIPOutputStream(out, bufferSize)){
859+
860+
int x;
861+
while ( (x = inputStream.read(b)) != -1) {
862+
gz.write(b,0,x);
863+
}
864+
865+
gz.finish();
866+
}
867+
}
868+
}
869+
870+
859871
private java.util.concurrent.ConcurrentHashMap<String, ByteBuffer> cache = new java.util.concurrent.ConcurrentHashMap<>();
860872

861873

0 commit comments

Comments
 (0)