(defconstant +websocket-magic-key+ "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" "Fixed magic WebSocket UUIDv4 key use in handshakes") (defun html (path &aux (type "text/html")) (cond ((cl-ppcre:scan "js$" path) (setf type "application/javascript")) ((cl-ppcre:scan "css$" path) (setf type "text/css"))) (multiple-value-bind (html length) ;; (read-file "/files/Beej's Guide to Network Programming.html") (read-file (concat "~/celwk/static" (if (string= path "/") "/ifs.htm" path))) (values (fmt "HTTP/1.1 200 OK~%~ Connection: keep-alive~%~ Access-Control-Allow-Origin: *~%~ Content-Disposition: attachment; filename=\"cool.html\" When: ~A~%~ Content-Type: ~A~%~ Content-Length: ~A~% ~A" (now) type length html) length))) (build-memorized http html) (defun redirect () "HTTP/1.1 301 Moved Permanently Server: Leovinci Date: Mon, 21 Mar 2022 02:23:43 GMT Location: http://can.cn/ ") (defun not-found () "HTTP/1.1 404 Not Found Server: leovinci Some-Thing: You Can't Say Content-Length: 8 Nothing ") (defun connect-websocket (key) (fmt "HTTP/1.1 101 Switching Protocols~c Connection: Upgrade~c Upgrade: websocket~c Content-Type: application/octet-stream~c Content-Length: 0~c Sec-WebSocket-Accept: ~A~c ~c " #\Return #\Return #\Return #\Return #\Return (sha1:sha1-base64 (concat (trim key) +websocket-magic-key+) #'base64:base64-encode) #\Return #\Return))