Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: Verify content length using the content itself when the content-length header is absent.Β #436

@kevalmahajan

Description

@kevalmahajan

🐞 Bug Summary

Encountered a scenario in deployed environments where the content-length header is occasionally missing from the response when calling the version/metrics tab. This is causing issues in content-length validation and proper response processing.
In cases where the Content-Length header is absent, we should be able to verify the content length using the content itself. The lack of this header leads to inconsistencies and bugs in client-side or server-side code that depend on it.

Root cause could be:

  1. Intermediaries such as Proxies or load balancers might strip or modify the Content-Length header.
  2. In case of Chunked transfer encoding, response body is sent in chunks, making Content-Length unnecessary.
  3. Compressed content may omit Content-Length as the server doesn’t adjust for compressed size.

🧩 Affected Component

Select the area of the project impacted:

  • mcpgateway - API
  • mcpgateway - UI (admin panel)
  • mcpgateway.wrapper - stdio wrapper
  • Federation or Transports
  • CLI, Makefiles, or shell scripts
  • Container setup (Docker/Podman/Compose)
  • Other (explain below)

πŸ” Steps to Reproduce

  1. In deployed Environments, with middleware in place.
  2. Version and Metrics tab fails to load.

πŸ€” Expected Behavior

Check for content-length header in response, if not present, check the length on the response received to confirm if the content is empty or not.


πŸ““ Logs / Error Output

Version Tab:
Image

Metrics Tab:
Image


🧩 Additional Context (optional)

Code changes:

Current code:

if (
    response.status === 0 ||
    (response.ok &&
        response.status === 200 &&
        !response.headers.get("content-length"))
) {
    console.warn(`Empty response received from ${url}`);
    throw new Error("Server returned an empty response");
}

New Code:

if (response.status === 0 || (response.ok && response.status === 200)) {
    const contentLength = response.headers.get("content-length");

    // Check Content-Length if present
    if (contentLength !== null) {
        if (parseInt(contentLength, 10) === 0) {
            throw new Error("Server returned an empty response (via header)");
        }
    } else {
        // Fallback: check actual body
        const cloned = response.clone();
        const text = await cloned.text();
        if (!text.trim()) {
            throw new Error("Server returned an empty response (via body)");
        }
    }
}

🧠 Environment Info

You can retrieve most of this from the /version endpoint.

Key Value
Version or commit e.g. v0.9.0 or main@a1b2c3d
Runtime e.g. Python 3.11, Gunicorn
Platform / OS e.g. Ubuntu 22.04, macOS
Container e.g. Docker, Podman, none

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingtriageIssues / Features awaiting triage

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions