TeamNewPipe/NewPipe · warning · IOException
Invalid content length
Error message
Invalid content length
What it means
Thrown by DownloaderImpl.getContentLength when the HTTP HEAD response's Content-Length header cannot be parsed as a long via Long.parseLong. This happens when the header is absent (response.getHeader returns null), empty, or a non-numeric value. The method fires a HEAD request to the URL and expects a numeric Content-Length to determine the byte size of remote content.
Source
Thrown at app/src/main/java/org/schabi/newpipe/DownloaderImpl.java:125
YOUTUBE_RESTRICTED_MODE_COOKIE);
} else {
removeCookie(YOUTUBE_RESTRICTED_MODE_COOKIE_KEY);
}
InfoCache.getInstance().clearCache();
}
/**
* Get the size of the content that the url is pointing by firing a HEAD request.
*
* @param url an url pointing to the content
* @return the size of the content, in bytes
*/
public long getContentLength(final String url) throws IOException {
try {
final Response response = head(url);
return Long.parseLong(response.getHeader("Content-Length"));
} catch (final NumberFormatException e) {
throw new IOException("Invalid content length", e);
} catch (final ReCaptchaException e) {
throw new IOException(e);
}
}
@Override
public Response execute(@NonNull final Request request)
throws IOException, ReCaptchaException {
final String httpMethod = request.httpMethod();
final String url = request.url();
final Map<String, List<String>> headers = request.headers();
final byte[] dataToSend = request.dataToSend();
RequestBody requestBody = null;
if (dataToSend != null) {
requestBody = RequestBody.create(dataToSend);
}
View on GitHub (pinned to 9e8be09156)
Solutions
- Treat a missing/invalid Content-Length as unknown (LENGTH_UNSET) rather than throwing: return -1 or C.LENGTH_UNSET when the header is null or unparseable, so callers can fall back to ranged requests.
- Guard for null before parsing: read the header, check it is non-null and matches a numeric pattern before calling Long.parseLong.
- Switch from a HEAD request to a ranged GET request if the server does not support HEAD or omits Content-Length on HEAD responses.
- Log the offending header value and URL to identify which host/URL triggers it, then add host-specific handling.
Example fix
// before
final Response response = head(url);
return Long.parseLong(response.getHeader("Content-Length"));
// after
final Response response = head(url);
final String lengthHeader = response.getHeader("Content-Length");
if (lengthHeader == null || lengthHeader.isEmpty()) {
return -1; // unknown length
}
try {
return Long.parseLong(lengthHeader);
} catch (final NumberFormatException e) {
return -1; // unknown length
} Defensive patterns
Strategy: validation
Validate before calling
final Response response = head(url);
final String lengthHeader = response.getHeader("Content-Length");
if (lengthHeader == null || !lengthHeader.matches("\\d+")) {
return -1; // treat as unknown length
}
return Long.parseLong(lengthHeader); Try / catch
try {
return getContentLength(url);
} catch (final IOException e) {
// header missing or unparseable: treat length as unknown
return -1;
} Prevention
- Never assume Content-Length is present; always null-check and validate the header before parsing.
- Use a ranged GET to discover length when the server does not answer HEAD with Content-Length.
- Return a sentinel (LENGTH_UNSET / -1) for unknown lengths instead of propagating an exception.
When it happens
Trigger: Calling getContentLength(url) against a server that omits the Content-Length header, uses chunked transfer-encoding, returns a header value like "-1" or a multi-value string, or returns a body-less HEAD response where the upstream stripped the header. Any NumberFormatException from Long.parseLong (including null input, since Long.parseLong(null) throws NPE, but an empty/non-numeric string throws NFE) maps to this IOException.
Common situations: Servers behind CDNs that gate HEAD requests differently from GET (returning 405/200 without Content-Length), dynamic/streamed responses that legitimately lack a known length, misconfigured reverse proxies that drop the header, or content served with Transfer-Encoding: chunked. Also when the extractor passes a URL that the host answers with HTML/redirect instead of the expected media.
Related errors
AI-assisted analysis of TeamNewPipe/NewPipe@9e8be09156 (2026-08-14).
Data as JSON: /api/errors/497cc2f4901eab6b.
Report an issue: GitHub.