diff options
author | adamw <adam@warski.org> | 2017-07-31 14:26:06 +0200 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-07-31 14:26:06 +0200 |
commit | f34ad777382ca49c70a1dce655bfb6e483995716 (patch) | |
tree | 8a80eaa026b1761db3c13e8d05b3915025b5ce93 /core/src/main/scala/com | |
parent | 4d23d6fd21317e6bf28cd23b0e149d570b55f5e8 (diff) | |
download | sttp-f34ad777382ca49c70a1dce655bfb6e483995716.tar.gz sttp-f34ad777382ca49c70a1dce655bfb6e483995716.tar.bz2 sttp-f34ad777382ca49c70a1dce655bfb6e483995716.zip |
Support for user info in the Uri class
Diffstat (limited to 'core/src/main/scala/com')
-rw-r--r-- | core/src/main/scala/com/softwaremill/sttp/Uri.scala | 37 | ||||
-rw-r--r-- | core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala | 2 |
2 files changed, 25 insertions, 14 deletions
diff --git a/core/src/main/scala/com/softwaremill/sttp/Uri.scala b/core/src/main/scala/com/softwaremill/sttp/Uri.scala index 1ee4337..6b2e840 100644 --- a/core/src/main/scala/com/softwaremill/sttp/Uri.scala +++ b/core/src/main/scala/com/softwaremill/sttp/Uri.scala @@ -15,6 +15,7 @@ import scala.collection.immutable.Seq * added manually as part of the plain query fragment. */ case class Uri(scheme: String, + userInfo: Option[String], host: String, port: Option[Int], path: Seq[String], @@ -22,20 +23,24 @@ case class Uri(scheme: String, fragment: Option[String]) { def this(host: String) = - this("http", host, None, Vector.empty, Vector.empty, None) + this("http", None, host, None, Vector.empty, Vector.empty, None) def this(host: String, port: Int) = - this("http", host, Some(port), Vector.empty, Vector.empty, None) + this("http", None, host, Some(port), Vector.empty, Vector.empty, None) def this(host: String, port: Int, path: Seq[String]) = - this("http", host, Some(port), path, Vector.empty, None) + this("http", None, host, Some(port), path, Vector.empty, None) def this(scheme: String, host: String) = - this(scheme, host, None, Vector.empty, Vector.empty, None) + this(scheme, None, host, None, Vector.empty, Vector.empty, None) def this(scheme: String, host: String, port: Int) = - this(scheme, host, Some(port), Vector.empty, Vector.empty, None) + this(scheme, None, host, Some(port), Vector.empty, Vector.empty, None) def this(scheme: String, host: String, port: Int, path: Seq[String]) = - this(scheme, host, Some(port), path, Vector.empty, None) + this(scheme, None, host, Some(port), path, Vector.empty, None) def scheme(s: String): Uri = this.copy(scheme = s) + def userInfo(ui: String): Uri = this.copy(userInfo = Some(ui)) + + def userInfo(ui: Option[String]): Uri = this.copy(userInfo = ui) + def host(h: String): Uri = this.copy(host = h) def port(p: Int): Uri = this.copy(port = Some(p)) @@ -87,12 +92,10 @@ case class Uri(scheme: String, def fragment(f: Option[String]): Uri = this.copy(fragment = f) override def toString: String = { - val schemeS = encode(scheme) - val hostS = encode(host) - val portS = port.fold("")(":" + _) - val pathPrefixS = if (path.isEmpty) "" else "/" - val pathS = path.map(encode).mkString("/") - val queryPrefixS = if (queryFragments.isEmpty) "" else "?" + def encodeUserInfo(ui: String): String = ui.split(":", 2) match { + case Array(u) => encode(u) + case Array(u, p) => encode(u) + ":" + encode(p) + } @tailrec def encodeQueryFragments(qfs: List[QueryFragment], @@ -111,11 +114,19 @@ case class Uri(scheme: String, encodeQueryFragments(t, previousWasKV = true, sb) } + val schemeS = encode(scheme) + val userInfoS = userInfo.fold("")(encodeUserInfo(_) + "@") + val hostS = encode(host) + val portS = port.fold("")(":" + _) + val pathPrefixS = if (path.isEmpty) "" else "/" + val pathS = path.map(encode).mkString("/") + val queryPrefixS = if (queryFragments.isEmpty) "" else "?" + val queryS = encodeQueryFragments(queryFragments.toList, previousWasKV = false, new StringBuilder()) val fragS = fragment.fold("")("#" + _) - s"$schemeS://$hostS$portS$pathPrefixS$pathS$queryPrefixS$queryS$fragS" + s"$schemeS://$userInfoS$hostS$portS$pathPrefixS$pathS$queryPrefixS$queryS$fragS" } private def encode(s: Any): String = { diff --git a/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala b/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala index 26b9827..025f626 100644 --- a/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala +++ b/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala @@ -57,7 +57,7 @@ object UriInterpolator { private def append(x: String): Scheme = Scheme(v + x) - override def build: Uri = Uri(v, "", None, Nil, Nil, None) + override def build: Uri = Uri(v, None, "", None, Nil, Nil, None) } case class Authority(s: Scheme, v: String = "") extends UriBuilder { |