 (defn as-sequential [s]
   (if (sequential? s) s [s]))
 
-(defn cors [allow-origin]
-  (fn [ctx]
-    (if-let [origin (p/allow-origin allow-origin ctx)]
-      (update-in ctx [:response :headers]
-                 merge {"access-control-allow-origin"
-                        origin
-                        "access-control-expose-headers"
-                        (apply str
-                               (interpose ", " ["Server" "Date" "Content-Length" "Access-Control-Allow-Origin"]))})
-      ctx)))
-
 (defn exists-flow [method state req status headers body post allow-origin]
   (fn [ctx]
     (case method

              ;; Split the flow based on the existence of the resource
              (fn [ctx]

                (cond
                  ;; 'Exists' flow
                  ;; TODO: Not sure that exists-flow is what we're doing here - exists-flow has all kinds of things complected in it. Break up into individual cases, perhaps based on method. We are only using this to avoid the 404 of the 'not exists' flow. Perhaps check for existence and throw the 404 now if necessary.
                  (or state body (#{:post :put} method))
                  (d/chain
                   ctx
                   ;; Not sure we should use exists-flow for CORS pre-flight requests, should handle further above
                   (exists-flow method state req status headers body post allow-origin)
                   (cors allow-origin)

                   (fn [ctx]
                     (merge
                      {:status (or (get-in ctx [:response :status])
                                   (p/status status ctx)
                                   200)
                       :headers (merge
                                 (get-in ctx [:response :headers])
                                 (p/headers headers ctx))
                       ;; TODO :status and :headers should be implemented like this in all cases
                       :body (get-in ctx [:response :body])}))
                   )

                  ;; 'Not exists' flow
                  :otherwise
                  (d/chain ctx (constantly {:status 404})))))

             ;; Handle exits
             (d/catch clojure.lang.ExceptionInfo

               ;; temporarily storing the whole (potentially huge) body
               ;; somewhere.
               #_(assoc req
                        ;; We assoc in a 'delayed' body to the context's
                        ;; version of the request. The first caller to
                        ;; deref causes the slurp (which to do ahead of
                        ;; time might be unnecessarily wasteful).
                        :body (delay
                               (if-let [body (:body req)]
                                 ;; TODO Don't slurp, read into a byte array
                                 ;; Does aleph provide access to the underlying javax.nio.ByteBuffer(s)?
                                 ;; (yes - provided option raw-stream? is true)
                                 (slurp body :encoding (or (character-encoding req) "utf8"))
                                 nil)))
               :resource-map resource-map})

             (d/chain
