misc updates to server code; improve Cache-Control settings
This commit is contained in:
@ -29,4 +29,4 @@
|
|||||||
(with [f (open path "rb")]
|
(with [f (open path "rb")]
|
||||||
(setv data (.read f)))
|
(setv data (.read f)))
|
||||||
|
|
||||||
(return #({"Content-Type" mime-type "Cache-Control" "max-age=0, stale-while-revalidate=31536000"} data)))
|
(return #({"Content-Type" mime-type "Cache-Control" "max-age=300, stale-while-revalidate=3600"} data)))
|
||||||
|
@ -19,8 +19,8 @@
|
|||||||
(setv [http-method route http-version] (.split (.decode request-line "utf-8") " "))
|
(setv [http-method route http-version] (.split (.decode request-line "utf-8") " "))
|
||||||
|
|
||||||
(doto request
|
(doto request
|
||||||
(assoc :method http-method)
|
(assoc "method" http-method)
|
||||||
(assoc :route (let
|
(assoc "route" (let
|
||||||
[[_ _ path query _] (urlsplit route)]
|
[[_ _ path query _] (urlsplit route)]
|
||||||
(dict
|
(dict
|
||||||
:path (.join "/"
|
:path (.join "/"
|
||||||
@ -35,8 +35,8 @@
|
|||||||
|
|
||||||
:parameters (parse-url-encoded query))))
|
:parameters (parse-url-encoded query))))
|
||||||
|
|
||||||
(assoc :version http-version)
|
(assoc "version" http-version)
|
||||||
(assoc :headers (dict
|
(assoc "headers" (dict
|
||||||
(dfor header
|
(dfor header
|
||||||
(map (fn [x]
|
(map (fn [x]
|
||||||
(.split (.decode x "utf-8") ": ")) headers)
|
(.split (.decode x "utf-8") ": ")) headers)
|
||||||
@ -44,7 +44,7 @@
|
|||||||
(get header 1))))
|
(get header 1))))
|
||||||
|
|
||||||
|
|
||||||
(assoc :body (branch (in it (. request (get "headers") (get "Content-Type" "")))
|
(assoc "body" (branch (in it (. request (get "headers") (get "Content-Type" "")))
|
||||||
"application/x-www-form-urlencoded" (parse-url-encoded (.decode (.join b"\n" body) "utf-8"))
|
"application/x-www-form-urlencoded" (parse-url-encoded (.decode (.join b"\n" body) "utf-8"))
|
||||||
"" ""
|
"" ""
|
||||||
;;"multipart/form-data" ;(do
|
;;"multipart/form-data" ;(do
|
||||||
|
18
srv/main.hy
18
srv/main.hy
@ -16,11 +16,11 @@
|
|||||||
(.bind socket #(ADDRESS PORT))
|
(.bind socket #(ADDRESS PORT))
|
||||||
(.listen socket 10)
|
(.listen socket 10)
|
||||||
(.debug log "socket bound")
|
(.debug log "socket bound")
|
||||||
|
|
||||||
(while True
|
(while True
|
||||||
(try
|
(try
|
||||||
(.start
|
(.start
|
||||||
(Thread
|
(Thread
|
||||||
:target (fn [client-socket address]
|
:target (fn [client-socket address]
|
||||||
(try
|
(try
|
||||||
(setv request-data (bytes))
|
(setv request-data (bytes))
|
||||||
@ -28,7 +28,7 @@
|
|||||||
(+= request-data data)
|
(+= request-data data)
|
||||||
(when (< (len data) 1024)
|
(when (< (len data) 1024)
|
||||||
(break)))
|
(break)))
|
||||||
|
|
||||||
(setv parsed-request (http.request.parse-data request-data))
|
(setv parsed-request (http.request.parse-data request-data))
|
||||||
(.debug log parsed-request)
|
(.debug log parsed-request)
|
||||||
(.info log (+ (str (cond
|
(.info log (+ (str (cond
|
||||||
@ -37,19 +37,19 @@
|
|||||||
|
|
||||||
(setv response (match-request parsed-request))
|
(setv response (match-request parsed-request))
|
||||||
(.sendall client-socket (http.response.send #** response))
|
(.sendall client-socket (http.response.send #** response))
|
||||||
|
|
||||||
(except [e Exception]
|
(except [e Exception]
|
||||||
(.warn log (format-exc))
|
(.warn log (format-exc))
|
||||||
(.close client-socket))))
|
(.close client-socket))))
|
||||||
|
|
||||||
:args #(#* (socket.accept))))
|
:args #(#* (socket.accept))))
|
||||||
|
|
||||||
(except [e Exception]
|
(except [e Exception]
|
||||||
(.warn log (format-exc)))))
|
(.warn log (format-exc)))))
|
||||||
|
|
||||||
(except [e Exception]
|
(except [e Exception]
|
||||||
(.critical log (format-exc)))
|
(.critical log (format-exc)))
|
||||||
|
|
||||||
(finally
|
(finally
|
||||||
(.close socket)
|
(.close socket)
|
||||||
(.info log "server shut down")))))
|
(.info log "server shut down")))))
|
||||||
|
Reference in New Issue
Block a user