From 6087155fa89c84101503779e654c2bbe4e28269f Mon Sep 17 00:00:00 2001 From: Shaka Chen Date: Mon, 8 Aug 2022 11:54:53 +0800 Subject: [PATCH 1/8] use (or form-1 form-2) instead of (if form-1 form-1 form-2) --- source/clog-system.lisp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/clog-system.lisp b/source/clog-system.lisp index a08fc96..553f358 100644 --- a/source/clog-system.lisp +++ b/source/clog-system.lisp @@ -97,8 +97,7 @@ example." (set-on-new-window on-new-window-handler :path "/" :boot-file boot-file)) (unless *clog-running* (setf *clog-running* t) - (setf *static-root* (truename (if *overide-static-root* - *overide-static-root* + (setf *static-root* (truename (or *overide-static-root* static-root))) (clog-connection:initialize #'on-connect :host host From b78c5588cf5c99c52c25e42685895b5f570dd5d4 Mon Sep 17 00:00:00 2001 From: Shaka Chen Date: Mon, 8 Aug 2022 11:57:13 +0800 Subject: [PATCH 2/8] auto indented --- source/clog-system.lisp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/clog-system.lisp b/source/clog-system.lisp index 553f358..57bc8b7 100644 --- a/source/clog-system.lisp +++ b/source/clog-system.lisp @@ -71,7 +71,7 @@ the same as the clog directy this overides the relative paths used in them.") (static-boot-html nil) (static-boot-js nil) (static-root (merge-pathnames "./static-files/" - (asdf:system-source-directory :clog)))) + (asdf:system-source-directory :clog)))) "Inititalize CLOG on a socket using HOST and PORT to serve BOOT-FILE as the default route to establish web-socket connections and static files located at STATIC-ROOT. The webserver used with CLACK can be chosed with :SERVER. If From edc5e1107b9088ddc0515b15b3b7b1ac173626f8 Mon Sep 17 00:00:00 2001 From: Shaka Chen Date: Mon, 8 Aug 2022 11:58:22 +0800 Subject: [PATCH 3/8] Fewer duplicate symbols. It could be better to decompose clog-connection:initialize into multiple functions which clog-system:initialize can call. --- source/clog-system.lisp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/source/clog-system.lisp b/source/clog-system.lisp index 57bc8b7..2d3232c 100644 --- a/source/clog-system.lisp +++ b/source/clog-system.lisp @@ -60,6 +60,7 @@ the same as the clog directy this overides the relative paths used in them.") (defun initialize (on-new-window-handler + &rest rest &key (host "0.0.0.0") (port 8080) @@ -92,6 +93,16 @@ compiled version. boot-function if set is called with the url and the contents of boot-file and its return value replaces the contents sent to the brower, this allows adding content for search engine optimization, see tutorial 12 for an example." + (declare (ignorable host + port + server + extended-routing + long-poll-first + boot-file + boot-function + static-boot-html + static-boot-js + static-root)) (setf *extended-routing* extended-routing) (when on-new-window-handler (set-on-new-window on-new-window-handler :path "/" :boot-file boot-file)) @@ -99,17 +110,9 @@ example." (setf *clog-running* t) (setf *static-root* (truename (or *overide-static-root* static-root))) - (clog-connection:initialize #'on-connect - :host host - :port port - :server server - :long-poll-first long-poll-first - :extended-routing extended-routing - :boot-file boot-file - :boot-function boot-function - :static-boot-html static-boot-html - :static-boot-js static-boot-js - :static-root *static-root*))) + (apply #'clog-connection:initialize + (append (list #'on-connect :static-root *static-root*) + rest)))) ;;;;;;;;;;;;;;;;;;;;;;; ;; set-on-new-window ;; From 1787996e024ec8e89164574e221803164af2c416 Mon Sep 17 00:00:00 2001 From: Shaka Chen Date: Mon, 8 Aug 2022 12:05:56 +0800 Subject: [PATCH 4/8] basic api for lack middlewares --- source/clog-connection.lisp | 3 +++ source/clog-system.lisp | 1 + 2 files changed, 4 insertions(+) diff --git a/source/clog-connection.lisp b/source/clog-connection.lisp index 6f8cf00..b4013a3 100644 --- a/source/clog-connection.lisp +++ b/source/clog-connection.lisp @@ -378,6 +378,7 @@ the default answer. (Private)" (host "0.0.0.0") (port 8080) (server :hunchentoot) + (lack-middleware-list nil) (extended-routing nil) (long-poll-first nil) (boot-file "/boot.html") @@ -513,6 +514,8 @@ the contents sent to the brower." ;; Handle Websocket connection (lambda (env) (clog-server env)))) + (dolist (lack-middleware lack-middleware-list) + (setf *app* (funcall lack-middleware *app*))) (setf *client-handler* (clack:clackup *app* :server server :address host :port port)) (format t "HTTP listening on : ~A:~A~%" host port) (format t "HTML root : ~A~%" static-root) diff --git a/source/clog-system.lisp b/source/clog-system.lisp index 2d3232c..f6c5ff2 100644 --- a/source/clog-system.lisp +++ b/source/clog-system.lisp @@ -65,6 +65,7 @@ the same as the clog directy this overides the relative paths used in them.") (host "0.0.0.0") (port 8080) (server :hunchentoot) + (lack-middleware-list nil) (extended-routing nil) (long-poll-first nil) (boot-file "/boot.html") From b5fd7e458a971799d83541dab4b4f7a3a45d6a01 Mon Sep 17 00:00:00 2001 From: Shaka Chen Date: Mon, 8 Aug 2022 13:46:56 +0800 Subject: [PATCH 5/8] more functional style and fixed middleware ordering --- source/clog-connection.lisp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/source/clog-connection.lisp b/source/clog-connection.lisp index b4013a3..4a36422 100644 --- a/source/clog-connection.lisp +++ b/source/clog-connection.lisp @@ -514,8 +514,11 @@ the contents sent to the brower." ;; Handle Websocket connection (lambda (env) (clog-server env)))) - (dolist (lack-middleware lack-middleware-list) - (setf *app* (funcall lack-middleware *app*))) + ;; Wrap lack middlewares + (setf *app* (reduce #'funcall + lack-middleware-list + :initial-value *app* + :from-end t)) (setf *client-handler* (clack:clackup *app* :server server :address host :port port)) (format t "HTTP listening on : ~A:~A~%" host port) (format t "HTML root : ~A~%" static-root) From e44d2b247be772f0d3214d04e50fbe7abf60186d Mon Sep 17 00:00:00 2001 From: Shaka Chen Date: Mon, 8 Aug 2022 23:13:35 +0800 Subject: [PATCH 6/8] fixed read-sequence type error when there are more lack middlewares Error message: The value [NUMBER] is not of type CHARACTER when setting an element of (ARRAY CHARACTER). Thus, the updated code checks the type of RAW-BODY to decide what to do. --- source/clog-connection.lisp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/source/clog-connection.lisp b/source/clog-connection.lisp index 4a36422..0e58ffa 100644 --- a/source/clog-connection.lisp +++ b/source/clog-connection.lisp @@ -453,8 +453,17 @@ the contents sent to the brower." (setf post-data id))) (when (equal (getf env :content-type) "application/x-www-form-urlencoded") - (setf post-data (make-string (getf env :content-length))) - (read-sequence post-data (getf env :raw-body))) + (setf post-data (cond ((eq (class-name (class-of (getf env :raw-body))) + 'circular-streams:circular-input-stream) + (let ((array-buffer (make-array (getf env :content-length) + :adjustable t + :fill-pointer t))) + (read-sequence array-buffer (getf env :raw-body)) + (flex:octets-to-string array-buffer))) + (t + (let ((string-buffer (make-string (getf env :content-length)))) + (read-sequence string-buffer (getf env :raw-body)) + string-buffer))))) (cond (long-poll-first (let ((id (random-hex-string))) (setf (gethash id *connection-data*) (make-hash-table* :test #'equal)) From d79785044d846ebed916992ae397c211fb4db108 Mon Sep 17 00:00:00 2001 From: Shaka Chen Date: Tue, 9 Aug 2022 12:37:58 +0800 Subject: [PATCH 7/8] docstring updated for :LACK-MIDDLEWARE-LIST --- source/clog-connection.lisp | 5 +++-- source/clog-system.lisp | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/source/clog-connection.lisp b/source/clog-connection.lisp index 0e58ffa..fb1108d 100644 --- a/source/clog-connection.lisp +++ b/source/clog-connection.lisp @@ -388,8 +388,9 @@ the default answer. (Private)" (static-root #P"./static-files/")) "Initialize CLOG on a socket using HOST and PORT to serve BOOT-FILE as the default route for '/' to establish web-socket connections and static files -located at STATIC-ROOT. The webserver used with CLACK can be chosed with -:SERVER. If LONG-POLLING-FIRST is t, the output is sent as HTML instead of +located at STATIC-ROOT. The webserver used with CLACK can be chosen with +:SERVER and middlewares prepended with :LACK-MIDDLEWARE-LIST. +If LONG-POLLING-FIRST is t, the output is sent as HTML instead of websocket commands until the end of the on-new-window-handler, if LONG-POLLING-FIRST is a number will keep long polling till that number of queries to browser. LONG-POLLING-FIRST is used in webserver applications to diff --git a/source/clog-system.lisp b/source/clog-system.lisp index f6c5ff2..fb61d8f 100644 --- a/source/clog-system.lisp +++ b/source/clog-system.lisp @@ -76,8 +76,9 @@ the same as the clog directy this overides the relative paths used in them.") (asdf:system-source-directory :clog)))) "Inititalize CLOG on a socket using HOST and PORT to serve BOOT-FILE as the default route to establish web-socket connections and static files located at -STATIC-ROOT. The webserver used with CLACK can be chosed with :SERVER. If -EXTENDED-ROUTING is t routes will match even if extend with additional / and +STATIC-ROOT. The webserver used with CLACK can be chosen with :SERVER and +middlewares prepended with :LACK-MIDDLEWARE-LIST. +If EXTENDED-ROUTING is t routes will match even if extend with additional / and additional paths. If LONG-POLLING-FIRST is t then long polling continues until the on-new-window-handler ends, if LONG-POLLING-FIRST is a number continues long polling until that number of queries to browser. LONG-POLLING-FIRST is used in From 839d995a84b743f336bbc964940d09b2ff24077b Mon Sep 17 00:00:00 2001 From: Shaka Chen Date: Tue, 9 Aug 2022 14:23:40 +0800 Subject: [PATCH 8/8] Inserting "NOT supporting LACK.BUILDER DSL" into docstring --- source/clog-connection.lisp | 3 ++- source/clog-system.lisp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/source/clog-connection.lisp b/source/clog-connection.lisp index fb1108d..4882559 100644 --- a/source/clog-connection.lisp +++ b/source/clog-connection.lisp @@ -389,7 +389,8 @@ the default answer. (Private)" "Initialize CLOG on a socket using HOST and PORT to serve BOOT-FILE as the default route for '/' to establish web-socket connections and static files located at STATIC-ROOT. The webserver used with CLACK can be chosen with -:SERVER and middlewares prepended with :LACK-MIDDLEWARE-LIST. +:SERVER and middlewares prepended with :LACK-MIDDLEWARE-LIST, +NOT supporting LACK.BUILDER DSL. If LONG-POLLING-FIRST is t, the output is sent as HTML instead of websocket commands until the end of the on-new-window-handler, if LONG-POLLING-FIRST is a number will keep long polling till that number of diff --git a/source/clog-system.lisp b/source/clog-system.lisp index fb61d8f..ac1296e 100644 --- a/source/clog-system.lisp +++ b/source/clog-system.lisp @@ -77,7 +77,7 @@ the same as the clog directy this overides the relative paths used in them.") "Inititalize CLOG on a socket using HOST and PORT to serve BOOT-FILE as the default route to establish web-socket connections and static files located at STATIC-ROOT. The webserver used with CLACK can be chosen with :SERVER and -middlewares prepended with :LACK-MIDDLEWARE-LIST. +middlewares prepended with :LACK-MIDDLEWARE-LIST, NOT supporting LACK.BUILDER DSL. If EXTENDED-ROUTING is t routes will match even if extend with additional / and additional paths. If LONG-POLLING-FIRST is t then long polling continues until the on-new-window-handler ends, if LONG-POLLING-FIRST is a number continues long