New API for extending request processing without interferring with
[hunchentoot.git] / specials.lisp
blobb3f7b3decfd2af22b120c7ae83bc87db743988dc
1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: HUNCHENTOOT; Base: 10 -*-
2 ;;; $Header: /usr/local/cvsrep/hunchentoot/specials.lisp,v 1.33 2008/04/08 14:39:18 edi Exp $
4 ;;; Copyright (c) 2004-2009, Dr. Edmund Weitz. All rights reserved.
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
13 ;;; * Redistributions in binary form must reproduce the above
14 ;;; copyright notice, this list of conditions and the following
15 ;;; disclaimer in the documentation and/or other materials
16 ;;; provided with the distribution.
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 (in-package :hunchentoot)
32 (defmacro defconstant (name value &optional doc)
33 "Make sure VALUE is evaluated only once \(to appease SBCL)."
34 `(cl:defconstant ,name (if (boundp ',name) (symbol-value ',name) ,value)
35 ,@(when doc (list doc))))
37 (eval-when (:compile-toplevel :execute :load-toplevel)
38 (defmacro defvar-unbound (name &optional (doc-string ""))
39 "Convenience macro to declare unbound special variables with a
40 documentation string."
41 `(progn
42 (defvar ,name)
43 (setf (documentation ',name 'variable) ,doc-string)))
45 (defvar *http-reason-phrase-map* (make-hash-table)
46 "Used to map numerical return codes to reason phrases.")
48 (defmacro def-http-return-code (name value reason-phrase)
49 "Shortcut to define constants for return codes. NAME is a
50 Lisp symbol, VALUE is the numerical value of the return code, and
51 REASON-PHRASE is the phrase \(a string) to be shown in the
52 server's status line."
53 `(eval-when (:compile-toplevel :execute :load-toplevel)
54 (defconstant ,name ,value ,(format nil "HTTP return code \(~A) for '~A'."
55 value reason-phrase))
56 (setf (gethash ,value *http-reason-phrase-map*) ,reason-phrase))))
58 (defconstant +crlf+
59 (make-array 2 :element-type '(unsigned-byte 8)
60 :initial-contents (mapcar 'char-code '(#\Return #\Linefeed)))
61 "A 2-element array consisting of the character codes for a CRLF
62 sequence.")
64 (def-http-return-code +http-continue+ 100 "Continue")
65 (def-http-return-code +http-switching-protocols+ 101 "Switching Protocols")
66 (def-http-return-code +http-ok+ 200 "OK")
67 (def-http-return-code +http-created+ 201 "Created")
68 (def-http-return-code +http-accepted+ 202 "Accepted")
69 (def-http-return-code +http-non-authoritative-information+ 203 "Non-Authoritative Information")
70 (def-http-return-code +http-no-content+ 204 "No Content")
71 (def-http-return-code +http-reset-content+ 205 "Reset Content")
72 (def-http-return-code +http-partial-content+ 206 "Partial Content")
73 (def-http-return-code +http-multi-status+ 207 "Multi-Status")
74 (def-http-return-code +http-multiple-choices+ 300 "Multiple Choices")
75 (def-http-return-code +http-moved-permanently+ 301 "Moved Permanently")
76 (def-http-return-code +http-moved-temporarily+ 302 "Moved Temporarily")
77 (def-http-return-code +http-see-other+ 303 "See Other")
78 (def-http-return-code +http-not-modified+ 304 "Not Modified")
79 (def-http-return-code +http-use-proxy+ 305 "Use Proxy")
80 (def-http-return-code +http-temporary-redirect+ 307 "Temporary Redirect")
81 (def-http-return-code +http-bad-request+ 400 "Bad Request")
82 (def-http-return-code +http-authorization-required+ 401 "Authorization Required")
83 (def-http-return-code +http-payment-required+ 402 "Payment Required")
84 (def-http-return-code +http-forbidden+ 403 "Forbidden")
85 (def-http-return-code +http-not-found+ 404 "Not Found")
86 (def-http-return-code +http-method-not-allowed+ 405 "Method Not Allowed")
87 (def-http-return-code +http-not-acceptable+ 406 "Not Acceptable")
88 (def-http-return-code +http-proxy-authentication-required+ 407 "Proxy Authentication Required")
89 (def-http-return-code +http-request-time-out+ 408 "Request Time-out")
90 (def-http-return-code +http-conflict+ 409 "Conflict")
91 (def-http-return-code +http-gone+ 410 "Gone")
92 (def-http-return-code +http-length-required+ 411 "Length Required")
93 (def-http-return-code +http-precondition-failed+ 412 "Precondition Failed")
94 (def-http-return-code +http-request-entity-too-large+ 413 "Request Entity Too Large")
95 (def-http-return-code +http-request-uri-too-large+ 414 "Request-URI Too Large")
96 (def-http-return-code +http-unsupported-media-type+ 415 "Unsupported Media Type")
97 (def-http-return-code +http-requested-range-not-satisfiable+ 416 "Requested range not satisfiable")
98 (def-http-return-code +http-expectation-failed+ 417 "Expectation Failed")
99 (def-http-return-code +http-failed-dependency+ 424 "Failed Dependency")
100 (def-http-return-code +http-internal-server-error+ 500 "Internal Server Error")
101 (def-http-return-code +http-not-implemented+ 501 "Not Implemented")
102 (def-http-return-code +http-bad-gateway+ 502 "Bad Gateway")
103 (def-http-return-code +http-service-unavailable+ 503 "Service Unavailable")
104 (def-http-return-code +http-gateway-time-out+ 504 "Gateway Time-out")
105 (def-http-return-code +http-version-not-supported+ 505 "Version not supported")
107 (defvar *approved-return-codes* '(#.+http-ok+ #.+http-no-content+
108 #.+http-multi-status+
109 #.+http-not-modified+)
110 "A list of return codes the server should not treat as an error -
111 see *HANDLE-HTTP-ERRORS-P*.")
113 (defconstant +day-names+
114 #("Mon" "Tue" "Wed" "Thu" "Fri" "Sat" "Sun")
115 "The three-character names of the seven days of the week - needed
116 for cookie date format.")
118 (defconstant +month-names+
119 #("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
120 "The three-character names of the twelve months - needed for cookie
121 date format.")
123 (defvar *rewrite-for-session-urls* t
124 "Whether HTML pages should possibly be rewritten for cookie-less
125 session-management.")
127 (defvar *content-types-for-url-rewrite*
128 '("text/html" "application/xhtml+xml")
129 "The content types for which url-rewriting is OK. See
130 *REWRITE-FOR-SESSION-URLS*.")
132 (defparameter *the-random-state* (make-random-state t)
133 "A fresh random state.")
135 (defvar-unbound *session-secret*
136 "A random ASCII string that's used to encode the public session
137 data. This variable is initially unbound and will be set \(using
138 RESET-SESSION-SECRET) the first time a session is created, if
139 necessary. You can prevent this from happening if you set the value
140 yourself before starting acceptors.")
142 (defvar-unbound *hunchentoot-stream*
143 "The stream representing the socket Hunchentoot is listening on.")
145 (defvar *close-hunchentoot-stream* nil
146 "Will be set to T if the Hunchentoot socket stream has to be
147 closed at the end of the request.")
149 (defvar *headers-sent* nil
150 "Used internally to check whether the reply headers have
151 already been sent for this request.")
153 (defvar *file-upload-hook* nil
154 "If this is not NIL, it should be a unary function which will
155 be called with a pathname for each file which is uploaded to
156 Hunchentoot. The pathname denotes the temporary file to which
157 the uploaded file is written. The hook is called directly before
158 the file is created.")
160 (defvar *session-db* nil
161 "The default \(global) session database.")
163 (defvar *session-max-time* #.(* 30 60)
164 "The default time \(in seconds) after which a session times out.")
166 (defvar *session-gc-frequency* 50
167 "A session GC \(see function SESSION-GC) will happen every
168 *SESSION-GC-FREQUENCY* requests \(counting only requests which create
169 a new session) if this variable is not NIL. See SESSION-CREATED.")
171 (defvar *use-user-agent-for-sessions* t
172 "Whether the 'User-Agent' header should be encoded into the session
173 string. If this value is true, a session will cease to be accessible
174 if the client sends a different 'User-Agent' header.")
176 (defvar *use-remote-addr-for-sessions* nil
177 "Whether the client's remote IP \(as returned by REAL-REMOTE-ADDR)
178 should be encoded into the session string. If this value is true, a
179 session will cease to be accessible if the client's remote IP changes.
181 This might for example be an issue if the client uses a proxy server
182 which doesn't send correct 'X_FORWARDED_FOR' headers.")
184 (defvar *default-content-type* "text/html; charset=iso-8859-1"
185 "The default content-type header which is returned to the client.")
187 (defvar *methods-for-post-parameters* '(:post)
188 "A list of the request method types \(as keywords) for which
189 Hunchentoot will try to compute POST-PARAMETERS.")
191 (defvar *header-stream* nil
192 "If this variable is not NIL, it should be bound to a stream to
193 which incoming and outgoing headers will be written for debugging
194 purposes.")
196 (defvar *show-lisp-errors-p* nil
197 "Whether Lisp errors in request handlers should be shown in HTML output.")
199 (defvar *log-lisp-errors-p* t
200 "Whether Lisp errors in request handlers should be logged.")
202 (defvar *log-lisp-warnings-p* t
203 "Whether Lisp warnings in request handlers should be logged.")
205 (defvar *lisp-errors-log-level* :error
206 "Log level for Lisp errors. Should be one of :ERROR \(the default),
207 :WARNING, or :INFO.")
209 (defvar *lisp-warnings-log-level* :warning
210 "Log level for Lisp warnings. Should be one of :ERROR, :WARNING
211 \(the default), or :INFO.")
213 (defvar *message-log-pathname* nil
214 "A designator for the pathname of the message log file used by the
215 LOG-MESSAGE-TO-FILE function. The initial value is NIL which means
216 that nothing will be logged.")
218 (defvar *access-log-pathname* nil
219 "A designator for the pathname of the access log file used by the
220 LOG-ACCESS-TO-FILE function. The initial value is NIL which means
221 that nothing will be logged.")
223 (defvar *message-log-lock* (make-lock "global-message-log-lock")
224 "A global lock to prevent concurrent access to the log file
225 used by LOG-MESSAGE-TO-FILE function.")
227 (defvar *access-log-lock* (make-lock "global-access-log-lock")
228 "A global lock to prevent concurrent access to the log file
229 used by LOG-ACCESS-TO-FILE function.")
231 (defvar-unbound *acceptor*
232 "The current ACCEPTOR object while in the context of a request.")
234 (defvar-unbound *request*
235 "The current REQUEST object while in the context of a request.")
237 (defvar *within-request-p* nil
238 "True while in the context of a request (while *request* is bound),
239 otherwise nil. Outside callers should use exported function
240 within-request-p to test this.")
242 (defvar-unbound *reply*
243 "The current REPLY object while in the context of a request.")
245 (defvar-unbound *session*
246 "The current session while in the context of a request, or NIL.")
248 (defvar *break-even-while-reading-request-type-p* nil
249 "If this variable is set to true, Hunchentoot will not bind
250 *BREAK-ON-SIGNALS* to NIL while reading the next request type from an
251 incoming connection. By default, Hunchentoot will not enter the
252 debugger if an error occurs during the reading of the request type, as
253 this will happen regularily and legitimately. \(The incoming
254 connection times out or the client closes the connection without
255 initiating another request, which is permissible.)")
257 (defconstant +implementation-link+
258 #+:cmu "http://www.cons.org/cmucl/"
259 #+:sbcl "http://www.sbcl.org/"
260 #+:allegro "http://www.franz.com/products/allegrocl/"
261 #+:lispworks "http://www.lispworks.com/"
262 #+:openmcl "http://openmcl.clozure.com/"
263 "A link to the website of the underlying Lisp implementation.")
265 (defvar *dispatch-table* (list 'dispatch-easy-handlers 'default-dispatcher)
266 "A global list of dispatch functions.")
268 (defvar *default-handler* 'default-handler
269 "The name of the function which is always returned by
270 DEFAULT-DISPATCHER.")
272 (defvar *easy-handler-alist* nil
273 "An alist of \(URI acceptor-names function) lists defined by
274 DEFINE-EASY-HANDLER.")
276 (defvar *http-error-handler* nil
277 "Contains NIL \(the default) or a function of one argument which is
278 called if the content handler has set a return code which is not in
279 *APPROVED-RETURN-CODES* and *HANDLE-HTTP-ERRORS* is true.")
281 (defvar *handle-http-errors-p* t
282 "A generalized boolean that determines whether return codes which
283 are not in *APPROVED-RETURN-CODES* are treated specially. When its
284 value is true \(the default), either a default body for the return
285 code or the result of calling *HTTP-ERROR-HANDLER* is used. When the
286 value is NIL, no special action is taken and you are expected to
287 supply your own response body to describe the error.")
289 (defvar *session-removal-hook* (constantly nil)
290 "A function of one argument \(a session object) which is called
291 whenever a session is garbage-collected.")
293 (defvar *tmp-directory*
294 #+(or :win32 :mswindows) "c:\\hunchentoot-temp\\"
295 #-(or :win32 :mswindows) "/tmp/hunchentoot/"
296 "Directory for temporary files created by MAKE-TMP-FILE-NAME.")
298 (defvar *tmp-files* nil
299 "A list of temporary files created while a request was handled.")
301 (defconstant +latin-1+
302 (make-external-format :latin1 :eol-style :lf)
303 "A FLEXI-STREAMS external format used for `faithful' input and
304 output of binary data.")
306 (defconstant +utf-8+
307 (make-external-format :utf8 :eol-style :lf)
308 "A FLEXI-STREAMS external format used internally for logging and to
309 encode cookie values.")
311 (defvar *hunchentoot-default-external-format* +latin-1+
312 "The external format used to compute the REQUEST object.")
314 (defconstant +buffer-length+ 8192
315 "Length of buffers used for internal purposes.")
317 (defvar *default-connection-timeout* 20
318 "The default connection timeout used when an acceptor is reading
319 from and writing to a socket stream.")
321 (define-symbol-macro *supports-threads-p*
322 #+:lispworks t
323 #-:lispworks bt:*supports-threads-p*)
325 (defvar *global-session-db-lock*
326 (load-time-value (and *supports-threads-p* (make-lock "global-session-db-lock")))
327 "A global lock to prevent two threads from modifying *session-db* at
328 the same time \(or NIL for Lisps which don't have threads).")
330 #-:lispworks
331 (defconstant +new-connection-wait-time+ 2
332 "Time in seconds to wait for a new connection to arrive before
333 performing a cleanup run.")
335 (pushnew :hunchentoot *features*)
337 ;; stuff for Nikodemus Siivola's HYPERDOC
338 ;; see <http://common-lisp.net/project/hyperdoc/>
339 ;; and <http://www.cliki.net/hyperdoc>
341 (defvar *hyperdoc-base-uri* "http://weitz.de/hunchentoot/")
343 (let ((exported-symbols-alist
344 (loop for symbol being the external-symbols of :hunchentoot
345 collect (cons symbol (concatenate 'string "#" (string-downcase symbol))))))
346 (defun hyperdoc-lookup (symbol type)
347 (declare (ignore type))
348 (cdr (assoc symbol exported-symbols-alist :test #'eq))))