Re: [Simpleweb-Support] HTTP Compression
Brought to you by:
niallg
|
From: Brian D. <wic...@gm...> - 2005-12-13 20:21:26
|
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 =3D
> Logger.getLogger(ServerHandler.class);
>
> // =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 wh=
ere
> stream getter methods return a GZIP stream:
>
>
> public class GZipRequest implements Request { //extends FilterRequest {
> private int _size;
> private Request _req;
>
> // =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
>
> /**
> * 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
>
> /**
> * 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;
>
> // =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
> * section 10, Status Code Definitions.
> *
> * @return the status code that this HTTP response has
> */
> public int getCode(){
> return _resp.getCode();
> }
>
> /**
> * This method allows the status for the response to be changed.
> * This must be reflected the the response content given to the
> * client. For a description of the codes see RFC 2616 section
> * 10, Status Code Definitions.
> *
> * @param code the new status code for the HTTP response
> */
> 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
> >
> > 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..
> >
> > thanks!
> >
> > Brian
> >
> >
> > -------------------------------------------------------
> > 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
>
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep through log fi=
les
> 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&opclick
> _______________________________________________
> Simpleweb-Support mailing list
> Sim...@li...
> https://lists.sourceforge.net/lists/listinfo/simpleweb-support
>
|