diff options
Diffstat (limited to 'jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip')
8 files changed, 0 insertions, 1932 deletions
diff --git a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/AbstractCompressedStream.java b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/AbstractCompressedStream.java deleted file mode 100644 index 3110285a2a..0000000000 --- a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/AbstractCompressedStream.java +++ /dev/null @@ -1,404 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.servlets.gzip; - -import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; - -import javax.servlet.ServletOutputStream; -import javax.servlet.WriteListener; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.eclipse.jetty.server.Response; -import org.eclipse.jetty.util.ByteArrayOutputStream2; - -/* ------------------------------------------------------------ */ -/** - * Skeletal implementation of a CompressedStream. This class adds compression features to a ServletOutputStream and takes care of setting response headers, etc. - * Major work and configuration is done here. Subclasses using different kinds of compression only have to implement the abstract methods doCompress() and - * setContentEncoding() using the desired compression and setting the appropriate Content-Encoding header string. - */ -public abstract class AbstractCompressedStream extends ServletOutputStream -{ - private final String _encoding; - protected final String _vary; - protected final CompressedResponseWrapper _wrapper; - protected final HttpServletResponse _response; - protected OutputStream _out; - protected ByteArrayOutputStream2 _bOut; - protected OutputStream _compressedOutputStream; - protected boolean _closed; - protected boolean _doNotCompress; - - /** - * Instantiates a new compressed stream. - * - */ - public AbstractCompressedStream(String encoding,HttpServletRequest request, CompressedResponseWrapper wrapper,String vary) - throws IOException - { - _encoding=encoding; - _wrapper = wrapper; - _response = (HttpServletResponse)wrapper.getResponse(); - _vary=vary; - - if (_wrapper.getMinCompressSize()==0) - doCompress(); - } - - /* ------------------------------------------------------------ */ - /** - * Reset buffer. - */ - public void resetBuffer() - { - if (_response.isCommitted() || _compressedOutputStream!=null ) - throw new IllegalStateException("Committed"); - _closed = false; - _out = null; - _bOut = null; - _doNotCompress = false; - } - - /* ------------------------------------------------------------ */ - public void setBufferSize(int bufferSize) - { - if (_bOut!=null && _bOut.getBuf().length<bufferSize) - { - ByteArrayOutputStream2 b = new ByteArrayOutputStream2(bufferSize); - b.write(_bOut.getBuf(),0,_bOut.size()); - _bOut=b; - } - } - - /* ------------------------------------------------------------ */ - public void setContentLength() - { - if (_doNotCompress) - { - long length=_wrapper.getContentLength(); - if (length>=0) - { - if (length < Integer.MAX_VALUE) - _response.setContentLength((int)length); - else - _response.setHeader("Content-Length",Long.toString(length)); - } - } - } - - /* ------------------------------------------------------------ */ - /** - * @see java.io.OutputStream#flush() - */ - @Override - public void flush() throws IOException - { - if (_out == null || _bOut != null) - { - long length=_wrapper.getContentLength(); - if (length > 0 && length < _wrapper.getMinCompressSize()) - doNotCompress(false); - else - doCompress(); - } - - _out.flush(); - } - - /* ------------------------------------------------------------ */ - /** - * @see java.io.OutputStream#close() - */ - @Override - public void close() throws IOException - { - if (_closed) - return; - - if (_wrapper.getRequest().getAttribute("javax.servlet.include.request_uri") != null) - flush(); - else - { - if (_bOut != null) - { - long length=_wrapper.getContentLength(); - if (length < 0) - { - length = _bOut.getCount(); - _wrapper.setContentLength(length); - } - if (length < _wrapper.getMinCompressSize()) - doNotCompress(false); - else - doCompress(); - } - else if (_out == null) - { - // No output - doNotCompress(false); - } - - if (_compressedOutputStream != null) - _compressedOutputStream.close(); - else - _out.close(); - _closed = true; - } - } - - /** - * Finish. - * - * @throws IOException - * Signals that an I/O exception has occurred. - */ - public void finish() throws IOException - { - if (!_closed) - { - if (_out == null || _bOut != null) - { - long length=_wrapper.getContentLength(); - if (length<0 &&_bOut==null || length >= 0 && length < _wrapper.getMinCompressSize()) - doNotCompress(false); - else - doCompress(); - } - - if (_compressedOutputStream != null && !_closed) - { - _closed = true; - _compressedOutputStream.close(); - } - } - } - - /* ------------------------------------------------------------ */ - /** - * @see java.io.OutputStream#write(int) - */ - @Override - public void write(int b) throws IOException - { - checkOut(1); - _out.write(b); - } - - /* ------------------------------------------------------------ */ - /** - * @see java.io.OutputStream#write(byte[]) - */ - @Override - public void write(byte b[]) throws IOException - { - checkOut(b.length); - _out.write(b); - } - - /* ------------------------------------------------------------ */ - /** - * @see java.io.OutputStream#write(byte[], int, int) - */ - @Override - public void write(byte b[], int off, int len) throws IOException - { - checkOut(len); - _out.write(b,off,len); - } - - /** - * Do compress. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - public void doCompress() throws IOException - { - if (_compressedOutputStream==null) - { - if (_response.isCommitted()) - throw new IllegalStateException(); - - if (_encoding!=null) - { - String prefix=Response.getResponse(_response).isIncluding()?Response.SET_INCLUDE_HEADER_PREFIX:""; - - setHeader(prefix+"Content-Encoding", _encoding); - if (_response.containsHeader("Content-Encoding")) - { - addHeader(prefix+"Vary",_vary); - _out=_compressedOutputStream=createStream(); - if (_out!=null) - { - if (_bOut!=null) - { - _out.write(_bOut.getBuf(),0,_bOut.getCount()); - _bOut=null; - } - - String etag=_wrapper.getETag(); - if (etag!=null) - { - int end = etag.length()-1; - if (etag.charAt(end)=='"') - setHeader(prefix+"ETag",etag.substring(0,end)+"--"+_encoding+'"'); - else - setHeader(prefix+"ETag",etag+"--"+_encoding); - } - return; - } - } - } - - doNotCompress(true); // Send vary as it could have been compressed if encoding was present - } - } - - /** - * Do not compress. - * - * @throws IOException - * Signals that an I/O exception has occurred. - */ - public void doNotCompress(boolean sendVary) throws IOException - { - if (_compressedOutputStream != null) - throw new IllegalStateException("Compressed output stream is already assigned."); - if (_out == null || _bOut != null) - { - if (sendVary) - addHeader("Vary",_vary); - if (_wrapper.getETag()!=null) - setHeader("ETag",_wrapper.getETag()); - - _doNotCompress = true; - - _out = _response.getOutputStream(); - setContentLength(); - - if (_bOut != null) - _out.write(_bOut.getBuf(),0,_bOut.getCount()); - _bOut = null; - } - } - - /** - * Check out. - * - * @param lengthToWrite - * the length - * @throws IOException - * Signals that an I/O exception has occurred. - */ - private void checkOut(int lengthToWrite) throws IOException - { - if (_closed) - throw new IOException("CLOSED"); - - if (_out == null) - { - // If this first write is larger than buffer size, then we are committing now - if (lengthToWrite>_wrapper.getBufferSize()) - { - // if we know this is all the content and it is less than minimum, then do not compress, otherwise do compress - long length=_wrapper.getContentLength(); - if (length>=0 && length<_wrapper.getMinCompressSize()) - doNotCompress(false); // Not compressing by size, so no vary on request headers - else - doCompress(); - } - else - { - // start aggregating writes into a buffered output stream - _out = _bOut = new ByteArrayOutputStream2(_wrapper.getBufferSize()); - } - } - // else are we aggregating writes? - else if (_bOut !=null) - { - // We are aggregating into the buffered output stream. - - // If this write fills the buffer, then we are committing - if (lengthToWrite>=(_bOut.getBuf().length - _bOut.getCount())) - { - // if we know this is all the content and it is less than minimum, then do not compress, otherwise do compress - long length=_wrapper.getContentLength(); - if (length>=0 && length<_wrapper.getMinCompressSize()) - doNotCompress(false); // Not compressing by size, so no vary on request headers - else - doCompress(); - } - } - } - - public OutputStream getOutputStream() - { - return _out; - } - - public boolean isClosed() - { - return _closed; - } - - /** - * Allows derived implementations to replace PrintWriter implementation. - */ - protected PrintWriter newWriter(OutputStream out, String encoding) throws UnsupportedEncodingException - { - return encoding == null?new PrintWriter(out):new PrintWriter(new OutputStreamWriter(out,encoding)); - } - - protected void addHeader(String name,String value) - { - _response.addHeader(name, value); - } - - protected void setHeader(String name,String value) - { - _response.setHeader(name, value); - } - - @Override - public void setWriteListener(WriteListener writeListener) - { - throw new UnsupportedOperationException("Use AsyncGzipFilter"); - } - - - @Override - public boolean isReady() - { - throw new UnsupportedOperationException("Use AsyncGzipFilter"); - } - - /** - * Create the stream fitting to the underlying compression type. - * - * @throws IOException - * Signals that an I/O exception has occurred. - */ - protected abstract OutputStream createStream() throws IOException; - - -} diff --git a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/CompressedResponseWrapper.java b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/CompressedResponseWrapper.java deleted file mode 100644 index b9ad084495..0000000000 --- a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/CompressedResponseWrapper.java +++ /dev/null @@ -1,485 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.servlets.gzip; - -import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; -import java.util.Set; - -import javax.servlet.ServletOutputStream; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpServletResponseWrapper; - -import org.eclipse.jetty.util.StringUtil; - -/*------------------------------------------------------------ */ -/** - */ -public abstract class CompressedResponseWrapper extends HttpServletResponseWrapper -{ - - public static final int DEFAULT_BUFFER_SIZE = 8192; - public static final int DEFAULT_MIN_COMPRESS_SIZE = 256; - - private Set<String> _mimeTypes; - private boolean _excludeMimeTypes; - private int _bufferSize=DEFAULT_BUFFER_SIZE; - private int _minCompressSize=DEFAULT_MIN_COMPRESS_SIZE; - protected HttpServletRequest _request; - - private PrintWriter _writer; - private AbstractCompressedStream _compressedStream; - private String _etag; - private long _contentLength=-1; - private boolean _noCompression; - - /* ------------------------------------------------------------ */ - public CompressedResponseWrapper(HttpServletRequest request, HttpServletResponse response) - { - super(response); - _request = request; - } - - - /* ------------------------------------------------------------ */ - public long getContentLength() - { - return _contentLength; - } - - /* ------------------------------------------------------------ */ - @Override - public int getBufferSize() - { - return _bufferSize; - } - - /* ------------------------------------------------------------ */ - public int getMinCompressSize() - { - return _minCompressSize; - } - - /* ------------------------------------------------------------ */ - public String getETag() - { - return _etag; - } - - /* ------------------------------------------------------------ */ - public HttpServletRequest getRequest() - { - return _request; - } - - /* ------------------------------------------------------------ */ - /** - */ - public void setMimeTypes(Set<String> mimeTypes,boolean excludeMimeTypes) - { - _excludeMimeTypes=excludeMimeTypes; - _mimeTypes = mimeTypes; - } - - /* ------------------------------------------------------------ */ - /** - */ - @Override - public void setBufferSize(int bufferSize) - { - _bufferSize = bufferSize; - if (_compressedStream!=null) - _compressedStream.setBufferSize(bufferSize); - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper#setMinCompressSize(int) - */ - public void setMinCompressSize(int minCompressSize) - { - _minCompressSize = minCompressSize; - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper#setContentType(java.lang.String) - */ - @Override - public void setContentType(String ct) - { - super.setContentType(ct); - - if (!_noCompression && (_compressedStream==null || _compressedStream.getOutputStream()==null)) - { - if (ct!=null) - { - int colon=ct.indexOf(";"); - if (colon>0) - ct=ct.substring(0,colon); - - if (_mimeTypes.contains(StringUtil.asciiToLowerCase(ct))==_excludeMimeTypes) - noCompression(); - } - } - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper#setStatus(int, java.lang.String) - */ - @SuppressWarnings("deprecation") - @Override - public void setStatus(int sc, String sm) - { - super.setStatus(sc,sm); - if (sc<200 || sc==204 || sc==205 || sc>=300) - noCompression(); - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper#setStatus(int) - */ - @Override - public void setStatus(int sc) - { - super.setStatus(sc); - if (sc<200 || sc==204 || sc==205 || sc>=300) - noCompression(); - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper#setContentLength(int) - */ - @Override - public void setContentLength(int length) - { - if (_noCompression) - super.setContentLength(length); - else - setContentLength((long)length); - } - - /* ------------------------------------------------------------ */ - protected void setContentLength(long length) - { - _contentLength=length; - if (_compressedStream!=null) - _compressedStream.setContentLength(); - else if (_noCompression && _contentLength>=0) - { - HttpServletResponse response = (HttpServletResponse)getResponse(); - if(_contentLength<Integer.MAX_VALUE) - { - response.setContentLength((int)_contentLength); - } - else - { - response.setHeader("Content-Length", Long.toString(_contentLength)); - } - } - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper#addHeader(java.lang.String, java.lang.String) - */ - @Override - public void addHeader(String name, String value) - { - if ("content-length".equalsIgnoreCase(name)) - { - _contentLength=Long.parseLong(value); - if (_compressedStream!=null) - _compressedStream.setContentLength(); - } - else if ("content-type".equalsIgnoreCase(name)) - { - setContentType(value); - } - else if ("content-encoding".equalsIgnoreCase(name)) - { - super.addHeader(name,value); - if (!isCommitted()) - { - noCompression(); - } - } - else if ("etag".equalsIgnoreCase(name)) - _etag=value; - else - super.addHeader(name,value); - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper#flushBuffer() - */ - @Override - public void flushBuffer() throws IOException - { - if (_writer!=null) - _writer.flush(); - if (_compressedStream!=null) - _compressedStream.flush(); - else - getResponse().flushBuffer(); - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper#reset() - */ - @Override - public void reset() - { - super.reset(); - if (_compressedStream!=null) - _compressedStream.resetBuffer(); - _writer=null; - _compressedStream=null; - _noCompression=false; - _contentLength=-1; - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper#resetBuffer() - */ - @Override - public void resetBuffer() - { - super.resetBuffer(); - if (_compressedStream!=null) - _compressedStream.resetBuffer(); - _writer=null; - _compressedStream=null; - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper#sendError(int, java.lang.String) - */ - @Override - public void sendError(int sc, String msg) throws IOException - { - resetBuffer(); - super.sendError(sc,msg); - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper#sendError(int) - */ - @Override - public void sendError(int sc) throws IOException - { - resetBuffer(); - super.sendError(sc); - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper#sendRedirect(java.lang.String) - */ - @Override - public void sendRedirect(String location) throws IOException - { - resetBuffer(); - super.sendRedirect(location); - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper#noCompression() - */ - public void noCompression() - { - if (!_noCompression) - setDeferredHeaders(); - _noCompression=true; - if (_compressedStream!=null) - { - try - { - _compressedStream.doNotCompress(false); - } - catch (IOException e) - { - throw new IllegalStateException(e); - } - } - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper#finish() - */ - public void finish() throws IOException - { - if (_writer!=null && !_compressedStream.isClosed()) - _writer.flush(); - if (_compressedStream!=null) - _compressedStream.finish(); - else - setDeferredHeaders(); - } - - /* ------------------------------------------------------------ */ - private void setDeferredHeaders() - { - if (!isCommitted()) - { - if (_contentLength>=0) - { - if (_contentLength < Integer.MAX_VALUE) - super.setContentLength((int)_contentLength); - else - super.setHeader("Content-Length",Long.toString(_contentLength)); - } - if(_etag!=null) - super.setHeader("ETag",_etag); - } - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper#setHeader(java.lang.String, java.lang.String) - */ - @Override - public void setHeader(String name, String value) - { - if (_noCompression) - super.setHeader(name,value); - else if ("content-length".equalsIgnoreCase(name)) - { - setContentLength(Long.parseLong(value)); - } - else if ("content-type".equalsIgnoreCase(name)) - { - setContentType(value); - } - else if ("content-encoding".equalsIgnoreCase(name)) - { - super.setHeader(name,value); - if (!isCommitted()) - { - noCompression(); - } - } - else if ("etag".equalsIgnoreCase(name)) - _etag=value; - else - super.setHeader(name,value); - } - - /* ------------------------------------------------------------ */ - @Override - public boolean containsHeader(String name) - { - if (!_noCompression && "etag".equalsIgnoreCase(name) && _etag!=null) - return true; - return super.containsHeader(name); - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper#getOutputStream() - */ - @Override - public ServletOutputStream getOutputStream() throws IOException - { - if (_compressedStream==null) - { - if (getResponse().isCommitted() || _noCompression) - return getResponse().getOutputStream(); - - _compressedStream=newCompressedStream(_request,(HttpServletResponse)getResponse()); - } - else if (_writer!=null) - throw new IllegalStateException("getWriter() called"); - - return _compressedStream; - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper#getWriter() - */ - @Override - public PrintWriter getWriter() throws IOException - { - if (_writer==null) - { - if (_compressedStream!=null) - throw new IllegalStateException("getOutputStream() called"); - - if (getResponse().isCommitted() || _noCompression) - return getResponse().getWriter(); - - _compressedStream=newCompressedStream(_request,(HttpServletResponse)getResponse()); - _writer=newWriter(_compressedStream,getCharacterEncoding()); - } - return _writer; - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper#setIntHeader(java.lang.String, int) - */ - @Override - public void setIntHeader(String name, int value) - { - if ("content-length".equalsIgnoreCase(name)) - { - _contentLength=value; - if (_compressedStream!=null) - _compressedStream.setContentLength(); - } - else - super.setIntHeader(name,value); - } - - /* ------------------------------------------------------------ */ - /** - * Allows derived implementations to replace PrintWriter implementation. - * - * @param out the out - * @param encoding the encoding - * @return the prints the writer - * @throws UnsupportedEncodingException the unsupported encoding exception - */ - protected PrintWriter newWriter(OutputStream out,String encoding) throws UnsupportedEncodingException - { - return encoding==null?new PrintWriter(out):new PrintWriter(new OutputStreamWriter(out,encoding)); - } - - /* ------------------------------------------------------------ */ - /** - *@return the underlying CompressedStream implementation - */ - protected abstract AbstractCompressedStream newCompressedStream(HttpServletRequest _request, HttpServletResponse response) throws IOException; - -} diff --git a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/DeflatedOutputStream.java b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/DeflatedOutputStream.java deleted file mode 100644 index bcc05a02b2..0000000000 --- a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/DeflatedOutputStream.java +++ /dev/null @@ -1,101 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.servlets.gzip; - -import java.io.FilterOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.util.zip.Deflater; - -/** - * Reimplementation of {@link java.util.zip.DeflaterOutputStream} that supports reusing the buffer. - */ -public class DeflatedOutputStream extends FilterOutputStream -{ - protected final Deflater _def; - protected final byte[] _buf; - protected boolean closed = false; - - public DeflatedOutputStream(OutputStream out, Deflater deflater, byte[] buffer) - { - super(out); - _def = deflater; - _buf = buffer; - } - - @Override - public void write(int b) throws IOException - { - byte[] buf = new byte[1]; - buf[0] = (byte)(b & 0xff); - write(buf,0,1); - } - - @Override - public void write(byte[] b, int off, int len) throws IOException - { - if (_def.finished()) - throw new IOException("Stream already finished"); - if ((off | len | (off + len) | (b.length - (off + len))) < 0) - throw new IndexOutOfBoundsException(); - if (len == 0) - return; - if (!_def.finished()) - { - _def.setInput(b,off,len); - while (!_def.needsInput()) - { - deflate(); - } - } - } - - private void deflate() throws IOException - { - int len = _def.deflate(_buf,0,_buf.length); - if (len > 0) - { - out.write(_buf,0,len); - } - } - - public synchronized void finish() throws IOException - { - if (!_def.finished()) - { - _def.finish(); - while (!_def.finished()) - { - deflate(); - } - } - } - - @Override - public synchronized void close() throws IOException - { - if (!closed) - { - finish(); - out.close(); - closed = true; - } - } - -} diff --git a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/GzipFactory.java b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/GzipFactory.java deleted file mode 100644 index 33daa313c9..0000000000 --- a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/GzipFactory.java +++ /dev/null @@ -1,38 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.servlets.gzip; - -import java.util.zip.Deflater; - -import org.eclipse.jetty.http.HttpField; -import org.eclipse.jetty.server.Request; - -public interface GzipFactory -{ - int getBufferSize(); - - HttpField getVaryField(); - - Deflater getDeflater(Request request, long content_length); - - boolean isExcludedMimeType(String asciiToLowerCase); - - void recycle(Deflater deflater); - -} diff --git a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/GzipHandler.java b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/GzipHandler.java deleted file mode 100644 index c9976e4795..0000000000 --- a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/GzipHandler.java +++ /dev/null @@ -1,404 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.servlets.gzip; - -import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; -import java.util.HashSet; -import java.util.Set; -import java.util.StringTokenizer; -import java.util.zip.DeflaterOutputStream; -import java.util.zip.GZIPOutputStream; - -import javax.servlet.AsyncEvent; -import javax.servlet.AsyncListener; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.eclipse.jetty.http.HttpMethod; -import org.eclipse.jetty.http.MimeTypes; -import org.eclipse.jetty.server.Request; -import org.eclipse.jetty.server.handler.HandlerWrapper; -import org.eclipse.jetty.util.log.Log; -import org.eclipse.jetty.util.log.Logger; - -/* ------------------------------------------------------------ */ -/** - * GZIP Handler This handler will gzip the content of a response if: - * <ul> - * <li>The handler is mapped to a matching path</li> - * <li>The response status code is >=200 and <300 - * <li>The content length is unknown or more than the <code>minGzipSize</code> initParameter or the minGzipSize is 0(default)</li> - * <li>The content-type matches one of the set of mimetypes to be compressed</li> - * <li>The content-type does NOT match one of the set of mimetypes AND setExcludeMimeTypes is <code>true</code></li> - * <li>No content-encoding is specified by the resource</li> - * </ul> - * - * <p> - * Compressing the content can greatly improve the network bandwidth usage, but at a cost of memory and CPU cycles. If this handler is used for static content, - * then use of efficient direct NIO may be prevented, thus use of the gzip mechanism of the <code>org.eclipse.jetty.servlet.DefaultServlet</code> is advised instead. - * </p> - */ -public class GzipHandler extends HandlerWrapper -{ - private static final Logger LOG = Log.getLogger(GzipHandler.class); - - final protected Set<String> _mimeTypes=new HashSet<>(); - protected boolean _excludeMimeTypes=false; - protected Set<String> _excludedUA; - protected int _bufferSize = 8192; - protected int _minGzipSize = 256; - protected String _vary = "Accept-Encoding, User-Agent"; - - /* ------------------------------------------------------------ */ - /** - * Instantiates a new gzip handler. - */ - public GzipHandler() - { - } - - /* ------------------------------------------------------------ */ - /** - * Get the mime types. - * - * @return mime types to set - */ - public Set<String> getMimeTypes() - { - return _mimeTypes; - } - - /* ------------------------------------------------------------ */ - /** - * Set the mime types. - * - * @param mimeTypes - * the mime types to set - */ - public void setMimeTypes(Set<String> mimeTypes) - { - _excludeMimeTypes=false; - _mimeTypes.clear(); - _mimeTypes.addAll(mimeTypes); - } - - /* ------------------------------------------------------------ */ - /** - * Set the mime types. - * - * @param mimeTypes - * the mime types to set - */ - public void setMimeTypes(String mimeTypes) - { - if (mimeTypes != null) - { - _excludeMimeTypes=false; - _mimeTypes.clear(); - StringTokenizer tok = new StringTokenizer(mimeTypes,",",false); - while (tok.hasMoreTokens()) - { - _mimeTypes.add(tok.nextToken()); - } - } - } - - /* ------------------------------------------------------------ */ - /** - * Set the mime types. - */ - public void setExcludeMimeTypes(boolean exclude) - { - _excludeMimeTypes=exclude; - } - - /* ------------------------------------------------------------ */ - /** - * Get the excluded user agents. - * - * @return excluded user agents - */ - public Set<String> getExcluded() - { - return _excludedUA; - } - - /* ------------------------------------------------------------ */ - /** - * Set the excluded user agents. - * - * @param excluded - * excluded user agents to set - */ - public void setExcluded(Set<String> excluded) - { - _excludedUA = excluded; - } - - /* ------------------------------------------------------------ */ - /** - * Set the excluded user agents. - * - * @param excluded - * excluded user agents to set - */ - public void setExcluded(String excluded) - { - if (excluded != null) - { - _excludedUA = new HashSet<String>(); - StringTokenizer tok = new StringTokenizer(excluded,",",false); - while (tok.hasMoreTokens()) - _excludedUA.add(tok.nextToken()); - } - } - - /* ------------------------------------------------------------ */ - /** - * @return The value of the Vary header set if a response can be compressed. - */ - public String getVary() - { - return _vary; - } - - /* ------------------------------------------------------------ */ - /** - * Set the value of the Vary header sent with responses that could be compressed. - * <p> - * By default it is set to 'Accept-Encoding, User-Agent' since IE6 is excluded by - * default from the excludedAgents. If user-agents are not to be excluded, then - * this can be set to 'Accept-Encoding'. Note also that shared caches may cache - * many copies of a resource that is varied by User-Agent - one per variation of the - * User-Agent, unless the cache does some normalization of the UA string. - * @param vary The value of the Vary header set if a response can be compressed. - */ - public void setVary(String vary) - { - _vary = vary; - } - - /* ------------------------------------------------------------ */ - /** - * Get the buffer size. - * - * @return the buffer size - */ - public int getBufferSize() - { - return _bufferSize; - } - - /* ------------------------------------------------------------ */ - /** - * Set the buffer size. - * - * @param bufferSize - * buffer size to set - */ - public void setBufferSize(int bufferSize) - { - _bufferSize = bufferSize; - } - - /* ------------------------------------------------------------ */ - /** - * Get the minimum reponse size. - * - * @return minimum reponse size - */ - public int getMinGzipSize() - { - return _minGzipSize; - } - - /* ------------------------------------------------------------ */ - /** - * Set the minimum reponse size. - * - * @param minGzipSize - * minimum reponse size - */ - public void setMinGzipSize(int minGzipSize) - { - _minGzipSize = minGzipSize; - } - - /* ------------------------------------------------------------ */ - @Override - protected void doStart() throws Exception - { - if (_mimeTypes.size()==0 && _excludeMimeTypes) - { - _excludeMimeTypes = true; - for (String type:MimeTypes.getKnownMimeTypes()) - { - if (type.equals("image/svg+xml")) //always compressable (unless .svgz file) - continue; - if (type.startsWith("image/")|| - type.startsWith("audio/")|| - type.startsWith("video/")) - _mimeTypes.add(type); - } - _mimeTypes.add("application/compress"); - _mimeTypes.add("application/zip"); - _mimeTypes.add("application/gzip"); - } - super.doStart(); - } - - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.server.handler.HandlerWrapper#handle(java.lang.String, org.eclipse.jetty.server.Request, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) - */ - @Override - public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException - { - if (_handler!=null && isStarted()) - { - String ae = request.getHeader("accept-encoding"); - if (ae != null && ae.indexOf("gzip")>=0 && !response.containsHeader("Content-Encoding") - && !HttpMethod.HEAD.is(request.getMethod())) - { - if (_excludedUA!=null) - { - String ua = request.getHeader("User-Agent"); - if (_excludedUA.contains(ua)) - { - _handler.handle(target,baseRequest, request, response); - return; - } - } - - final CompressedResponseWrapper wrappedResponse = newGzipResponseWrapper(request,response); - - boolean exceptional=true; - try - { - _handler.handle(target, baseRequest, request, wrappedResponse); - exceptional=false; - } - finally - { - if (request.isAsyncStarted()) - { - request.getAsyncContext().addListener(new AsyncListener() - { - - @Override - public void onTimeout(AsyncEvent event) throws IOException - { - } - - @Override - public void onStartAsync(AsyncEvent event) throws IOException - { - } - - @Override - public void onError(AsyncEvent event) throws IOException - { - } - - @Override - public void onComplete(AsyncEvent event) throws IOException - { - try - { - wrappedResponse.finish(); - } - catch(IOException e) - { - LOG.warn(e); - } - } - }); - } - else if (exceptional && !response.isCommitted()) - { - wrappedResponse.resetBuffer(); - wrappedResponse.noCompression(); - } - else - wrappedResponse.finish(); - } - } - else - { - _handler.handle(target,baseRequest, request, response); - } - } - } - - /** - * Allows derived implementations to replace ResponseWrapper implementation. - * - * @param request the request - * @param response the response - * @return the gzip response wrapper - */ - protected CompressedResponseWrapper newGzipResponseWrapper(HttpServletRequest request, HttpServletResponse response) - { - return new CompressedResponseWrapper(request,response) - { - { - super.setMimeTypes(GzipHandler.this._mimeTypes,GzipHandler.this._excludeMimeTypes); - super.setBufferSize(GzipHandler.this._bufferSize); - super.setMinCompressSize(GzipHandler.this._minGzipSize); - } - - @Override - protected AbstractCompressedStream newCompressedStream(HttpServletRequest request,HttpServletResponse response) throws IOException - { - return new AbstractCompressedStream("gzip",request,this,_vary) - { - @Override - protected DeflaterOutputStream createStream() throws IOException - { - return new GZIPOutputStream(_response.getOutputStream(),_bufferSize); - } - }; - } - - @Override - protected PrintWriter newWriter(OutputStream out,String encoding) throws UnsupportedEncodingException - { - return GzipHandler.this.newWriter(out,encoding); - } - }; - } - - /** - * Allows derived implementations to replace PrintWriter implementation. - * - * @param out the out - * @param encoding the encoding - * @return the prints the writer - * @throws UnsupportedEncodingException - */ - protected PrintWriter newWriter(OutputStream out,String encoding) throws UnsupportedEncodingException - { - return encoding==null?new PrintWriter(out):new PrintWriter(new OutputStreamWriter(out,encoding)); - } -} diff --git a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/GzipHttpOutput.java b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/GzipHttpOutput.java deleted file mode 100644 index c7ffcd2aba..0000000000 --- a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/GzipHttpOutput.java +++ /dev/null @@ -1,405 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.servlets.gzip; - -import java.nio.ByteBuffer; -import java.nio.channels.WritePendingException; -import java.util.concurrent.atomic.AtomicReference; -import java.util.zip.CRC32; -import java.util.zip.Deflater; - -import org.eclipse.jetty.http.HttpFields; -import org.eclipse.jetty.http.HttpGenerator; -import org.eclipse.jetty.http.HttpHeader; -import org.eclipse.jetty.http.MimeTypes; -import org.eclipse.jetty.server.HttpChannel; -import org.eclipse.jetty.server.HttpOutput; -import org.eclipse.jetty.server.Response; -import org.eclipse.jetty.servlets.AsyncGzipFilter; -import org.eclipse.jetty.util.BufferUtil; -import org.eclipse.jetty.util.Callback; -import org.eclipse.jetty.util.IteratingNestedCallback; -import org.eclipse.jetty.util.StringUtil; -import org.eclipse.jetty.util.log.Log; -import org.eclipse.jetty.util.log.Logger; - -public class GzipHttpOutput extends HttpOutput -{ - public static Logger LOG = Log.getLogger(GzipHttpOutput.class); - private final static HttpGenerator.CachedHttpField CONTENT_ENCODING_GZIP=new HttpGenerator.CachedHttpField(HttpHeader.CONTENT_ENCODING,"gzip"); - private final static byte[] GZIP_HEADER = new byte[] { (byte)0x1f, (byte)0x8b, Deflater.DEFLATED, 0, 0, 0, 0, 0, 0, 0 }; - - private enum GZState { NOT_COMPRESSING, MIGHT_COMPRESS, COMMITTING, COMPRESSING, FINISHED}; - private final AtomicReference<GZState> _state = new AtomicReference<>(GZState.NOT_COMPRESSING); - private final CRC32 _crc = new CRC32(); - - private Deflater _deflater; - private GzipFactory _factory; - private ByteBuffer _buffer; - - public GzipHttpOutput(HttpChannel<?> channel) - { - super(channel); - } - - @Override - public void reset() - { - _state.set(GZState.NOT_COMPRESSING); - super.reset(); - } - - @Override - protected void write(ByteBuffer content, boolean complete, Callback callback) - { - switch (_state.get()) - { - case NOT_COMPRESSING: - super.write(content,complete,callback); - return; - - case MIGHT_COMPRESS: - commit(content,complete,callback); - break; - - case COMMITTING: - callback.failed(new WritePendingException()); - break; - - case COMPRESSING: - gzip(content,complete,callback); - break; - - default: - callback.failed(new IllegalStateException("state="+_state.get())); - break; - } - } - - private void superWrite(ByteBuffer content, boolean complete, Callback callback) - { - super.write(content,complete,callback); - } - - private void addTrailer() - { - int i=_buffer.limit(); - _buffer.limit(i+8); - - int v=(int)_crc.getValue(); - _buffer.put(i++,(byte)(v & 0xFF)); - _buffer.put(i++,(byte)((v>>>8) & 0xFF)); - _buffer.put(i++,(byte)((v>>>16) & 0xFF)); - _buffer.put(i++,(byte)((v>>>24) & 0xFF)); - - v=_deflater.getTotalIn(); - _buffer.put(i++,(byte)(v & 0xFF)); - _buffer.put(i++,(byte)((v>>>8) & 0xFF)); - _buffer.put(i++,(byte)((v>>>16) & 0xFF)); - _buffer.put(i++,(byte)((v>>>24) & 0xFF)); - } - - - private void gzip(ByteBuffer content, boolean complete, final Callback callback) - { - if (content.hasRemaining() || complete) - { - if (content.hasArray()) - new GzipArrayCB(content,complete,callback).iterate(); - else - new GzipBufferCB(content,complete,callback).iterate(); - } - else - callback.succeeded(); - } - - protected void commit(ByteBuffer content, boolean complete, Callback callback) - { - // Are we excluding because of status? - Response response=getHttpChannel().getResponse(); - int sc = response.getStatus(); - if (sc>0 && (sc<200 || sc==204 || sc==205 || sc>=300)) - { - LOG.debug("{} exclude by status {}",this,sc); - noCompression(); - super.write(content,complete,callback); - return; - } - - // Are we excluding because of mime-type? - String ct = getHttpChannel().getResponse().getContentType(); - if (ct!=null) - { - ct=MimeTypes.getContentTypeWithoutCharset(ct); - if (_factory.isExcludedMimeType(StringUtil.asciiToLowerCase(ct))) - { - LOG.debug("{} exclude by mimeType {}",this,ct); - noCompression(); - super.write(content,complete,callback); - return; - } - } - - // Has the Content-Encoding header already been set? - String ce=getHttpChannel().getResponse().getHeader("Content-Encoding"); - if (ce != null) - { - LOG.debug("{} exclude by content-encoding {}",this,ce); - noCompression(); - super.write(content,complete,callback); - return; - } - - // Are we the thread that commits? - if (_state.compareAndSet(GZState.MIGHT_COMPRESS,GZState.COMMITTING)) - { - // We are varying the response due to accept encoding header. - HttpFields fields = response.getHttpFields(); - fields.add(_factory.getVaryField()); - - long content_length = response.getContentLength(); - if (content_length<0 && complete) - content_length=content.remaining(); - - _deflater = _factory.getDeflater(getHttpChannel().getRequest(),content_length); - - if (_deflater==null) - { - LOG.debug("{} exclude no deflater",this); - _state.set(GZState.NOT_COMPRESSING); - super.write(content,complete,callback); - return; - } - - fields.put(CONTENT_ENCODING_GZIP); - _crc.reset(); - _buffer=getHttpChannel().getByteBufferPool().acquire(_factory.getBufferSize(),false); - BufferUtil.fill(_buffer,GZIP_HEADER,0,GZIP_HEADER.length); - - // Adjust headers - response.setContentLength(-1); - String etag=fields.get(HttpHeader.ETAG); - if (etag!=null) - fields.put(HttpHeader.ETAG,etag.substring(0,etag.length()-1)+AsyncGzipFilter.ETAG_GZIP+ '"'); - - LOG.debug("{} compressing {}",this,_deflater); - _state.set(GZState.COMPRESSING); - - gzip(content,complete,callback); - } - else - callback.failed(new WritePendingException()); - } - - public void noCompression() - { - while (true) - { - switch (_state.get()) - { - case NOT_COMPRESSING: - return; - - case MIGHT_COMPRESS: - if (_state.compareAndSet(GZState.MIGHT_COMPRESS,GZState.NOT_COMPRESSING)) - return; - break; - - default: - throw new IllegalStateException(_state.get().toString()); - } - } - } - - public void noCompressionIfPossible() - { - while (true) - { - switch (_state.get()) - { - case COMPRESSING: - case NOT_COMPRESSING: - return; - - case MIGHT_COMPRESS: - if (_state.compareAndSet(GZState.MIGHT_COMPRESS,GZState.NOT_COMPRESSING)) - return; - break; - - default: - throw new IllegalStateException(_state.get().toString()); - } - } - } - - public boolean mightCompress() - { - return _state.get()==GZState.MIGHT_COMPRESS; - } - - public void mightCompress(GzipFactory factory) - { - while (true) - { - switch (_state.get()) - { - case NOT_COMPRESSING: - _factory=factory; - if (_state.compareAndSet(GZState.NOT_COMPRESSING,GZState.MIGHT_COMPRESS)) - { - LOG.debug("{} might compress",this); - return; - } - _factory=null; - break; - - default: - throw new IllegalStateException(_state.get().toString()); - } - } - } - - private class GzipArrayCB extends IteratingNestedCallback - { - private final boolean _complete; - public GzipArrayCB(ByteBuffer content, boolean complete, Callback callback) - { - super(callback); - _complete=complete; - - byte[] array=content.array(); - int off=content.arrayOffset()+content.position(); - int len=content.remaining(); - _crc.update(array,off,len); - _deflater.setInput(array,off,len); - if (complete) - _deflater.finish(); - content.position(content.limit()); - } - - @Override - protected Action process() throws Exception - { - if (_deflater.needsInput()) - { - if (_deflater.finished()) - { - _factory.recycle(_deflater); - _deflater=null; - getHttpChannel().getByteBufferPool().release(_buffer); - _buffer=null; - return Action.SUCCEEDED; - } - - if (!_complete) - return Action.SUCCEEDED; - - _deflater.finish(); - } - - BufferUtil.compact(_buffer); - int off=_buffer.arrayOffset()+_buffer.limit(); - int len=_buffer.capacity()-_buffer.limit()- (_complete?8:0); - if (len>0) - { - int produced=_deflater.deflate(_buffer.array(),off,len,Deflater.NO_FLUSH); - _buffer.limit(_buffer.limit()+produced); - } - boolean complete=_deflater.finished(); - if (complete) - addTrailer(); - - superWrite(_buffer,complete,this); - return Action.SCHEDULED; - } - } - - private class GzipBufferCB extends IteratingNestedCallback - { - private final ByteBuffer _input; - private final ByteBuffer _content; - private final boolean _last; - public GzipBufferCB(ByteBuffer content, boolean complete, Callback callback) - { - super(callback); - _input=getHttpChannel().getByteBufferPool().acquire(Math.min(_factory.getBufferSize(),content.remaining()),false); - _content=content; - _last=complete; - } - - @Override - protected Action process() throws Exception - { - if (_deflater.needsInput()) - { - if (BufferUtil.isEmpty(_content)) - { - if (_deflater.finished()) - { - _factory.recycle(_deflater); - _deflater=null; - getHttpChannel().getByteBufferPool().release(_buffer); - _buffer=null; - return Action.SUCCEEDED; - } - - if (!_last) - { - return Action.SUCCEEDED; - } - - _deflater.finish(); - } - else - { - BufferUtil.clearToFill(_input); - int took=BufferUtil.put(_content,_input); - BufferUtil.flipToFlush(_input,0); - if (took==0) - throw new IllegalStateException(); - - byte[] array=_input.array(); - int off=_input.arrayOffset()+_input.position(); - int len=_input.remaining(); - - _crc.update(array,off,len); - _deflater.setInput(array,off,len); - if (_last && BufferUtil.isEmpty(_content)) - _deflater.finish(); - } - } - - BufferUtil.compact(_buffer); - int off=_buffer.arrayOffset()+_buffer.limit(); - int len=_buffer.capacity()-_buffer.limit() - (_last?8:0); - if (len>0) - { - int produced=_deflater.deflate(_buffer.array(),off,len,Deflater.NO_FLUSH); - _buffer.limit(_buffer.limit()+produced); - } - boolean finished=_deflater.finished(); - - if (finished) - addTrailer(); - - superWrite(_buffer,finished,this); - return Action.SCHEDULED; - } - } -} diff --git a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/GzipOutputStream.java b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/GzipOutputStream.java deleted file mode 100644 index cb645a5f3b..0000000000 --- a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/GzipOutputStream.java +++ /dev/null @@ -1,72 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.servlets.gzip; - -import java.io.IOException; -import java.io.OutputStream; -import java.util.zip.CRC32; -import java.util.zip.Deflater; - -/** - * Reimplementation of {@link java.util.zip.GZIPOutputStream} that supports reusing a {@link Deflater} instance. - */ -public class GzipOutputStream extends DeflatedOutputStream -{ - - private final static byte[] GZIP_HEADER = new byte[] - { (byte)0x1f, (byte)0x8b, Deflater.DEFLATED, 0, 0, 0, 0, 0, 0, 0 }; - - private final CRC32 _crc = new CRC32(); - - public GzipOutputStream(OutputStream out, Deflater deflater, byte[] buffer) throws IOException - { - super(out,deflater,buffer); - out.write(GZIP_HEADER); - } - - @Override - public synchronized void write(byte[] buf, int off, int len) throws IOException - { - super.write(buf,off,len); - _crc.update(buf,off,len); - } - - @Override - public synchronized void finish() throws IOException - { - if (!_def.finished()) - { - super.finish(); - byte[] trailer = new byte[8]; - writeInt((int)_crc.getValue(),trailer,0); - writeInt(_def.getTotalIn(),trailer,4); - out.write(trailer); - } - } - - private void writeInt(int i, byte[] buf, int offset) - { - int o = offset; - buf[o++] = (byte)(i & 0xFF); - buf[o++] = (byte)((i >>> 8) & 0xFF); - buf[o++] = (byte)((i >>> 16) & 0xFF); - buf[o++] = (byte)((i >>> 24) & 0xFF); - } - -} diff --git a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/package-info.java b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/package-info.java deleted file mode 100644 index 59c969ad31..0000000000 --- a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/gzip/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -/** - * Jetty Servlets : GZIP Filter Classes - */ -package org.eclipse.jetty.servlets.gzip; - |