From d2d1135260df4e5b648c0e03646705a3d05a1db1 Mon Sep 17 00:00:00 2001 From: Alex Osborne Date: Wed, 6 Dec 2023 15:02:43 +0900 Subject: [PATCH] CdxRequestEncoder: Catch IllegalArgumentException when request has invalid content-type header --- .../jwarc/cdx/CdxRequestEncoder.java | 7 ++++++- .../jwarc/cdx/CdxRequestEncoderTest.java | 17 ++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/org/netpreserve/jwarc/cdx/CdxRequestEncoder.java b/src/org/netpreserve/jwarc/cdx/CdxRequestEncoder.java index db57870..5d0c735 100644 --- a/src/org/netpreserve/jwarc/cdx/CdxRequestEncoder.java +++ b/src/org/netpreserve/jwarc/cdx/CdxRequestEncoder.java @@ -28,7 +28,12 @@ public static String encode(HttpRequest httpRequest) throws IOException { out.append("__wb_method="); out.append(httpRequest.method()); int maxLength = out.length() + 1 + QUERY_STRING_LIMIT; - MediaType baseContentType = httpRequest.contentType().base(); + MediaType baseContentType; + try { + baseContentType = httpRequest.contentType().base(); + } catch (IllegalArgumentException e) { + baseContentType = MediaType.OCTET_STREAM; + } InputStream stream = new BufferedInputStream(httpRequest.body().stream(), BUFFER_SIZE); if (baseContentType.equals(MediaType.WWW_FORM_URLENCODED)) { encodeFormBody(stream, out); diff --git a/test/org/netpreserve/jwarc/cdx/CdxRequestEncoderTest.java b/test/org/netpreserve/jwarc/cdx/CdxRequestEncoderTest.java index a427bb5..6972aad 100644 --- a/test/org/netpreserve/jwarc/cdx/CdxRequestEncoderTest.java +++ b/test/org/netpreserve/jwarc/cdx/CdxRequestEncoderTest.java @@ -57,6 +57,8 @@ public class CdxRequestEncoderTest { String.join(" ", Collections.nCopies(1000, "this is very long")) + "\"}", "x=1"), new Case("__wb_method=POST&x=%2", MediaType.WWW_FORM_URLENCODED, "x=%2"), + new Case("__wb_method=POST&__wb_post_data=eD0lMg==", + "application/x-www-form-urlencoded, text/plain", "x=%2"), generateLargeCase() }; @@ -74,6 +76,7 @@ private static Case generateLargeCase() { public static class Case { final String expected; final MediaType requestType; + String requestTypeString; final byte[] requestBody; final String queryString; @@ -96,14 +99,22 @@ public Case(String expected, MediaType requestType, byte[] requestBody, String q this.queryString = queryString; } + public Case(String expected, String requestType, String requestBody) { + this(expected, MediaType.parse("null/null"), requestBody); + requestTypeString = requestType; + } + public HttpRequest request() { String target = "/foo"; if (queryString != null) { target += "?" + queryString; } - return new HttpRequest.Builder("POST", target) - .body(requestType, requestBody) - .build(); + HttpRequest.Builder builder = new HttpRequest.Builder("POST", target) + .body(requestType, requestBody); + if (requestTypeString != null) { + builder.setHeader("Content-Type", requestTypeString); + } + return builder.build(); } }