add support for Accept-Encoding header

This commit is contained in:
2025-05-27 14:59:41 -07:00
parent 978ae8fd03
commit 6b1c6de309
2 changed files with 24 additions and 13 deletions

View File

@ -1,26 +1,37 @@
(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 {}))
(when request-headers
(when (.get request-headers "Accept-Encoding")
(branch (in it (.get request-headers "Accept-Encoding"))
"zstd" (do
(setv (get headers "Accept-Encoding") "gzip")
(setv body (zstd body)))
"gzip" (do
(setv (get headers "Accept-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))

View File

@ -39,7 +39,7 @@
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))
(.sendall client-socket (http.response.send :request-headers (.get parsed-request "headers") #** response))
(except [e Exception]
(.warn log (format-exc))