FG
💻 Software📡 Networking

Getting a lot of ERR_CONTENT_LENGTH_MISMATCH errors for HTTPS

Fresh5 days ago
Mar 14, 20260 views
Confidence Score53%
53%

Problem

When proxying HTTPS to HTTP, I am getting a lot of ERR_CONTENT_LENGTH_MISMATCH errors in Chrome. For example, if I request an image from the web server through the HTTPS proxy, I get a content length of 50KB but the downloaded image is 30KB (and the image appears to be half-downloaded). This is true for _any_ HTTPS request, not just images. This is also true for _any_ web browser. I am testing in a Windows environment with a fairly slow Internet connection. Perhaps the connection is ending prematurely? Any thoughts on this?

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix ERR_CONTENT_LENGTH_MISMATCH by Adjusting Proxy Configuration

Medium Risk

The ERR_CONTENT_LENGTH_MISMATCH error occurs when the content length specified in the HTTP headers does not match the actual size of the response body. This can happen when proxying HTTPS to HTTP if the connection is not properly maintained or if the response is truncated. In this case, the proxy may not be handling the transfer encoding or chunked responses correctly, leading to premature connection termination and incomplete downloads.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Update Proxy Configuration

    Ensure that the proxy is correctly configured to handle HTTPS requests. This includes setting the correct headers and managing connection persistence. If using Node.js, consider using the 'http-proxy' library with proper options.

    javascript
    const httpProxy = require('http-proxy');
    
    const proxy = httpProxy.createProxyServer({
      changeOrigin: true,
      ws: true,
      secure: false // Set to true if you have valid SSL certificates
    });
  2. 2

    Handle Chunked Responses

    Ensure that your proxy server can handle chunked transfer encoding. This is important for large files or slow connections. You may need to modify the response handling logic to properly concatenate chunks.

    javascript
    proxy.on('proxyRes', function (proxyRes, req, res) {
      proxyRes.on('data', function (chunk) {
        // Handle each chunk as it comes in
        res.write(chunk);
      });
      proxyRes.on('end', function () {
        res.end();
      });
    });
  3. 3

    Set Proper Timeout Values

    Adjust the timeout settings for both the proxy and the backend server to prevent premature disconnections, especially on slow connections. This can help ensure that the entire response is received before the connection is closed.

    javascript
    proxy.timeout = 30000; // Set timeout to 30 seconds
    
    const server = http.createServer((req, res) => {
      req.setTimeout(30000); // Set request timeout to 30 seconds
      proxy.web(req, res, { target: 'http://backend-server' });
    });
  4. 4

    Enable Keep-Alive Connections

    Enable HTTP keep-alive connections in your proxy settings to maintain persistent connections, which can help reduce the likelihood of connection drops during data transfer.

    javascript
    const http = require('http');
    
    const server = http.createServer((req, res) => {
      res.setHeader('Connection', 'keep-alive');
      proxy.web(req, res, { target: 'http://backend-server' });
    });

Validation

To confirm the fix worked, perform multiple HTTPS requests through the proxy and verify that the downloaded content matches the expected content length. Monitor the logs for any ERR_CONTENT_LENGTH_MISMATCH errors and ensure that all responses are fully downloaded without truncation.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

proxyhttpnode.js