Repository navigation
feat: add max_*_size options #67
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,10 @@ pub type HttpError { | |
| FailedToConnect(ip4: ConnectError, ip6: ConnectError) | ||
| /// The response was not received within the configured timeout period. | ||
| ResponseTimeout | ||
| /// The cumulative bytes of the response header was larger than `max_header_size`. | ||
| ResponseHeaderTooLarge | ||
| /// The response body was larger than `max_body_size`. | ||
| ResponseBodyTooLarge | ||
| } | ||
|
|
||
| pub type ConnectError { | ||
|
|
@@ -29,6 +33,12 @@ fn default_user_agent() -> #(Charlist, Charlist) | |
| @external(erlang, "gleam_httpc_ffi", "normalise_error") | ||
| fn normalise_error(error: Dynamic) -> HttpError | ||
|
|
||
| @external(erlang, "gleam_httpc_ffi", "max_size_bytes") | ||
| fn erl_max_size_bytes(n: Int) -> ErlMaxSize | ||
|
|
||
| @external(erlang, "gleam_httpc_ffi", "max_size_nolimit") | ||
| fn erl_max_size_nolimit() -> ErlMaxSize | ||
|
|
||
| type ErlHttpOption { | ||
| Ssl(List(ErlSslOption)) | ||
| Autoredirect(Bool) | ||
|
|
@@ -43,6 +53,8 @@ type ErlOption { | |
| BodyFormat(BodyFormat) | ||
| SocketOpts(List(SocketOpt)) | ||
| Ipv6HostWithBrackets(Bool) | ||
| MaxHeaderSize(ErlMaxSize) | ||
| MaxBodySize(ErlMaxSize) | ||
| } | ||
|
|
||
| type SocketOpt { | ||
|
|
@@ -61,6 +73,13 @@ type ErlVerifyOption { | |
| VerifyNone | ||
| } | ||
|
|
||
| type ErlMaxSize | ||
|
|
||
| pub type MaxSize { | ||
| Bytes(Int) | ||
| Nolimit | ||
| } | ||
|
|
||
| @external(erlang, "httpc", "request") | ||
| fn erl_request( | ||
| a: Method, | ||
|
|
@@ -136,10 +155,15 @@ pub fn dispatch_bits( | |
| True -> erl_http_options | ||
| False -> [Ssl([Verify(VerifyNone)]), ..erl_http_options] | ||
| } | ||
|
|
||
| let erl_max_header_size = erl_max_size(config.max_header_size) | ||
| let erl_max_body_size = erl_max_size(config.max_body_size) | ||
| let erl_options = [ | ||
| BodyFormat(Binary), | ||
| SocketOpts([Ipfamily(Inet6fb4)]), | ||
| Ipv6HostWithBrackets(True), | ||
| MaxHeaderSize(erl_max_header_size), | ||
| MaxBodySize(erl_max_body_size), | ||
| ] | ||
|
|
||
| use response <- result.try( | ||
|
|
@@ -187,6 +211,13 @@ pub opaque type Configuration { | |
| /// Timeout for the request in milliseconds. | ||
| /// | ||
| timeout: Int, | ||
| /// Maximum total number of bytes allowed for the response headers | ||
| /// (cumulative across all header lines). | ||
| /// | ||
| max_header_size: MaxSize, | ||
| /// Maximum number of bytes allowed for the response body. | ||
| /// | ||
| max_body_size: MaxSize, | ||
| ) | ||
| } | ||
|
|
||
|
|
@@ -200,7 +231,13 @@ pub opaque type Configuration { | |
| /// request is sent. | ||
| /// | ||
| pub fn configure() -> Configuration { | ||
| Builder(verify_tls: True, follow_redirects: False, timeout: 30_000) | ||
| Builder( | ||
| verify_tls: True, | ||
| follow_redirects: False, | ||
| timeout: 30_000, | ||
| max_header_size: Bytes(10_240), | ||
| max_body_size: Nolimit, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to ever permit no limit? Surely the same problem exists with the body, and a limit is always desired.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, this is the https://www.erlang.org/doc/apps/inets/httpc.html#request/5
|
||
| ) | ||
| } | ||
|
|
||
| /// Set whether to verify the TLS certificate of the server. | ||
|
|
@@ -230,6 +267,33 @@ pub fn timeout(config: Configuration, timeout: Int) -> Configuration { | |
| Builder(..config, timeout:) | ||
| } | ||
|
|
||
| /// Set the maximum size limit in bytes for the combination of all the headers | ||
| /// of the response. | ||
| /// | ||
| /// The default is 10_240 bytes. | ||
| /// | ||
| /// If a response's headers are larger than this limit then the | ||
| /// `ResponseHeaderTooLarge` error is returned. | ||
| pub fn max_header_size( | ||
| config: Configuration, | ||
| max_header_size: MaxSize, | ||
| ) -> Configuration { | ||
| Builder(..config, max_header_size:) | ||
| } | ||
|
|
||
| /// Set the maximum size limit in bytes for the body of the response. | ||
| /// | ||
| /// The default is unlimited. | ||
| /// | ||
| /// If a response's body is larger than this limit then the | ||
| /// `ResponseBodyTooLarge` error is returned. | ||
| pub fn max_body_size( | ||
| config: Configuration, | ||
| max_body_size: MaxSize, | ||
| ) -> Configuration { | ||
| Builder(..config, max_body_size:) | ||
| } | ||
|
|
||
| /// Send a HTTP request of unicode data. | ||
| /// | ||
| pub fn dispatch( | ||
|
|
@@ -276,3 +340,10 @@ fn prepare_headers_loop( | |
| } | ||
| } | ||
| } | ||
|
|
||
| fn erl_max_size(max_size: MaxSize) -> ErlMaxSize { | ||
| case max_size { | ||
| Bytes(bytes) -> erl_max_size_bytes(bytes) | ||
| Nolimit -> erl_max_size_nolimit() | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's go for 2kb rather than 1kib.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just so you know i set the same default as
httpcitself:https://www.erlang.org/doc/apps/inets/httpc.html#request/5