Skip to content

Commit

Permalink
apply a monkey patch to the server responseto
Browse files Browse the repository at this point in the history
to not raise an error on unbuffered close
  • Loading branch information
mamantoha committed Dec 4, 2024
1 parent 8ac2cbf commit fa36d70
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/initializers/kemal.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "../../src/ext/http/server/response"
require "kemal"
require "kemal-session"
require "kemal-session-redis-engine"
Expand Down
30 changes: 30 additions & 0 deletions src/ext/http/server/response.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class HTTP::Server
class Response < IO
private def unbuffered_write(slice : Bytes) : Nil
return if slice.empty?

if response.headers["Transfer-Encoding"]? == "chunked"
@chunked = true
elsif !response.wrote_headers?
if response.version != "HTTP/1.0" && !response.headers.has_key?("Content-Length")
response.headers["Transfer-Encoding"] = "chunked"
@chunked = true
end
end

ensure_headers_written

if @chunked
slice.size.to_s(@io, 16)
@io << "\r\n"
@io.write(slice)
@io << "\r\n"
else
@io.write(slice)
end
rescue ex : IO::Error
unbuffered_close
# raise ClientError.new("Error while writing data to the client", ex)
end
end
end

0 comments on commit fa36d70

Please sign in to comment.