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

Feature: Add data in Request trait #1017

Merged
merged 1 commit into from
Feb 11, 2022
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
30 changes: 18 additions & 12 deletions zio-http/src/main/scala/zhttp/http/Request.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ trait Request extends HeaderExtension[Request] { self =>
override def url: URL = u
override def headers: Headers = h
override def remoteAddress: Option[InetAddress] = self.remoteAddress
override private[zhttp] def bodyAsByteBuf = self.bodyAsByteBuf
override def data: HttpData = self.data
}
}

/**
* Decodes the body as a HttpData
*/
def data: HttpData

/**
* Decodes the content of request as a Chunk of Bytes
*/
Expand Down Expand Up @@ -83,7 +88,7 @@ trait Request extends HeaderExtension[Request] { self =>
*/
def url: URL

private[zhttp] def bodyAsByteBuf: Task[ByteBuf]
private[zhttp] def bodyAsByteBuf: Task[ByteBuf] = data.toByteBuf
}

object Request {
Expand All @@ -102,12 +107,13 @@ object Request {
val u = url
val h = headers
val ra = remoteAddress
val d = data
new Request {
override def method: Method = m
override def url: URL = u
override def headers: Headers = h
override def remoteAddress: Option[InetAddress] = ra
override private[zhttp] def bodyAsByteBuf: Task[ByteBuf] = data.toByteBuf
override def method: Method = m
override def url: URL = u
override def headers: Headers = h
override def remoteAddress: Option[InetAddress] = ra
override def data: HttpData = d
}
}

Expand All @@ -127,11 +133,11 @@ object Request {
* Lift request to TypedRequest with option to extract params
*/
final class ParameterizedRequest[A](req: Request, val params: A) extends Request {
override def headers: Headers = req.headers
override def method: Method = req.method
override def remoteAddress: Option[InetAddress] = req.remoteAddress
override def url: URL = req.url
override private[zhttp] def bodyAsByteBuf: Task[ByteBuf] = req.bodyAsByteBuf
override def headers: Headers = req.headers
override def method: Method = req.method
override def remoteAddress: Option[InetAddress] = req.remoteAddress
override def url: URL = req.url
override def data: HttpData = req.data
}

object ParameterizedRequest {
Expand Down
7 changes: 3 additions & 4 deletions zio-http/src/main/scala/zhttp/service/Handler.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package zhttp.service

import io.netty.buffer.ByteBuf
import io.netty.channel.ChannelHandler.Sharable
import io.netty.channel.{ChannelHandlerContext, SimpleChannelInboundHandler}
import io.netty.handler.codec.http._
import zhttp.http._
import zhttp.service.server.WebSocketUpgrade
import zio.{Task, UIO, ZIO}
import zio.{UIO, ZIO}

import java.net.{InetAddress, InetSocketAddress}

Expand All @@ -33,14 +32,14 @@ private[zhttp] final case class Handler[R](

override def headers: Headers = Headers.make(jReq.headers())

override private[zhttp] def bodyAsByteBuf: Task[ByteBuf] = Task(jReq.content())

override def remoteAddress: Option[InetAddress] = {
ctx.channel().remoteAddress() match {
case m: InetSocketAddress => Some(m.getAddress)
case _ => None
}
}

override def data: HttpData = HttpData.fromByteBuf(jReq.content())
},
)
}
Expand Down