Skip to content

Commit

Permalink
CdxRequestEncoder: Catch IllegalArgumentException when request has in…
Browse files Browse the repository at this point in the history
…valid content-type header
  • Loading branch information
ato committed Dec 6, 2023
1 parent 0c2503f commit d2d1135
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/org/netpreserve/jwarc/cdx/CdxRequestEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 14 additions & 3 deletions test/org/netpreserve/jwarc/cdx/CdxRequestEncoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
};

Expand All @@ -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;

Expand All @@ -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();
}
}

Expand Down

0 comments on commit d2d1135

Please sign in to comment.