Compare commits

2 Commits

Author SHA1 Message Date
nat
3244a017c6 use isal instead of builtin compression funcitons 2025-05-27 15:26:35 -07:00
nat
6b1c6de309 add support for Accept-Encoding header 2025-05-27 15:07:17 -07:00
2 changed files with 25 additions and 13 deletions

View File

@ -1,26 +1,38 @@
(import hyrule.collections [assoc])
(import isal.igzip [compress :as gzip])
(import isal.isal_zlib [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))