Re: [Simpleweb-Support] HTTP Compression
Brought to you by:
niallg
From: Niall G. <gal...@ya...> - 2005-12-14 21:45:58
|
Hi Brian, Take a look a http://zoe.nu, this uses compression with Simple. I did implement compression in Simple, I was not impressed with the performance results and it added complexity I did not need. Also, compression is not always best implemented in the Response object. I am unsure how you are serving content. However encoding in a cache, or file system interface is a good solution. For example: public class GzipContent implements Content { public GzipContent(SomeSource source) { // set up your source } public void write(OutputStream out) { compressedBytes = // get from cache or dynamic source out.write(compressedBytes); } public String getMimeType() { return "text/html; charset=utf-8" } public String getEncoding() { return "gzip"; } } This allows you to use the object to transfer info and meta-data easily, also it gives you more choice in what gets compressed. Niall --- Brian Davis <wic...@gm...> wrote: > Hmm... can't seem to get this to work.. > > also a couple things bother me... first.. in order > to do it this way I > won't be able to set the content-length because > whatever i set would > be wrong.. also.. compression should be handled at > the framework level > if possible.. so.. I was really just wondering why > it wasn't > supported... out of the box per-se > > anyway.. thanks for the help! > > /Brian > > On 12/13/05, Brian Davis <wic...@gm...> > wrote: > > Hey cool.. I will look at this in a day or two and > let you know how it > > goes.. did you guys do anything with deflate or > just gzip? > > > > /Brian > > > > > > On 12/13/05, Christophe Roudet > <cr...@ac...> wrote: > > > I have done this with a custom ProtocolHandler: > > > > > > public class ServerHandler implements > ProtocolHandler { > > > private ProtocolHandler _handler; > > > private static final Logger _logger = > > > Logger.getLogger(ServerHandler.class); > > > > > > // ============= > > > // Constructor > > > // ============= > > > > > > public ServerHandler(ProtocolHandler handler) > { > > > _handler = handler; > > > } > > > > > > // =============== > > > // Public methods > > > // =============== > > > > > > public void handle(Request req, Response resp) > { > > > resp.set("Server", "Simple"); > > > resp.setDate("Date", > System.currentTimeMillis()); > > > // check for compressed request > > > String contentEncoding = > req.getValue("Content-Encoding"); > > > if ((contentEncoding != null) > > > && > (contentEncoding.toLowerCase().indexOf("gzip") > > -1)) { > > > _logger.debug("Request is zipped."); > > > req = new GZipRequest(req, > SimpleWeb.BUFFER_SIZE); > > > } > > > // does the client accept compressed > response? > > > String acceptEncoding = > req.getValue("Accept-Encoding"); > > > if ((!"GET".equals(req.getMethod())) && > (acceptEncoding != null) > > > && > (acceptEncoding.toLowerCase().indexOf("gzip") > -1)) > { > > > _logger.debug("Response will be zipped."); > > > resp = new GZipResponse(resp); > > > } > > > /* > > > * Hand the request over to the real server > so that targeted service can > > > be > > > * executed. > > > */ > > > _handler.handle(req, resp); > > > } > > > } > > > > > > -------------------- > > > And the GZipRequest and GZipResponse are mainly > a wrapper on a request where > > > stream getter methods return a GZIP stream: > > > > > > > > > public class GZipRequest implements Request { > //extends FilterRequest { > > > private int _size; > > > private Request _req; > > > > > > // ============= > > > // Constructor > > > // ============= > > > > > > /** > > > * Wraps the specified request stream into a > GZipStream. > > > */ > > > public GZipRequest(Request request, int size) > { > > > //super(request); > > > _req = request; > > > if (size <=0) { > > > _size = 2048; > > > } else { > > > _size = size; > > > } > > > } > > > > > > // =============== > > > // Public methods > > > // =============== > > > > > > /** > > > * Returns a GZIPInputStream. > > > */ > > > public InputStream getInputStream() throws > IOException { > > > return new > GZIPInputStream(_req.getInputStream(), _size); > > > } > > > > > > /** > > > * This can be used to get the URI specified > for this HTTP > > > * request. This corresponds to the /index > part of a > > > * http://www.domain.com/index URL but may > contain the full > > > * URL. This can be set using > <code>setURI</code>. > > > * > > > * @return the URI that this HTTP request is > targeting > > > */ > > > public String getURI(){ > > > return _req.getURI(); > > > } > > > > > > /** > > > * This can be used to set the URI for this > HTTP request. > > > * The <code>getURI</code> will return the > String entered > > > * which can be a full HTTP URL or a relative > path URL. > > > * > > > * @param uri the URI that this HTTP request > is to use > > > */ > > > public void setURI(String uri){ > > > _req.setURI(uri); > > > } > > > > > > /** > > > * This can be used to get the HTTP method for > this > > > * request. The HTTP specification RFC 2616 > specifies the > > > * HTTP request methods in section 9, Method > Definitions. > > > * > > > * @return the request method for this request > > > */ > > > public String getMethod(){ > > > return _req.getMethod(); > > > } > > > > > > ... > > > > > > ------------------ > > > > > > public class GZipResponse implements Response { > //extends FilterResponse { > > > private Response _resp; > > > > > > // ============= > > > // Constructors > > > // ============= > > > > > > /** > > > * Creates a GZipResponse for the specified > Response. > > > */ > > > public GZipResponse(Response response) { > > > //super(response); > > > _resp = response; > > > _resp.set("Content-Encoding", "gzip"); > > > } > > > > > > // ============== > > > // Public methods > > > // =============== > === message truncated === Niall Gallagher __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com |