简体   繁体   English

如何匹配但不消耗 Akka HTTP 中的路径前缀?

[英]How to match, but not consume, a path prefix in Akka HTTP?

my Akka HTTP application reverse-proxies some requests to an internal HTTP system:我的 Akka HTTP 应用程序将一些请求反向代理到内部 HTTP 系统:

pathPrefix("/api/") {
  (path("some-upload-endpoint") & post) {
    withSizeLimit(10 * 1024 * 1024) {
      reverseProxyToBackend(rc)
    }
  } ~ reverseProxyToBackend(rc)
}

reverseProxyToBackend is a custom directive that takes the incoming requests, adds some headers and forwards it to the backend system, based on the remaining unmatched path. reverseProxyToBackend是一个自定义指令,它根据剩余的未匹配路径接收传入请求,添加一些标头并将其转发到后端系统。 IOW, /api/foobar becomes backend.example.com/foobar . IOW, /api/foobar变成backend.example.com/foobar

For a certain path some-upload-endpoint however I need to increase the default size limit of the frontend, as we expect larger requests to this endpoint.对于某个路径some-upload-endpoint但是我需要增加前端的默认大小限制,因为我们期望对该端点的更大请求。

Unfortunately, path("some-upload-endpoint") consumes the remaining path, so with the above code reverseProxyToBackend would forward to backend.example.com rather than backend.example.com/some-upload-endpoint , which is not what I want.不幸的是, path("some-upload-endpoint")消耗了剩余的路径,因此使用上面的代码reverseProxyToBackend将转发到backend.example.com而不是backend.example.com/some-upload-endpoint ,这不是我想要。

How can I match a path in Akka HTTP, but not consume it?如何匹配 Akka HTTP 中的路径,但使用它? In other words, what directives can I use to keep the unmatched path untouched by a path matcher?换句话说,我可以使用哪些指令来使路径匹配器不触及未匹配的路径? And if there's no such directive, how can I restore the unmatched path?如果没有这样的指令,我如何恢复不匹配的路径?

extractMatchedPath should "reconstitute" the path that was already matched. extractMatchedPath应该“重构”已经匹配的路径。 From the documentation:从文档:

The extractMatchedPath directive extracts the path that was already matched by any of the PathDirectives (or any custom ones that change the unmatched path field of the request context). extractMatchedPath 指令提取已经与任何 PathDirectives 匹配的路径(或任何更改请求上下文的不匹配路径字段的自定义路径)。 You can use it for building directives that use already matched part in their logic.您可以使用它来构建在其逻辑中使用已匹配部分的指令。

Adding to Ramon's answer, you can restore the unmatched path using mapRequestContext:添加到 Ramon 的答案中,您可以使用 mapRequestContext 恢复不匹配的路径:

extractMatchedPath { matchedPath =>
  mapRequestContext(_.withUnmatchedPath(matchedPath)) { // reset the "unmatchedPath"
    ...
  }
}

You might also be interested in pathPrefixTest, rawPathPrefixTest, etc. Basically pathPrefixTest is like pathPrefix but it does not "consume" the path segments.您可能还对 pathPrefixTest、rawPathPrefixTest 等感兴趣。基本上 pathPrefixTest 类似于 pathPrefix,但它不会“消耗”路径段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM