Skip to content

Commit 7e81bdc

Browse files
committed
Merge pull request #1179 from couchbase/feature/issue_1110_router_contentType2
Fixed #1110 - PUT with Content-Type other than application/json succeeds
2 parents c573342 + 1102186 commit 7e81bdc

1 file changed

Lines changed: 24 additions & 21 deletions

File tree

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

Lines changed: 24 additions & 21 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)) {
@@ -546,10 +548,8 @@ public void start() {
546548
// Send myself a message based on the components:
547549
Status status = null;
548550
try {
549-
550551
Method m = Router.class.getMethod(message, Database.class, String.class, String.class);
551552
status = (Status) m.invoke(this, db, docID, attachmentName);
552-
553553
} catch (NoSuchMethodException msme) {
554554
try {
555555
String errorMessage = String.format("Router unable to route request to %s", message);
@@ -563,22 +563,25 @@ public void start() {
563563
} catch (Exception e) {
564564
//default status is internal server error
565565
Log.e(Log.TAG_ROUTER, "Router attempted do_UNKNWON fallback, but that threw an exception", e);
566+
status = new Status(Status.NOT_FOUND);
566567
Map<String, Object> result = new HashMap<String, Object>();
567-
result.put("error", "not_found");
568+
result.put("status", status.getHTTPCode());
569+
result.put("error", status.getHTTPMessage());
568570
result.put("reason", "Router unable to route request");
569571
connection.setResponseBody(new Body(result));
570-
status = new Status(Status.NOT_FOUND);
571572
}
572573
} catch (Exception e) {
573574
String errorMessage = "Router unable to route request to " + message;
574575
Log.e(Log.TAG_ROUTER, errorMessage, e);
575576
Map<String, Object> result = new HashMap<String, Object>();
576577
if (e.getCause() != null && e.getCause() instanceof CouchbaseLiteException) {
577578
status = ((CouchbaseLiteException) e.getCause()).getCBLStatus();
579+
result.put("status", status.getHTTPCode());
578580
result.put("error", status.getHTTPMessage());
579581
result.put("reason", errorMessage + e.getCause().toString());
580582
} else {
581583
status = new Status(Status.NOT_FOUND);
584+
result.put("status", status.getHTTPCode());
582585
result.put("error", status.getHTTPMessage());
583586
result.put("reason", errorMessage + e.toString());
584587
}

0 commit comments

Comments
 (0)