Skip to content

Commit 0ed3f50

Browse files
committed
Merge pull request #1181 from couchbase/feature/issue_1180_PUT_Doc_IfMatch
Fixed #1180 - Router: PUT /{db}/{doc} If-Match header is not supported
2 parents 7e81bdc + 55e4bfd commit 0ed3f50

1 file changed

Lines changed: 21 additions & 33 deletions

File tree

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

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,9 @@ private boolean cacheWithEtag(String etag) {
198198

199199
private Map<String, Object> getBodyAsDictionary() throws CouchbaseLiteException {
200200
// 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-
}
201+
String contentType = getRequestHeaderContentType();
202+
if (contentType != null && !contentType.equals("application/json"))
203+
throw new CouchbaseLiteException(Status.NOT_ACCEPTABLE);
206204

207205
// parse body text
208206
InputStream contentStream = connection.getRequestInputStream();
@@ -289,7 +287,7 @@ private boolean getQueryOptions(QueryOptions options) {
289287
}
290288

291289
private String getMultipartRequestType() {
292-
String accept = connection.getRequestProperty("Accept");
290+
String accept = getRequestHeaderValue("Accept");
293291
if (accept.startsWith("multipart/")) {
294292
return accept;
295293
}
@@ -338,41 +336,25 @@ private void sendResponse() {
338336
}
339337
}
340338

341-
private void sendErrorResponse(Status status) {
342-
Map<String, Object> result = new HashMap<String, Object>();
343-
result.put("error", status.getHTTPMessage());
344-
result.put("status", status.getHTTPCode());
345-
connection.setResponseBody(new Body(result));
346-
connection.setResponseCode(status.getCode());
347-
sendResponseHeaders(status);
348-
setResponse();
349-
sendResponse();
350-
}
351-
352339
// get Content-Type from URLConnection
353-
private static String getContentType(URLConnection connection) {
354-
String contentType = connection.getRequestProperty("Content-Type");
355-
if (contentType == null)
356-
// From Android: http://developer.android.com/reference/java/net/URLConnection.html
357-
contentType = connection.getRequestProperty("content-type");
358-
340+
private String getRequestHeaderContentType() {
341+
String contentType = getRequestHeaderValue("Content-Type");
359342
if (contentType != null) {
360343
// remove parameter (Content-Type := type "/" subtype *[";" parameter] )
361344
int index = contentType.indexOf(';');
362345
if (index > 0)
363346
contentType = contentType.substring(0, index);
364347
contentType = contentType.trim();
365348
}
366-
367349
return contentType;
368350
}
369351

370-
private static String getAccept(URLConnection connection) {
371-
String accept = connection.getRequestProperty("Accept");
372-
if (accept == null)
352+
private String getRequestHeaderValue(String paramName) {
353+
String value = connection.getRequestProperty(paramName);
354+
if (value == null)
373355
// From Android: http://developer.android.com/reference/java/net/URLConnection.html
374-
accept = connection.getRequestProperty("accept");
375-
return accept;
356+
value = connection.getRequestProperty(paramName.toLowerCase());
357+
return value;
376358
}
377359

378360
public void start() {
@@ -676,7 +658,7 @@ private Status sendResponseHeaders(Status status) {
676658
connection.getResHeader().add("Server", String.format("Couchbase Lite %s", getVersionString()));
677659

678660
// Check for a mismatch between the Accept request header and the response type:
679-
String accept = getAccept(connection);
661+
String accept = getRequestHeaderValue("Accept");
680662
if (accept != null && !"*/*".equals(accept)) {
681663
String responseType = connection.getBaseContentType();
682664
if (responseType != null && responseType.indexOf(accept) < 0) {
@@ -1748,7 +1730,7 @@ private void stopHeartbeat() {
17481730
*/
17491731

17501732
private String getRevIDFromIfMatchHeader() {
1751-
String ifMatch = connection.getRequestProperty("If-Match");
1733+
String ifMatch = getRequestHeaderValue("If-Match");
17521734
if (ifMatch == null) {
17531735
return null;
17541736
}
@@ -2088,7 +2070,13 @@ private Status update(Database _db, String docID, Body body, boolean deleting) {
20882070
if (docID != null && docID.isEmpty() == false) {
20892071
// On PUT/DELETE, get revision ID from either ?rev= query or doc body:
20902072
String revParam = getQuery("rev");
2091-
2073+
String ifMatch = getRequestHeaderValue("If-Match");
2074+
if (ifMatch != null) {
2075+
if(revParam == null)
2076+
revParam = ifMatch;
2077+
else if(!ifMatch.equals(revParam))
2078+
return new Status(Status.BAD_REQUEST);
2079+
}
20922080
if (revParam != null && body != null) {
20932081
String revProp = (String) body.getProperties().get("_rev");
20942082
if (revProp == null) {
@@ -2179,7 +2167,7 @@ private Status updateAttachment(String attachment,
21792167
RevisionInternal rev = db.updateAttachment(
21802168
attachment,
21812169
body,
2182-
getContentType(connection),
2170+
getRequestHeaderContentType(),
21832171
AttachmentInternal.AttachmentEncoding.AttachmentEncodingNone,
21842172
docID,
21852173
revID,

0 commit comments

Comments
 (0)