Permalink
Browse files

HostPort should accept any int for port and separate out the host, fi…

…xes #114
  • Loading branch information...
1 parent a74507c commit e8b8ae3a858eebfd86da3a4571de65a1c6398683 @n8han n8han committed Mar 28, 2012
View
7 filter/src/main/scala/bindings.scala
@@ -5,26 +5,25 @@ import unfiltered.response.HttpResponse
import unfiltered.request.HttpRequest
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
import unfiltered.Cookie
-import unfiltered.util.Optional
class RequestBinding(req: HttpServletRequest) extends HttpRequest(req) {
def inputStream = req.getInputStream
def reader = req.getReader
def protocol = req.getProtocol
def method = req.getMethod.toUpperCase
- def uri = req.getRequestURI :: Nil ++ Optional(req.getQueryString).map("?%s".format(_)) mkString("")
+ def uri = req.getRequestURI :: Nil ++ Option(req.getQueryString).map("?%s".format(_)) mkString("")
def parameterNames = new JEnumerationIterator(
req.getParameterNames.asInstanceOf[java.util.Enumeration[String]]
)
- def parameterValues(param: String) = Optional[Seq[String]](req.getParameterValues(param)).getOrElse(Nil)
+ def parameterValues(param: String) = Option[Seq[String]](req.getParameterValues(param)).getOrElse(Nil)
def headers(name: String) = new JEnumerationIterator(
req.getHeaders(name).asInstanceOf[java.util.Enumeration[String]]
)
lazy val cookies = req.getCookies match {
case null => Nil
case jcookies =>
(List[Cookie]() /: jcookies)((l, c) =>
- Cookie(c.getName, c.getValue, Optional(c.getDomain), Optional(c.getPath), Optional(c.getMaxAge), Optional(c.getSecure)) :: l)
+ Cookie(c.getName, c.getValue, Option(c.getDomain), Option(c.getPath), Option(c.getMaxAge), Option(c.getSecure)) :: l)
}
def isSecure = req.isSecure
View
7 library/src/main/scala/kit/secure.scala
@@ -10,13 +10,12 @@ object Secure {
{ _ => Pass },
{
case (HTTPS(req), rf) => rf
- case (req @ Host(host), rf) =>
- val h = host.split(':')(0)
+ case (req @ HostPort(host, _), rf) =>
Redirect(
if (port > -1)
- "https://%s:%d%s".format(h, port, req.uri)
+ "https://%s:%d%s".format(host, port, req.uri)
else
- "https://%s%s".format(h, req.uri)
+ "https://%s%s".format(host, req.uri)
)
}
)
View
10 library/src/main/scala/request/headers.scala
@@ -145,14 +145,16 @@ object Charset {
}.headOption
}
-/** Extracts the port number from the Host header, if present */
+/** Extracts hostname and port separately from the Host header, setting
+ * a default port of 80 or 443 when none is specified */
object HostPort {
- val Port = """^\S+[:](\d{4})$""".r
+ import unfiltered.util.Of
def unapply[T](req: HttpRequest[T]): Option[(String, Int)] =
req match {
- case Host(hostname) => hostname match {
- case Port(port) => Some(hostname, port.toInt)
+ case Host(hostname) => hostname.split(':') match {
+ case Array(host, Of.Int(port)) => Some(host, port.toInt)
case _ => Some(hostname, if(req.isSecure) 443 else 80)
}
+ case _ => None
}
}
View
3 netty/src/main/scala/bindings.scala
@@ -16,7 +16,6 @@ import org.jboss.netty.handler.codec.http.{HttpResponse=>NHttpResponse,
HttpRequest=>NHttpRequest}
import java.nio.charset.{Charset => JNIOCharset}
import unfiltered.Cookie
-import unfiltered.util.Optional
object HttpConfig {
val DEFAULT_CHARSET = "UTF-8"
@@ -67,7 +66,7 @@ extends HttpRequest(msg) with Async.Responder[NHttpResponse] {
val cookieDecoder = new CookieDecoder
val decCookies = Set(cookieDecoder.decode(cookieString).toArray(new Array[NCookie](0)): _*)
(List[Cookie]() /: decCookies)((l, c) =>
- Cookie(c.getName, c.getValue, Optional(c.getDomain), Optional(c.getPath), Optional(c.getMaxAge), Optional(c.isSecure)) :: l)
+ Cookie(c.getName, c.getValue, Option(c.getDomain), Option(c.getPath), Option(c.getMaxAge), Option(c.isSecure)) :: l)
} else {
Nil
}
View
22 util/src/main/scala/utils.scala
@@ -1,5 +1,7 @@
package unfiltered.util
+import scala.util.control.Exception.allCatch
+
object Port {
/** Finds any available port and returns it */
def any = {
@@ -23,8 +25,24 @@ object Browser {
} catch { case e => Some(e) }
}
-/** Shim for what scala 2.8 Option#apply does so 2.7 can play too */
-object Optional {
+/** Extractors that match on strings that can be converted to types */
+object Of {
+ object Int {
+ def unapply(str: String) = allCatch.opt { str.toInt }
+ }
+ object Long {
+ def unapply(str: String) = allCatch.opt { str.toLong }
+ }
+ object Float {
+ def unapply(str: String) = allCatch.opt { str.toFloat }
+ }
+ object Double {
+ def unapply(str: String) = allCatch.opt { str.toDouble }
+ }
+}
+
+/** not supporting Scala 2.7 any more */
+@deprecated("use Option.apply") object Optional {
def apply[T](x: T) = if(x == null) None else Some(x)
def unapply[T](x: T) = apply(x)
}

0 comments on commit e8b8ae3

Please sign in to comment.