add support for Content-Encoding header
This commit is contained in:
@ -1,26 +1,39 @@
|
||||
(import hyrule.collections [assoc])
|
||||
(import gzip [compress :as gzip compress :as zstd])
|
||||
|
||||
(defn send-code [code]
|
||||
(import hyrule.collections [assoc])
|
||||
(require hyrule.control [branch])
|
||||
|
||||
(defn pack-code [code]
|
||||
(.encode f"HTTP/1.1 {code}\r\n" "utf-8"))
|
||||
|
||||
(defn send-headers [headers]
|
||||
(defn pack-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
|
||||
(defn send [[code 200] [headers None] [body ""] [request-headers None]]
|
||||
(when (is headers None)
|
||||
(setv headers {}))
|
||||
|
||||
(setv 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 {}))
|
||||
(print request-headers)
|
||||
|
||||
(when request-headers
|
||||
(when (.get request-headers "Accept-Encoding")
|
||||
(branch (in it (.get request-headers "Accept-Encoding"))
|
||||
"zstd" (do
|
||||
(setv (get headers "Content-Encoding") "gzip")
|
||||
(setv body (zstd body)))
|
||||
"gzip" (do
|
||||
(setv (get headers "Content-Encoding") "gzip")
|
||||
(setv body (gzip body))))))
|
||||
|
||||
(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)))
|
||||
(pack-code code)
|
||||
(pack-headers headers)
|
||||
body))
|
||||
|
@ -39,7 +39,8 @@
|
||||
True (get address 0))) f": {(. parsed-request (get "method"))} {(. parsed-request (get "route") (get "path"))}"))
|
||||
|
||||
(setv response (match-request parsed-request))
|
||||
(.sendall client-socket (http.response.send #** response))
|
||||
(print (.get parsed-request "headers"))
|
||||
(.sendall client-socket (http.response.send :request-headers (.get parsed-request "headers") #** response))
|
||||
|
||||
(except [e Exception]
|
||||
(.warn log (format-exc))
|
||||
|
Reference in New Issue
Block a user