RE: [Simpleweb-Support] HTTP Compression
Brought to you by:
niallg
From: Christophe R. <cr...@ac...> - 2005-12-13 18:14:51
|
I have done this with a custom ProtocolHandler: public class ServerHandler implements ProtocolHandler { private ProtocolHandler _handler; private static final Logger _logger =3D Logger.getLogger(ServerHandler.class); =20 // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D // Constructor // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D public ServerHandler(ProtocolHandler handler) { _handler =3D handler; } // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D // Public methods // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D public void handle(Request req, Response resp) { resp.set("Server", "Simple"); resp.setDate("Date", System.currentTimeMillis()); // check for compressed request String contentEncoding =3D req.getValue("Content-Encoding"); if ((contentEncoding !=3D null) && (contentEncoding.toLowerCase().indexOf("gzip") > -1)) { _logger.debug("Request is zipped."); req =3D new GZipRequest(req, SimpleWeb.BUFFER_SIZE); } // does the client accept compressed response? String acceptEncoding =3D req.getValue("Accept-Encoding"); if ((!"GET".equals(req.getMethod())) && (acceptEncoding !=3D null) && (acceptEncoding.toLowerCase().indexOf("gzip") > -1)) { _logger.debug("Response will be zipped."); resp =3D 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; =20 // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D // Constructor // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 /** * Wraps the specified request stream into a GZipStream. */ public GZipRequest(Request request, int size) { //super(request); _req =3D request; if (size <=3D0) { _size =3D 2048; } else { _size =3D size; } } // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D // Public methods // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 /** * Returns a GZIPInputStream. */ public InputStream getInputStream() throws IOException { return new GZIPInputStream(_req.getInputStream(), _size); } =20 /** * This can be used to get the URI specified for this HTTP * request. This corresponds to the /index part of a=20 * 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 */=20 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 */ =20 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 */=20 public String getMethod(){ return _req.getMethod(); } ... ------------------ public class GZipResponse implements Response { //extends FilterResponse = { private Response _resp; =20 // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D // Constructors // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D /** * Creates a GZipResponse for the specified Response. */ public GZipResponse(Response response) { //super(response); _resp =3D response; _resp.set("Content-Encoding", "gzip"); } // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D // Public methods // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D /** * Returns a GZIPOutputStream. * @see simple.http.Response#getOutputStream() */ public OutputStream getOutputStream() throws IOException { return new GZIPOutputStream(_resp.getOutputStream()); } /** * Returns a GZIPOutputStream. * @see simple.http.Response#getOutputStream(int) */ public OutputStream getOutputStream(int size) throws IOException { return new GZIPOutputStream(_resp.getOutputStream(size), size); } /** * Returns a GZIPOutputStream wrapped into a PrintStream. * @see simple.http.Response#getPrintStream() */ public PrintStream getPrintStream() throws IOException { return getPrintStream(); } /** * Returns a GZIPOutputStream wrapped into a PrintStream. * @see simple.http.Response#getPrintStream(int) */ public PrintStream getPrintStream(int size) throws IOException { return new PrintStream(getOutputStream(size), false); } /** * This represents the status code of the HTTP response. The HTTP * response code represents the type of message that is being sent * to the client. For a description of the codes see RFC 2616=20 * section 10, Status Code Definitions.=20 * * @return the status code that this HTTP response has */=20 public int getCode(){ return _resp.getCode(); } =20 /** * This method allows the status for the response to be changed.=20 * This must be reflected the the response content given to the=20 * client. For a description of the codes see RFC 2616 section=20 * 10, Status Code Definitions. * * @param code the new status code for the HTTP response */=20 public void setCode(int code){ _resp.setCode(code); } .... Christophe > -----Original Message----- > From: sim...@li... [mailto:simpleweb- > sup...@li...] On Behalf Of Brian Davis > Sent: Tuesday, December 13, 2005 10:20 AM > To: sim...@li... > Subject: [Simpleweb-Support] HTTP Compression >=20 > Is there any plans to support gzip or deflate.. or is it there and I > am not finding it.. also.. if not why? and.. if not.. any recommends > on where to add it.. >=20 > thanks! >=20 > Brian >=20 >=20 > ------------------------------------------------------- > This SF.net email is sponsored by: Splunk Inc. Do you grep through log > files > for problems? Stop! Download the new AJAX search engine that makes > searching your log files as easy as surfing the web. DOWNLOAD = SPLUNK! > http://ads.osdn.com/?ad_idv37&alloc_id=16865&op=3Dick > _______________________________________________ > Simpleweb-Support mailing list > Sim...@li... > https://lists.sourceforge.net/lists/listinfo/simpleweb-support |