Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added cookie helpers #2439

Merged
merged 7 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 15 additions & 1 deletion docs/dsl/cookies.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,21 @@ It updates the response header `Set-Cookie` as ```Set-Cookie: <cookie-name>=<coo

## Getting Cookie from Request

In HTTP requests, cookies are stored in the `cookie` header. `cookiesDecoded` can be used to get all the cookies in the request:
From HTTP requests, a single cookie can be retrieved with `cookie`.

```scala mdoc
private val app4 =
Routes(
Method.GET / "cookie" -> handler { (req: Request) =>
val cookieContent = req.cookie("sessionId").map(_.content)
Response.text(s"cookie content: $cookieContent")
}
)
```

## Getting Cookie from Header

In HTTP requests, cookies are stored in the `cookie` header.

```scala mdoc
private val app3 =
Expand Down
35 changes: 34 additions & 1 deletion zio-http/src/main/scala/zio/http/Request.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package zio.http
import java.net.InetAddress

import zio.stacktracer.TracingImplicits.disableAutoTrace
import zio.{Trace, ZIO}
import zio.{NonEmptyChunk, Trace, ZIO}

import zio.http.internal.HeaderOps

Expand Down Expand Up @@ -106,6 +106,39 @@ final case class Request(
*/
def unnest(prefix: Path): Request =
copy(url = self.url.copy(path = self.url.path.unnest(prefix)))

/**
* Returns the cookie with the given name if it exists.
*/
def cookie(name: String): Option[Cookie] =
cookies.flatMap(_.filter(_.name == name).headOption)

/**
* Uses the cookie with the given name if it exists and runs `f` afterwards.
*/
def cookieWith[R, A](name: String)(f: Cookie => ZIO[R, Throwable, A])(implicit trace: Trace): ZIO[R, Throwable, A] =
Copy link
Member

Choose a reason for hiding this comment

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

cookieWithZIO

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

cookieWithOrFailImpl(name)(identity)(f)

/**
* Uses the cookie with the given name if it exists and runs `f` afterwards.
*
* Also, you can replace a `NoSuchElementException` from an absent cookie with
* `E`.
*/
def cookieWithOrFail[R, E, A](name: String)(e: E)(f: Cookie => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def cookieWithOrFail[R, E, A](name: String)(e: E)(f: Cookie => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
def cookieWithOrFail[R, E, A](name: String)(missingCookieError: E)(f: Cookie => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

cookieWithOrFailImpl(name)(_ => e)(f)

private def cookieWithOrFailImpl[R, E, A](name: String)(e: Throwable => E)(f: Cookie => ZIO[R, E, A])(implicit
trace: Trace,
): ZIO[R, E, A] =
ZIO.getOrFailWith(e(new java.util.NoSuchElementException(s"cookie doesn't exist: $name")))(cookie(name)).flatMap(f)
Copy link
Contributor

Choose a reason for hiding this comment

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

This will create an exception with stacktrace just to be thrown away immediately. This does not seem to be efficient.
I'd change it just simply to

Suggested change
ZIO.getOrFailWith(e(new java.util.NoSuchElementException(s"cookie doesn't exist: $name")))(cookie(name)).flatMap(f)
cookie(name) match {
case Some(cookie) => f(e)
case None => ZIO.fail(e)
}

where e is not a function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added your suggestions, thank you!


/**
* Returns all cookies from the request.
*/
def cookies: Option[NonEmptyChunk[Cookie]] =
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def cookies: Option[NonEmptyChunk[Cookie]] =
def cookies: Chunk[Cookie] =

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

header(Header.Cookie).map(_.value)

}

object Request {
Expand Down