Update domain name to kpe.io
[cl-modlisp.git] / base.lisp
index 15ff3e12a450b4b42d3b2cfd136ad3292735b25e..ef54314d12761284a40b2417280c577d008601d5 100644 (file)
--- a/base.lisp
+++ b/base.lisp
 ;;;; Programmer:    Kevin M. Rosenberg
 ;;;; Date Started:  Dec 2002
 ;;;;
-;;;; $Id: base.lisp,v 1.2 2003/07/04 22:41:06 kevin Exp $
+;;;; $Id$
 ;;;; *************************************************************************
 
 (in-package #:modlisp)
 
-
-(let ((*listener-socket* nil)
-      (*listener-process* nil))
-
-  (defun modlisp-start (&key (port +default-apache-port+)
-                            (function 'demo-apache-command-processor))
-    (handler-case
-       (make-socket-server (next-server-name) function port
-                           :format :text :wait nil)
-      (error (e)
-       (format t "Error ~A" e)
-       (decf *listener-count*)
-       nil)
-      (:no-error (process socket)
-       (setq *listener-socket* socket)
-       (setq *listener-process* process)
-       process)))
-  
-  (defun modlisp-stop ()
-    (when *listener-process*
-      (format t "~&; killing process ~d~%" *listener-process*)
-      #+sbcl (sb-thread:destory-thread *listener-process*)
-      #+allegro (mp:process-kill *listener-process*)
-      #+allegro (mp:process-allow-schedule)
-      #+lispworks (mp:process-kill *listener-process*)
-      #+cmucl (mp:destroy process *listener-process*)
-      (setq *listener-process* nil))
-    (when *listener-socket* 
-      (ignore-errors (close *listener-socket*))
-      (setq *listener-socket* nil)))
-  
- )
-    
-(defun next-server-name ()
-  (format nil "modlisp-socket-server-~d" (incf *listener-count*))) 
-
-(defun next-worker-name ()
-  (format nil "modlisp-worker-~d" (incf *worker-count*)))
-
-
-(defun apache-command-issuer (*apache-socket* processor-fun)
-  "generates commands from apache, issues commands to processor-fun"
-  (let ((*close-apache-socket* t))
-    (unwind-protect
-       (loop for *apache-nb-use-socket* from 0
-           for command = (get-apache-command)
-           while command 
-           do (funcall processor-fun command)
-             (force-output *apache-socket*)
-           until *close-apache-socket*)
-      (close *apache-socket*))))
-
-(defun get-apache-command ()
+(defun modlisp-start (&key (port +default-modlisp-port+)
+                           (processor 'demo-modlisp-command-processor)
+                           (processor-args nil)
+                           (catch-errors t)
+                           timeout
+                           number-fixed-workers
+                           remote-host-checker)
+  (let* ((server (make-instance 'ml-server
+                   :processor processor
+                   :processor-args processor-args
+                   :port port))
+         (listener (make-instance 'listener :port port
+                                  :base-name "modlisp"
+                                  :function 'modlisp-command-issuer
+                                  :function-args (list server)
+                                  :format :text
+                                  :wait nil
+                                  :catch-errors catch-errors
+                                  :timeout timeout
+                                  :number-fixed-workers number-fixed-workers
+                                  :remote-host-checker remote-host-checker)))
+    (setf (listener server) listener)
+    (init/listener listener :start)
+    (setf *ml-server* server)
+    server))
+
+
+(defun modlisp-stop (server)
+  (init/listener (listener server) :stop)
+  (setf (listener server) nil)
+  server)
+
+(defun modlisp-stop-all ()
+  (stop-all/listener))
+
+;; Internal functions
+
+(defun modlisp-command-issuer (*modlisp-socket* server)
+  "generates commands from modlisp, issues commands to processor-fun"
+  (unwind-protect
+       (progn
+         (let ((*number-worker-requests* 0)
+               (*close-modlisp-socket* t)
+               (*ml-server* server))
+           (do ((command (read-modlisp-command) (read-modlisp-command)))
+               ((null command))
+             (apply (processor server) command (processor-args server))
+             (finish-output *modlisp-socket*)
+             (incf *number-worker-requests*)
+             (incf *number-server-requests*)
+             (when *close-modlisp-socket*
+               (return)))))
+    (close-active-socket *modlisp-socket*)))
+
+(defun header-value (header key)
+  "Returns the value of a modlisp header"
+  (cdr (assoc key header :test #'eq)))
+
+(defun read-modlisp-command ()
   (ignore-errors
-   (let* ((header (loop for key = (read-line *apache-socket* nil nil)
-                       while (and key
-                                  (string-not-equal key "end")
-                                  (> (length key) 1))
-                     for value = (read-line *apache-socket* nil nil)
-                     collect (cons key value)))
-         (content-length (cdr (assoc "content-length" header :test #'equal)))
-         (content (when content-length 
-                    (make-string
-                     (parse-integer content-length :junk-allowed t)))))
-     (when content
-       (read-sequence content *apache-socket*)
-       (push (cons "posted-content" content) header))
-     header)))
+    (let* ((header (read-modlisp-header))
+           (content-length (header-value header :content-length))
+           (content (when content-length
+                      (make-string
+                       (parse-integer content-length :junk-allowed t)))))
+          (when content
+            (read-sequence content *modlisp-socket*)
+            (push (cons :posted-content content) header))
+          header)))
+
+
+(defun read-modlisp-line ()
+  (kmrcl:string-right-trim-one-char
+   #\return
+   (read-line *modlisp-socket* nil nil)))
+
+
+(defun read-modlisp-header ()
+  (loop for key = (read-modlisp-line)
+      while (and key (string-not-equal key "end"))
+      for value = (read-modlisp-line)
+      collect (cons (ensure-keyword key) value)))
 
 (defun write-header-line (key value)
-  (write-string key *apache-socket*)
-  (write-char #\NewLine *apache-socket*)
-  (write-string value *apache-socket*)
-  (write-char #\NewLine *apache-socket*))
-
-(defun header-value (command key)
-  (cdr (assoc key command :test #'string=)))
-
-
-;;; Default (demo) processor
-
-(defun demo-apache-command-processor (command)
-  "Sample function to process an apache command"
-  (if (equal (header-value command "url") "/asp/fixed")
-      (fixed-request)
-      (debug-request command)))
-
-(defun fixed-request ()
-  (let ((html (fixed-html)))
-    (write-header-line "Status" "200 OK")
-    (write-header-line "Content-Type" "text/html")
-    (write-header-line "Content-Length" (format nil "~d" (length html)))
-    (write-header-line "Keep-Socket" "1")
-    (write-string "end" *apache-socket*)
-    (write-char #\NewLine *apache-socket*)
-    (write-string html *apache-socket*)
-    (setq *close-apache-socket* nil))  )
-
-(defun debug-request (command)
-  (let ((html (debug-table command)))
-    (write-header-line "Status" "200 OK")
-    (write-header-line "Content-Type" "text/html")
-    (write-header-line "Keep-Socket" "0")
-    (write-string "end" *apache-socket*)
-    (write-char #\NewLine *apache-socket*)
-    (write-string html *apache-socket*)
-    (setq *close-apache-socket* t))  )
-
-(defun debug-table (command)
-  (with-output-to-string (s)
-   (write-string "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">
-<html><head></head>
-<body>
-<table><tbody>
-<tr><t colspan=\"2\">mod_lisp debug</th></tr>
-<tr><th>Key</th><th>Value</th></tr>" s)
-   (format s "<TR bgcolor=\"#F0F0c0\"><TD>apache-nb-use-socket</TD><TD>~a</TD></TR>"  *apache-nb-use-socket*)
-   (loop for (key . value) in command do
-        (format s "<tr><td>~a</td><td>~a</td></tr>" key value))
-   (write-string "</tbody></table></body></html>" s)))
-
-(defun fixed-html ()
-  "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">
-<html><head></head><body><h1>mod_lisp</h1>
-<p>This is a constant html string sent by mod_lisp</p></body></html>")
+  (write-string (string key) *modlisp-socket*)
+  (write-char #\NewLine *modlisp-socket*)
+  (write-string value *modlisp-socket*)
+  (write-char #\NewLine *modlisp-socket*))
+
+