2010-12-13

teepeedee2でfaviconを表示

手探りでやるしかないから、えらく苦労した。

(eval-when (:compile-toplevel :load-toplevel :execute)
  (asdf:load-system :teepeedee2))

(defpackage :tpd2-favicon (:use :cl :tpd2 :tpd2.ml.html))
(in-package :tpd2-favicon)

(defconstant +icon-content-type+
  (byte-vector-cat "Content-Type: image/x-icon" tpd2.io:+newline+))

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defun favicon ()
    (with-open-file (in "work/tpd2/favicon.ico" :element-type '(unsigned-byte 8))
      (let ((buf (make-array (file-length in) :element-type '(unsigned-byte 8))))
        (read-sequence buf in)
        buf))))

(dispatcher-register-path
 (find-or-make-dispatcher "localhost:8080")
 "/favicon.ico"
 #'(lambda (dispatcher con done)
     (declare (ignore dispatcher))
     (start-http-response :content-type +icon-content-type+)
     (send-http-response con done (with-sendbuf () #.(favicon)))))

(defsite *favicon-site* :dispatcher (find-or-make-dispatcher "localhost:8080"))

(with-site (*favicon-site*)
  (defpage "/" ()
    (<p "favicon test")))

(http-start-server 8080)
(ccl:process-run-function "tpd2" #'event-loop)

0 件のコメント: