Skip to content

Commit

Permalink
fix #43: last set-cookie headers on a page overwrites the ones before
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Apr 3, 2022
1 parent 5e8daab commit a1dab16
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## 0.1.2

- fix [#43](https://github.com/babashka/babashka.curl/issues/43): last set-cookie headers on a page overwrites the ones before

## 0.1.1

- Add `:as :bytes` [#38](https://github.com/babashka/babashka.curl/issues/38)
Expand Down
2 changes: 1 addition & 1 deletion script/test
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash

clojure -M:test
clojure -M:test "$@"
29 changes: 20 additions & 9 deletions src/babashka/curl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,25 @@
(io/copy in bout)
(.toByteArray bout)))

(defn- parse-headers [headers]
(reduce (fn [[status parsed-headers :as acc] header-line]
(if (str/starts-with? header-line "HTTP/")
[(Integer/parseInt (second (str/split header-line #" "))) parsed-headers]
(let [[k v] (str/split header-line #":" 2)]
(if (and k v)
[status (update parsed-headers (str/lower-case k)
(fn [prev]
(let [v (str/trim v)]
(cond (nil? prev)
v ;; backward compatibility
(vector? prev)
(conj prev v)
:else
(conj [prev] v)))))]
acc))))
[nil {}]
headers))

(defn- curl-response->map
"Parses a curl response input stream into a map"
[opts]
Expand All @@ -194,15 +213,7 @@
(slurp is)) (slurp err) (.waitFor ^java.lang.Process process)])
headers (read-headers (:header-file opts))
[status headers]
(reduce (fn [[status parsed-headers :as acc] header-line]
(if (str/starts-with? header-line "HTTP/")
[(Integer/parseInt (second (str/split header-line #" "))) parsed-headers]
(let [[k v] (str/split header-line #":" 2)]
(if (and k v)
[status (assoc parsed-headers (str/lower-case k) (str/trim v))]
acc))))
[nil {}]
headers)
(parse-headers headers)
response {:status status
:headers headers
:body body
Expand Down
5 changes: 5 additions & 0 deletions test/babashka/curl_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,8 @@

(testing "follow redirects set to false"
(is (= 302 (:status (curl/get "https://httpstat.us/302" {:follow-redirects false}))))))

(deftest parse-headers-test
(testing "cookie headers"
(is (= {"set-cookie" ["foo=bar" "baz=quux"]}
(second (#'curl/parse-headers ["Set-Cookie: foo=bar" "Set-Cookie: baz=quux"]))))))

0 comments on commit a1dab16

Please sign in to comment.