Merge branch 'master' of github.com:edicl/hunchentoot
[hunchentoot.git] / specials.lisp
blobe3639d20bcf2c2e48e8632d8750fcaecaf33f723
1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: HUNCHENTOOT; Base: 10 -*-
3 ;;; Copyright (c) 2004-2010, Dr. Edmund Weitz. All rights reserved.
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
12 ;;; * Redistributions in binary form must reproduce the above
13 ;;; copyright notice, this list of conditions and the following
14 ;;; disclaimer in the documentation and/or other materials
15 ;;; provided with the distribution.
17 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
18 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 (in-package :hunchentoot)
31 (defmacro defconstant (name value &optional doc)
32 "Make sure VALUE is evaluated only once \(to appease SBCL)."
33 `(cl:defconstant ,name (if (boundp ',name) (symbol-value ',name) ,value)
34 ,@(when doc (list doc))))
36 (eval-when (:compile-toplevel :execute :load-toplevel)
37 (defmacro defvar-unbound (name &optional (doc-string ""))
38 "Convenience macro to declare unbound special variables with a
39 documentation string."
40 `(progn
41 (defvar ,name)
42 (setf (documentation ',name 'variable) ,doc-string)))
44 (defvar *http-reason-phrase-map* (make-hash-table)
45 "Used to map numerical return codes to reason phrases.")
47 (defmacro def-http-return-code (name value reason-phrase)
48 "Shortcut to define constants for return codes. NAME is a
49 Lisp symbol, VALUE is the numerical value of the return code, and
50 REASON-PHRASE is the phrase \(a string) to be shown in the
51 server's status line."
52 `(eval-when (:compile-toplevel :execute :load-toplevel)
53 (defconstant ,name ,value ,(format nil "HTTP return code \(~A) for '~A'."
54 value reason-phrase))
55 (setf (gethash ,value *http-reason-phrase-map*) ,reason-phrase))))
57 (defconstant +crlf+
58 (make-array 2 :element-type '(unsigned-byte 8)
59 :initial-contents (mapcar 'char-code '(#\Return #\Linefeed)))
60 "A 2-element array consisting of the character codes for a CRLF
61 sequence.")
63 (def-http-return-code +http-continue+ 100 "Continue")
64 (def-http-return-code +http-switching-protocols+ 101 "Switching Protocols")
65 (def-http-return-code +http-ok+ 200 "OK")
66 (def-http-return-code +http-created+ 201 "Created")
67 (def-http-return-code +http-accepted+ 202 "Accepted")
68 (def-http-return-code +http-non-authoritative-information+ 203 "Non-Authoritative Information")
69 (def-http-return-code +http-no-content+ 204 "No Content")
70 (def-http-return-code +http-reset-content+ 205 "Reset Content")
71 (def-http-return-code +http-partial-content+ 206 "Partial Content")
72 (def-http-return-code +http-multi-status+ 207 "Multi-Status")
73 (def-http-return-code +http-multiple-choices+ 300 "Multiple Choices")
74 (def-http-return-code +http-moved-permanently+ 301 "Moved Permanently")
75 (def-http-return-code +http-moved-temporarily+ 302 "Moved Temporarily")
76 (def-http-return-code +http-see-other+ 303 "See Other")
77 (def-http-return-code +http-not-modified+ 304 "Not Modified")
78 (def-http-return-code +http-use-proxy+ 305 "Use Proxy")
79 (def-http-return-code +http-temporary-redirect+ 307 "Temporary Redirect")
80 (def-http-return-code +http-bad-request+ 400 "Bad Request")
81 (def-http-return-code +http-authorization-required+ 401 "Authorization Required")
82 (def-http-return-code +http-payment-required+ 402 "Payment Required")
83 (def-http-return-code +http-forbidden+ 403 "Forbidden")
84 (def-http-return-code +http-not-found+ 404 "Not Found")
85 (def-http-return-code +http-method-not-allowed+ 405 "Method Not Allowed")
86 (def-http-return-code +http-not-acceptable+ 406 "Not Acceptable")
87 (def-http-return-code +http-proxy-authentication-required+ 407 "Proxy Authentication Required")
88 (def-http-return-code +http-request-time-out+ 408 "Request Time-out")
89 (def-http-return-code +http-conflict+ 409 "Conflict")
90 (def-http-return-code +http-gone+ 410 "Gone")
91 (def-http-return-code +http-length-required+ 411 "Length Required")
92 (def-http-return-code +http-precondition-failed+ 412 "Precondition Failed")
93 (def-http-return-code +http-request-entity-too-large+ 413 "Request Entity Too Large")
94 (def-http-return-code +http-request-uri-too-large+ 414 "Request-URI Too Large")
95 (def-http-return-code +http-unsupported-media-type+ 415 "Unsupported Media Type")
96 (def-http-return-code +http-requested-range-not-satisfiable+ 416 "Requested range not satisfiable")
97 (def-http-return-code +http-expectation-failed+ 417 "Expectation Failed")
98 (def-http-return-code +http-failed-dependency+ 424 "Failed Dependency")
99 (def-http-return-code +http-internal-server-error+ 500 "Internal Server Error")
100 (def-http-return-code +http-not-implemented+ 501 "Not Implemented")
101 (def-http-return-code +http-bad-gateway+ 502 "Bad Gateway")
102 (def-http-return-code +http-service-unavailable+ 503 "Service Unavailable")
103 (def-http-return-code +http-gateway-time-out+ 504 "Gateway Time-out")
104 (def-http-return-code +http-version-not-supported+ 505 "Version not supported")
106 (defconstant +day-names+
107 #("Mon" "Tue" "Wed" "Thu" "Fri" "Sat" "Sun")
108 "The three-character names of the seven days of the week - needed
109 for cookie date format.")
111 (defconstant +month-names+
112 #("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
113 "The three-character names of the twelve months - needed for cookie
114 date format.")
116 (defvar *rewrite-for-session-urls* t
117 "Whether HTML pages should possibly be rewritten for cookie-less
118 session-management.")
120 (defvar *content-types-for-url-rewrite*
121 '("text/html" "application/xhtml+xml")
122 "The content types for which url-rewriting is OK. See
123 *REWRITE-FOR-SESSION-URLS*.")
125 (defvar *the-random-state* (make-random-state t)
126 "A fresh random state.")
128 (defvar-unbound *session-secret*
129 "A random ASCII string that's used to encode the public session
130 data. This variable is initially unbound and will be set \(using
131 RESET-SESSION-SECRET) the first time a session is created, if
132 necessary. You can prevent this from happening if you set the value
133 yourself before starting acceptors.")
135 (defvar-unbound *hunchentoot-stream*
136 "The stream representing the socket Hunchentoot is listening on.")
138 (defvar *close-hunchentoot-stream* nil
139 "Will be set to T if the Hunchentoot socket stream has to be
140 closed at the end of the request.")
142 (defvar *headers-sent* nil
143 "Used internally to check whether the reply headers have
144 already been sent for this request.")
146 (defvar *file-upload-hook* nil
147 "If this is not NIL, it should be a unary function which will
148 be called with a pathname for each file which is uploaded to
149 Hunchentoot. The pathname denotes the temporary file to which
150 the uploaded file is written. The hook is called directly before
151 the file is created.")
153 (defvar *session-db* nil
154 "The default \(global) session database.")
156 (defvar *session-max-time* #.(* 30 60)
157 "The default time \(in seconds) after which a session times out.")
159 (defvar *session-gc-frequency* 50
160 "A session GC \(see function SESSION-GC) will happen every
161 *SESSION-GC-FREQUENCY* requests \(counting only requests which create
162 a new session) if this variable is not NIL. See SESSION-CREATED.")
164 (defvar *use-user-agent-for-sessions* t
165 "Whether the 'User-Agent' header should be encoded into the session
166 string. If this value is true, a session will cease to be accessible
167 if the client sends a different 'User-Agent' header.")
169 (defvar *use-remote-addr-for-sessions* nil
170 "Whether the client's remote IP \(as returned by REAL-REMOTE-ADDR)
171 should be encoded into the session string. If this value is true, a
172 session will cease to be accessible if the client's remote IP changes.
174 This might for example be an issue if the client uses a proxy server
175 which doesn't send correct 'X_FORWARDED_FOR' headers.")
177 (defvar *default-content-type* "text/html"
178 "The default content-type header which is returned to the client.
179 If this is text content type, the character set used for encoding the
180 response will automatically be added to the content type in a
181 ``charset'' attribute.")
183 (defvar *methods-for-post-parameters* '(:post)
184 "A list of the request method types \(as keywords) for which
185 Hunchentoot will try to compute POST-PARAMETERS.")
187 (defvar *header-stream* nil
188 "If this variable is not NIL, it should be bound to a stream to
189 which incoming and outgoing headers will be written for debugging
190 purposes.")
192 (defvar *show-lisp-errors-p* nil
193 "Whether Lisp errors in request handlers should be shown in HTML output.")
195 (defvar *show-lisp-backtraces-p* t
196 "Whether Lisp errors shown in HTML output should contain backtrace information.")
198 (defvar *log-lisp-errors-p* t
199 "Whether Lisp errors in request handlers should be logged.")
201 (defvar *log-lisp-backtraces-p* t
202 "Whether Lisp backtraces should be logged. Only has an effect if
203 *LOG-LISP-ERRORS-P* is true as well.")
205 (defvar *log-lisp-warnings-p* t
206 "Whether Lisp warnings in request handlers should be logged.")
208 (defvar *lisp-errors-log-level* :error
209 "Log level for Lisp errors. Should be one of :ERROR \(the default),
210 :WARNING, or :INFO.")
212 (defvar *lisp-warnings-log-level* :warning
213 "Log level for Lisp warnings. Should be one of :ERROR, :WARNING
214 \(the default), or :INFO.")
216 (defvar *message-log-lock* (make-lock "global-message-log-lock")
217 "A global lock to prevent concurrent access to the log file used by
218 the ACCEPTOR-LOG-MESSAGE function.")
220 (defvar *access-log-lock* (make-lock "global-access-log-lock")
221 "A global lock to prevent concurrent access to the log file used by
222 the ACCEPTOR-LOG-ACCESS function.")
224 (defvar *catch-errors-p* t
225 "Whether Hunchentoot should catch and log errors \(or rather invoke
226 the debugger).")
228 (defvar-unbound *acceptor*
229 "The current ACCEPTOR object while in the context of a request.")
231 (defvar-unbound *request*
232 "The current REQUEST object while in the context of a request.")
234 (defvar-unbound *reply*
235 "The current REPLY object while in the context of a request.")
237 (defvar-unbound *session*
238 "The current session while in the context of a request, or NIL.")
240 (defconstant +implementation-link+
241 #+:cmu "http://www.cons.org/cmucl/"
242 #+:sbcl "http://www.sbcl.org/"
243 #+:allegro "http://www.franz.com/products/allegrocl/"
244 #+:lispworks "http://www.lispworks.com/"
245 #+:openmcl "http://openmcl.clozure.com/"
246 "A link to the website of the underlying Lisp implementation.")
248 (defvar *tmp-directory*
249 #+(or :win32 :mswindows) "c:\\hunchentoot-temp\\"
250 #-(or :win32 :mswindows) "/tmp/hunchentoot/"
251 "Directory for temporary files created by MAKE-TMP-FILE-NAME.")
253 (defvar *tmp-files* nil
254 "A list of temporary files created while a request was handled.")
256 (defconstant +latin-1+
257 (make-external-format :latin1 :eol-style :lf)
258 "A FLEXI-STREAMS external format used for `faithful' input and
259 output of binary data.")
261 (defconstant +utf-8+
262 (make-external-format :utf8 :eol-style :lf)
263 "A FLEXI-STREAMS external format used internally for logging and to
264 encode cookie values.")
266 (defvar *hunchentoot-default-external-format* +utf-8+
267 "The external format used to compute the REQUEST object.")
269 (defconstant +buffer-length+ 8192
270 "Length of buffers used for internal purposes.")
272 (defvar *default-connection-timeout* 20
273 "The default connection timeout used when an acceptor is reading
274 from and writing to a socket stream.")
276 (eval-when (:compile-toplevel :load-toplevel :execute)
277 (define-symbol-macro *supports-threads-p*
278 #+:lispworks t
279 #-:lispworks bt:*supports-threads-p*))
281 (defvar *global-session-db-lock*
282 (load-time-value (and *supports-threads-p* (make-lock "global-session-db-lock")))
283 "A global lock to prevent two threads from modifying *session-db* at
284 the same time \(or NIL for Lisps which don't have threads).")
286 #-:lispworks
287 (defconstant +new-connection-wait-time+ 2
288 "Time in seconds to wait for a new connection to arrive before
289 performing a cleanup run.")
291 (pushnew :hunchentoot *features*)
293 ;; stuff for Nikodemus Siivola's HYPERDOC
294 ;; see <http://common-lisp.net/project/hyperdoc/>
295 ;; and <http://www.cliki.net/hyperdoc>
297 (defvar *hyperdoc-base-uri* "http://weitz.de/hunchentoot/")
299 (let ((exported-symbols-alist
300 (loop for symbol being the external-symbols of :hunchentoot
301 collect (cons symbol (concatenate 'string "#" (string-downcase symbol))))))
302 (defun hyperdoc-lookup (symbol type)
303 (declare (ignore type))
304 (cdr (assoc symbol exported-symbols-alist :test #'eq))))