-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+htp akka#18929 add withSizeLimit directive (akka#20760)
withSizeLimit and withoutSizeLimit directives added
- Loading branch information
Showing
15 changed files
with
487 additions
and
1 deletion.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
akka-docs/rst/java/code/docs/http/javadsl/server/directives/MiscDirectivesExamplesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (C) 2016-2016 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
package docs.http.javadsl.server.directives; | ||
|
||
import akka.http.javadsl.model.HttpRequest; | ||
import akka.http.javadsl.model.StatusCodes; | ||
import akka.http.javadsl.server.Route; | ||
import akka.http.javadsl.server.Unmarshaller; | ||
import akka.http.javadsl.testkit.JUnitRouteTest; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.function.Function; | ||
|
||
public class MiscDirectivesExamplesTest extends JUnitRouteTest { | ||
|
||
@Test | ||
public void testWithSizeLimit() { | ||
//#withSizeLimitExample | ||
final Route route = withSizeLimit(500, () -> | ||
entity(Unmarshaller.entityToString(), (entity) -> | ||
complete("ok") | ||
) | ||
); | ||
|
||
Function<Integer, HttpRequest> withEntityOfSize = (sizeLimit) -> { | ||
char[] charArray = new char[sizeLimit]; | ||
Arrays.fill(charArray, '0'); | ||
return HttpRequest.POST("/").withEntity(new String(charArray)); | ||
}; | ||
|
||
// tests: | ||
testRoute(route).run(withEntityOfSize.apply(500)) | ||
.assertStatusCode(StatusCodes.OK); | ||
|
||
testRoute(route).run(withEntityOfSize.apply(501)) | ||
.assertStatusCode(StatusCodes.BAD_REQUEST); | ||
//#withSizeLimitExample | ||
} | ||
|
||
@Test | ||
public void testWithoutSizeLimit() { | ||
//#withoutSizeLimitExample | ||
final Route route = withoutSizeLimit(() -> | ||
entity(Unmarshaller.entityToString(), (entity) -> | ||
complete("ok") | ||
) | ||
); | ||
|
||
Function<Integer, HttpRequest> withEntityOfSize = (sizeLimit) -> { | ||
char[] charArray = new char[sizeLimit]; | ||
Arrays.fill(charArray, '0'); | ||
return HttpRequest.POST("/").withEntity(new String(charArray)); | ||
}; | ||
|
||
// tests: | ||
// will work even if you have configured akka.http.parsing.max-content-length = 500 | ||
testRoute(route).run(withEntityOfSize.apply(501)) | ||
.assertStatusCode(StatusCodes.OK); | ||
//#withoutSizeLimitExample | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ MiscDirectives | |
requestEntityPresent | ||
selectPreferredLanguage | ||
validate | ||
withoutSizeLimit | ||
withSizeLimit |
20 changes: 20 additions & 0 deletions
20
akka-docs/rst/java/http/routing-dsl/directives/misc-directives/withSizeLimit.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
.. _-withSizeLimit-java-: | ||
|
||
withSizeLimit | ||
=============== | ||
|
||
Description | ||
----------- | ||
Fails the stream with ``EntityStreamSizeException`` if its request entity size exceeds given limit. Limit given | ||
as parameter overrides limit configured with ``akka.http.parsing.max-content-length``. | ||
|
||
The whole mechanism of entity size checking is intended to prevent certain Denial-of-Service attacks. | ||
So suggested setup is to have ``akka.http.parsing.max-content-length`` relatively low and use ``withSizeLimit`` | ||
directive for endpoints which expects bigger entities. | ||
|
||
See also :ref:`-withoutSizeLimit-java-` for skipping request entity size check. | ||
|
||
Example | ||
------- | ||
|
||
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/MiscDirectivesExamplesTest.java#withSizeLimitExample |
19 changes: 19 additions & 0 deletions
19
...-docs/rst/java/http/routing-dsl/directives/misc-directives/withoutSizeLimit.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
.. _-withoutSizeLimit-java-: | ||
|
||
withoutSizeLimit | ||
================ | ||
|
||
Description | ||
----------- | ||
Skips request entity size verification. | ||
|
||
The whole mechanism of entity size checking is intended to prevent certain Denial-of-Service attacks. | ||
So suggested setup is to have ``akka.http.parsing.max-content-length`` relatively low and use ``withoutSizeLimit`` | ||
directive just for endpoints for which size verification should not be performed. | ||
|
||
See also :ref:`-withSizeLimit-java-` for setting request entity size limit. | ||
|
||
Example | ||
------- | ||
|
||
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/MiscDirectivesExamplesTest.java#withSizeLimitExample |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
akka-docs/rst/scala/http/routing-dsl/directives/misc-directives/withSizeLimit.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
.. _-withSizeLimit-: | ||
|
||
withSizeLimit | ||
=============== | ||
|
||
Signature | ||
--------- | ||
|
||
.. includecode2:: /../../akka-http/src/main/scala/akka/http/scaladsl/server/directives/MiscDirectives.scala | ||
:snippet: withSizeLimit | ||
|
||
Description | ||
----------- | ||
Fails the stream with ``EntityStreamSizeException`` if its request entity size exceeds given limit. Limit given | ||
as parameter overrides limit configured with ``akka.http.parsing.max-content-length``. | ||
|
||
The whole mechanism of entity size checking is intended to prevent certain Denial-of-Service attacks. | ||
So suggested setup is to have ``akka.http.parsing.max-content-length`` relatively low and use ``withSizeLimit`` | ||
directive for endpoints which expects bigger entities. | ||
|
||
See also :ref:`-withoutSizeLimit-` for skipping request entity size check. | ||
|
||
Examples | ||
-------- | ||
|
||
.. includecode2:: ../../../../code/docs/http/scaladsl/server/directives/MiscDirectivesExamplesSpec.scala | ||
:snippet: withSizeLimit-example | ||
|
||
Beware that request entity size check is executed when entity is consumed. Therefore in the following example | ||
even request with entity greater than argument to ``withSizeLimit`` will succeed (because this route | ||
does not consume entity): | ||
|
||
.. includecode2:: ../../../../code/docs/http/scaladsl/server/directives/MiscDirectivesExamplesSpec.scala | ||
:snippet: withSizeLimit-execution-moment-example | ||
|
||
Directive ``withSizeLimit`` is implemented in terms of ``HttpEntity.withSizeLimit`` which means that in case of | ||
nested ``withSizeLimit`` directives the innermost is applied: | ||
|
||
.. includecode2:: ../../../../code/docs/http/scaladsl/server/directives/MiscDirectivesExamplesSpec.scala | ||
:snippet: withSizeLimit-nested-example |
26 changes: 26 additions & 0 deletions
26
...docs/rst/scala/http/routing-dsl/directives/misc-directives/withoutSizeLimit.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
.. _-withoutSizeLimit-: | ||
|
||
withoutSizeLimit | ||
================ | ||
|
||
Signature | ||
--------- | ||
|
||
.. includecode2:: /../../akka-http/src/main/scala/akka/http/scaladsl/server/directives/MiscDirectives.scala | ||
:snippet: withoutSizeLimit | ||
|
||
Description | ||
----------- | ||
Skips request entity size verification. | ||
|
||
The whole mechanism of entity size checking is intended to prevent certain Denial-of-Service attacks. | ||
So suggested setup is to have ``akka.http.parsing.max-content-length`` relatively low and use ``withoutSizeLimit`` | ||
directive just for endpoints for which size verification should not be performed. | ||
|
||
See also :ref:`-withSizeLimit-` for setting request entity size limit. | ||
|
||
Example | ||
------- | ||
|
||
.. includecode2:: ../../../../code/docs/http/scaladsl/server/directives/MiscDirectivesExamplesSpec.scala | ||
:snippet: withoutSizeLimit-example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.