Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize server request .uri for pekko-http and akka-http #3566

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sttp.tapir.server.akkahttp
import akka.http.scaladsl.server.RequestContext
import akka.http.scaladsl.model.headers.{`Content-Length`, `Content-Type`}
import akka.http.scaladsl.model.{Uri => AkkaUri}
import sttp.model.Uri.{Authority, FragmentSegment, HostSegment, PathSegments, QuerySegment}
import sttp.model.{Header, Method, QueryParams, Uri}
import sttp.tapir.{AttributeKey, AttributeMap}
import sttp.tapir.model.{ConnectionInfo, ServerRequest}
Expand All @@ -27,7 +28,35 @@ private[akkahttp] case class AkkaServerRequest(ctx: RequestContext, attributes:
}
override lazy val queryParameters: QueryParams = QueryParams.fromMultiMap(ctx.request.uri.query().toMultiMap)
override lazy val method: Method = Method(ctx.request.method.value.toUpperCase)
override lazy val uri: Uri = Uri.unsafeParse(ctx.request.uri.toString())

private def queryToSegments(query: AkkaUri.Query): List[QuerySegment] = {
@tailrec
def run(q: AkkaUri.Query, acc: List[QuerySegment]): List[QuerySegment] = q match {
case AkkaUri.Query.Cons(k, v, tail) => {
if (k.isEmpty)
run(tail, QuerySegment.Value(v) :: acc)
else if (v.isEmpty)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's not necessarily correct - you'd have to check how Uris: ?a (just the value), ?a= and ?=b (is this even valid?) get parsed by both. There might be some edge cases where in one of those cases we actually have a key-value pair with an empty value, as opposed to an only-value query segment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamw I checked it, and pekko-http parses "http://example.com?a=&=c&d to:

  • key="a", value=""
  • key="", value="c"
  • key=""d", value=""

And if we do sttp.model.Uri.unsafeParse(pekkoUri.toString), Tapir will construct:

  • KeyValue(k="a", v="")
  • KeyValue(k="", v="c")
  • Value(v="d")

Getting such a representation manually doesn't seem possible by traversing pekko's datatypes, because ?d is a Cons(k="d", v=""), just like it would be a ?d=.

What do we risk specifically by requiring this case to be a Value(v ="d") instead of a KeyValue(k="d", v="")?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can then simply represent a= as a KeyValue, and the others as a Value? That should be close enough. If pekko doesn't differentiate here, we don't have to as well

Copy link
Member Author

@kciesielski kciesielski Mar 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no difference between ?a and ?a= for Pekko, so we need to decide whether to treat them both as Value or a KeyValue with an empty value.

run(tail, QuerySegment.Value(k) :: acc)
else
run(tail, QuerySegment.KeyValue(k, v) :: acc)
}
case AkkaUri.Query.Empty => acc.reverse
}
run(query, Nil)
}

override lazy val uri: Uri = {
val pekkoUri = ctx.request.uri
Uri(
Some(pekkoUri.scheme),
// UserInfo is available only as a raw string, but we can skip it as it's not needed
Some(Authority(userInfo = None, HostSegment(pekkoUri.authority.host.address), Some(pekkoUri.effectivePort))),
PathSegments.absoluteOrEmptyS(pathSegments ++ (if (pekkoUri.path.endsWithSlash) Seq("") else Nil)),
queryToSegments(ctx.request.uri.query()),
ctx.request.uri.fragment.map(f => FragmentSegment(f))
)
}


private val EmptyContentType = "none/none"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package sttp.tapir.server.pekkohttp

import org.apache.pekko.http.scaladsl.server.RequestContext
import org.apache.pekko.http.scaladsl.model.{Uri => PekkoUri}
import org.apache.pekko.http.scaladsl.server.RequestContext
import sttp.model.Uri.{Authority, FragmentSegment, HostSegment, PathSegments, QuerySegment}
import sttp.model.{Header, HeaderNames, Method, QueryParams, Uri}
import sttp.tapir.{AttributeKey, AttributeMap}
import sttp.tapir.model.{ConnectionInfo, ServerRequest}
import sttp.tapir.{AttributeKey, AttributeMap}

import scala.annotation.tailrec
import scala.collection.immutable.Seq
Expand All @@ -26,7 +27,34 @@ private[pekkohttp] case class PekkoServerRequest(ctx: RequestContext, attributes
}
override lazy val queryParameters: QueryParams = QueryParams.fromMultiMap(ctx.request.uri.query().toMultiMap)
override lazy val method: Method = Method(ctx.request.method.value.toUpperCase)
override lazy val uri: Uri = Uri.unsafeParse(ctx.request.uri.toString())

private def queryToSegments(query: PekkoUri.Query): List[QuerySegment] = {
@tailrec
def run(q: PekkoUri.Query, acc: List[QuerySegment]): List[QuerySegment] = q match {
case PekkoUri.Query.Cons(k, v, tail) => {
if (k.isEmpty)
run(tail, QuerySegment.Value(v) :: acc)
else if (v.isEmpty)
run(tail, QuerySegment.Value(k) :: acc)
else
run(tail, QuerySegment.KeyValue(k, v) :: acc)
}
case PekkoUri.Query.Empty => acc.reverse
}
run(query, Nil)
}

override lazy val uri: Uri = {
val pekkoUri = ctx.request.uri
Uri(
Some(pekkoUri.scheme),
// UserInfo is available only as a raw string, but we can skip it as it's not needed
Some(Authority(userInfo = None, HostSegment(pekkoUri.authority.host.address), Some(pekkoUri.effectivePort))),
PathSegments.absoluteOrEmptyS(pathSegments ++ (if (pekkoUri.path.endsWithSlash) Seq("") else Nil)),
queryToSegments(ctx.request.uri.query()),
ctx.request.uri.fragment.map(f => FragmentSegment(f))
)
}

private val EmptyContentType = "none/none"

Expand Down
Loading