dakrone / clj-http

An idiomatic clojure http client wrapping the apache client. Officially supported version.
http://clojars.org/clj-http
MIT License
1.77k stars 408 forks source link

Unable to add cookies to the cookie store #600

Open bryanhonof opened 3 years ago

bryanhonof commented 3 years ago

I'm trying to manually add a cookie to a cookie store, but I'm running into some problems.

Running the following code results in an empty set

(def my-cookie-store (clj-http.cookies/cookie-store))
(clj-http.cookies/add-cookie my-cookie-store (clj-http.cookies/to-basic-client-cookie ["foo" {:value "bar"}]))
(clojure.pprint/pprint (clj-http.cookies/get-cookies my-cookie-store))

=> {}

Whilst running the following results in the expected set of 1 cookie.

(def my-cookie-store (clj-http.cookies/cookie-store))
(clj-http.cookies/add-cookie my-cookie-store (org.apache.http.impl.cookie.BasicClientCookie2. "foo" "bar"))
(clojure.pprint/pprint (clj-http.cookies/get-cookies my-cookie-store))

=> {"foo" {:discard true, :secure false, :value "bar", :version 0}}

Am I using the add-cookie, or to-basic-client-cookie, wrong? Because as far as I understand the source code, both versions of my code should achieve the same results.

rdivyanshu commented 3 years ago

@bryanhonof Reason is to-basic-client-cookie set discard to true https://github.com/dakrone/clj-http-async/blob/877ee3b0dd3d586281e3528a5386ad4bd1f68a7d/src/clj_http/cookies.clj#L50

user=> (def c (clj-http.cookies/to-basic-client-cookie ["foo" {:value "bar"}]))
#'user/c
user=> (.isExpired c (new java.util.Date))
true
user=> (def d (org.apache.http.impl.cookie.BasicClientCookie2. "foo" "bar"))
#'user/d
user=> (.isExpired d (new java.util.Date))
false
user=> (.getExpiryDate c)
nil
user=> (.getExpiryDate d)
nil
user=>

https://github.com/apache/httpcomponents-client/blob/dd76cf91c8f9125c7e0e29770622271f9417b46d/httpclient/src/main/java/org/apache/http/impl/client/BasicCookieStore.java#L90

https://github.com/apache/httpcomponents-client/blob/dd76cf91c8f9125c7e0e29770622271f9417b46d/httpclient/src/main/java/org/apache/http/impl/cookie/BasicClientCookie2.java#L88

It is showing discard true because it is calling (.isPersistent cookie) which is false in both cases, discard is private variable https://github.com/dakrone/clj-http/blob/dd15359451645f677b3e294164cf70330b92241d/src/clj_http/cookies.clj#L36