Skip to content

Commit

Permalink
Remove internal decisions for conditional requests
Browse files Browse the repository at this point in the history
Remove *-valid-date? and *-exists and more internal decision points
which did not make sense to be customized. The decision logic has been
incorporated into the implementation into the actual decision points.

Removes

* if-match-star
* if-modified-since-exists?
* if-modified-since-valid-date?
* if-unmodified-since-exists?
* if-unmodified-since-valid-date?
* if-none-match-exists?
* if-none-match-star?
* if-match-exists?
* if-match-star?
  • Loading branch information
ordnungswidrig committed Aug 31, 2015
1 parent b9fc758 commit 4ac1786
Showing 1 changed file with 30 additions and 66 deletions.
96 changes: 30 additions & 66 deletions src/liberator/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
(doseq [l *loggers*]
(l category values)))

(declare if-none-match-exists?)

(defn map-values [f m]
(persistent! (reduce-kv (fn [out-m k v] (assoc! out-m k (f v))) (transient {}) m)))

Expand Down Expand Up @@ -192,12 +190,6 @@
`(defn ~name [context#]
(run-handler '~name ~status ~message context#)))

(defn header-exists? [header context]
(get-in context [:request :headers header]))

(defn if-match-star [context]
(= "*" (get-in context [:request :headers "if-match"])))

(defn =method [method context]
(= (get-in context [:request :request-method]) method))

Expand Down Expand Up @@ -283,7 +275,7 @@
(defhandler handle-precondition-failed 412 "Precondition failed.")

(defdecision if-match-star-exists-for-missing?
if-match-star
(fn [context] (= "*" (get-in context [:request :headers "if-match"])))
handle-precondition-failed
method-put?)

Expand Down Expand Up @@ -314,78 +306,50 @@
method-patch?)

(defdecision modified-since?
(fn [context]
(let [last-modified (gen-last-modified context)]
[(and last-modified (.after last-modified (::if-modified-since-date context)))
{::last-modified last-modified}]))
(fn [{:keys [request] :as context}]
(let [modified-since (parse-http-date (get-in request [:headers "if-modified-since"]))]
(or (nil? modified-since)
(let [last-modified (gen-last-modified context)]
[(and last-modified (.after last-modified modified-since))
{::last-modified last-modified}]))))
method-delete?
handle-not-modified)

(defdecision if-modified-since-valid-date?
(fn [context]
(if-let [date (parse-http-date (get-in context [:request :headers "if-modified-since"]))]
{::if-modified-since-date date}))
modified-since?
method-delete?)

(defdecision if-modified-since-exists?
(partial header-exists? "if-modified-since")
if-modified-since-valid-date?
method-delete?)

(defdecision etag-matches-for-if-none?
(fn [context]
(let [etag (gen-etag context)]
[(= (get-in context [:request :headers "if-none-match"]) etag)
{::etag etag}]))
if-none-match?
if-modified-since-exists?)

(defdecision if-none-match-star?
#(= "*" (get-in % [:request :headers "if-none-match"]))
(fn [{:keys [request] :as context}]
(if-let [if-none-match (get-in context [:request :headers "if-none-match"])]
(let [etag (gen-etag context)]
[(#{"*" etag} if-none-match)
{::etag etag}])))
if-none-match?
etag-matches-for-if-none?)

(defdecision if-none-match-exists? (partial header-exists? "if-none-match")
if-none-match-star? if-modified-since-exists?)
modified-since?)

(defdecision unmodified-since?
(fn [context]
(let [last-modified (gen-last-modified context)]
[(and last-modified
(.after last-modified
(::if-unmodified-since-date context)))
{::last-modified last-modified}]))
(fn [{:keys [request] :as context}]
(when-let [unmodified-since (parse-http-date (get-in request [:headers "if-unmodified-since"]))]
(let [last-modified (gen-last-modified context)]
[(and last-modified (.after last-modified unmodified-since))
{::last-modified last-modified}])))
handle-precondition-failed
if-none-match-exists?)

(defdecision if-unmodified-since-valid-date?
(fn [context]
(when-let [date (parse-http-date (get-in context [:request :headers "if-unmodified-since"]))]
{::if-unmodified-since-date date}))
unmodified-since?
if-none-match-exists?)
etag-matches-for-if-none?)

(defdecision if-unmodified-since-exists? (partial header-exists? "if-unmodified-since")
if-unmodified-since-valid-date? if-none-match-exists?)
(defn- match-etag-for-existing [{:keys [request resource] :as context}]
(let [if-match (get-in request [:headers "if-match"])]
(or (empty? if-match)
(= "*" if-match)
(let [etag (gen-etag context)]
[(= etag if-match)
{::etag etag}]))))

(defdecision etag-matches-for-if-match?
(fn [context]
(let [etag (gen-etag context)]
[(= etag (get-in context [:request :headers "if-match"]))
{::etag etag}]))
if-unmodified-since-exists?
match-etag-for-existing
unmodified-since?
handle-precondition-failed)

(defdecision if-match-star?
if-match-star if-unmodified-since-exists? etag-matches-for-if-match?)

(defdecision if-match-exists? (partial header-exists? "if-match")
if-match-star? if-unmodified-since-exists?)

(defdecision exists? if-match-exists? if-match-star-exists-for-missing?)
(defdecision exists? etag-matches-for-if-match? if-match-star-exists-for-missing?)

(defhandler handle-unprocessable-entity 422 "Unprocessable entity.")

(defdecision processable? exists? handle-unprocessable-entity)

(defhandler handle-not-acceptable 406 "No acceptable resource available.")
Expand Down

0 comments on commit 4ac1786

Please sign in to comment.