Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
gerdriesselmann committed Jul 26, 2011
1 parent 1a19324 commit fa127a2
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
50 changes: 50 additions & 0 deletions GZipResponseString.scala
@@ -0,0 +1,50 @@
package net.gerdriesselmann.unfiltered

import unfiltered.request.{HttpRequest, AcceptEncoding}
import unfiltered.response._

/**
* GZip a response, if possible
*/
case class GZipResponseString[T](request: HttpRequest[T], content: String) extends Responder[Any] {
/**
* Respond
*/
override def respond(res: HttpResponse[Any]) {
request match {
case AcceptEncoding(values) => if (values.exists { s => s.equalsIgnoreCase("gzip") }) {
streamGZip(res)
} else {
streamRes(res)
}
case _ => streamRes(res)
}
}

/**
* Turn string into bytes
*/
def contentToBytes = content.getBytes("UTF-8")

/**
* Stream using the default output stream
*/
def streamRes(res: HttpResponse[Any]) = stream(res.getOutputStream, content)

/**
* Stream using gzip encoding
*/
def streamGZip(res: HttpResponse[Any]) = {
res.addHeader("Content-Encoding", "gzip")
val gos = new java.util.zip.GZIPOutputStream(res.getOutputStream)
stream(gos, content)
}

/**
* Write into given stream
*/
def stream(os: java.io.OutputStream, content: String) {
try { os.write(contentToBytes) }
finally { os.close() }
}
}
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
Copyright (c) 2011 Gerd Riesselmann

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

14 changes: 14 additions & 0 deletions README
@@ -0,0 +1,14 @@
This is a GZip reponse writer for Unfiltered (https://github.com/n8han/Unfiltered).

It can be used as a drop in replacement for ResponseString, like this:

object Echo extends unfiltered.filter.Plan {
def intent = {
case req @ Path(Seg(p :: Nil)) => GZipResponseString(req, p)
}
}

The GZipResponseString will check if the client supports gzip encoding and
will send the required headers.


0 comments on commit fa127a2

Please sign in to comment.