=htp Extract RemoteAddress.Unknown when cannot find client ip (#21289)

* handle unknown client ip

* updated doc

* fixed java dsl test
This commit is contained in:
dyk 2016-08-26 16:47:36 +02:00 committed by Konrad Malawski
parent 3e86c2a44f
commit 0e302d2236
4 changed files with 9 additions and 3 deletions

View file

@ -11,7 +11,7 @@ Signature
Description
-----------
Provides the value of ``X-Forwarded-For``, ``Remote-Address``, or ``X-Real-IP`` headers as an instance of ``HttpIp``.
Provides the value of ``X-Forwarded-For``, ``Remote-Address``, or ``X-Real-IP`` headers as an instance of ``RemoteAddress``.
The akka-http server engine adds the ``Remote-Address`` header to every request automatically if the respective
setting ``akka.http.server.remote-address-header`` is set to ``on``. Per default it is set to ``off``.

View file

@ -72,7 +72,7 @@ public class MiscDirectivesTest extends JUnitRouteTest {
route
.run(HttpRequest.create())
.assertStatusCode(StatusCodes.NOT_FOUND);
.assertStatusCode(StatusCodes.OK);
}
@Test

View file

@ -30,6 +30,11 @@ class MiscDirectivesSpec extends RoutingSpec {
extractClientIP { echoComplete }
} ~> check { responseAs[String] shouldEqual "1.2.3.4" }
}
"extract unknown when no headers" in {
Get() ~> {
extractClientIP { echoComplete }
} ~> check { responseAs[String] shouldEqual "unknown" }
}
}
"the selectPreferredLanguage directive" should {

View file

@ -104,7 +104,8 @@ object MiscDirectives extends MiscDirectives {
private val _extractClientIP: Directive1[RemoteAddress] =
headerValuePF { case `X-Forwarded-For`(Seq(address, _*)) address } |
headerValuePF { case `Remote-Address`(address) address } |
headerValuePF { case `X-Real-Ip`(address) address }
headerValuePF { case `X-Real-Ip`(address) address } |
extract[RemoteAddress] { case _ RemoteAddress.Unknown }
private val _requestEntityEmpty: Directive0 =
extract(_.request.entity.isKnownEmpty).flatMap(if (_) pass else reject)