Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/main/java/com/couchbase/lite/replicator/Replication.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import okhttp3.Cookie;
import okhttp3.HttpUrl;

/**
* The external facade for the Replication API
*/
Expand Down Expand Up @@ -916,10 +919,35 @@ public Map<String, Object> getFilterParams() {
*/
@InterfaceAudience.Public
public void setHeaders(Map<String, Object> requestHeadersParam) {
// Fix - https://github.com/couchbase/couchbase-lite-java-core/issues/1617
storeCookiesIntoCookieJar(requestHeadersParam);

properties.put(ReplicationField.REQUEST_HEADERS, requestHeadersParam);
replicationInternal.setHeaders(requestHeadersParam);
}

private void storeCookiesIntoCookieJar(Map<String, Object> requestHeadersParam) {
try {
if (requestHeadersParam != null
&& requestHeadersParam.containsKey("Cookie")
&& requestHeadersParam.get("Cookie") instanceof String) {
String cookieString = (String) requestHeadersParam.get("Cookie");
if (remote != null) {
// NOTE: In case that the path of URL is not end with `/`,
// last segment of a path is not stored as a path of Cookie.
Cookie cookie = Cookie.parse(HttpUrl.get(remote), cookieString);
if (cookie != null) {
replicationInternal.setCookie(cookie);
// remove Cookie value from requestHeadersParam if cookie is successfully stored into the cookie jar.
requestHeadersParam.remove("Cookie");
}
}
}
} catch (Exception e) {
Log.e(Log.TAG_SYNC, "Failed to store SyncGatewaySession into the CookieJar.", e);
}
}

/**
* Extra HTTP headers to send in all requests to the remote server.
* Should map strings (header names) to strings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package com.couchbase.lite.replicator;

import com.couchbase.lite.AsyncTask;
import com.couchbase.lite.CouchbaseLiteException;
import com.couchbase.lite.Database;
import com.couchbase.lite.Misc;
import com.couchbase.lite.ReplicationFilter;
Expand Down Expand Up @@ -1807,6 +1806,12 @@ public void setCookie(String name, String value, String path, Date expirationDat
this.clientFactory.addCookies(cookies);
}

public void setCookie(Cookie cookie) {
if (cookie == null)
return;
this.clientFactory.addCookies(Collections.singletonList(cookie));
}

/**
* For java docs, see Replication.deleteCookie()
*/
Expand Down