8000 Improve doc for 10MB body limit by algesten · Pull Request #1061 · algesten/ureq · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Improve doc for 10MB body limit #1061

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

Merged
merged 1 commit into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

* Improve doc for 10MB limit (#1061)

# 3.0.11

* Fix CONNECT proxy bug (#1057)
Expand Down
72 changes: 72 additions & 0 deletions src/body/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ const MAX_BODY_SIZE: u64 = 10 * 1024 * 1024;

/// A response body returned as [`http::Response<Body>`].
///
/// # Default size limit
///
/// Methods like `read_to_string()`, `read_to_vec()`, and `read_json()` have a **default 10MB limit**
/// to prevent memory exhaustion. To download larger files, use `with_config().limit(new_size)`:
///
/// ```
/// // Download a 20MB file
/// let bytes = ureq::get("http://httpbin.org/bytes/200000000")
/// .call()?
/// .body_mut()
/// .with_config()
/// .limit(20 * 1024 * 1024) // 20MB
/// .read_to_vec()?;
/// # Ok::<_, ureq::Error>(())
/// ```
///
/// # Body lengths
///
/// HTTP/1.1 has two major modes of transfering body data. Either a `Content-Length`
Expand Down Expand Up @@ -251,6 +267,19 @@ impl Body {
/// assert_eq!(s, "User-agent: *\nDisallow: /deny\n");
/// # Ok::<_, ureq::Error>(())
/// ```
///
/// For larger text files, you must explicitly increase the limit:
///
/// ```
/// // Read a large text file (25MB)
/// let text = ureq::get("http://httpbin.org/get")
/// .call()?
/// .body_mut()
/// .with_config()
/// .limit(25 * 1024 * 1024) // 25MB
/// .read_to_string()?;
/// # Ok::<_, ureq::Error>(())
/// ```
pub fn read_to_string(&mut self) -> Result<String, Error> {
self.with_config()
.limit(MAX_BODY_SIZE)
Expand All @@ -271,6 +300,19 @@ impl Body {
/// assert_eq!(bytes.len(), 100);
/// # Ok::<_, ureq::Error>(())
/// ```
///
/// For larger files, you must explicitly increase the limit:
///
/// ```
/// // Download a larger file (50MB)
/// let bytes = ureq::get("http://httpbin.org/bytes/200000000")
/// .call()?
/// .body_mut()
/// .with_config()
/// .limit(50 * 1024 * 1024) // 50MB
/// .read_to_vec()?;
/// # Ok::<_, ureq::Error>(())
/// ```
pub fn read_to_vec(&mut self) -> Result<Vec<u8>, Error> {
self.with_config()
//
Expand Down Expand Up @@ -308,6 +350,21 @@ impl Body {
/// assert_eq!(body.slideshow.author, "Yours Truly");
/// # Ok::<_, ureq::Error>(())
/// ```
///
/// For larger JSON files, you must explicitly increase the limit:
///
/// ```
/// use serde_json::Value;
///
/// // Parse a large JSON file (30MB)
/// let json: Value = ureq::get("https://httpbin.org/json")
/// .call()?
/// .body_mut()
/// .with_config()
/// .limit(30 * 1024 * 1024) // 30MB
/// .read_json()?;
/// # Ok::<_, ureq::Error>(())
/// ```
#[cfg(feature = "json")]
pub fn read_json<T: serde::de::DeserializeOwned>(&mut self) -> Result<T, Error> {
let reader = self.with_config().limit(MAX_BODY_SIZE).reader();
Expand Down Expand Up @@ -369,6 +426,21 @@ impl Body {
/// * [Body::with_config()]
/// * [Body::into_with_config()]
///
/// # Handling large responses
///
/// The `BodyWithConfig` is the primary way to increase the default 10MB size limit
/// when downloading large files to memory:
///
/// ```
/// // Download a 50MB file
/// let large_data = ureq::get("http://httpbin.org/bytes/200000000")
/// .call()?
/// .body_mut()
/// .with_config()
/// .limit(50 * 1024 * 1024) // 50MB
/// .read_to_vec()?;
/// # Ok::<_, ureq::Error>(())
/// ```
pub struct BodyWithConfig<'a> {
handler: BodySourceRef<'a>,
info: Arc<ResponseInfo>,
Expand Down
15 changes: 15 additions & 0 deletions src/unversioned/transport/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ fn setup_default_handlers(handlers: &mut Vec<TestHandler>) {
handlers,
);

maybe_add(
TestHandler::new("/bytes/200000000", |_uri, _req, w| {
write!(
w,
"HTTP/1.1 200 OK\r\n\
Content-Type: application/octet-stream\r\n\
Content-Length: 100\r\n\
\r\n"
)?;
// We don't actually want 200MB of data in memory.
write!(w, "{}", "1".repeat(100))
}),
handlers,
);

maybe_add(
TestHandler::new("/get", |_uri, req, w| {
write!(
Expand Down
0