Skip to content

Commit

Permalink
Optimize Http4sServerRequest.uri (#3543)
Browse files Browse the repository at this point in the history
  • Loading branch information
kciesielski authored Feb 29, 2024
1 parent 485a616 commit 1a86175
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package sttp.tapir.server.http4s

import org.http4s.Request
import sttp.model.Uri.{Authority, FragmentSegment, HostSegment, PathSegments, QuerySegment, UserInfo}
import sttp.model.{Header, Method, QueryParams, Uri}
import sttp.tapir.{AttributeKey, AttributeMap}
import sttp.tapir.model.{ConnectionInfo, ServerRequest}
import sttp.tapir.{AttributeKey, AttributeMap}

import scala.collection.immutable.Seq

Expand All @@ -24,7 +25,14 @@ private[http4s] case class Http4sServerRequest[F[_]](req: Request[F], attributes
override lazy val queryParameters: QueryParams = QueryParams.fromMultiMap(req.multiParams)

override def method: Method = Method(req.method.name.toUpperCase)
override def uri: Uri = Uri.unsafeParse(req.uri.toString())
override lazy val uri: Uri =
Uri.apply(
req.uri.scheme.map(_.value),
req.uri.authority.map(a => Authority(a.userInfo.map(u => UserInfo(u.username, u.password)), HostSegment(a.host.value), a.port)),
PathSegments.absoluteOrEmptyS(req.uri.path.segments.map(_.decoded()) ++ (if (req.uri.path.endsWithSlash) Seq("") else Nil)),
req.uri.query.pairs.map(kv => kv._2.map(v => QuerySegment.KeyValue(kv._1, v)).getOrElse(QuerySegment.Value(kv._1))),
req.uri.fragment.map(f => FragmentSegment(f))
)
override lazy val headers: Seq[Header] = req.headers.headers.map(h => Header(h.name.toString, h.value))

override def attribute[T](k: AttributeKey[T]): Option[T] = attributes.get(k)
Expand Down

0 comments on commit 1a86175

Please sign in to comment.