Skip to content

Commit

Permalink
Add headers options to define-handler
Browse files Browse the repository at this point in the history
- make-closing-handler (and by extension define-handler and
  define-json-handler) now exposes a `headers` name which should contain
  an association list of headers that will be written out to the
  response stream
  • Loading branch information
inaimathi committed May 29, 2017
1 parent 51ed405 commit a5c2f08
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
14 changes: 11 additions & 3 deletions define-handler.lisp
Expand Up @@ -84,7 +84,10 @@ parameters with a lower priority can refer to parameters of a higher priority.")
`(lambda (sock ,cookie? session request)
(declare (ignorable session request))
,(arguments args
`(let* ((result (progn ,@body))
`(let* ((headers (list (cons "Access-Control-Allow-Origin" "*")
(cons "Cache-Control" "no-cache, no-store, must-revalidate")
(cons "Access-Control-Allow-Headers" "Content-Type")))
(result (progn ,@body))
(response
(if (typep result 'response)
result
Expand All @@ -93,6 +96,7 @@ parameters with a lower priority can refer to parameters of a higher priority.")
:content-type ,content-type
:cookie (unless ,cookie? (token session))
:body result))))
(setf (headers response) headers)
(write! response (flex-stream sock))
(socket-close sock))))))

Expand All @@ -101,12 +105,16 @@ parameters with a lower priority can refer to parameters of a higher priority.")
`(lambda (sock ,cookie? session request)
(declare (ignorable session request))
,(arguments args
`(let ((res (progn ,@body))
`(let ((headers (list (cons "Access-Control-Allow-Origin" "*")
(cons "Cache-Control" "no-cache, no-store, must-revalidate")
(cons "Access-Control-Allow-Headers" "Content-Type")))
(res (progn ,@body))
(stream (flex-stream sock)))
(write!
(make-instance
'response
:keep-alive? t :content-type "text/event-stream"
:headers headers
:cookie (unless ,cookie? (token session)))
stream)
(crlf stream)
Expand Down Expand Up @@ -142,7 +150,7 @@ parameters with a lower priority can refer to parameters of a higher priority.")
`(make-closing-handler (:content-type ,content-type) ,full-params ,@body)
`(make-stream-handler ,full-params ,@body)))))

(defmacro define-json-handler ((name) (&rest args) &body body)
(defmacro define-json-handler ((name &key (method :any)) (&rest args) &body body)
`(define-handler (,name :content-type "application/json") ,args
(json:encode-json-to-string (progn ,@body))))

Expand Down
7 changes: 5 additions & 2 deletions house.lisp
Expand Up @@ -145,8 +145,11 @@
(defmethod write! ((res response) (stream stream))
(write-ln stream "HTTP/1.1 " (response-code res))
(write-ln stream "Content-Type: " (content-type res) "; charset=" (charset res))
(write-ln stream "Cache-Control: no-cache, no-store, must-revalidate")
(write-ln stream "Access-Control-Allow-Origin: *")
(loop for (name . value) in (headers res)
when (not (member
name '("Content-Type" "Set-Cookie" "Location" "Connection" "Expires" "Content-Length")
:test-not #'string/=))
do (write-ln stream name ": " value))
(awhen (cookie res)
(if (null *cookie-domains*)
(write-ln stream "Set-Cookie: name=" it)
Expand Down
1 change: 1 addition & 0 deletions model.lisp
Expand Up @@ -34,6 +34,7 @@

(defclass response ()
((content-type :accessor content-type :initform "text/html" :initarg :content-type)
(headers :accessor headers :initarg :headers :initform nil)
(charset :accessor charset :initform "utf-8")
(response-code :accessor response-code :initform "200 OK" :initarg :response-code)
(cookie :accessor cookie :initform nil :initarg :cookie)
Expand Down

0 comments on commit a5c2f08

Please sign in to comment.