;;;; 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.3 2003/07/05 00:51:04 kevin Exp $
;;;; *************************************************************************
(in-package #:modlisp)
(make-socket-server (next-server-name) function port
:format :text :wait nil)
(error (e)
- (format t "Error ~A" e)
+ (format t "~&Error while trying to start modlisp server~& ~A" e)
(decf *listener-count*)
nil)
(:no-error (process socket)
(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*
+ (format t "~&; killing modlisp server process ~A~%" *listener-process*)
+ (handler-case
+ (progn
+ #+sbcl (sb-thread:destroy-thread *listener-process*)
+ #+cmucl (mp:destroy-process *listener-process*)
+ #+allegro (mp:process-kill *listener-process*)
+ #+allegro (mp:process-allow-schedule)
+ #+lispworks (mp:process-kill *listener-process*))
+ (error (e)
+ (format t "~&Error while trying to kill modlisp server~& ~A" e))
+ (:no-error (res)
+ (declare (ignore res))
+ (setq *listener-process* nil))))
+ (when *listener-socket*
(ignore-errors (close *listener-socket*))
(setq *listener-socket* nil)))
- )
+ ) ;; closure
(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))
+(let ((*number-server-requests* 0)
+ (*number-worker-requests* 0)
+ (*close-apache-socket* t))
+
+ (defun apache-command-issuer (*apache-socket* processor-fun)
+ "generates commands from apache, issues commands to processor-fun"
(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*))))
+ (progn
+ (setq *number-worker-requests* 0)
+ (do ((command (read-apache-command) (read-apache-command)))
+ ((null command) 'done)
+ (funcall processor-fun command)
+ (force-output *apache-socket*)
+ (incf *number-worker-requests*)
+ (incf *number-server-requests*)
+ (when *close-apache-socket*
+ (return))))
+ (close *apache-socket*)))
+
+ (defun get-number-worker-requests ()
+ *number-worker-requests*)
+
+ (defun get-number-server-requests ()
+ *number-server-requests*)
+
+ (defun set-close-apache-socket (close?)
+ (setq *close-apache-socket* close?))
+
+ ) ;; closure
-(defun get-apache-command ()
+(defun read-apache-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)))
+ (let* ((header (read-apache-header))
(content-length (cdr (assoc "content-length" header :test #'equal)))
(content (when content-length
(make-string
(push (cons "posted-content" content) header))
header)))
+(defun read-apache-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)))
+
(defun write-header-line (key value)
(write-string key *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>")