(defun string-split (x) 
  (setq x (string-left-trim " " x))
  (let ((cur ""))
    (loop 
    until (or (equal x "") (equal (char x 0) #\space) )  do
      (setq cur (concatenate 'string cur (string (char x 0))))
      (setq x (subseq x 1)))
    (if (equal x "") (cons cur nil) (cons cur (string-split x)))))

(defun server (port)
  (let ( (bound-fd (create-inet-listener port)) 
    (fd nil)
    (stream nil)
    (line nil) 
    (file nil) )
    (loop
      (setq fd (accept-tcp-connection bound-fd))
      (setq stream (system:make-fd-stream 
    	    fd 
    	    :input t :output t 
    	    :element-type 'character))
      (setq line (string-split (read-line stream nil nil)))
      (if (and (second line) (string= (string-upcase (first line)) "GET"))
      (progn
        (when (equal (second line) "/") 
          (setf (second line) "/index.html"))
        (setf (second line) 
          (concatenate 'string "." (second line)))
        (if (open (second line) :direction :probe)
    	(progn
    	  (setq file (open (second line)))
    	  (loop while 
    		(write-line (read-line file nil) stream))
    	  (close file))
          (write-line "404 / File not found" stream)))
    (write-line 
     "501 / I don't know how to do what you're asking!" stream))
      (close stream)
      (unix:unix-close fd))))

(server 8080)