Skip to content

Instantly share code, notes, and snippets.

@dpk
Created November 27, 2011 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpk/1397778 to your computer and use it in GitHub Desktop.
Save dpk/1397778 to your computer and use it in GitHub Desktop.
Plan: a web server. (This is not Scheme.)
(set! default-headers* '{content-type "text/html; charset=utf-8"
server "Plan httpd"})
;; OHSHI-
(set! response-codes* '{100 "Continue" ; 100-199 -- informational
101 "Switching Protocols"
102 "Processing"
200 "OK" ; 200-299 -- success
201 "Created"
202 "Accepted"
203 "Non-Authoritative Information"
204 "No Content"
205 "Reset Content"
206 "Partial Content"
207 "Multi-Status"
300 "Multiple Choices" ; 300-399 -- redirection
301 "Moved Permanently"
302 "Found"
303 "See Other"
304 "Not Modified"
305 "Use Proxy"
306 "Switch Proxy"
307 "Temporary Redirect"
400 "Bad Request" ; 400-499 -- client error
401 "Unauthorized"
402 "Payment Required"
403 "Forbidden"
404 "Not Found"
405 "Method Not Allowed"
406 "Not Acceptable"
407 "Proxy Authentication Required"
408 "Request Timeout"
409 "Conflict"
410 "Gone"
411 "Length Required"
412 "Precondition Failed"
413 "Request Entity Too Large"
414 "Request-URI Too Long"
415 "Unsupported Media Type"
416 "Requested Range Not Satisfiable"
417 "Expectation Failed"
418 "I'm a teapot" ; srsly
422 "Unprocessable Entity"
423 "Locked"
424 "Failed Dependency"
425 "Unordered Collection"
426 "Upgrade Required"
500 "Internal Server Error" ; 500-599 -- server made a boo-boo
501 "Not Implemented"
502 "Bad Gateway"
503 "Service Unavailable"
504 "Gateway Timeout"
505 "HTTP Version Not Supported"
506 "Variant Also Negotiates"
507 "Insufficient Storage"
510 "Not Extended" })
(deffn (http-server host port request-handler)
(let sock (socket 'inet 'stream)
(bind sock host port)
(listen sock 5)
((rfn (inthandl conninfo)
(let conn (car conninfo)
(let rql (read-request-line conn)
(request-handler
{'method (car rql)
'uri (cadr rql)
'http-version (caddr rql)
'headers (read-headers conn)
'ip (cadr conninfo)
'port (caddr conninfo)
'stream conn}
(responder conn))) (inthandl (accept sock)))) (accept sock))))
(deffn (responder conn)
(fn (resp)
(if (string? resp)
(send-response 200 default-headers* resp)
(and (list? resp) (= (length resp) 2))
(send-response (car resp) default-headers* (cadr resp))
(and (list? resp) (= (length resp) 3))
(send-response (car resp) (dictionary-merge (cadr resp) default-headers*) (caddr resp)))))
(deffn (send-response conn status headers body)
(printrn conn "HTTP/1.1 " status " " (response-codes* status))
(each header (dictionary->alist headers)
(printrn conn (car header) ": " (cadr header)))
(printrn conn)
(printrn conn body)
(close conn))
(deffn (read-request-line conn)
(let line (regexp-match (strip (gets conn)) r{^(\w+) (.*) HTTP/(\d\.\d)$}i)
(list (symbol (line 1)) (line 2) (line 3))))
(deffn (read-headers conn)
((rfn (reader headers ln)
(if (= "" ln) headers
(let parts (regexp-match ln /^(.+?): (.*)$/)
(set (headers (symbol (parts 1))) (parts 2))
(reader headers (strip (gets conn)))))) {} (strip (gets conn))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment