Skip to content

Commit

Permalink
Fix: Host header must include port (#1373)
Browse files Browse the repository at this point in the history
* Fix for #1372 - Host header must include port

* Fix for #1372 - code review fixes

* Fix for #1372 - fix formatting

* Fix for #1372 - disambiguate priorities in test

* Fix for #1372 - formatting

* Fix for #1372 - Scala 2.12 compatibility for the tests
  • Loading branch information
paul-lysak authored Aug 29, 2022
1 parent 0273be9 commit 7e76ce3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
8 changes: 2 additions & 6 deletions zio-http/src/main/scala/zhttp/service/Client.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import zhttp.service.client.{ClientInboundHandler, ClientSSLHandler}
import zhttp.socket.SocketApp
import zio.{Promise, Scope, Task, ZIO}

import java.net.{InetSocketAddress, URI}
import java.net.InetSocketAddress

final case class Client[R](rtm: HttpRuntime[R], cf: JChannelFactory[JChannel], el: JEventLoopGroup)
extends HttpMessageCodec {
Expand Down Expand Up @@ -78,11 +78,7 @@ final case class Client[R](rtm: HttpRuntime[R], cf: JChannelFactory[JChannel], e
): JChannelFuture = {

try {
val uri = new URI(jReq.uri())
val host = if (uri.getHost == null) jReq.headers().get(HeaderNames.host) else uri.getHost

assert(host != null, "Host name is required")

val host = req.url.host.getOrElse { assert(false, "Host name is required"); "" }
val port = req.url.port.getOrElse(80)

val isWebSocket = req.url.scheme.exists(_.isWebSocket)
Expand Down
4 changes: 2 additions & 2 deletions zio-http/src/main/scala/zhttp/service/EncodeRequest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ trait EncodeRequest {
val encodedReqHeaders = req.headers.encode

val headers = req.url.host match {
case Some(value) => encodedReqHeaders.set(HttpHeaderNames.HOST, value)
case None => encodedReqHeaders
case Some(host) => encodedReqHeaders.set(HttpHeaderNames.HOST, req.url.port.fold(host)(port => s"$host:$port"))
case _ => encodedReqHeaders
}

val writerIndex = content.writerIndex()
Expand Down
10 changes: 8 additions & 2 deletions zio-http/src/test/scala/zhttp/http/EncodeRequestSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,20 @@ object EncodeRequestSpec extends ZIOSpecDefault with EncodeRequest {
check(anyClientParam) { params =>
val req =
encode(params).map(i => Option(i.headers().get(HttpHeaderNames.HOST)))
assertZIO(req)(equalTo(params.url.host))
assertZIO(req)(equalTo((params.url.host, params.url.port) match {
case (Some(host), Some(port)) => Some(s"$host:$port")
case _ => params.url.host
}))
}
},
test("host header when absolute url") {
check(clientParamWithAbsoluteUrl) { params =>
val req = encode(params)
.map(i => Option(i.headers().get(HttpHeaderNames.HOST)))
assertZIO(req)(equalTo(params.url.host))
assertZIO(req)(equalTo((params.url.host, params.url.port) match {
case (Some(host), Some(port)) => Some(s"$host:$port")
case _ => params.url.host
}))
}
},
test("only one host header exists") {
Expand Down

0 comments on commit 7e76ce3

Please sign in to comment.