Skip to content

Commit

Permalink
Merge branch 'main' into fix_cors_allowed_headers
Browse files Browse the repository at this point in the history
  • Loading branch information
jdegoes authored Jul 30, 2024
2 parents f384fa1 + 12f0c26 commit 126cb87
Show file tree
Hide file tree
Showing 33 changed files with 1,419 additions and 482 deletions.
50 changes: 37 additions & 13 deletions .github/workflows/site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,40 +57,64 @@ jobs:
run: sbt docs/publishToNpm
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
generate-readme:
name: Generate README
update-readme:
name: Update README
runs-on: ubuntu-latest
if: ${{ (github.event_name == 'push') || ((github.event_name == 'release') && (github.event.action == 'published')) }}
continue-on-error: false
if: ${{ github.event_name == 'push' }}
steps:
- name: Git Checkout
uses: actions/checkout@v3.3.0
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
fetch-depth: '0'
- name: Install libuv
run: sudo apt-get update && sudo apt-get install -y libuv1-dev
- name: Setup Scala
uses: actions/setup-java@v3.9.0
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 17
distribution: corretto
java-version: '17'
check-latest: true
- name: Cache Dependencies
uses: coursier/cache-action@v6
- name: Generate Readme
run: sbt docs/generateReadme
run: sbt docs/generateReadme
- name: Commit Changes
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git config --local user.email "zio-assistant[bot]@users.noreply.github.com"
git config --local user.name "ZIO Assistant"
git add README.md
git commit -m "Update README.md" || echo "No changes to commit"
- name: Generate Token
id: generate-token
uses: zio/generate-github-app-token@v1.0.0
with:
app_id: ${{ secrets.APP_ID }}
app_private_key: ${{ secrets.APP_PRIVATE_KEY }}
- name: Create Pull Request
uses: peter-evans/create-pull-request@v4.2.3
id: cpr
uses: peter-evans/create-pull-request@v6
with:
body: |-
Autogenerated changes after running the `sbt docs/generateReadme` command of the [zio-sbt-website](https://zio.dev/zio-sbt) plugin.
I will automatically update the README.md file whenever there is new change for README.md, e.g.
I will automatically update the README.md file whenever there is a new change for README.md, e.g.
- After each release, I will update the version in the installation section.
- After any changes to the "docs/index.md" file, I will update the README.md file accordingly.
branch: zio-sbt-website/update-readme
commit-message: Update README.md
token: ${{ steps.generate-token.outputs.token }}
delete-branch: true
title: Update README.md
- name: Approve PR
if: ${{ steps.cpr.outputs.pull-request-number }}
run: gh pr review "$PR_URL" --approve
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_URL: ${{ steps.cpr.outputs.pull-request-url }}
- name: Enable Auto-Merge
if: ${{ steps.cpr.outputs.pull-request-number }}
run: gh pr merge --auto --squash "$PR_URL" || gh pr merge --squash "$PR_URL"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_URL: ${{ steps.cpr.outputs.pull-request-url }}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Some of the key features of ZIO HTTP are:
Setup via `build.sbt`:

```scala
libraryDependencies += "dev.zio" %% "zio-http" % "3.0.0-RC7"
libraryDependencies += "dev.zio" %% "zio-http" % "3.0.0-RC9"
```

**NOTES ON VERSIONING:**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ private[cli] object HttpOptions {
private[cli] def optionsFromSegment(segment: SegmentCodec[_]): Options[String] = {
def fromSegment[A](segment: SegmentCodec[A]): Options[String] =
segment match {
case SegmentCodec.UUID(name) =>
case SegmentCodec.UUID(name) =>
Options
.text(name)
.mapOrFail(str =>
Expand All @@ -324,13 +324,14 @@ private[cli] object HttpOptions {
},
)
.map(_.toString)
case SegmentCodec.Text(name) => Options.text(name)
case SegmentCodec.IntSeg(name) => Options.integer(name).map(_.toInt).map(_.toString)
case SegmentCodec.LongSeg(name) => Options.integer(name).map(_.toInt).map(_.toString)
case SegmentCodec.BoolSeg(name) => Options.boolean(name).map(_.toString)
case SegmentCodec.Literal(value) => Options.Empty.map(_ => value)
case SegmentCodec.Trailing => Options.none.map(_.toString)
case SegmentCodec.Empty => Options.none.map(_.toString)
case SegmentCodec.Text(name) => Options.text(name)
case SegmentCodec.IntSeg(name) => Options.integer(name).map(_.toInt).map(_.toString)
case SegmentCodec.LongSeg(name) => Options.integer(name).map(_.toInt).map(_.toString)
case SegmentCodec.BoolSeg(name) => Options.boolean(name).map(_.toString)
case SegmentCodec.Literal(value) => Options.Empty.map(_ => value)
case SegmentCodec.Trailing => Options.none.map(_.toString)
case SegmentCodec.Empty => Options.none.map(_.toString)
case SegmentCodec.Combined(_, _, _) => throw new IllegalArgumentException("Combined segment not supported")
}

fromSegment(segment)
Expand Down
18 changes: 10 additions & 8 deletions zio-http-cli/src/test/scala/zio/http/endpoint/cli/CommandGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ object CommandGen {
def getSegment(segment: SegmentCodec[_]): (String, String) = {
def fromSegment[A](segment: SegmentCodec[A]): (String, String) =
segment match {
case SegmentCodec.UUID(name) => (name, "text")
case SegmentCodec.Text(name) => (name, "text")
case SegmentCodec.IntSeg(name) => (name, "integer")
case SegmentCodec.LongSeg(name) => (name, "integer")
case SegmentCodec.BoolSeg(name) => (name, "boolean")
case SegmentCodec.Literal(_) => ("", "")
case SegmentCodec.Trailing => ("", "")
case SegmentCodec.Empty => ("", "")
case SegmentCodec.UUID(name) => (name, "text")
case SegmentCodec.Text(name) => (name, "text")
case SegmentCodec.IntSeg(name) => (name, "integer")
case SegmentCodec.LongSeg(name) => (name, "integer")
case SegmentCodec.BoolSeg(name) => (name, "boolean")
case SegmentCodec.Literal(_) => ("", "")
case SegmentCodec.Trailing => ("", "")
case SegmentCodec.Empty => ("", "")
case SegmentCodec.Combined(left, right, combiner) =>
???
}

fromSegment(segment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ object CodeGenSpec extends ZIOSpecDefault {
val code = EndpointGen.fromOpenAPI(openAPI)

val tempDir = Files.createTempDirectory("codegen")
println(tempDir)
CodeGen.writeFiles(code, java.nio.file.Paths.get(tempDir.toString, "test"), "test", Some(scalaFmtPath))

fileShouldBe(
Expand Down Expand Up @@ -240,7 +239,6 @@ object CodeGenSpec extends ZIOSpecDefault {
val code = EndpointGen.fromOpenAPI(openAPI)

val tempDir = Files.createTempDirectory("codegen")
println(tempDir)
CodeGen.writeFiles(code, java.nio.file.Paths.get(tempDir.toString, "test"), "test", Some(scalaFmtPath))

fileShouldBe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ object NettyResponse {
onComplete.unsafe.done(Exit.succeed(ChannelState.forStatus(status)))
Response(status, headers, Body.empty)
} else {
val contentType = headers.get(Header.ContentType)
val responseHandler = new ClientResponseStreamHandler(onComplete, keepAlive, status)
ctx
.pipeline()
Expand All @@ -64,7 +65,11 @@ object NettyResponse {
responseHandler,
): Unit

val data = NettyBody.fromAsync(callback => responseHandler.connect(callback), knownContentLength)
val data = NettyBody.fromAsync(
callback => responseHandler.connect(callback),
knownContentLength,
contentType.map(_.renderedValue),
)
Response(status, headers, data)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@

package zio.http.netty.client

import zio.Unsafe
import zio.stacktracer.TracingImplicits.disableAutoTrace
import zio.{Task, Trace, Unsafe, ZIO}

import zio.http.netty._
import zio.http.netty.model.Conversions
import zio.http.{Body, Request}

import io.netty.buffer.{ByteBuf, EmptyByteBuf, Unpooled}
import io.netty.buffer.Unpooled
import io.netty.handler.codec.http.{DefaultFullHttpRequest, DefaultHttpRequest, HttpHeaderNames, HttpRequest}

private[zio] object NettyRequestEncoder {
Expand All @@ -34,12 +33,7 @@ private[zio] object NettyRequestEncoder {
def encode(req: Request): HttpRequest = {
val method = Conversions.methodToNetty(req.method)
val jVersion = Conversions.versionToNetty(req.version)

def replaceEmptyPathWithSlash(url: zio.http.URL) = if (url.path.isEmpty) url.addLeadingSlash else url

// As per the spec, the path should contain only the relative path.
// Host and port information should be in the headers.
val path = replaceEmptyPathWithSlash(req.url).relative.addLeadingSlash.encode
val path = Conversions.urlToNetty(req.url)

val headers = Conversions.headersToNetty(req.allHeaders)

Expand Down Expand Up @@ -69,4 +63,5 @@ private[zio] object NettyRequestEncoder {
new DefaultHttpRequest(jVersion, method, path, headers)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ private[netty] object Conversions {
case Headers.Empty => new DefaultHttpHeaders()
}

def urlToNetty(url: URL): String = {
// As per the spec, the path should contain only the relative path.
// Host and port information should be in the headers.
val url0 = if (url.path.isEmpty) url.addLeadingSlash else url
url0.relative.addLeadingSlash.encode
}

private def nettyHeadersIterator(headers: HttpHeaders): Iterator[Header] =
new AbstractIterator[Header] {
private val nettyIterator = headers.iteratorCharSequence()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import java.io.IOException
import java.net.InetSocketAddress
import java.util.concurrent.atomic.LongAdder

import scala.annotation.tailrec
import scala.util.control.NonFatal

import zio._
Expand All @@ -29,7 +28,6 @@ import zio.stacktracer.TracingImplicits.disableAutoTrace
import zio.http.Body.WebsocketBody
import zio.http._
import zio.http.netty._
import zio.http.netty.client.NettyRequestEncoder
import zio.http.netty.model.Conversions
import zio.http.netty.socket.NettySocketProtocol

Expand Down Expand Up @@ -287,7 +285,12 @@ private[zio] final case class ServerInboundHandler(
)
.addLast(Names.WebSocketHandler, new WebSocketAppHandler(runtime, queue, None))

val jReq = NettyRequestEncoder.encode(request)
val jReq = new DefaultFullHttpRequest(
Conversions.versionToNetty(request.version),
Conversions.methodToNetty(request.method),
Conversions.urlToNetty(request.url),
)
jReq.headers().setAll(Conversions.headersToNetty(request.allHeaders))
ctx.channel().eventLoop().submit { () => ctx.fireChannelRead(jReq) }: Unit
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ object ClientStreamingSpec extends HttpRunnableSpec {
port <- server(streamingServer)
client <- ZIO.service[Client]
result <- check(Gen.int(1, N)) { chunkSize =>
(for {
for {
bytes <- Random.nextBytes(N)
form = Form(
Chunk(
Expand All @@ -233,7 +233,7 @@ object ClientStreamingSpec extends HttpRunnableSpec {
collected.map.contains("file"),
collected.map.contains("foo"),
collected.get("file").get.asInstanceOf[FormField.Binary].data == bytes,
)).tapErrorCause(cause => ZIO.debug(cause.prettyPrint))
)
}
} yield result
} @@ samples(20) @@ TestAspect.ifEnvNotSet("CI"),
Expand Down
2 changes: 1 addition & 1 deletion zio-http/jvm/src/test/scala/zio/http/DualSSLSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object DualSSLSpec extends ZIOHttpSpec {
includeClientCert = true,
)

val config = Server.Config.default.port(8073).ssl(sslConfigWithTrustedClient)
val config = Server.Config.default.port(8073).ssl(sslConfigWithTrustedClient).logWarningOnFatalError(false)

val payload = Gen.alphaNumericStringBounded(10000, 20000)

Expand Down
17 changes: 7 additions & 10 deletions zio-http/jvm/src/test/scala/zio/http/MultipartMixedSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ object MultipartMixedSpec extends ZIOHttpSpec {

test("property") {
check(gens.genTestCase) { testCase =>
zio.Console.printLine(testCase) *> testCase.runTests
testCase.runTests
}
} @@ TestAspect.shrinks(0)

Expand Down Expand Up @@ -248,8 +248,8 @@ object MultipartMixedSpec extends ZIOHttpSpec {
gens.breaker.fixed(512),
)

val innerTests = inner.runTests.map(_.label("inner")).debug("inner")
val outerTests = outer.runTests.map(_.label("outer")).debug("outer")
val innerTests = inner.runTests.map(_.label("inner"))
val outerTests = outer.runTests.map(_.label("outer"))

val nestedTests = {
val expectedNested = Nested.Multi(
Expand All @@ -262,7 +262,6 @@ object MultipartMixedSpec extends ZIOHttpSpec {
outer.partsToNested.map { collected =>
zio.test.assert(collected)(Assertion.equalTo(expectedNested)).label("nestedTests")
}
.debug("nestedTests")
}

(innerTests <*> outerTests <*> nestedTests).map { case (i, o, n) =>
Expand Down Expand Up @@ -308,8 +307,8 @@ object MultipartMixedSpec extends ZIOHttpSpec {
gens.breaker.fixed(512),
)

val innerTests = inner.runTests.map(_.label("inner")).debug("inner")
val outerTests = outer.runTests.map(_.label("outer")).debug("outer")
val innerTests = inner.runTests.map(_.label("inner"))
val outerTests = outer.runTests.map(_.label("outer"))

val nestedTests = {
val expectedNested = Nested.Multi(
Expand All @@ -322,7 +321,6 @@ object MultipartMixedSpec extends ZIOHttpSpec {
outer.partsToNested.map { collected =>
zio.test.assert(collected)(Assertion.equalTo(expectedNested)).label("nestedTests")
}
.debug("nestedTests")
}

(innerTests <*> outerTests <*> nestedTests).map { case (i, o, n) =>
Expand Down Expand Up @@ -364,8 +362,8 @@ object MultipartMixedSpec extends ZIOHttpSpec {
gens.breaker.fixed(512),
)

val innerTests = inner.runTests.map(_.label("inner")).debug("inner")
val outerTests = outer.runTests.map(_.label("outer")).debug("outer")
val innerTests = inner.runTests.map(_.label("inner"))
val outerTests = outer.runTests.map(_.label("outer"))

val nestedTests = {
val expectedNested = Nested.Multi(
Expand All @@ -379,7 +377,6 @@ object MultipartMixedSpec extends ZIOHttpSpec {
outer.partsToNested.map { collected =>
zio.test.assert(collected)(Assertion.equalTo(expectedNested)).label("nestedTests")
}
.debug("nestedTests")
}

(innerTests <*> outerTests <*> nestedTests).map { case (i, o, n) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,14 @@ object RequestStreamingServerSpec extends HttpRunnableSpec {
val host = req.headers.get(Header.Host).get
val newRequest =
req.copy(url = req.url.path("/2").host(host.hostAddress).port(host.port.getOrElse(80)))
ZIO.debug(s"#1: got response, forwarding") *>
ZIO.serviceWithZIO[Client] { client =>
client.request(newRequest)
}
ZIO.serviceWithZIO[Client] { client =>
client.request(newRequest)
}
},
Method.POST / "2" -> handler { (req: Request) =>
ZIO.debug("#2: got response, collecting") *>
req.body.asChunk.map { body =>
Response.text(body.length.toString)
}
req.body.asChunk.map { body =>
Response.text(body.length.toString)
}
},
).sandbox
val sizes = Chunk(0, 8192, 1024 * 1024)
Expand Down
2 changes: 1 addition & 1 deletion zio-http/jvm/src/test/scala/zio/http/SSLSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import zio.http.netty.client.NettyClientDriver
object SSLSpec extends ZIOHttpSpec {

val sslConfig = SSLConfig.fromResource("server.crt", "server.key")
val config = Server.Config.default.port(8073).ssl(sslConfig)
val config = Server.Config.default.port(8073).ssl(sslConfig).logWarningOnFatalError(false)

val clientSSL1 = ClientSSLConfig.FromCertResource("server.crt")
val clientSSL2 = ClientSSLConfig.FromCertResource("ss2.crt.pem")
Expand Down
Loading

0 comments on commit 126cb87

Please sign in to comment.