Skip to content

Conversation

@edsiper
Copy link
Member

@edsiper edsiper commented Sep 1, 2025

  • Add missing validation for flb_http_get_response_data() return value
  • Handle all possible return values from check_connection():
    • FLB_HTTP_ERROR: propagate parsing errors
    • FLB_HTTP_NOT_FOUND: continue normally (valid per HTTP spec)
    • FLB_HTTP_OK: process connection close logic
  • Fix silent failures that could cause undefined behavior
  • Maintain full backward compatibility with existing callers

Fluent Bit is licensed under Apache 2.0, by submitting this pull request I understand that this code will be released under the terms of that license.

Summary by CodeRabbit

  • Bug Fixes
    • Improved HTTP response handling to prevent stale data between requests.
    • More consistent error signaling and early error propagation for failed requests.
    • Gracefully handles missing Connection headers without interrupting processing.
    • More reliable keep-alive/close behavior based on connection checks.
  • Refactor
    • Centralized response reset logic to ensure clean initialization before each HTTP request.

- Add missing validation for flb_http_get_response_data() return value
- Handle all possible return values from check_connection():
  - FLB_HTTP_ERROR: propagate parsing errors
  - FLB_HTTP_NOT_FOUND: continue normally (valid per HTTP spec)
  - FLB_HTTP_OK: process connection close logic
- Fix silent failures that could cause undefined behavior
- Maintain full backward compatibility with existing callers

Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
@edsiper edsiper added this to the Fluent Bit v4.1 milestone Sep 1, 2025
@coderabbitai
Copy link

coderabbitai bot commented Sep 1, 2025

Walkthrough

Updates to src/flb_http_client.c modify error signaling, introduce a response reset helper, integrate it into request initialization, and refine control flow in flb_http_do for early error returns and explicit handling of Connection header outcomes.

Changes

Cohort / File(s) Summary
HTTP client response lifecycle and connection handling
src/flb_http_client.c
- check_connection: on allocation failure return FLB_HTTP_ERROR (was -1)
- Add http_client_response_reset(c) to clear response fields
- Use reset in flb_http_do_request instead of manual zeroing
- In flb_http_do: return early on non-OK processing results
- Refine Connection header handling: ERROR → return; OK → existing keepalive/close logic; NOT_FOUND → no-op path documented

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant App
  participant HTTP as flb_http_do_request
  participant Client as flb_http_do
  participant Conn as check_connection

  App->>HTTP: start request
  Note over HTTP: Initialize response state
  HTTP->>HTTP: http_client_response_reset(c)

  HTTP->>Client: send/receive loop
  Client->>Client: process response data
  alt processing error
    Client-->>HTTP: ret != FLB_HTTP_OK
    HTTP-->>App: return error (early)
  else processing ok
    Client->>Conn: check_connection(headers)
    alt Conn == FLB_HTTP_ERROR
      Client-->>HTTP: error
      HTTP-->>App: return error
    else Conn == FLB_HTTP_OK
      Note over Client: Preserve keepalive/close logic
      Client-->>HTTP: continue
      HTTP-->>App: success
    else Conn == FLB_HTTP_NOT_FOUND
      Note over Client: No-op path (explicit)
      Client-->>HTTP: continue
      HTTP-->>App: success
    end
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I twitch my ears at tidy code,
Reset the slate, then hit the road—
If headers hide, I won’t despair,
NOT_FOUND noted, onward hare!
Errors hop back early, neat—
Connections judged with careful feet.
Thump! Another stable beat.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch http_client_return_statutes

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a 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

🧹 Nitpick comments (3)
src/flb_http_client.c (3)

243-260: OOM now returns FLB_HTTP_ERROR; add bounds check to avoid over-read in strncasecmp

Good call switching OOM to FLB_HTTP_ERROR for consistent signaling. Minor safety nit: guard the "close" compare to avoid reading past the buffer when len < 5.

Apply:

@@
-    if (strncasecmp(buf, "close", 5) == 0) {
+    if (len >= 5 && strncasecmp(buf, "close", 5) == 0) {
         c->resp.connection_close = FLB_TRUE;
     }

268-279: Reset helper is solid; also null-terminate the response buffer head

Resetting all response fields centrally is great. Consider setting data[0] to '\0' to avoid any accidental stale scans before the first read.

Apply:

 static inline void http_client_response_reset(struct flb_http_client *c)
 {
     c->resp.data_len = 0;
+    if (c->resp.data) {
+        c->resp.data[0] = '\0';
+    }
     c->resp.status = 0;
     c->resp.content_length = -1;
     c->resp.chunked_encoding = FLB_FALSE;
     c->resp.connection_close = -1;
     c->resp.headers_end = NULL;
     c->resp.payload = NULL;
     c->resp.payload_size = 0;
     c->resp.chunk_processed_end = NULL;
 }

1565-1585: Explicit tri-state handling of Connection header improves robustness; consider defensive ‘MORE’ case or switch

Handling ERROR/OK/NOT_FOUND explicitly is good. Since check_connection theoretically can return FLB_HTTP_MORE only when headers are incomplete, which shouldn’t happen post-OK, you may add a debug-only guard (or switch) for clarity.

Example:

-    ret = check_connection(c);
+    ret = check_connection(c);
     if (ret == FLB_HTTP_ERROR) {
         return ret;
     }
-    else if (ret == FLB_HTTP_OK) {
+    else if (ret == FLB_HTTP_OK) {
         ...
     }
     else if (ret == FLB_HTTP_NOT_FOUND) {
         /* Connection header not found, continue normally */
     }
+    /* Unexpected here, but keep a defensive branch for readability */
+    /* else if (ret == FLB_HTTP_MORE) { */
+    /*     flb_debug("[http_client] connection header incomplete after full response"); */
+    /* } */
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 1132279 and 481a4f6.

📒 Files selected for processing (1)
  • src/flb_http_client.c (5 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (29)
  • GitHub Check: pr-windows-build / call-build-windows-package (Windows 64bit (Arm64), amd64_arm64, -DCMAKE_SYSTEM_NAME=Windows -DCMA...
  • GitHub Check: pr-windows-build / call-build-windows-package (Windows 32bit, x86, x86-windows-static, 3.31.6)
  • GitHub Check: pr-windows-build / call-build-windows-package (Windows 64bit, x64, x64-windows-static, 3.31.6)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_ARROW=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SIMD=Off, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SIMD=Off, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SIMD=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SANITIZE_THREAD=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SANITIZE_THREAD=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SANITIZE_MEMORY=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_COVERAGE=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SANITIZE_MEMORY=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SIMD=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DSANITIZE_UNDEFINED=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_JEMALLOC=Off, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SMALL=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DSANITIZE_UNDEFINED=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_JEMALLOC=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SMALL=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DSANITIZE_ADDRESS=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DSANITIZE_ADDRESS=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_JEMALLOC=Off, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_JEMALLOC=On, 3.31.6, gcc, g++)
  • GitHub Check: pr-compile-system-libs (-DFLB_PREFER_SYSTEM_LIBS=On, 3.31.6, clang, clang++, ubuntu-24.04, clang-14)
  • GitHub Check: pr-compile-system-libs (-DFLB_PREFER_SYSTEM_LIBS=On, 3.31.6, clang, clang++, ubuntu-22.04, clang-12)
  • GitHub Check: pr-compile-system-libs (-DFLB_PREFER_SYSTEM_LIBS=On, 3.31.6, gcc, g++, ubuntu-22.04, clang-12)
  • GitHub Check: PR - fuzzing test
  • GitHub Check: pr-compile-system-libs (-DFLB_PREFER_SYSTEM_LIBS=On, 3.31.6, gcc, g++, ubuntu-24.04, clang-14)
  • GitHub Check: pr-compile-centos-7
🔇 Additional comments (3)
src/flb_http_client.c (3)

1434-1435: Good: use the centralized reset before reading response

This eliminates partial-state carryover between requests.


1550-1556: Helpful inline rationale for bytes_consumed=0 in flb_http_do

Comment clarifies the contract for chunked handling here.


1559-1562: Early return on non-OK cleans up control flow and propagates errors

Prevents silent fallthrough on malformed/incomplete responses.

@edsiper edsiper merged commit 4db4f21 into master Sep 1, 2025
58 checks passed
@edsiper edsiper deleted the http_client_return_statutes branch September 1, 2025 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants