Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions http/src/main/scala/spinoco/protocol/http/HttpScheme.scala
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
package spinoco.protocol.http

import scodec.Codec
import scodec.{Codec, Err}
import codec.helper._
import spinoco.protocol.common.util._

/**
* A generic protocol for Uri.
*
* @param tpe The type of the protocol.
*/
case class Scheme private[http](tpe: String)

object Scheme {

def parseScheme(toParse: String): Option[Scheme] = {
toParse.headOption.flatMap { firstChar =>
if (!firstChar.isLetter) None
else if (toParse.tail.forall(c => c.isLetterOrDigit || c == '+' || c == '-' || c == '.')) {
Some(new Scheme(toParse.toLowerCase))
} else None
}
}

object HttpScheme extends Enumeration {
val codec: Codec[Scheme] = {
import scodec.Attempt._
trimmedAsciiToken.exmap(
s => fromOption(parseScheme(s), Err(s"The specified scheme is not valid according to RFC 3986: $s"))
, s => successful(s.tpe)
)
}

val HTTP = Value("http")
}

val HTTPS = Value("https")
object HttpScheme {

val WS = Value("ws")
val HTTP = Scheme("http")

val WSS = Value("wss")
val HTTPS = Scheme("https")

val WS = Scheme("ws")

val codec: Codec[HttpScheme.Value] = {
import scodec.Attempt._
trimmedAsciiToken.exmap(
s => attempt(HttpScheme.withName(s.toLowerCase))
, s => successful(s.toString)
)
}
val WSS = Scheme("wss")

}
4 changes: 2 additions & 2 deletions http/src/main/scala/spinoco/protocol/http/Uri.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import scala.annotation.tailrec
* All values are decoded (no % escaping)
*/
sealed case class Uri(
scheme: HttpScheme.Value
scheme: Scheme
, host: HostPort
, path: Uri.Path
, query: Uri.Query
Expand Down Expand Up @@ -96,7 +96,7 @@ object Uri {
)
}

(terminated(HttpScheme.codec, Terminator.constantString1("://")) ~ hostPathQueryCodec)
(terminated(Scheme.codec, Terminator.constantString1("://")) ~ hostPathQueryCodec)
.xmap(
{ case (scheme, (host, path, query)) => Uri(scheme, host, path, query) }
, uri => (uri.scheme, (uri.host, uri.path, uri.query))
Expand Down
24 changes: 24 additions & 0 deletions http/src/test/scala/spinoco/protocol/http/SchemeSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package spinoco.protocol.http

import org.scalacheck.Properties
import org.scalacheck.Prop._

object SchemeSpec extends Properties("Schema"){

property("parse.valid") = protect{
Scheme.parseScheme("http-1+3.two") ?= Some(Scheme("http-1+3.two"))
}

property("parse.valid.upper-case") = protect{
Scheme.parseScheme("http-1+3.TWO") ?= Some(Scheme("http-1+3.two"))
}

property("parse.invalid.starts-digit") = protect{
Scheme.parseScheme("1http-1+3.two") ?= None
}

property("parse.invalid.non-valid-char") = protect{
Scheme.parseScheme("http-1:3.two") ?= None
}

}