diff --git a/akka-http-core/src/main/scala/akka/http/engine/rendering/RenderSupport.scala b/akka-http-core/src/main/scala/akka/http/engine/rendering/RenderSupport.scala index 05f01f2980..af65eadcd2 100644 --- a/akka-http-core/src/main/scala/akka/http/engine/rendering/RenderSupport.scala +++ b/akka-http-core/src/main/scala/akka/http/engine/rendering/RenderSupport.scala @@ -68,13 +68,13 @@ private object RenderSupport { def onNext(elem: ByteString): List[ByteString] = { sent += elem.length if (sent > length) - throw new InvalidContentLengthException(s"HTTP message had declared Content-Length $length but entity chunk stream amounts to more bytes") + throw new InvalidContentLengthException(s"HTTP message had declared Content-Length $length but entity data stream amounts to more bytes") elem :: Nil } override def onTermination(e: Option[Throwable]): List[ByteString] = { if (sent < length) - throw new InvalidContentLengthException(s"HTTP message had declared Content-Length $length but entity chunk stream amounts to ${length - sent} bytes less") + throw new InvalidContentLengthException(s"HTTP message had declared Content-Length $length but entity data stream amounts to ${length - sent} bytes less") Nil } } diff --git a/akka-http-core/src/test/scala/akka/http/engine/rendering/ResponseRendererSpec.scala b/akka-http-core/src/test/scala/akka/http/engine/rendering/ResponseRendererSpec.scala index 3915bcb42e..1a525e8c15 100644 --- a/akka-http-core/src/test/scala/akka/http/engine/rendering/ResponseRendererSpec.scala +++ b/akka-http-core/src/test/scala/akka/http/engine/rendering/ResponseRendererSpec.scala @@ -142,14 +142,14 @@ class ResponseRendererSpec extends FreeSpec with Matchers with BeforeAndAfterAll the[RuntimeException] thrownBy { HttpResponse(200, entity = Default(ContentTypes.`application/json`, 10, source(ByteString("body123")))) should renderTo("") - } should have message "HTTP message had declared Content-Length 10 but entity chunk stream amounts to 3 bytes less" + } should have message "HTTP message had declared Content-Length 10 but entity data stream amounts to 3 bytes less" } "one chunk and incorrect (too small) Content-Length" in new TestSetup() { the[RuntimeException] thrownBy { HttpResponse(200, entity = Default(ContentTypes.`application/json`, 5, source(ByteString("body123")))) should renderTo("") - } should have message "HTTP message had declared Content-Length 5 but entity chunk stream amounts to more bytes" + } should have message "HTTP message had declared Content-Length 5 but entity data stream amounts to more bytes" } } diff --git a/akka-http-testkit/src/main/scala/akka/http/testkit/RouteTest.scala b/akka-http-testkit/src/main/scala/akka/http/testkit/RouteTest.scala index a1da02ef6e..3edf4a4def 100644 --- a/akka-http-testkit/src/main/scala/akka/http/testkit/RouteTest.scala +++ b/akka-http-testkit/src/main/scala/akka/http/testkit/RouteTest.scala @@ -126,7 +126,7 @@ trait RouteTest extends RequestBuilding with RouteTestResultComponent { request.withEffectiveUri( securedConnection = defaultHostInfo.securedConnection, defaultHostHeader = defaultHostInfo.host) - val ctx = new RequestContextImpl(effectiveRequest, setup.routingLog.requestLog(effectiveRequest)) + val ctx = new RequestContextImpl(effectiveRequest, setup.routingLog.requestLog(effectiveRequest), setup.settings) val sealedExceptionHandler = { import setup._ if (exceptionHandler.isDefault) exceptionHandler diff --git a/akka-http-tests/src/test/scala/akka/http/server/directives/BasicDirectivesSpec.scala b/akka-http-tests/src/test/scala/akka/http/server/directives/BasicDirectivesSpec.scala index 18a0fc37e5..61a192ad72 100644 --- a/akka-http-tests/src/test/scala/akka/http/server/directives/BasicDirectivesSpec.scala +++ b/akka-http-tests/src/test/scala/akka/http/server/directives/BasicDirectivesSpec.scala @@ -7,10 +7,10 @@ package directives class BasicDirectivesSpec extends RoutingSpec { - "The `rewriteUnmatchedPath` directive" should { - "rewrite the unmatched path" in { + "The `mapUnmatchedPath` directive" should { + "map the unmatched path" in { Get("/abc") ~> { - rewriteUnmatchedPath(_ / "def") { + mapUnmatchedPath(_ / "def") { path("abc" / "def") { completeOk } } } ~> check { response shouldEqual Ok } diff --git a/akka-http-tests/src/test/scala/akka/http/server/directives/MiscDirectivesSpec.scala b/akka-http-tests/src/test/scala/akka/http/server/directives/MiscDirectivesSpec.scala index 41bfea88dd..fd182d35c8 100644 --- a/akka-http-tests/src/test/scala/akka/http/server/directives/MiscDirectivesSpec.scala +++ b/akka-http-tests/src/test/scala/akka/http/server/directives/MiscDirectivesSpec.scala @@ -13,20 +13,20 @@ import Uri._ class MiscDirectivesSpec extends RoutingSpec { - "the clientIP directive" should { + "the extractClientIP directive" should { "extract from a X-Forwarded-For header" in { Get() ~> addHeaders(`X-Forwarded-For`("2.3.4.5"), RawHeader("x-real-ip", "1.2.3.4")) ~> { - clientIP { echoComplete } + extractClientIP { echoComplete } } ~> check { responseAs[String] shouldEqual "2.3.4.5" } } "extract from a Remote-Address header" in { Get() ~> addHeaders(RawHeader("x-real-ip", "1.2.3.4"), `Remote-Address`(RemoteAddress("5.6.7.8"))) ~> { - clientIP { echoComplete } + extractClientIP { echoComplete } } ~> check { responseAs[String] shouldEqual "5.6.7.8" } } "extract from a X-Real-IP header" in { Get() ~> addHeader(RawHeader("x-real-ip", "1.2.3.4")) ~> { - clientIP { echoComplete } + extractClientIP { echoComplete } } ~> check { responseAs[String] shouldEqual "1.2.3.4" } } } diff --git a/akka-http-tests/src/test/scala/akka/http/server/directives/PathDirectivesSpec.scala b/akka-http-tests/src/test/scala/akka/http/server/directives/PathDirectivesSpec.scala index 97d2f1ab24..c532717a11 100644 --- a/akka-http-tests/src/test/scala/akka/http/server/directives/PathDirectivesSpec.scala +++ b/akka-http-tests/src/test/scala/akka/http/server/directives/PathDirectivesSpec.scala @@ -7,7 +7,7 @@ package akka.http.server.directives import akka.http.server._ class PathDirectivesSpec extends RoutingSpec { - val echoUnmatchedPath = unmatchedPath { echoComplete } + val echoUnmatchedPath = extractUnmatchedPath { echoComplete } def echoCaptureAndUnmatchedPath[T]: T ⇒ Route = capture ⇒ ctx ⇒ ctx.complete(capture.toString + ":" + ctx.unmatchedPath) diff --git a/akka-http-tests/src/test/scala/akka/http/server/directives/SchemeDirectivesSpec.scala b/akka-http-tests/src/test/scala/akka/http/server/directives/SchemeDirectivesSpec.scala index 30aa251c3f..cce87b45d0 100644 --- a/akka-http-tests/src/test/scala/akka/http/server/directives/SchemeDirectivesSpec.scala +++ b/akka-http-tests/src/test/scala/akka/http/server/directives/SchemeDirectivesSpec.scala @@ -8,9 +8,9 @@ package directives import akka.http.model.StatusCodes._ class SchemeDirectivesSpec extends RoutingSpec { - "the schemeName directive" should { + "the extractScheme directive" should { "extract the Uri scheme" in { - Put("http://localhost/", "Hello") ~> schemeName { echoComplete } ~> check { responseAs[String] shouldEqual "http" } + Put("http://localhost/", "Hello") ~> extractScheme { echoComplete } ~> check { responseAs[String] shouldEqual "http" } } } diff --git a/akka-http/src/main/scala/akka/http/server/RequestContext.scala b/akka-http/src/main/scala/akka/http/server/RequestContext.scala index 6717800d2f..e650e77ab8 100644 --- a/akka-http/src/main/scala/akka/http/server/RequestContext.scala +++ b/akka-http/src/main/scala/akka/http/server/RequestContext.scala @@ -31,10 +31,18 @@ trait RequestContext { */ def log: LoggingAdapter + /** + * The default RoutingSettings to be used for configuring directives. + */ + def settings: RoutingSettings + /** * Returns a copy of this context with the given fields updated. */ - def reconfigure(executionContext: ExecutionContext = executionContext, log: LoggingAdapter = log): RequestContext + def reconfigure( + executionContext: ExecutionContext = executionContext, + log: LoggingAdapter = log, + settings: RoutingSettings = settings): RequestContext /** * Completes the request with the given ToResponseMarshallable. @@ -68,10 +76,15 @@ trait RequestContext { */ def withLog(log: LoggingAdapter): RequestContext + /** + * Returns a copy of this context with the new RoutingSettings. + */ + def withSettings(settings: RoutingSettings): RequestContext + /** * Returns a copy of this context with the HttpRequest transformed by the given function. */ - def withRequestMapped(f: HttpRequest ⇒ HttpRequest): RequestContext + def mapRequest(f: HttpRequest ⇒ HttpRequest): RequestContext /** * Returns a copy of this context with the unmatched path updated to the given one. @@ -81,7 +94,7 @@ trait RequestContext { /** * Returns a copy of this context with the unmatchedPath transformed by the given function. */ - def withUnmatchedPathMapped(f: Uri.Path ⇒ Uri.Path): RequestContext + def mapUnmatchedPath(f: Uri.Path ⇒ Uri.Path): RequestContext /** * Removes a potentially existing Accept header from the request headers. diff --git a/akka-http/src/main/scala/akka/http/server/RequestContextImpl.scala b/akka-http/src/main/scala/akka/http/server/RequestContextImpl.scala index 48618edffa..ba4660dcc3 100644 --- a/akka-http/src/main/scala/akka/http/server/RequestContextImpl.scala +++ b/akka-http/src/main/scala/akka/http/server/RequestContextImpl.scala @@ -18,13 +18,14 @@ private[http] class RequestContextImpl( val request: HttpRequest, val unmatchedPath: Uri.Path, val executionContext: ExecutionContext, - val log: LoggingAdapter) extends RequestContext { + val log: LoggingAdapter, + val settings: RoutingSettings) extends RequestContext { - def this(request: HttpRequest, log: LoggingAdapter)(implicit ec: ExecutionContext) = - this(request, request.uri.path, ec, log) + def this(request: HttpRequest, log: LoggingAdapter, settings: RoutingSettings)(implicit ec: ExecutionContext) = + this(request, request.uri.path, ec, log, settings) - def reconfigure(executionContext: ExecutionContext, log: LoggingAdapter): RequestContext = - copy(executionContext = executionContext, log = log) + def reconfigure(executionContext: ExecutionContext, log: LoggingAdapter, settings: RoutingSettings): RequestContext = + copy(executionContext = executionContext, log = log, settings = settings) override def complete(trm: ToResponseMarshallable): Future[RouteResult] = trm(request)(executionContext) @@ -46,13 +47,16 @@ private[http] class RequestContextImpl( override def withLog(log: LoggingAdapter): RequestContext = copy(log = log) - override def withRequestMapped(f: HttpRequest ⇒ HttpRequest): RequestContext = + override def withSettings(settings: RoutingSettings): RequestContext = + copy(settings = settings) + + override def mapRequest(f: HttpRequest ⇒ HttpRequest): RequestContext = copy(request = f(request)) override def withUnmatchedPath(path: Uri.Path): RequestContext = copy(unmatchedPath = path) - override def withUnmatchedPathMapped(f: Uri.Path ⇒ Uri.Path): RequestContext = + override def mapUnmatchedPath(f: Uri.Path ⇒ Uri.Path): RequestContext = copy(unmatchedPath = f(unmatchedPath)) override def withContentNegotiationDisabled: RequestContext = @@ -61,6 +65,7 @@ private[http] class RequestContextImpl( private def copy(request: HttpRequest = request, unmatchedPath: Uri.Path = unmatchedPath, executionContext: ExecutionContext = executionContext, - log: LoggingAdapter = log) = - new RequestContextImpl(request, unmatchedPath, executionContext, log) + log: LoggingAdapter = log, + settings: RoutingSettings = settings) = + new RequestContextImpl(request, unmatchedPath, executionContext, log, settings) } diff --git a/akka-http/src/main/scala/akka/http/server/ScalaRoutingDSL.scala b/akka-http/src/main/scala/akka/http/server/ScalaRoutingDSL.scala index 5f63dfac63..f9d7da029d 100644 --- a/akka-http/src/main/scala/akka/http/server/ScalaRoutingDSL.scala +++ b/akka-http/src/main/scala/akka/http/server/ScalaRoutingDSL.scala @@ -65,7 +65,7 @@ trait ScalaRoutingDSL extends Directives { import setup._ val sealedRoute = sealRoute(route)(setup) request ⇒ - sealedRoute(new RequestContextImpl(request, routingLog.requestLog(request))).fast.map { + sealedRoute(new RequestContextImpl(request, routingLog.requestLog(request), setup.settings)).fast.map { case RouteResult.Complete(response) ⇒ response case RouteResult.Rejected(rejected) ⇒ throw new IllegalStateException(s"Unhandled rejections '$rejected', unsealed RejectionHandler?!") } diff --git a/akka-http/src/main/scala/akka/http/server/directives/BasicDirectives.scala b/akka-http/src/main/scala/akka/http/server/directives/BasicDirectives.scala index 787b8f15ae..384d04ae74 100644 --- a/akka-http/src/main/scala/akka/http/server/directives/BasicDirectives.scala +++ b/akka-http/src/main/scala/akka/http/server/directives/BasicDirectives.scala @@ -23,7 +23,7 @@ trait BasicDirectives { mapInnerRoute { inner ⇒ ctx ⇒ inner(f(ctx)) } def mapRequest(f: HttpRequest ⇒ HttpRequest): Directive0 = - mapRequestContext(_ withRequestMapped f) + mapRequestContext(_ mapRequest f) def mapRouteResultFuture(f: Future[RouteResult] ⇒ Future[RouteResult]): Directive0 = Directive { inner ⇒ ctx ⇒ f(inner(())(ctx)) } @@ -111,23 +111,23 @@ trait BasicDirectives { /** * Transforms the unmatchedPath of the RequestContext using the given function. */ - def rewriteUnmatchedPath(f: Uri.Path ⇒ Uri.Path): Directive0 = - mapRequestContext(_ withUnmatchedPathMapped f) + def mapUnmatchedPath(f: Uri.Path ⇒ Uri.Path): Directive0 = + mapRequestContext(_ mapUnmatchedPath f) /** * Extracts the unmatched path from the RequestContext. */ - def unmatchedPath: Directive1[Uri.Path] = BasicDirectives._unmatchedPath + def extractUnmatchedPath: Directive1[Uri.Path] = BasicDirectives._extractUnmatchedPath /** * Extracts the complete request. */ - def requestInstance: Directive1[HttpRequest] = BasicDirectives._requestInstance + def extractRequest: Directive1[HttpRequest] = BasicDirectives._extractRequest /** * Extracts the complete request URI. */ - def requestUri: Directive1[Uri] = BasicDirectives._requestUri + def extractUri: Directive1[Uri] = BasicDirectives._extractUri /** * Runs its inner route with the given alternative [[ExecutionContext]]. @@ -152,6 +152,24 @@ trait BasicDirectives { def extractLog: Directive1[LoggingAdapter] = BasicDirectives._extractLog + /** + * Runs its inner route with the given alternative [[RoutingSettings]]. + */ + def withSettings(settings: RoutingSettings): Directive0 = + mapRequestContext(_ withSettings settings) + + /** + * Runs the inner route with settings mapped by the given function. + */ + def mapSettings(f: RoutingSettings ⇒ RoutingSettings): Directive0 = + mapRequestContext(ctx ⇒ ctx.withSettings(f(ctx.settings))) + + /** + * Extracts the [[RoutingSettings]] from the [[RequestContext]]. + */ + def extractSettings: Directive1[RoutingSettings] = + BasicDirectives._extractSettings + /** * Extracts the [[RequestContext]] itself. */ @@ -159,10 +177,11 @@ trait BasicDirectives { } object BasicDirectives extends BasicDirectives { - private val _unmatchedPath: Directive1[Uri.Path] = extract(_.unmatchedPath) - private val _requestInstance: Directive1[HttpRequest] = extract(_.request) - private val _requestUri: Directive1[Uri] = extract(_.request.uri) + private val _extractUnmatchedPath: Directive1[Uri.Path] = extract(_.unmatchedPath) + private val _extractRequest: Directive1[HttpRequest] = extract(_.request) + private val _extractUri: Directive1[Uri] = extract(_.request.uri) private val _extractExecutionContext: Directive1[ExecutionContext] = extract(_.executionContext) private val _extractLog: Directive1[LoggingAdapter] = extract(_.log) + private val _extractSettings: Directive1[RoutingSettings] = extract(_.settings) private val _extractRequestContext: Directive1[RequestContext] = extract(akka.http.util.identityFunc) } \ No newline at end of file diff --git a/akka-http/src/main/scala/akka/http/server/directives/CacheConditionDirectives.scala b/akka-http/src/main/scala/akka/http/server/directives/CacheConditionDirectives.scala index 2f48420b42..daeb506ed1 100644 --- a/akka-http/src/main/scala/akka/http/server/directives/CacheConditionDirectives.scala +++ b/akka-http/src/main/scala/akka/http/server/directives/CacheConditionDirectives.scala @@ -35,7 +35,7 @@ trait CacheConditionDirectives { def complete304(): Route = addResponseHeaders(complete(HttpResponse(NotModified))) def complete412(): Route = _.complete(PreconditionFailed) - requestInstance.flatMap { request ⇒ + extractRequest.flatMap { request ⇒ import request._ mapInnerRoute { route ⇒ def innerRouteWithRangeHeaderFilteredOut: Route = diff --git a/akka-http/src/main/scala/akka/http/server/directives/HostDirectives.scala b/akka-http/src/main/scala/akka/http/server/directives/HostDirectives.scala index 26c274a7e6..fdf05ea277 100644 --- a/akka-http/src/main/scala/akka/http/server/directives/HostDirectives.scala +++ b/akka-http/src/main/scala/akka/http/server/directives/HostDirectives.scala @@ -15,7 +15,7 @@ trait HostDirectives { /** * Extracts the hostname part of the Host header value in the request. */ - def hostName: Directive1[String] = HostDirectives._hostName + def extractHost: Directive1[String] = HostDirectives._extractHost /** * Rejects all requests with a host name different from the given ones. @@ -25,7 +25,7 @@ trait HostDirectives { /** * Rejects all requests for whose host name the given predicate function returns false. */ - def host(predicate: String ⇒ Boolean): Directive0 = hostName.require(predicate) + def host(predicate: String ⇒ Boolean): Directive0 = extractHost.require(predicate) /** * Rejects all requests with a host name that doesn't have a prefix matching the given regular expression. @@ -35,7 +35,7 @@ trait HostDirectives { */ def host(regex: Regex): Directive1[String] = { def forFunc(regexMatch: String ⇒ Option[String]): Directive1[String] = { - hostName.flatMap { name ⇒ + extractHost.flatMap { name ⇒ regexMatch(name) match { case Some(matched) ⇒ provide(matched) case None ⇒ reject @@ -56,6 +56,6 @@ trait HostDirectives { object HostDirectives extends HostDirectives { import BasicDirectives._ - private val _hostName: Directive1[String] = + private val _extractHost: Directive1[String] = extract(_.request.uri.authority.host.address) } diff --git a/akka-http/src/main/scala/akka/http/server/directives/MiscDirectives.scala b/akka-http/src/main/scala/akka/http/server/directives/MiscDirectives.scala index 9eef1fa050..e0d0cd28d3 100644 --- a/akka-http/src/main/scala/akka/http/server/directives/MiscDirectives.scala +++ b/akka-http/src/main/scala/akka/http/server/directives/MiscDirectives.scala @@ -22,7 +22,7 @@ trait MiscDirectives { * Directive extracting the IP of the client from either the X-Forwarded-For, Remote-Address or X-Real-IP header * (in that order of priority). */ - def clientIP: Directive1[RemoteAddress] = MiscDirectives._clientIP + def extractClientIP: Directive1[RemoteAddress] = MiscDirectives._extractClientIP /** * Rejects the request if its entity is not empty. @@ -49,7 +49,7 @@ object MiscDirectives extends MiscDirectives { import RouteDirectives._ import RouteResult._ - private val _clientIP: Directive1[RemoteAddress] = + private val _extractClientIP: Directive1[RemoteAddress] = headerValuePF { case `X-Forwarded-For`(Seq(address, _*)) ⇒ address } | headerValuePF { case `Remote-Address`(address) ⇒ address } | headerValuePF { case h if h.is("x-real-ip") ⇒ RemoteAddress(h.value) } diff --git a/akka-http/src/main/scala/akka/http/server/directives/SchemeDirectives.scala b/akka-http/src/main/scala/akka/http/server/directives/SchemeDirectives.scala index 2616fb137f..6e7815f68a 100644 --- a/akka-http/src/main/scala/akka/http/server/directives/SchemeDirectives.scala +++ b/akka-http/src/main/scala/akka/http/server/directives/SchemeDirectives.scala @@ -11,17 +11,17 @@ trait SchemeDirectives { /** * Extracts the Uri scheme from the request. */ - def schemeName: Directive1[String] = SchemeDirectives._schemeName + def extractScheme: Directive1[String] = SchemeDirectives._extractScheme /** * Rejects all requests whose Uri scheme does not match the given one. */ def scheme(name: String): Directive0 = - schemeName.require(_ == name, SchemeRejection(name)) & cancelRejections(classOf[SchemeRejection]) + extractScheme.require(_ == name, SchemeRejection(name)) & cancelRejections(classOf[SchemeRejection]) } object SchemeDirectives extends SchemeDirectives { import BasicDirectives._ - private val _schemeName: Directive1[String] = extract(_.request.uri.scheme) + private val _extractScheme: Directive1[String] = extract(_.request.uri.scheme) }