Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reading text/stream content type #202

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))