Browse Source

Merge pull request #1820 from square/jw/malformed-content-type

Eagerly catch malformed content type headers.
Jesse Wilson 3 years ago
parent
commit
1da3a185a0

+ 5
- 1
retrofit/src/main/java/retrofit2/RequestBuilder.java View File

@@ -75,7 +75,11 @@ final class RequestBuilder {
75 75
 
76 76
   void addHeader(String name, String value) {
77 77
     if ("Content-Type".equalsIgnoreCase(name)) {
78
-      contentType = MediaType.parse(value);
78
+      MediaType type = MediaType.parse(value);
79
+      if (type == null) {
80
+        throw new IllegalArgumentException("Malformed content type: " + value);
81
+      }
82
+      contentType = type;
79 83
     } else {
80 84
       requestBuilder.addHeader(name, value);
81 85
     }

+ 5
- 1
retrofit/src/main/java/retrofit2/ServiceMethod.java View File

@@ -314,7 +314,11 @@ final class ServiceMethod<T> {
314 314
         String headerName = header.substring(0, colon);
315 315
         String headerValue = header.substring(colon + 1).trim();
316 316
         if ("Content-Type".equalsIgnoreCase(headerName)) {
317
-          contentType = MediaType.parse(headerValue);
317
+          MediaType type = MediaType.parse(headerValue);
318
+          if (type == null) {
319
+            throw methodError("Malformed content type: %s", headerValue);
320
+          }
321
+          contentType = type;
318 322
         } else {
319 323
           builder.add(headerName, headerValue);
320 324
         }

+ 34
- 0
retrofit/src/test/java/retrofit2/RequestBuilderTest.java View File

@@ -2395,6 +2395,24 @@ public final class RequestBuilderTest {
2395 2395
     assertThat(request.body().contentType().toString()).isEqualTo("text/not-plain");
2396 2396
   }
2397 2397
 
2398
+  @Test public void malformedContentTypeHeaderThrows() {
2399
+    class Example {
2400
+      @POST("/") //
2401
+      @Headers("Content-Type: hello, world!") //
2402
+      Call<ResponseBody> method(@Body RequestBody body) {
2403
+        return null;
2404
+      }
2405
+    }
2406
+    RequestBody body = RequestBody.create(MediaType.parse("text/plain"), "hi");
2407
+    try {
2408
+      buildRequest(Example.class, body);
2409
+      fail();
2410
+    } catch (IllegalArgumentException e) {
2411
+      assertThat(e).hasMessage("Malformed content type: hello, world!\n"
2412
+          + "    for method Example.method");
2413
+    }
2414
+  }
2415
+
2398 2416
   @Test public void contentTypeAnnotationHeaderAddsHeaderWithNoBody() {
2399 2417
     class Example {
2400 2418
       @DELETE("/") //
@@ -2419,6 +2437,22 @@ public final class RequestBuilderTest {
2419 2437
     assertThat(request.body().contentType().toString()).isEqualTo("text/not-plain");
2420 2438
   }
2421 2439
 
2440
+  @Test public void malformedContentTypeParameterThrows() {
2441
+    class Example {
2442
+      @POST("/") //
2443
+      Call<ResponseBody> method(@Header("Content-Type") String contentType, @Body RequestBody body) {
2444
+        return null;
2445
+      }
2446
+    }
2447
+    RequestBody body = RequestBody.create(MediaType.parse("text/plain"), "hi");
2448
+    try {
2449
+      buildRequest(Example.class, "hello, world!", body);
2450
+      fail();
2451
+    } catch (IllegalArgumentException e) {
2452
+      assertThat(e).hasMessage("Malformed content type: hello, world!");
2453
+    }
2454
+  }
2455
+
2422 2456
   @Test public void malformedAnnotationRelativeUrlThrows() {
2423 2457
     class Example {
2424 2458
       @GET("ftp://example.org")

Loading…
Cancel
Save