Skip to content

Commit

Permalink
updated cookie implementation to fix failing tests. There is one brea…
Browse files Browse the repository at this point in the history
…king change on the Cookie API - Cookie.decodeRequestCookie is returning LIst[String]
  • Loading branch information
gciuloaica committed May 23, 2022
1 parent fb20c10 commit b37d9ed
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
51 changes: 34 additions & 17 deletions zio-http/src/main/scala/zhttp/http/Cookie.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import java.time.Instant
import java.util.Base64.getEncoder
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import scala.util.Try
import scala.util.{Failure, Success, Try}

final case class Cookie(
name: String,
Expand Down Expand Up @@ -187,7 +187,12 @@ object Cookie {
* Decodes from Set-Cookie header value inside of Response into a cookie
*/
def decodeResponseCookie(headerValue: String, secret: Option[String] = None): Option[Cookie] =
Try(unsafeDecodeResponseCookie(headerValue, secret)).toOption
Try(unsafeDecodeResponseCookie(headerValue, secret)) match {
case Failure(exception) =>
println(exception)
None
case Success(value) => Some(value)
}

private[zhttp] def unsafeDecodeResponseCookie(headerValue: String, secret: Option[String] = None): Cookie = {
var name: String = null
Expand Down Expand Up @@ -267,10 +272,20 @@ object Cookie {
sameSite = Option(sameSite),
)
else
null
Cookie(
"",
"",
expires = Option(expires),
maxAge = maxAge,
domain = Option(domain),
path = Option(path),
isSecure = secure,
isHttpOnly = httpOnly,
sameSite = Option(sameSite),
)

secret match {
case Some(s) => {
case Some(s) if s.nonEmpty => {
if (decodedCookie != null) {
val index = decodedCookie.content.lastIndexOf('.')
val signature = decodedCookie.content.slice(index + 1, decodedCookie.content.length)
Expand All @@ -281,25 +296,27 @@ object Cookie {
else null
} else decodedCookie
}
case None => decodedCookie
case _ => decodedCookie.copy(secret = secret)
}

}

/**
* Decodes from `Cookie` header value inside of Request into a cookie
*/
def decodeRequestCookie(headerValue: String): Option[List[Cookie]] = {
val cookies: Array[String] = headerValue.split(';').map(_.trim)
val x: List[Option[Cookie]] = cookies.toList.map(a => {
val (name, content) = splitNameContent(a)
if (name.isEmpty && content.isEmpty) None
else Some(Cookie(name, content))
})

if (x.contains(None))
None
else Some(x.map(_.get))
def decodeRequestCookie(headerValue: String): List[Cookie] = {
if (headerValue.nonEmpty) {
val cookies: Array[String] = headerValue.split(';').map(_.trim)
val x: List[Option[Cookie]] = cookies.toList.map(a => {
val (name, content) = splitNameContent(a)
if (name.isEmpty && content.isEmpty) Some(Cookie("", ""))
else Some(Cookie(name, content))
})

if (x.contains(None))
List.empty
else x.map(_.get)
} else List.empty
}

@inline
Expand All @@ -308,7 +325,7 @@ object Cookie {
if (i >= 0) {
(str.substring(0, i).trim, str.substring(i + 1).trim)
} else {
(str.trim, null)
(str.trim, "")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,7 @@ trait HeaderGetters[+A] { self =>

final def cookiesDecoded: List[Cookie] =
headerValues(HeaderNames.cookie).flatMap { header =>
Cookie.decodeRequestCookie(header) match {
case None => Nil
case Some(list) => list
}
Cookie.decodeRequestCookie(header)
}

final def date: Option[CharSequence] =
Expand Down
3 changes: 1 addition & 2 deletions zio-http/src/test/scala/zhttp/http/CookieSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ object CookieSpec extends DefaultRunnableSpec {
cookieList <- Gen.listOf(Gen.const(Cookie(name, content)))
cookieString <- Gen.const(cookieList.map(x => s"${x.name}=${x.content}").mkString(";"))
} yield (cookieList, cookieString)) { case (cookies, message) =>
println(s"c= $cookies, m=$message")
assert(Cookie.decodeRequestCookie(message))(isSome(equalTo(cookies)))
assert(Cookie.decodeRequestCookie(message))(equalTo(cookies))
}
}
}
Expand Down

2 comments on commit b37d9ed

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance Benchmark:

Concurrency: 256
Requests/sec: 942553.09

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance Benchmark:

Concurrency: 256
Requests/sec: 925973.83

Please sign in to comment.