apache/hadoop · error · IOException

Content-Length is missing: ${headers}

Error message

Content-Length is missing: ${headers}

What it means

When ByteRangeInputStream opens a ranged HTTP read (WebHDFS OPEN), it computes the file length from the response: chunked responses have unknown length, everything else must carry a Content-Length header. If the response is neither chunked nor has Content-Length, this IOException listing the full header map is thrown. Well-behaved WebHDFS servers always set Content-Length, so a missing one almost always means an intermediary (proxy, gateway) or a non-HDFS endpoint mangled the response.

Source

Thrown at hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/ByteRangeInputStream.java:153

    // Use the original url if no resolved url exists, eg. if
    // it's the first time a request is made.
    final boolean resolved = resolvedURL.getURL() != null;
    final URLOpener opener = resolved? resolvedURL: originalURL;

    final HttpURLConnection connection = opener.connect(startOffset, resolved);
    resolvedURL.setURL(getResolvedUrl(connection));

    InputStream in = connection.getInputStream();
    final Long length;
    final Map<String, List<String>> headers = connection.getHeaderFields();
    if (isChunkedTransferEncoding(headers)) {
      // file length is not known
      length = null;
    } else {
      // for non-chunked transfer-encoding, get content-length
      final String cl = connection.getHeaderField(HttpHeaders.CONTENT_LENGTH);
      if (cl == null) {
        throw new IOException(HttpHeaders.CONTENT_LENGTH + " is missing: "
            + headers);
      }
      final long streamlength = Long.parseLong(cl);
      length = startOffset + streamlength;

      // Java has a bug with >2GB request streams.  It won't bounds check
      // the reads so the transfer blocks until the server times out
      in = new BoundedInputStream(in, streamlength);
    }

    return new InputStreamAndFileLength(length, in);
  }

  private static boolean isChunkedTransferEncoding(
      final Map<String, List<String>> headers) {
    return contains(headers, HttpHeaders.TRANSFER_ENCODING, "chunked")
        || contains(headers, HttpHeaders.TE, "chunked");
  }

View on GitHub (pinned to 2add963021)

Solutions

  1. Check the headers printed in the message: a non-HDFS response (proxy error page, auth challenge) is visible there
  2. Bypass the intermediary: connect the client directly to the NameNode/DataNode HTTP port, or fix the proxy to pass Content-Length through unbuffered
  3. Clear stray JVM proxy settings (-Dhttp.proxyHost/-Dhttp.proxyPort, http.nonProxyHosts) so WebHDFS traffic is not proxied
  4. Reproduce with curl -v on the same OPEN URL to confirm which hop drops the header
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-flight: confirm the endpoint answers like WebHDFS before streaming
HttpURLConnection c = (HttpURLConnection) openUrl.openConnection();
c.setRequestMethod("GET");
boolean chunked = c.getHeaderField("Transfer-Encoding") != null;
if (c.getHeaderField("Content-Length") == null && !chunked) {
  throw new IOException("endpoint " + openUrl + " omits Content-Length; proxy or wrong URL?");
}

Try / catch

try {
  in = fs.open(path);
} catch (IOException e) {
  if (e.getMessage() != null && e.getMessage().contains("Content-Length is missing")) {
    // headers are in the message: inspect for proxy error pages, then bypass the intermediary
    LOG.error("WebHDFS response lacked Content-Length; check proxies on path to NN/DN: {}", e.getMessage());
  }
  throw e;
}

Prevention

When it happens

Trigger: Thrown at hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/ByteRangeInputStream.java:153 when the library encounters an invalid state.

Common situations: Corporate proxies or API gateways (Knox, nginx, custom LBs) between client and NameNode/DataNode HTTP ports that buffer or rewrite responses; transparent proxy inject error bodies without length; misconfigured http.proxyHost JVM properties routing WebHDFS traffic through a proxy.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/cb045c7266e3d13b. Report an issue: GitHub.