Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Relevance Pair committed Apr 5, 2013
2 parents 3645df3 + 601ca5e commit df7b59d
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 210 deletions.
62 changes: 0 additions & 62 deletions cors/NOTES.md

This file was deleted.

2 changes: 1 addition & 1 deletion cors/config/logback.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<!-- For loggers in the these namespaces, log at all levels. --> <!-- For loggers in the these namespaces, log at all levels. -->
<logger name="user" level="ALL" /> <logger name="user" level="ALL" />
<!-- To log pedestal internals, enable this and change ThresholdFilter to DEBUG <!-- To log pedestal internals, enable this and change ThresholdFilter to DEBUG
<logger name="pedestal" level="ALL" /> <logger name="io.pedestal" level="ALL" />
--> -->


</configuration> </configuration>
3 changes: 2 additions & 1 deletion cors/dev/dev.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
(def service (-> service/service (def service (-> service/service
(merge {:env :dev (merge {:env :dev
::bootstrap/join? false ::bootstrap/join? false
::bootstrap/routes #(deref #'service/routes)}) ::bootstrap/routes #(deref #'service/routes)
::bootstrap/allowed-origins (constantly true)})
(bootstrap/default-interceptors) (bootstrap/default-interceptors)
(bootstrap/dev-interceptors))) (bootstrap/dev-interceptors)))


Expand Down
5 changes: 3 additions & 2 deletions cors/project.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
:license {:name "Eclipse Public License" :license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"} :url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.0"] :dependencies [[org.clojure/clojure "1.5.0"]
[io.pedestal/pedestal.service "0.1.2"] [io.pedestal/pedestal.service "0.1.3"]
[io.pedestal/pedestal.jetty "0.1.2"] [io.pedestal/pedestal.jetty "0.1.3"]
[ch.qos.logback/logback-classic "1.0.7"]
[org.slf4j/jul-to-slf4j "1.7.2"] [org.slf4j/jul-to-slf4j "1.7.2"]
[org.slf4j/jcl-over-slf4j "1.7.2"] [org.slf4j/jcl-over-slf4j "1.7.2"]
[org.slf4j/log4j-over-slf4j "1.7.2"] [org.slf4j/log4j-over-slf4j "1.7.2"]
Expand Down
50 changes: 3 additions & 47 deletions cors/src/cors/service.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,54 +46,9 @@


(definterceptor thread-id-sender (sse-setup thread-id-sender)) (definterceptor thread-id-sender (sse-setup thread-id-sender))


(defafter cors-interceptor
"Interceptor that adds CORS headers when the origin matches the authorized origin."
[context]
(let [request (:request context)
access-control {:access-control-allow-origin #"localhost:8080"}]
(if (cors/allow-request? request access-control)
(do (log/debug :msg "allowing request"
:request request
:access-control access-control)
(update-in context [:response] #(cors/add-access-control request % access-control)))
(do (log/debug :msg "not allowing request") context))))

(defbefore cors-options-interceptor
"Interceptor that adds CORS headers when the origin matches the authorized origin."
[context]
;; special case options request
(let [request (:request context)
_ (log/debug :msg "options request headers" :headers (:headers request))
preflight-origin (get-in request [:headers "origin"])
preflight-headers (get-in request [:headers "access-control-request-headers"])]
(log/debug :msg "access-control-request-headers" :preflight-headers preflight-headers)
(assoc context :response
(when (= (:request-method request) :options)
(-> (ring-response/response "")
(ring-response/header "Access-Control-Allow-Origin" preflight-origin)
(ring-response/header "Access-Control-Allow-Methods" "GET, POST, PUT, DELETE, OPTIONS, HEAD")
(ring-response/header "Access-Control-Allow-Headers" preflight-headers))))))

(defbefore cors-sse-clever-hack-interceptor
[context]
(let [request (:request context)
servlet-response (:servlet-response request)
access-control {:access-control-allow-origin #"localhost:8080"}
response (-> (ring-response/response "")
(ring-response/content-type "text/event-stream")
(ring-response/charset "UTF-8")
(ring-response/header "Connection" "close")
(ring-response/header "Cache-control" "no-cache")
(#(cors/add-access-control request % access-control)))]
(servlet-interceptor/set-response servlet-response response)
(.flushBuffer servlet-response)
context))


(defroutes routes (defroutes routes
[[["/js" {:get send-js} ^:interceptors [cors-interceptor]] [[["/js" {:get send-js}]
["/" ^:interceptors [cors-interceptor cors-sse-clever-hack-interceptor] ["/" {:get thread-id-sender}]]])
{:get thread-id-sender :options cors-options-interceptor}]]])




;; You can use this fn or a per-request fn via io.pedestal.service.http.route/url-for ;; You can use this fn or a per-request fn via io.pedestal.service.http.route/url-for
Expand All @@ -107,6 +62,7 @@
;; default interceptors will be ignored. ;; default interceptors will be ignored.
;; :bootstrap/interceptors [] ;; :bootstrap/interceptors []
::bootstrap/routes routes ::bootstrap/routes routes
::bootstrap/allowed-origins ["http://localhost:8080"]
;; Root for resource interceptor that is available by default. ;; Root for resource interceptor that is available by default.
::bootstrap/resource-path "/public" ::bootstrap/resource-path "/public"
;; Choose from [:jetty :tomcat]. ;; Choose from [:jetty :tomcat].
Expand Down
97 changes: 0 additions & 97 deletions cors/src/user.clj

This file was deleted.

0 comments on commit df7b59d

Please sign in to comment.