1 | #| |
---|
2 | Copyright (c) 2006 Risto Laakso |
---|
3 | All rights reserved. |
---|
4 | |
---|
5 | Redistribution and use in source and binary forms, with or without |
---|
6 | modification, are permitted provided that the following conditions |
---|
7 | are met: |
---|
8 | 1. Redistributions of source code must retain the above copyright |
---|
9 | notice, this list of conditions and the following disclaimer. |
---|
10 | 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | notice, this list of conditions and the following disclaimer in the |
---|
12 | documentation and/or other materials provided with the distribution. |
---|
13 | 3. The name of the author may not be used to endorse or promote products |
---|
14 | derived from this software without specific prior written permission. |
---|
15 | |
---|
16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
---|
17 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
---|
18 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
---|
19 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
20 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
---|
21 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
22 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
23 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
---|
25 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
26 | |# |
---|
27 | (in-package :nio-httpd) |
---|
28 | |
---|
29 | (defparameter +dyn-pre+ (string-to-octets "<html><body>")) |
---|
30 | (defparameter +dyn-post+ (string-to-octets "<br/><br/><img src=\"logo.png\"><h5>Page created by <a href=\"http://common-lisp.net/project/nio\">NIO-HTTPD</a>.</h5></body></html>")) |
---|
31 | |
---|
32 | |
---|
33 | (defvar +pages-served+ 0) |
---|
34 | |
---|
35 | (defun dynamic-gen (method url data hdrs-start client) |
---|
36 | (declare (ignore method url data hdrs-start)) |
---|
37 | (destructuring-bind (family host port) (remote-info client) |
---|
38 | (serve-content client :ok "text/html" |
---|
39 | (concatenate 'vector |
---|
40 | +dyn-pre+ |
---|
41 | |
---|
42 | (string-to-octets |
---|
43 | (with-output-to-string (str) |
---|
44 | (princ "<h2>Dynamic test page</h2>" str) |
---|
45 | |
---|
46 | (princ "Today is " str) |
---|
47 | (multiple-value-bind (sec min hrs day mon yrs) (decode-universal-time (get-universal-time)) |
---|
48 | (declare (ignore sec min hrs)) |
---|
49 | (loop for elt in (list day "." mon "." yrs) do (princ elt str))) |
---|
50 | |
---|
51 | (princ "<br/>" str) |
---|
52 | |
---|
53 | (princ "I have now served " str) |
---|
54 | (princ (incf +pages-served+) str) |
---|
55 | (princ " pages.<br/>" str) |
---|
56 | |
---|
57 | (princ "Your request is IPv" str) |
---|
58 | (princ (if (equal family :inet6) "6" "4") str) |
---|
59 | (princ ", from ip " str) |
---|
60 | (princ host str) |
---|
61 | (princ " port " str) |
---|
62 | (princ port str) |
---|
63 | (princ ".<br/><br/>" str) |
---|
64 | |
---|
65 | str)) |
---|
66 | |
---|
67 | +dyn-post+)))) |
---|
68 | |
---|
69 | (defun main-page (method url data hdrs-start client) |
---|
70 | (declare (ignore method url data hdrs-start)) |
---|
71 | |
---|
72 | (serve-content client :ok "text/html" |
---|
73 | (string-to-octets |
---|
74 | (concatenate 'string |
---|
75 | "<html><body>" |
---|
76 | "<h2>Welcome!</h2>" |
---|
77 | "<p>This is NIO-HTTPD 04-02-2006, a non-blocking i/o http-server written in Common Lisp.</p>" |
---|
78 | "<p>The project homepage is at <a href=\"http://common-lisp.net/project/nio/\">common-lisp.net</a>.</p>" |
---|
79 | "<br/><br/>" |
---|
80 | "<img src=\"logo.png\">" |
---|
81 | "</body></html>" |
---|
82 | )))) |
---|
83 | |
---|
84 | (defun png-logo (method url data hdrs-start client) |
---|
85 | (declare (ignore method url data hdrs-start)) |
---|
86 | (serve-content client :ok "image/png" (file-to-octets "logo120x80.png"))) |
---|
87 | |
---|
88 | |
---|
89 | ;; register pages |
---|
90 | (setf (gethash (string-to-octets "/") +page-hash+) #'main-page |
---|
91 | (gethash (string-to-octets "/dyn") +page-hash+) #'dynamic-gen |
---|
92 | (gethash (string-to-octets "/logo.png") +page-hash+) #'png-logo) |
---|