Skip to content

Instantly share code, notes, and snippets.

@darkfrog26
Created November 13, 2012 18:34
Show Gist options
  • Save darkfrog26/4067521 to your computer and use it in GitHub Desktop.
Save darkfrog26/4067521 to your computer and use it in GitHub Desktop.
case class URL(method: Method = Method.Get,
protocol: String = "http",
host: String = null,
port: Int = 80,
path: String = "/",
parameters: Map[String, List[String]])
sealed class Method(val value: String) extends EnumEntry[Method]
object Method extends Enumerated[Method] {
val Get = new Method("get")
val Put = new Method("put")
val Trace = new Method("trace")
val Connect = new Method("connect")
val Head = new Method("head")
val Delete = new Method("delete")
val Patch = new Method("patch")
val Post = new Method("post")
val Options = new Method("options")
}
package object netty {
implicit def request2URL(request: HttpRequest) = {
val decoder = new QueryStringDecoder(request.getUri)
val hostAndPort = request.getHeader(HttpHeaders.HOST)
val hpSeparator = hostAndPort.indexOf(':')
val host = if (hpSeparator != -1) {
hostAndPort.substring(0, hpSeparator)
} else {
hostAndPort
}
val port = if (hpSeparator != -1) {
hostAndPort.substring(hpSeparator + 1).toInt
} else {
80
}
val parameters = decoder.getParameters.map {
case (key, values) => key -> values.toList
}.toMap
URL(method = httpMethod2Method(request.getMethod),
protocol = "http", // TODO: support other protocols
host = host,
port = port,
path = decoder.getPath,
parameters = parameters)
}
implicit def httpMethod2Method(m: HttpMethod) = Method.values.find(method => method.value == m.getName.toLowerCase).getOrElse(null)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment