aboutsummaryrefslogtreecommitdiff
path: root/lazybones-hunchentoot.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'lazybones-hunchentoot.lisp')
-rw-r--r--lazybones-hunchentoot.lisp51
1 files changed, 38 insertions, 13 deletions
diff --git a/lazybones-hunchentoot.lisp b/lazybones-hunchentoot.lisp
index 9d91b54..2b84ac1 100644
--- a/lazybones-hunchentoot.lisp
+++ b/lazybones-hunchentoot.lisp
@@ -2,58 +2,62 @@
(defpackage #:lazybones.backend/hunchentoot
(:use #:cl #:lazybones.backend)
- (:local-nicknames (#:h #:hunchentoot )))
+ (:local-nicknames (#:h #:hunchentoot)
+ (#:lzb #:lazybones)
+ (#:a #:alexandria)))
(in-package :lazybones.backend/hunchentoot)
-(defun request-path (request)
+;;; HTTP REQUEST READERS
+
+(defun request-path (&optional (request *request* ))
"Returns the PATH part of the REQUEST URL.
See Also: https://en.wikipedia.org/wiki/URL#Syntax."
(h:script-name request))
-(defun request-host (request)
+(defun request-host (&optional (request *request*))
"Returns the HOST part of the REQUEST URL.
See Also: https://en.wikipedia.org/wiki/URL#Syntax"
(h:host request))
-(defun request-url (request)
+(defun request-url (&optional (request *request*))
"Returns the full url of REQUST"
(h:request-uri* request))
-(defun request-port (request)
+(defun request-port (&optional (request *request*))
"The port associated with REQUEST."
(h:local-port* request))
-(defun request-query-string (request)
+(defun request-query-string (&optional (request *request*))
"Returns the full query string of the URL associated with REQUEST
See Also: https://en.wikipedia.org/wiki/URL#Syntax"
(h:query-string request))
-(defun request-parameter (name request)
+(defun request-parameter (name &optional (request *request*))
"Returns the the value of the query parameter named NAME, or NIL
if there there is none."
(h:get-parameter name request))
-(defun request-parameters (request)
+(defun request-parameters (&optional (request *request*))
"Returns an alist of parameters associated with REQUEST. Each
member of the list looks like (NAME . VALUE) where both are strings."
(h:get-parameters request))
-(defun request-headers (request)
+(defun request-headers (&optional (request *request*))
"Returns an alist of headers associated with REQUEST. Each member of
the list looks like (HEADER-NAME . VALUE) where HEADER-NAME is a
keyword or a string and VALUE is a string."
(h:headers-in request))
-(defun request-header (header-name request)
+(defun request-header (header-name &optional (request *request*))
"Returns the string value of the REQUEST header named HEADER-NAME.
HEADER-NAME can be a keyword or a string."
(h:header-in header-name request))
-(defun request-method (request)
+(defun request-method (&optional (request *request*))
"Returns a keyword representing the http method of the request."
(h:request-method request))
@@ -68,7 +72,7 @@ HEADER-NAME can be a keyword or a string."
(defparameter +hunchentoot-methods-with-body+
'(:post :put :patch))
-(defun request-body (request &key (want-stream-p nil))
+(defun request-body (&key (request *request*) (want-stream-p nil))
"Returns the decoded request body. The value returned depends upon
the value of the Content-Type request header."
(when (member (request-method request) +hunchentoot-methods-with-body+)
@@ -104,4 +108,25 @@ the value of the Content-Type request header."
collect (alexandria:make-keyword k)
collect value))
-
+;;; Hunchentoot Acceptor Subclass
+
+(defclass lazybones-acceptor (h:acceptor)
+ ((installed-apps
+ :accessor acceptor-apps
+ :initform nil
+ :documentation "Instances of LAZYBONES:APP installed to this
+ acceptor. APPs are, among other things, collections of ENDPOINT
+ instances. The acceptor instance uses them to dispatch handlers
+ on requests."))
+ (:default-initargs
+ :address "0.0.0.0"))
+
+(defmethod h:acceptor-dispatch-request ((acceptor lazybones-acceptor) request)
+ (let ((lzb:*request* request)
+ (lzb:*response* h:*reply*))
+ (loop for app in (acceptor-apps acceptor)
+ for (endpoint . args) = (lzb:find-endpoint app request)
+ when endpoint
+ return (lzb:run-endpoint endpoint args request)
+ ;; if no endpoint was found, call next method.
+ finally (call-next-method))))