1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: modlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: Utility functions for modlisp package
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Dec 2002
10 ;;;; $Id: base.lisp,v 1.11 2003/07/11 02:38:00 kevin Exp $
11 ;;;; *************************************************************************
13 (in-package #:modlisp)
15 (defun modlisp-start (&key (port +default-modlisp-port+)
16 (processor 'demo-modlisp-command-processor)
21 (let ((listener (make-instance 'listener :port port
23 :function 'modlisp-command-issuer
24 :function-args (cons processor processor-args)
27 :catch-errors catch-errors
29 :number-fixed-workers number-fixed-workers)))
30 (init/listener listener :start)))
33 (defun modlisp-stop (listener)
34 (init/listener listener :stop))
36 (defun modlisp-stop-all ()
41 (defun modlisp-command-issuer (*modlisp-socket* processor &rest args)
42 "generates commands from modlisp, issues commands to processor-fun"
45 (let ((*number-worker-requests* 0)
46 (*close-modlisp-socket* t))
47 (do ((command (read-modlisp-command) (read-modlisp-command)))
49 (apply processor command args)
50 (finish-output *modlisp-socket*)
51 (incf *number-worker-requests*)
52 (incf *number-server-requests*)
53 (when *close-modlisp-socket*
55 (close-active-socket *modlisp-socket*)))
57 (defun header-value (header key)
58 "Returns the value of a modlisp header"
59 (cdr (assoc key header :test #'string=)))
61 (defun read-modlisp-command ()
63 (let* ((header (read-modlisp-header))
64 (content-length (header-value header "content-length"))
65 (content (when content-length
67 (parse-integer content-length :junk-allowed t)))))
69 (read-sequence content *modlisp-socket*)
70 (push (cons "posted-content" content) header))
73 (defun read-modlisp-line ()
74 (kmrcl:string-right-trim-one-char
76 (read-line *modlisp-socket* nil nil)))
78 (defun read-modlisp-header ()
79 (loop for key = (read-modlisp-line)
81 (string-not-equal key "end")
83 for value = (read-modlisp-line)
84 collect (cons key value)))
86 (defun write-header-line (key value)
87 (write-string key *modlisp-socket*)
88 (write-char #\NewLine *modlisp-socket*)
89 (write-string value *modlisp-socket*)
90 (write-char #\NewLine *modlisp-socket*))