;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: modlisp -*- ;;;; ************************************************************************* ;;;; FILE IDENTIFICATION ;;;; ;;;; Name: base.lisp ;;;; Purpose: Utility functions for modlisp package ;;;; Programmer: Kevin M. Rosenberg ;;;; Date Started: Dec 2002 ;;;; ;;;; $Id: base.lisp,v 1.1 2003/07/04 19:52:32 kevin Exp $ ;;;; ************************************************************************* (in-package #:modlisp) (let ((*listener-socket* nil) (*listener-process* nil)) (defun modlisp-start (&key (port +default-apache-port+) (function 'sample-process-apache-command)) (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 (proc socket) (setq *listener-socket* socket) (setq *listener-proc* proc) proc))) (defun modlisp-stop () (when *listener-proc* (format t "~&; killing ~d~%" *listener-proc*) #+sbcl (sb-unix:unix-kill *listener-proc* :sigalrm) #+allegro (mp:process-kill *listener-proc*) #+allegro (mp:process-allow-schedule) ) (setq *listener-proc* 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* &optional (processor-fun 'demo-apache-command-processor)) "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 () (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))) (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 " " s) (format s "" *apache-nb-use-socket*) (loop for (key . value) in command do (format s "" key value)) (write-string "
ACL 6.2 + mod_lisp 2.0 + apache + Linux
KeyValue
apache-nb-use-socket~a
~a~a
" s))) (defun fixed-html () "

mod_lisp 2.0

This is a constant html string sent by mod_lisp

")