-
Notifications
You must be signed in to change notification settings - Fork 42
examples(fetch-rs): Harden fetch tool: add env-configurable options, enforce URL scheme & redirect loop detection, improve decoding & error reporting #339
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?
Conversation
…enforce URL scheme & redirect loop detection, improve decoding & error reporting Signed-off-by: Jiaxiao Zhou <[email protected]>
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.
Pull Request Overview
This PR significantly enhances the fetch-rs tool by adding comprehensive error handling, environment-configurable options, URL scheme validation, redirect management, and improved content decoding. The changes transform the simple fetch tool into a robust HTTP client with production-ready features.
- Adds environment-configurable options for limits (redirects, body size), timeout, and user agent
- Implements URL scheme validation, redirect loop detection, and comprehensive error handling
- Introduces content decoding support for gzip, deflate, and brotli compression with detailed response metadata
Reviewed Changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 4 comments.
File | Description |
---|---|
examples/fetch-rs/src/lib.rs | Complete rewrite adding structured error handling, redirect management, content decoding, and environment configuration |
examples/fetch-rs/Cargo.toml | Adds new dependencies for compression, URL parsing, MIME type handling, and removes scraper |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
if let Some(limit) = options.timeout_ms() { | ||
if metrics.elapsed_ms > limit as u128 { |
Copilot
AI
Oct 4, 2025
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.
The timeout check occurs after the request completes and body is processed, making it ineffective. The timeout should be enforced during the HTTP request itself, not after completion.
Copilot uses AI. Check for mistakes.
.map(|part| part.trim().to_ascii_lowercase()) | ||
.filter(|part| !part.is_empty()) | ||
.collect(); | ||
|
Copilot
AI
Oct 4, 2025
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.
The reason for reversing the encoding order should be documented. HTTP content-encoding headers list encodings in the order they were applied, so they must be decoded in reverse order.
// HTTP content-encoding headers list encodings in the order they were applied, | |
// so they must be decoded in reverse order. |
Copilot uses AI. Check for mistakes.
fn clip_bytes(data: &[u8], limit: usize) -> (Vec<u8>, bool) { | ||
if data.len() > limit { | ||
(data[..limit].to_vec(), true) | ||
} else { | ||
(data.to_vec(), false) | ||
} | ||
} |
Copilot
AI
Oct 4, 2025
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.
The function always clones the data even when no clipping is needed. Consider returning a slice reference when not truncated to avoid unnecessary allocation.
Copilot uses AI. Check for mistakes.
.skip_while(|b| b.is_ascii_whitespace()) | ||
.next() |
Copilot
AI
Oct 4, 2025
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.
The iterator chain could be simplified using find()
instead of skip_while().next()
for better readability and potentially better performance.
.skip_while(|b| b.is_ascii_whitespace()) | |
.next() | |
.find(|b| !b.is_ascii_whitespace()) |
Copilot uses AI. Check for mistakes.
@copilot open a new pull request to apply changes based on the comments in this thread and fix this test failure:
|
@copilot open a new pull request to apply changes based on the comments in this thread |
Signed-off-by: Jiaxiao Zhou [email protected]