source: trunk/src/nio-httpd-default-site.lisp

Last change on this file was 6, checked in by psmith, 16 years ago

Moved to standard directory structure

File size: 3.6 KB
Line 
1#|
2Copyright (c) 2006 Risto Laakso
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
81. Redistributions of source code must retain the above copyright
9   notice, this list of conditions and the following disclaimer.
102. 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.
133. 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
16THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25THIS 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)
Note: See TracBrowser for help on using the repository browser.