From a3d651ba460c321bab34197e3d9ed3f85c45ed42 Mon Sep 17 00:00:00 2001 From: Alex Osborne Date: Mon, 13 Dec 2021 15:41:38 +0900 Subject: [PATCH] FetchHTTP: Handle null characters in the Content-Length header If a null character appears in the Content-Length header Firefox and Chrome seem to ignore the rest of the value so let's do the same. Let's also handle the exception when parsing the content-length fails. Fixes #449 --- .../java/org/archive/modules/fetcher/FetchHTTP.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/modules/src/main/java/org/archive/modules/fetcher/FetchHTTP.java b/modules/src/main/java/org/archive/modules/fetcher/FetchHTTP.java index 45b2bf72..a089e678 100644 --- a/modules/src/main/java/org/archive/modules/fetcher/FetchHTTP.java +++ b/modules/src/main/java/org/archive/modules/fetcher/FetchHTTP.java @@ -685,8 +685,17 @@ public class FetchHTTP extends Processor implements Lifecycle { long contentLength = -1l; Header h = response.getLastHeader("content-length"); - if (h != null && h.getValue().trim().length()>0) { - contentLength = Long.parseLong(h.getValue()); + if (h != null) { + // browsers ignore everything after a null character and some buggy web servers rely on this + String contentLengthHeader = StringUtils.substringBefore(h.getValue(), "\0").trim(); + if (!contentLengthHeader.isEmpty()) { + try { + contentLength = Long.parseLong(contentLengthHeader); + } catch (NumberFormatException e) { + cleanup(curi, e, "invalid content-length header", S_CONNECT_LOST); + return; + } + } } try { if (!req.request.isAborted()) {