All Downloads are FREE. Search and download functionalities are using the official Maven repository.

zhttp.service.EncodeRequest.scala Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC11
Show newest version
package zhttp.service

import io.netty.handler.codec.http.{DefaultFullHttpRequest, FullHttpRequest, HttpHeaderNames}
import zhttp.http.Request
import zio.Task

trait EncodeRequest {

  /**
   * Converts client params to JFullHttpRequest
   */
  def encode(req: Request): Task[FullHttpRequest] =
    req.bodyAsByteBuf.map { content =>
      val method   = req.method.toJava
      val jVersion = req.version.toJava

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

      val encodedReqHeaders = req.headers.encode

      val headers = req.url.host match {
        case Some(value) => encodedReqHeaders.set(HttpHeaderNames.HOST, value)
        case None        => encodedReqHeaders
      }

      val writerIndex = content.writerIndex()
      if (writerIndex != 0) {
        headers.set(HttpHeaderNames.CONTENT_LENGTH, writerIndex.toString)
      }

      // TODO: we should also add a default user-agent req header as some APIs might reject requests without it.
      val jReq = new DefaultFullHttpRequest(jVersion, method, path, content)
      jReq.headers().set(headers)

      jReq
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy