-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f2b83c
commit ec9dc28
Showing
17 changed files
with
142 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import zhttp.http._ | ||
import zhttp.service.Server | ||
import zio._ | ||
import zio.stream.ZStream | ||
|
||
/** | ||
* Example to encode content using a ZStream | ||
*/ | ||
object StreamingResponse extends App { | ||
// Create a message as a Chunk[Byte] | ||
val message = Chunk.fromArray("Hello world !\r\n".getBytes(HTTP_CHARSET)) | ||
|
||
// Use `Http.collect` to match on route | ||
val app = Http.collect { | ||
|
||
// Simple (non-stream) based route | ||
case Method.GET -> Root / "health" => Response.ok | ||
|
||
// ZStream powered response | ||
case Method.GET -> Root / "stream" => | ||
Response.http( | ||
status = Status.OK, | ||
headers = List(Header.contentLength(message.length.toLong)), | ||
content = HttpData.fromStream(ZStream.succeed(message)), // Encoding content using a ZStream | ||
) | ||
|
||
} | ||
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = { | ||
|
||
// Starting the server (for more advanced startup configuration checkout `HelloWorldAdvanced`) | ||
Server.start(8090, app.silent).exitCode | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package zhttp.http | ||
|
||
import io.netty.buffer.ByteBuf | ||
import zio.Chunk | ||
import zio.stream.ZStream | ||
|
||
/** | ||
* Content holder for Requests and Responses | ||
*/ | ||
sealed trait HttpData[-R, +E] extends Product with Serializable | ||
|
||
object HttpData { | ||
case object Empty extends HttpData[Any, Nothing] | ||
final case class CompleteData(data: Chunk[Byte]) extends HttpData[Any, Nothing] | ||
final case class StreamData[R, E](data: ZStream[R, E, Chunk[Byte]]) extends HttpData[R, E] | ||
|
||
/** | ||
* Helper to create CompleteData from ByteBuf | ||
*/ | ||
def fromByteBuf(byteBuf: ByteBuf): HttpData[Any, Nothing] = { | ||
val bytes = new Array[Byte](byteBuf.readableBytes) | ||
byteBuf.readBytes(bytes) | ||
HttpData.CompleteData(Chunk.fromArray(bytes)) | ||
} | ||
|
||
/** | ||
* Helper to create StreamData from Stream of Chunks | ||
*/ | ||
def fromStream[R, E](data: ZStream[R, E, Chunk[Byte]]): HttpData[R, E] = HttpData.StreamData(data) | ||
|
||
/** | ||
* Helper to create Empty HttpData | ||
*/ | ||
def empty: HttpData[Any, Nothing] = Empty | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 18 additions & 17 deletions
35
zio-http/src/main/scala/zhttp/service/EncodeResponse.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,39 @@ | ||
package zhttp.service | ||
|
||
import io.netty.buffer.{Unpooled => JUnpooled} | ||
import io.netty.handler.codec.http.{HttpHeaderNames => JHttpHeaderNames, HttpVersion => JHttpVersion} | ||
import zhttp.core.{JDefaultFullHttpResponse, JDefaultHttpHeaders, JFullHttpResponse, JHttpHeaders} | ||
import zhttp.http.{HttpContent, Response} | ||
import io.netty.handler.codec.http.{ | ||
DefaultHttpResponse => JDefaultHttpResponse, | ||
HttpHeaderNames => JHttpHeaderNames, | ||
HttpVersion => JHttpVersion, | ||
} | ||
import zhttp.core.{JDefaultHttpHeaders, JHttpHeaders} | ||
import zhttp.http.{HttpData, Response} | ||
|
||
import java.time.ZonedDateTime | ||
import java.time.format.DateTimeFormatter | ||
|
||
trait EncodeResponse { | ||
private val SERVER_NAME: String = "ZIO-Http" | ||
private val jTrailingHeaders = new JDefaultHttpHeaders(false) | ||
|
||
/** | ||
* Encode the [[zhttp.http.UHttpResponse]] to [io.netty.handler.codec.http.FullHttpResponse] | ||
*/ | ||
def encodeResponse[R](jVersion: JHttpVersion, res: Response.HttpResponse[R]): JFullHttpResponse = { | ||
val jHttpHeaders = | ||
def encodeResponse[R, E](jVersion: JHttpVersion, res: Response.HttpResponse[R, E]): JDefaultHttpResponse = { | ||
val jHttpHeaders = | ||
res.headers.foldLeft[JHttpHeaders](new JDefaultHttpHeaders()) { (jh, hh) => | ||
jh.set(hh.name, hh.value) | ||
} | ||
val jStatus = res.status.toJHttpStatus | ||
val jContentBytBuf = res.content match { | ||
case HttpContent.Complete(data) => | ||
jHttpHeaders.set(JHttpHeaderNames.SERVER, SERVER_NAME) | ||
jHttpHeaders.set(JHttpHeaderNames.DATE, s"${DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now)}") | ||
val jStatus = res.status.toJHttpStatus | ||
res.content match { | ||
case HttpData.CompleteData(data) => | ||
jHttpHeaders.set(JHttpHeaderNames.CONTENT_LENGTH, data.length) | ||
jHttpHeaders.set(JHttpHeaderNames.SERVER, SERVER_NAME) | ||
jHttpHeaders.set(JHttpHeaderNames.DATE, s"${DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now)}") | ||
JUnpooled.copiedBuffer(data.toArray) | ||
|
||
case _ => | ||
case HttpData.StreamData(_) => () | ||
|
||
case HttpData.Empty => | ||
jHttpHeaders.set(JHttpHeaderNames.CONTENT_LENGTH, 0) | ||
JUnpooled.buffer(0) | ||
} | ||
|
||
new JDefaultFullHttpResponse(jVersion, jStatus, jContentBytBuf, jHttpHeaders, jTrailingHeaders) | ||
new JDefaultHttpResponse(jVersion, jStatus, jHttpHeaders) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.