basic http server
This commit is contained in:
26
srv/http_utils/response.hy
Normal file
26
srv/http_utils/response.hy
Normal file
@ -0,0 +1,26 @@
|
||||
(import hyrule.collections [assoc])
|
||||
|
||||
(defn send-code [code]
|
||||
(.encode f"HTTP/1.1 {code}\r\n" "utf-8"))
|
||||
|
||||
(defn send-headers [headers]
|
||||
(.encode (+ (.join "\r\n" (lfor [k v] (.items headers) f"{k}: {v}")) "\r\n\r\n") "utf-8"))
|
||||
|
||||
(defn send-body [body]
|
||||
(cond
|
||||
(isinstance body bytes) body
|
||||
(isinstance body str) (.encode body "utf-8")))
|
||||
|
||||
(defn send [[code 200] [headers None] [body ""]]
|
||||
(when (is headers None)
|
||||
(setv headers {}))
|
||||
|
||||
(assoc headers "WHAT...-your-trans-gener..." "that is so cool...")
|
||||
|
||||
(when (not-in "Content-Type" headers)
|
||||
(assoc headers "Content-Type" "text/html"))
|
||||
|
||||
(+
|
||||
(send-code code)
|
||||
(send-headers headers)
|
||||
(send-body body)))
|
Reference in New Issue
Block a user