Skip to content

Commit 210bdbc

Browse files
author
hideki
committed
Fixed #1110 - PUT with Content-Type other than application/json succeeds
Check if content-type request header value is `application/json` in case http request method is `PUSH` or `PUT`
1 parent 1639eaf commit 210bdbc

1 file changed

Lines changed: 39 additions & 3 deletions

File tree

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,49 @@ private void sendResponse() {
330330
}
331331
}
332332

333+
private void sendErrorResponse(Status status) {
334+
Map<String, Object> result = new HashMap<String, Object>();
335+
result.put("error", status.getHTTPMessage());
336+
result.put("status", status.getHTTPCode());
337+
connection.setResponseBody(new Body(result));
338+
connection.setResponseCode(status.getCode());
339+
sendResponseHeaders(status);
340+
setResponse();
341+
sendResponse();
342+
}
343+
344+
private static String getContentType(URLConnection connection) {
345+
String contentType = connection.getRequestProperty("Content-Type");
346+
if (contentType == null)
347+
// From Android: http://developer.android.com/reference/java/net/URLConnection.html
348+
contentType = connection.getRequestProperty("content-type");
349+
return contentType;
350+
}
351+
333352
public void start() {
334353
// Refer to: http://wiki.apache.org/couchdb/Complete_HTTP_API_Reference
335354

355+
String method = connection.getRequestMethod();
356+
357+
// check if Content-Type is ""application/json" in case method is PUSH or PUT.
358+
// https://github.com/couchbase/couchbase-lite-java-core/issues/1110
359+
if (method != null && (method.equals("PUT") || method.equals("PUT"))) {
360+
String contentType = getContentType(connection);
361+
if (contentType != null) {
362+
// application/json; charset=utf-8
363+
String[] fields = contentType.split(";");
364+
if (fields.length > 0) {
365+
if (!fields[0].trim().equals("application/json")) {
366+
sendErrorResponse(new Status(Status.NOT_ACCEPTABLE));
367+
return;
368+
}
369+
}
370+
}
371+
}
372+
336373
// We're going to map the request into a method call using reflection based on the method and path.
337374
// Accumulate the method name into the string 'message':
338-
String method = connection.getRequestMethod();
339-
if ("HEAD".equals(method)) {
375+
if ("HEAD".equals(method)) {
340376
method = "GET";
341377
}
342378
String message = String.format("do_%s", method);
@@ -2132,7 +2168,7 @@ private Status updateAttachment(String attachment,
21322168
RevisionInternal rev = db.updateAttachment(
21332169
attachment,
21342170
body,
2135-
connection.getRequestProperty("content-type"),
2171+
getContentType(connection),
21362172
AttachmentInternal.AttachmentEncoding.AttachmentEncodingNone,
21372173
docID,
21382174
revID,

0 commit comments

Comments
 (0)