#| Copyright (c) 2006 Risto Laakso All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |# (defpackage :nio-httpd (:use :cl :nio-server :nio) (:export start)) (in-package :nio-httpd) (defun method-sig (method) (logior (ash (logand (aref method 0) #xDF) 24) (ash (logand (aref method 1) #xDF) 16) (ash (logand (aref method 2) #xDF) 8) (ash (logand (aref method 3) #xDF) 0))) (defun map-method (method) (case (method-sig method) (#x47455400 :get) (#x504F5354 :post) (#x48454144 :head) (t :unknown))) (defun http-split-req (data) (let* ((line-end (position 13 data)) (line (subseq data 0 line-end)) (sp1 (position #.(char-code #\space) line)) (sp2 (position #.(char-code #\space) line :start (1+ sp1))) (req (subseq line 0 (1+ sp1))) (url (subseq line (1+ sp1) sp2)) (proto (subseq line (1+ sp2)))) (list (map-method req) url proto (+ line-end 2)))) (defun string-to-octets (string) (map 'vector #'char-code string)) (defparameter +status-lines+ (list (cons :ok (concatenate 'vector (string-to-octets "HTTP/1.0 200 OK") #(13 10))) (cons :not-found (concatenate 'vector (string-to-octets "HTTP/1.0 404 Not found") #(13 10))) (cons :method-not-implemented (concatenate 'vector (string-to-octets "HTTP/1.0 501 Method not implemented") #(13 10))) )) (defun make-status-line (status) (cdr (assoc status +status-lines+))) (defparameter +page-hash+ (make-hash-table :test #'equalp)) (defun integer-to-octets (int) (if (= int 0) #(48) (let* ((digits (floor (1+ (log int 10)))) (seq (make-sequence '(vector (unsigned-byte 8)) digits :initial-element 0)) (rem int)) (loop for idx from 0 below digits do (multiple-value-bind (rest last) (truncate rem 10) (setf (aref seq idx) (+ #.(char-code #\0) last) rem rest))) (nreverse seq)))) (defun file-to-octets (filename) (with-open-file (stream filename :direction :input :element-type 'unsigned-byte) (let* ((length (file-length stream)) (seq (make-sequence '(vector (unsigned-byte 8)) length :initial-element 0))) (read-sequence seq stream) seq))) (defun serve-content (client status content-type content) (let ((status-line (make-status-line status)) (content-type (concatenate 'vector (string-to-octets "Content-type: ") (string-to-octets content-type) #(13 10))) (content-len (concatenate 'vector (string-to-octets "Content-length: ") (integer-to-octets (length content)) #(13 10))) ) (let ((packet (concatenate 'vector status-line content-type content-len #(13 10) content))) (async-write-seq client packet) (close-async-fd client) ))) (defun page-not-found (client) (serve-content client :not-found "text/html" (string-to-octets "

404 Page not found

The requested URL was not found on this server."))) (defun method-not-implemented (client) (serve-content client :method-not-implemented "text/html" (string-to-octets "

501 Method not implemented

The requested method is not supported by this server."))) (defun http-accept-filter (sequence len) (search #(13 10 13 10) sequence :end2 len)) (defun start () (start-server #'(lambda (client data) (destructuring-bind (req url proto hdrs-start) (http-split-req data) (declare (ignore proto)) (case req (:get (let ((page (gethash url +page-hash+))) (if page (if (vectorp page) (serve-content client :ok "text/html" page) (funcall page :get url data hdrs-start client)) (page-not-found client)))) (:head (method-not-implemented client)) (:post (method-not-implemented client)) (t (method-not-implemented client))) )) #'http-accept-filter ;; :protocol :inet6 :host "::1" :host "127.0.0.1" ))