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

Add closed state to HTTP::Server::Response #6477

Merged
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions spec/std/http/server/server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ module HTTP
io = IO::Memory.new
response = Response.new(io)
response.close
response.closed?.should be_true
io.closed?.should be_false
expect_raises(IO::Error, "Closed stream") { response << "foo" }
io.to_s.should eq("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n")
end

Expand Down
12 changes: 12 additions & 0 deletions src/http/server/response.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class HTTP::Server

# :nodoc:
def reset
# This method is called by RequestProcessor to avoid allocating a new instance for each iteration.
@headers.clear
@cookies = nil
@status_code = 200
Expand Down Expand Up @@ -104,6 +105,11 @@ class HTTP::Server
@output.close
end

# Returns `true` if this response has been closed.
def closed?
@output.closed?
end

# Generates an error response using *message* and *code*.
#
# Calls `reset` and then writes the given message.
Expand Down Expand Up @@ -154,6 +160,7 @@ class HTTP::Server
@sync = false
@flush_on_newline = false
@chunked = false
@closed = false
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asterite This is in #reset. I'm not sure how useful this is, because when a request is closed, at least the headers has already been written to the underlying IO. This effectively can't be reset because the response is already sent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, cool. Reset is useful, can't remember now why, but response is reused for keep alive, check request processor.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, RequestProcessor resets the response to avoid allocating a new instance for each iteration.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add a comment about this.

end

private def unbuffered_read(slice : Bytes)
Expand Down Expand Up @@ -182,6 +189,10 @@ class HTTP::Server
end
end

def closed?
@closed
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this should be checked in unbuffered_write?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IO::Buffered#write already calls check_open. This is ensured with the expect_raises expectation.

def close
unless response.wrote_headers?
response.content_length = @out_count
Expand All @@ -204,6 +215,7 @@ class HTTP::Server

private def unbuffered_close
@io << "0\r\n\r\n" if @chunked
@closed = true
end

private def unbuffered_rewind
Expand Down