Permalink
Browse files

! http: add small extensions to Uri model

Specifically:
- additional `charset` parameter on `Uri.Host.apply` and `Uri.Query.apply`
- new method: Uri.Query::toMap
  • Loading branch information...
sirthias committed Aug 30, 2013
1 parent adfdab2 commit f625b5a68ae26583347673d4e71f9dead69a7223
@@ -164,7 +164,7 @@ object Uri {
def from(scheme: String = "", userinfo: String = "", host: String = "", port: Int = 0, path: String = "",
query: Query = Query.Empty, fragment: Option[String] = None,
mode: Uri.ParsingMode = Uri.ParsingMode.Relaxed): Uri =
- apply(scheme, Authority(Host(host, mode), normalizePort(port, scheme), userinfo), Path(path), query, fragment)
+ apply(scheme, Authority(Host(host, UTF8, mode), normalizePort(port, scheme), userinfo), Path(path), query, fragment)
/**
* Parses a string into a normalized absolute URI as defined by http://tools.ietf.org/html/rfc3986#section-4.3.
@@ -270,7 +270,7 @@ object Uri {
def toOption = None
def render[R <: Rendering](r: R): r.type = r
}
- def apply(string: String, mode: Uri.ParsingMode = Uri.ParsingMode.Relaxed): Host =
+ def apply(string: String, charset: Charset = UTF8, mode: Uri.ParsingMode = Uri.ParsingMode.Relaxed): Host =
if (!string.isEmpty) {
val parser = new UriParser(string, UTF8, mode)
import parser._
@@ -416,6 +416,11 @@ object Uri {
if (q.isEmpty) result else fetch(q.tail, if (q.key == key) q.value :: result else result)
fetch(this)
}
+ def toMap: Map[String, String] = {
+ @tailrec def append(map: Map[String, String], q: Query): Map[String, String] =
+ if (q.isEmpty) map else append(map.updated(q.key, q.value), q.tail)
+ append(Map.empty, this)
+ }
def toMultiMap: Map[String, List[String]] = {
@tailrec def append(map: Map[String, List[String]], q: Query): Map[String, List[String]] =
if (q.isEmpty) map else append(map.updated(q.key, q.value :: map.getOrElse(q.key, Nil)), q.tail)
@@ -445,16 +450,16 @@ object Uri {
* Empty strings will be parsed to `("", "") +: Query.Empty`
* If you want to allow for Query.Empty creation use the apply overload taking an `Option[String`.
*/
- def apply(string: String, mode: Uri.ParsingMode = Uri.ParsingMode.Relaxed): Query = {
- val parser = new UriParser(string, UTF8, mode)
+ def apply(string: String, charset: Charset = UTF8, mode: Uri.ParsingMode = Uri.ParsingMode.Relaxed): Query = {
+ val parser = new UriParser(string, charset, mode)
import parser._
complete("Query", query)
_query
}
def apply(input: Option[String]): Query = apply(input, Uri.ParsingMode.Relaxed)
def apply(input: Option[String], mode: Uri.ParsingMode): Query = input match {
case None Query.Empty
- case Some(string) apply(string, mode)
+ case Some(string) apply(string, mode = mode)
}
def apply(kvp: (String, String)*): Query =
kvp.foldRight(Query.Empty: Query) { case ((key, value), acc) Cons(key, value, acc) }
@@ -220,7 +220,7 @@ class UriSpec extends Specification {
}
"Uri.Query instances" should {
- def parser(mode: Uri.ParsingMode): String Query = Query(_, mode)
+ def parser(mode: Uri.ParsingMode): String Query = Query(_, mode = mode)
"be parsed and rendered correctly in strict mode" in {
val test = parser(Uri.ParsingMode.Strict)
test("") === ("", "") +: Query.Empty

0 comments on commit f625b5a

Please sign in to comment.