Skip to content

Commit 37980ef

Browse files
author
hideki
committed
Fixed #1110 - PUT with Content-Type other than application/json succeeds
- Checking content-type for any PUSH/PUT requests does not make sense. For example, push attachment. In stead of checking content-type in `start()` method, check content-type in `getBodyAsDictionary()` which parses JSON body text.
1 parent e8c489a commit 37980ef

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

src/main/java/com/couchbase/lite/router/Router.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ private boolean cacheWithEtag(String etag) {
197197
}
198198

199199
private Map<String, Object> getBodyAsDictionary() throws CouchbaseLiteException {
200+
// check if content-type is `application/json`
201+
String contentType = getContentType(connection);
202+
if (contentType != null) {
203+
if (!contentType.equals("application/json"))
204+
throw new CouchbaseLiteException(Status.NOT_ACCEPTABLE);
205+
}
206+
207+
// parse body text
200208
InputStream contentStream = connection.getRequestInputStream();
201209
try {
202210
return Manager.getObjectMapper().readValue(contentStream, Map.class);
@@ -341,13 +349,24 @@ private void sendErrorResponse(Status status) {
341349
sendResponse();
342350
}
343351

352+
// get Content-Type from URLConnection
344353
private static String getContentType(URLConnection connection) {
345354
String contentType = connection.getRequestProperty("Content-Type");
346355
if (contentType == null)
347356
// From Android: http://developer.android.com/reference/java/net/URLConnection.html
348357
contentType = connection.getRequestProperty("content-type");
358+
359+
if (contentType != null) {
360+
// remove parameter (Content-Type := type "/" subtype *[";" parameter] )
361+
int index = contentType.indexOf(';');
362+
if (index > 0)
363+
contentType = contentType.substring(0, index);
364+
contentType = contentType.trim();
365+
}
366+
349367
return contentType;
350368
}
369+
351370
private static String getAccept(URLConnection connection) {
352371
String accept = connection.getRequestProperty("Accept");
353372
if (accept == null)
@@ -360,23 +379,6 @@ public void start() {
360379
// Refer to: http://wiki.apache.org/couchdb/Complete_HTTP_API_Reference
361380

362381
String method = connection.getRequestMethod();
363-
364-
// check if Content-Type is ""application/json" in case method is PUSH or PUT.
365-
// https://github.com/couchbase/couchbase-lite-java-core/issues/1110
366-
if (method != null && (method.equals("PUT") || method.equals("PUT"))) {
367-
String contentType = getContentType(connection);
368-
if (contentType != null) {
369-
// application/json; charset=utf-8
370-
String[] fields = contentType.split(";");
371-
if (fields.length > 0) {
372-
if (!fields[0].trim().equals("application/json")) {
373-
sendErrorResponse(new Status(Status.NOT_ACCEPTABLE));
374-
return;
375-
}
376-
}
377-
}
378-
}
379-
380382
// We're going to map the request into a method call using reflection based on the method and path.
381383
// Accumulate the method name into the string 'message':
382384
if ("HEAD".equals(method)) {

0 commit comments

Comments
 (0)