From ecec5902ff572c86e09ffb6d0c506fd1acd2dffa Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Wed, 23 May 2012 20:08:57 -0700 Subject: [PATCH] leaving the record of attempted gzip encoding problem fix. --- .../HudsonAuthenticationEntryPoint.java | 46 ++++++++++++++++--- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/hudson/security/HudsonAuthenticationEntryPoint.java b/core/src/main/java/hudson/security/HudsonAuthenticationEntryPoint.java index d74eac7b8f..453d8ea8ec 100644 --- a/core/src/main/java/hudson/security/HudsonAuthenticationEntryPoint.java +++ b/core/src/main/java/hudson/security/HudsonAuthenticationEntryPoint.java @@ -33,6 +33,8 @@ import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN; + +import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; @@ -81,12 +83,14 @@ public class HudsonAuthenticationEntryPoint extends AuthenticationProcessingFilt PrintWriter out; try { OutputStream sout = rsp.getOutputStream(); - if (rsp.containsHeader("Content-Encoding")) { - // we serve Jelly pages with Content-Encoding:gzip. - // ServletResponse doesn't provide means for us to check the value of the header, - // so this is a hack. - sout = new GZIPOutputStream(sout); - } +// if (rsp.containsHeader("Content-Encoding")) { +// // we serve Jelly pages with Content-Encoding:gzip. +// // ServletResponse doesn't provide means for us to check the value of the header, +// // to make the matter worse, GZIPOutputStream writes a header right away, +// // so by the time we get here we already have GZip header. Skip that part away by skipping first 10 bytes. +// // this is a hack. +// sout = new GZIPOutputStream(new ChopHeaderOutputStream(sout,10)); +// } out = new PrintWriter(new OutputStreamWriter(sout)); } catch (IllegalStateException e) { out = rsp.getWriter(); @@ -103,7 +107,35 @@ public class HudsonAuthenticationEntryPoint extends AuthenticationProcessingFilt // See http://support.microsoft.com/kb/294807 for (int i=0; i < 10; i++) out.print(" "); - out.flush(); + out.close(); + } + } + + private static class ChopHeaderOutputStream extends FilterOutputStream { + private int count; + + ChopHeaderOutputStream(OutputStream out, int count) { + super(out); + this.count = count; + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + int i = Math.min(len,count); + count-=i; + off+=i; + len-=i; + + if (len>0) + out.write(b, off, len); + } + + @Override + public void write(int b) throws IOException { + if (count>0) + count--; + else + out.write(b); } } }