Skip to content

Commit

Permalink
Merge pull request #202 from pepe/add-stream-cleanup-http
Browse files Browse the repository at this point in the history
Fix reading text/stream content type
  • Loading branch information
bakpakin authored Nov 17, 2024
2 parents 1bed0c9 + 9d22547 commit 44e2601
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions spork/http.janet
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,23 @@
# In place content
(when-let [cl (in headers "content-length")]
(def {:buffer buf
:connection conn
:head-size head-size} req)
:connection conn} req)
(def content-length (scan-number cl))
(def remaining (- content-length (length buf)))
(when (pos? remaining)
(ev/chunk conn remaining buf))
(put req :body buf)
(break buf))

# event stream aka SSE
(when (-?>> (in headers "content-type")
(string/has-prefix? "text/event-stream"))
(def {:buffer buf
:connection conn} req)
(read-until conn buf "\n\n")
(put req :body buf)
(break buf))

# Chunked encoding
# TODO: The specification can have multiple transfer encodings so this
# precise string matching may not work for every case.
Expand Down Expand Up @@ -385,13 +393,13 @@
[nextmw]
(fn cookie-mw [req]
(-> req
(put :cookies
(or (-?>> [:headers "cookie"]
(get-in req)
(peg/match cookie-grammar)
(apply table))
{}))
nextmw)))
(put :cookies
(or (-?>> [:headers "cookie"]
(get-in req)
(peg/match cookie-grammar)
(apply table))
{}))
nextmw)))

###
### Server boilerplate
Expand Down Expand Up @@ -493,4 +501,3 @@

# TODO - handle redirects with Location header
res))

0 comments on commit 44e2601

Please sign in to comment.