{"record":{"id":"497cc2f4901eab6b","repo":"TeamNewPipe/NewPipe","slug":"invalid-content-length","errorCode":null,"errorMessage":"Invalid content length","messagePattern":"Invalid content length","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"warning","filePath":"app/src/main/java/org/schabi/newpipe/DownloaderImpl.java","lineNumber":125,"sourceCode":"                    YOUTUBE_RESTRICTED_MODE_COOKIE);\n        } else {\n            removeCookie(YOUTUBE_RESTRICTED_MODE_COOKIE_KEY);\n        }\n        InfoCache.getInstance().clearCache();\n    }\n\n    /**\n     * Get the size of the content that the url is pointing by firing a HEAD request.\n     *\n     * @param url an url pointing to the content\n     * @return the size of the content, in bytes\n     */\n    public long getContentLength(final String url) throws IOException {\n        try {\n            final Response response = head(url);\n            return Long.parseLong(response.getHeader(\"Content-Length\"));\n        } catch (final NumberFormatException e) {\n            throw new IOException(\"Invalid content length\", e);\n        } catch (final ReCaptchaException e) {\n            throw new IOException(e);\n        }\n    }\n\n    @Override\n    public Response execute(@NonNull final Request request)\n            throws IOException, ReCaptchaException {\n        final String httpMethod = request.httpMethod();\n        final String url = request.url();\n        final Map<String, List<String>> headers = request.headers();\n        final byte[] dataToSend = request.dataToSend();\n\n        RequestBody requestBody = null;\n        if (dataToSend != null) {\n            requestBody = RequestBody.create(dataToSend);\n        }\n","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/TeamNewPipe/NewPipe/blob/9e8be091560a69f35d44bd252eb00bc7911c977b/app/src/main/java/org/schabi/newpipe/DownloaderImpl.java#L107-L143","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nfinal Response response = head(url);\nreturn Long.parseLong(response.getHeader(\"Content-Length\"));\n// after\nfinal Response response = head(url);\nfinal String lengthHeader = response.getHeader(\"Content-Length\");\nif (lengthHeader == null || lengthHeader.isEmpty()) {\n    return -1; // unknown length\n}\ntry {\n    return Long.parseLong(lengthHeader);\n} catch (final NumberFormatException e) {\n    return -1; // unknown length\n}","handlingStrategy":"validation","validationCode":"final Response response = head(url);\nfinal String lengthHeader = response.getHeader(\"Content-Length\");\nif (lengthHeader == null || !lengthHeader.matches(\"\\\\d+\")) {\n    return -1; // treat as unknown length\n}\nreturn Long.parseLong(lengthHeader);","typeGuard":null,"tryCatchPattern":"try {\n    return getContentLength(url);\n} catch (final IOException e) {\n    // header missing or unparseable: treat length as unknown\n    return -1;\n}","preventionTips":["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."],"tags":["network","http","header-parsing","downloader"],"backgroundTag":null,"analyzedSha":"9e8be091560a69f35d44bd252eb00bc7911c977b","analyzedAt":"2026-08-14T00:11:33.519Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}