X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=base.lisp;h=59c6f05fafc47b2a09ecb0c6cd2ffccf3765b8ea;hb=ea50229269c428ed82edcda11fa056786469cac7;hp=15ff3e12a450b4b42d3b2cfd136ad3292735b25e;hpb=1bb49e27c591c846849c80b6e543a8b207f9dad4;p=cl-modlisp.git diff --git a/base.lisp b/base.lisp index 15ff3e1..59c6f05 100644 --- a/base.lisp +++ b/base.lisp @@ -7,133 +7,86 @@ ;;;; Programmer: Kevin M. Rosenberg ;;;; Date Started: Dec 2002 ;;;; -;;;; $Id: base.lisp,v 1.2 2003/07/04 22:41:06 kevin Exp $ +;;;; $Id: base.lisp,v 1.11 2003/07/11 02:38:00 kevin Exp $ ;;;; ************************************************************************* (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 modlisp-start (&key (port +default-modlisp-port+) + (processor 'demo-modlisp-command-processor) + (processor-args nil) + (catch-errors t) + timeout + number-fixed-workers) + (let ((listener (make-instance 'listener :port port + :base-name "modlisp" + :function 'modlisp-command-issuer + :function-args (cons processor processor-args) + :format :text + :wait nil + :catch-errors catch-errors + :timeout timeout + :number-fixed-workers number-fixed-workers))) + (init/listener listener :start))) + + +(defun modlisp-stop (listener) + (init/listener listener :stop)) + +(defun modlisp-stop-all () + (stop-all/listener)) + +;; Internal functions + +(defun modlisp-command-issuer (*modlisp-socket* processor &rest args) + "generates commands from modlisp, issues commands to processor-fun" + (unwind-protect + (progn + (let ((*number-worker-requests* 0) + (*close-modlisp-socket* t)) + (do ((command (read-modlisp-command) (read-modlisp-command))) + ((null command)) + (apply processor command args) + (finish-output *modlisp-socket*) + (incf *number-worker-requests*) + (incf *number-server-requests*) + (when *close-modlisp-socket* + (return))))) + (close-active-socket *modlisp-socket*))) - ) - -(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 header-value (header key) + "Returns the value of a modlisp header" + (cdr (assoc key header :test #'string=))) -(defun get-apache-command () +(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))))) + (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 *apache-socket*) + (read-sequence content *modlisp-socket*) (push (cons "posted-content" content) header)) header))) -(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=))) - +(defun read-modlisp-line () + (kmrcl:string-right-trim-one-char + #\return + (read-line *modlisp-socket* nil nil))) -;;; Default (demo) processor +(defun read-modlisp-header () + (loop for key = (read-modlisp-line) + while (and key + (string-not-equal key "end") + (> (length key) 1)) + for value = (read-modlisp-line) + collect (cons key value))) -(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 write-header-line (key value) + (write-string key *modlisp-socket*) + (write-char #\NewLine *modlisp-socket*) + (write-string value *modlisp-socket*) + (write-char #\NewLine *modlisp-socket*)) -(defun debug-table (command) - (with-output-to-string (s) - (write-string " - - - -mod_lisp debug -" s) - (format s "" *apache-nb-use-socket*) - (loop for (key . value) in command do - (format s "" key value)) - (write-string "
KeyValue
apache-nb-use-socket~a
~a~a
" s))) -(defun fixed-html () - " -

mod_lisp

-

This is a constant html string sent by mod_lisp

")