-
Notifications
You must be signed in to change notification settings - Fork 1
try to fix body missing on prod #4
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
Conversation
when i used in prod ,find bug that the body is missing
WalkthroughThe changes update several core components of the OxHTTP project. Configuration files have been revised with a new package version and additional dependencies, along with a project description. Route definitions in both documentation and examples have been modified to use curly braces for path parameters. Core server logic has been improved by updating the request parsing to utilize Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant HttpServer
participant RequestParser
participant Router
participant Handler
Client->>HttpServer: Send HTTP request
HttpServer->>RequestParser: Parse request string
RequestParser-->>HttpServer: Return parsed Request (Ok/Err)
HttpServer->>Router: Match route using request URL
Router-->>HttpServer: Return matched Route & params
HttpServer->>Handler: Invoke route handler
Handler-->>HttpServer: Return response
HttpServer->>Client: Send HTTP response
Possibly related PRs
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🔇 Additional comments (2)
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 3
🧹 Nitpick comments (13)
src/request_parser.rs (3)
7-9: Use a more descriptive error type if possible.Switching from
Option<Request>toResult<Request, httparse::Error>is good for more granular error handling. However,httparse::Erroris sometimes too narrow to capture potential parsing issues (e.g., malformed headers). If you foresee other parsing errors, consider creating a custom error type or wrappinghttparse::Errorfor more descriptive feedback.
16-18: Consider user-defined defaults for Content-Type.Using
"text/plain"as a default forcontent_typeis okay, but for robust usage, you might let consumers override this default, especially for APIs returning JSON or binary.
20-33: Validate header usage beyond Content-Type and Content-Length.Storing headers in a
HashMapis good, but consider rejecting or sanitizing suspicious or malformed headers (like repeated content length). If any security constraints exist, ensure filtering or validation steps are in place before storing.src/routing.rs (5)
30-31: Catch potential attribute extraction errors.Extracting parameter names could fail if the Python logic changes. For safety, consider more robust error handling (e.g., ignoring non-string names).
34-37: Check for overshadowed fields.When creating the
Route, confirm that the previous fields (method, path, handler) aren’t overshadowed with local variables of the same name. Current usage looks correct, but keep an eye on expansions in the future.
44-44: Consider documenting the macro usage.The macro is helpful, but might be confusing for unfamiliar contributors. Add inline doc comments or examples.
63-68: Use consistent constructor approach.
#[new] -> Router::default()is concise. Validate that more complex init logic (e.g., setting telemetry or middlewares) doesn’t need additional arguments.
107-107: Clarify route pattern.Using
/{path}/{{*path}}can be confusing to new adopters. Ensure your docs highlight that the second{{*path}}captures all subsequent path segments.src/lib.rs (4)
81-90: Short-circuit approach is fine; logs can help debug.Once you successfully match a route, you
breakout of the loop. Consider some debug logging if multiple routers are attached to aid troubleshoot.
111-112: Extract route & params once.Cloning these each request might be inexpensive for small usage, but consider referencing them if performance becomes an issue under high load.
123-123: Add param logging.Iterating over
paramsis helpful. Possibly log or handle unexpected dynamic segments.
130-136: Body param matching logic clarity.Skipping
"app_data"is clear, but the chain of filters on lines 131-135 might be confusing to maintainers. Consider extracting to a small helper function or direct membership check for improved readability.src/request.rs (1)
80-82: Consider adding content validation in set_body.The new
set_bodymethod is crucial for fixing the missing body issue, but it could benefit from additional validation.Consider adding these validations:
pub fn set_body(&mut self, body: String) { + if body.len() != self.content_length { + // Either update content_length or warn about mismatch + self.content_length = body.len(); + } self.body = Some(body); }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
Cargo.lockis excluded by!**/*.lock
📒 Files selected for processing (8)
Cargo.toml(2 hunks)README.md(1 hunks)examples/main.py(1 hunks)pyproject.toml(1 hunks)src/lib.rs(4 hunks)src/request.rs(4 hunks)src/request_parser.rs(1 hunks)src/routing.rs(3 hunks)
✅ Files skipped from review due to trivial changes (2)
- pyproject.toml
- examples/main.py
🔇 Additional comments (19)
src/request_parser.rs (3)
11-15: Double-check default values for method/URL.Falling back to
"GET"and"/"is convenient, but might mask unintended parse failures when the request is incomplete. If that's intentional, consider clearly documenting it or logging a warning.
35-36: Confirm field ordering inRequest::new.You've extended
Request::newwith new parameters (content type, content length, etc). Verify that the argument order inRequest::newremains consistent with the struct definition to avoid subtle mismatches.
38-41: Guard against inaccurate body slicing.When
content_length > 0, the substring is taken using[header_size..]. Confirm thatheader_sizeindeed marks the start of the body under all circumstances (e.g., unusual or malformed headers). Otherwise, you might slice out partial or incorrect data.src/routing.rs (8)
3-3: Remove unused imports if not needed.
ffi::c_strmay not be needed unless you're using it for C-FFI bridging. Verify that each imported item is used.
5-5: Kudos on addingDebugtrait.Deriving
Debugis helpful for logging and troubleshooting.
9-9: Clarifypathusage.Renaming
regextopathis simpler, but confirm all references to the old format (<dynamic>) are removed in other files or docs.
17-18: Ensure method and path alignment.You’re now coupling
methodandpathinRoute::new. Validate that no additional route info (like query parameters) needs capturing.
47-48: Verify dynamic method creation.This approach elegantly generates
get, post, etc.methods. It’s recommended to confirm the uppercase conversion logic matches all HTTP verbs in use.
54-54: Great method coverage.Providing macros for the core HTTP methods is a clean approach that promotes consistency.
57-60: LeverageDefaultfor router initialization.The derived
DefaultandCloneare a neat approach for easy instantiation & copying.
70-72: Beware of concurrency.Storing the
middlewareas anArcis safe, but confirm it’s read-only or properly synchronized if mutated.src/lib.rs (2)
9-9: Confirm the new dependency usage.
use matchit::Match;is part of your new router structure. Make sure to remove any references to older router logic in the codebase or docs.
106-107: Method signature alignment.You replaced
params: HashMap<String, String>withmatch_route: &Match<'_, '_, &Route>. Verify that all internal references toparamsare updated accordingly.src/request.rs (3)
10-13: LGTM! The Request struct changes improve body handling.The changes to make
bodyoptional and addcontent_typeandcontent_lengthfields improve the robustness of request handling, especially for requests that may or may not have bodies.
19-33: Consider validating content_length against actual body size.While the constructor changes align with the new struct fields, there's potential for inconsistency between the declared
content_lengthand actual body size when set later.Consider adding validation in
set_bodyto ensure the body length matches the declaredcontent_length.
40-47: LGTM! Improved JSON handling for optional bodies.The updated
jsonmethod now safely handles cases where the body is missing by returning an empty dictionary instead of failing.Cargo.toml (2)
3-3: Verify version bump follows semantic versioning.The version has been bumped from 0.1.1 to 0.1.3. Since these changes fix a production bug and add new features, this appears appropriate.
13-14:Details
❓ Verification inconclusive
Verify compatibility of new dependencies.
The addition of
httparseandmatchitappears to support improved request parsing and routing.
🏁 Script executed:
#!/bin/bash # Check for any known vulnerabilities in the new dependencies cargo auditLength of output: 109
Action Required: Manually Verify Dependency Vulnerabilities
The new dependencies (
httparse = "1.10.0"andmatchit = "0.8.6") are added to enhance request parsing and routing. However, the automated vulnerability check usingcargo auditdid not run because the Cargo toolchain is not available in the current environment.
- Next Steps:
- Ensure that you have the Cargo toolchain installed in your local development environment.
- Run
cargo auditmanually in a suitable environment to verify that these dependencies do not introduce any known vulnerabilities.Please verify these items and confirm that the changes meet our security standards before merging.
README.md (1)
23-23: LGTM! Documentation updated to reflect new routing syntax.The route parameter syntax has been updated from
<name>to{name}to match the new implementation using the matchit router.
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.
Actionable comments posted: 0
🔭 Outside diff range comments (1)
src/lib.rs (1)
76-93:⚠️ Potential issueFix critical request handling issues.
Several critical issues in the request handling logic:
request_stris used before it's created (defined later on line 94)- The error handling branch has incorrect syntax
- Duplicate request handling logic exists between this block and lines 94-119
Apply this diff to fix the issues:
- if let Ok(ref request) = RequestParser::parse(&request_str) { - let mut response = Status::NOT_FOUND().into_response(); - - for router in &self.routers { - if let Ok(route) = &router.router.at(&request.url) { - response = match self.process_response(py, router, route, request) { - Ok(response) => response, - Err(e) => Status::INTERNAL_SERVER_ERROR() - .into_response() - .body(e.to_string()), - }; - break; - } - Err(_) => { - break; - } - } - } if let Ok(request_str) = String::from_utf8(request_data) { - if let Some(ref request) = RequestParser::parse(&request_str) { + if let Ok(ref request) = RequestParser::parse(&request_str) { let mut response = Status::NOT_FOUND().into_response(); for router in &self.routers { - for route in &router.routes { - if route.method == request.method { - if let Some(params) = route.match_path(&request.url) { - response = match self - .process_response(py, router, route, request, params) - { - Ok(response) => response, - Err(e) => Status::INTERNAL_SERVER_ERROR() - .into_response() - .body(e.to_string()), - }; - break; - } - } + if let Ok(route) = router.router.at(&request.url) { + response = match self.process_response(py, router, &route, request) { + Ok(response) => response, + Err(e) => Status::INTERNAL_SERVER_ERROR() + .into_response() + .body(e.to_string()), + }; + break; } }
♻️ Duplicate comments (1)
src/lib.rs (1)
73-75:⚠️ Potential issueEnsure complete request data is read.
The current implementation might not handle large requests correctly as it uses a fixed buffer size and doesn't check for complete request data.
Consider implementing proper request buffering:
- Read the Content-Length header
- Continue reading until all data is received
- Handle chunked transfer encoding
This could be the root cause of the "body missing on prod" issue mentioned in the PR objectives.
🧹 Nitpick comments (1)
src/lib.rs (1)
154-165: Simplify body parameter detection logic.The current implementation uses a complex filtering approach to check for body parameters. This can be simplified for better readability and performance.
Consider this simpler approach:
- for key in route.args.clone() { - if key != "app_data" - && params - .iter() - .filter(|(k, _)| *k == key) - .collect::<Vec<_>>() - .is_empty() - { - body_param_name = Some(key); - break; - } - } + body_param_name = route.args.iter() + .find(|key| *key != "app_data" && !params.contains_key(*key)) + .cloned();
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/lib.rs(4 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: windows (x86)
- GitHub Check: windows (x64)
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.
Actionable comments posted: 1
🔭 Outside diff range comments (1)
src/into_response.rs (1)
71-73: 🛠️ Refactor suggestionImprove error handling for type conversion failures.
To better diagnose production issues with missing bodies, consider:
- Including the actual type in the error message.
- Adding logging before throwing the error.
- return Err(pyo3::exceptions::PyException::new_err( - "Failed to convert this type to response", - )); + let type_name = result.get_type().name()?; + return Err(pyo3::exceptions::PyException::new_err( + format!("Failed to convert type '{}' to response", type_name), + ));
♻️ Duplicate comments (1)
src/lib.rs (1)
73-77:⚠️ Potential issueCritical: Improve request reading to handle large and partial requests.
The current implementation has several issues:
- Fixed 1024-byte buffer can truncate large requests
- No handling of partial requests that span multiple TCP packets
- No validation against Content-Length header
Consider this safer implementation:
- let mut buffer = [0; 1024]; - let n = socket.read(&mut buffer)?; - let request_str = String::from_utf8_lossy(&buffer[..n]); + let mut buffer = Vec::new(); + let mut reader = std::io::BufReader::new(&mut socket); + reader.read_to_end(&mut buffer)?; + if let Ok(request_str) = String::from_utf8(buffer) {
🧹 Nitpick comments (1)
src/lib.rs (1)
149-150: Add error context to response conversion.The error handling could be improved by providing more context about what failed during response conversion.
- return convert_to_response(result, py); + return convert_to_response(result, py) + .map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>( + format!("Failed to convert middleware response: {}", e) + )); - convert_to_response(result, py) + convert_to_response(result, py) + .map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>( + format!("Failed to convert handler response: {}", e) + ))Also applies to: 155-156
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/into_response.rs(2 hunks)src/lib.rs(5 hunks)src/response.rs(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (7)
- GitHub Check: linux (armv7)
- GitHub Check: windows (x86)
- GitHub Check: musllinux (armv7)
- GitHub Check: windows (x64)
- GitHub Check: musllinux (aarch64)
- GitHub Check: musllinux (x86)
- GitHub Check: musllinux (x86_64)
🔇 Additional comments (6)
src/lib.rs (2)
8-9: LGTM! Import changes align with the new functionality.The new imports support the enhanced routing and response handling capabilities.
106-113: LGTM! Improved routing with matchit.The change to use
matchit::Matchprovides a more efficient and type-safe way to handle route matching and parameters.src/response.rs (2)
1-1: LGTM! Import changes look good.The imports have been reorganized and updated to use the renamed
convert_to_responsefunction.Also applies to: 3-3
25-31: LGTM! Response creation and conversion look good.The variable renaming improves readability, and the function call has been correctly updated to use
convert_to_response.src/into_response.rs (2)
1-1: LGTM! Import changes look good.The imports have been reorganized and the explicit
Pyimport has been added.Also applies to: 5-5
77-77: LGTM! Function rename improves clarity.The function rename from
converttoconvert_to_responsebetter describes its purpose.
when i used in prod ,find bug that the body is missing
Summary by CodeRabbit
New Features
/hello/{name}) for consistency.Documentation
Chores
httparseandmatchit).