dakrone / clj-http

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

cookie-policy none #625

Open justin2004 opened 2 years ago

justin2004 commented 2 years ago

if there is 1 cookie assignment on a line from the webserver it works.

justin@parens:~$ cat headers 
content-type: application/json; charset=utf-8
set-cookie: expires=0
justin@parens:~$ cat return 
{"key":"val"}
justin@parens:~$ { echo -e 'HTTP/1.1 200 OK'; cat headers ; echo -e '\r\n' ; cat return ; sh test; } |  nc -q1 -vvv -l 9090 
Listening on [0.0.0.0] (family 2, port 9090)
Listening on 0.0.0.0 9090

then this works as expected (even though the cookie is malformed)

(pprint/pprint (client/get "http://127.0.0.1:9090" ))
{:cached nil,
 :request-time 5,
 :repeatable? false,
 :protocol-version {:name "HTTP", :major 1, :minor 1},
 :streaming? true,
 :http-client
 #object[org.apache.http.impl.client.InternalHttpClient 0x21cb26d1 "org.apache.http.impl.client.InternalHttpClient@21cb26d1"],
 :chunked? false,
 :cookies
 {"expires"
  {:discard true, :path "/", :secure false, :value "0", :version 0}},
 :reason-phrase "OK",
 :headers {"content-type" "application/json; charset=utf-8"},
 :orig-content-encoding nil,
 :status 200,
 :length -1,
 :body "\n{\"key\":\"val\"}\n",
 :trace-redirects []}

but if there are multiple cookie assignment on a line from the webserver we get an error that we can't ignore even if we want to ignore it:

justin@parens:~$ cat headers 
content-type: application/json; charset=utf-8
set-cookie: domain=.example.com;expires=0
justin@parens:~$ { echo -e 'HTTP/1.1 200 OK'; cat headers ; echo -e '\r\n' ; cat return ; sh test; } |  nc -q1 -vvv -l 9090 
Listening on [0.0.0.0] (family 2, port 9090)
Listening on 0.0.0.0 9090
(pprint/pprint (client/get "http://127.0.0.1:9090" {:cookie-policy :none :throw-exceptions false} ))
Execution error (MalformedCookieException) at org.apache.http.impl.cookie.BasicExpiresHandler/parse (BasicExpiresHandler.java:64).
Invalid 'expires' attribute: 0
#error {
 :cause "Invalid 'expires' attribute: 0"
 :via
 [{:type org.apache.http.cookie.MalformedCookieException
   :message "Invalid 'expires' attribute: 0"
   :at [org.apache.http.impl.cookie.BasicExpiresHandler parse "BasicExpiresHandler.java" 64]}]
 :trace
 [[org.apache.http.impl.cookie.BasicExpiresHandler parse "BasicExpiresHandler.java" 64]
  [org.apache.http.impl.cookie.BrowserCompatSpec parse "BrowserCompatSpec.java" 172]
  [clj_http.cookies$decode_cookie invokeStatic "cookies.clj" 69]
  [clj_http.cookies$decode_cookie invoke "cookies.clj" 61]
  [clojure.core$map$fn__5884 invoke "core.clj" 2757]
  [clojure.lang.LazySeq sval "LazySeq.java" 42]
  [clojure.lang.LazySeq seq "LazySeq.java" 51]
  [clojure.lang.RT seq "RT.java" 535]
  [clojure.core$seq__5419 invokeStatic "core.clj" 139]
  [clojure.core.protocols$seq_reduce invokeStatic "protocols.clj" 24]
  [clojure.core.protocols$fn__8168 invokeStatic "protocols.clj" 75]
  [clojure.core.protocols$fn__8168 invoke "protocols.clj" 75]
  [clojure.core.protocols
$fn__8110$G__8105__8123 invoke "protocols.clj" 13]
  [clojure.core$reduce invokeStatic "core.clj" 6830]
  [clojure.core$reduce invoke "core.clj" 6812]
  [clj_http.cookies$decode_cookies invokeStatic "cookies.clj" 82]
  [clj_http.cookies$decode_cookies invoke "cookies.clj" 79]
  [clj_http.cookies$decode_cookie_header invokeStatic "cookies.clj" 90]
  [clj_http.cookies$decode_cookie_header invoke "cookies.clj" 85]
  [clj_http.cookies$cookies_response invokeStatic "cookies.clj" 119]
  [clj_http.cookies$cookies_response invoke "cookies.clj" 115]
  [clj_http.cookies$wrap_cookies$fn__26377 invoke "cookies.clj" 128]
  [clj_http.links$wrap_links$fn__27898 invoke "links.clj" 63]
  [clj_http.client$wrap_unknown_host$fn__28535 invoke "client.clj" 1046]
  [clj_http.client$request_STAR_ invokeStatic "client.clj" 1174]
  [clj_http.client$request_STAR_ invoke "client.clj" 1167]
  [clj_http.client$get invokeStatic "client.clj" 1180]
  [clj_http.client$get doInvoke "client.clj" 1176]
  [clojure.lang.RestFn invoke "RestFn.java"
 410]
  [rule_30$eval28776 invokeStatic "NO_SOURCE_FILE" 9]
  [rule_30$eval28776 invoke "NO_SOURCE_FILE" 9]
  [clojure.lang.Compiler eval "Compiler.java" 7181]
  [clojure.lang.Compiler eval "Compiler.java" 7136]
  [clojure.core$eval invokeStatic "core.clj" 3202]
  [clojure.core$eval invoke "core.clj" 3198]
  [nrepl.middleware.interruptible_eval$evaluate$fn__1265$fn__1266 invoke "interruptible_eval.clj" 87]
  [clojure.lang.AFn applyToHelper "AFn.java" 152]
  [clojure.lang.AFn applyTo "AFn.java" 144]
  [clojure.core$apply invokeStatic "core.clj" 667]
  [clojure.core$with_bindings_STAR_ invokeStatic "core.clj" 1977]
  [clojure.core$with_bindings_STAR_ doInvoke "core.clj" 1977]
  [clojure.lang.RestFn invoke "RestFn.java" 425]
  [nrepl.middleware.interruptible_eval$evaluate$fn__1265 invoke "interruptible_eval.clj" 87]
  [clojure.main$repl$read_eval_print__9110$fn__9113 invoke "main.clj" 437]
  [clojure.main$repl$read_eval_print__9110 invoke "main.clj" 437]
  [clojure.main$repl$fn__9119 invoke "main.clj" 458]
  [cl
ojure.main$repl invokeStatic "main.clj" 458]
  [clojure.main$repl doInvoke "main.clj" 368]
  [clojure.lang.RestFn invoke "RestFn.java" 1523]
  [nrepl.middleware.interruptible_eval$evaluate invokeStatic "interruptible_eval.clj" 84]
  [nrepl.middleware.interruptible_eval$evaluate invoke "interruptible_eval.clj" 56]
  [nrepl.middleware.interruptible_eval$interruptible_eval$fn__1298$fn__1302 invoke "interruptible_eval.clj" 152]
  [clojure.lang.AFn run "AFn.java" 22]
  [nrepl.middleware.session$session_exec$main_loop__1368$fn__1372 invoke "session.clj" 218]
  [nrepl.middleware.session$session_exec$main_loop__1368 invoke "session.clj" 217]
  [clojure.lang.AFn run "AFn.java" 22]
  [java.lang.Thread run "Thread.java" 829]]}