-
Notifications
You must be signed in to change notification settings - Fork 322
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
Extract HTTP::Connection ala Reel (WIP: DO NOT MERGE) #72
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
require 'http/response/parser' | ||
|
||
module HTTP | ||
# A connection to the HTTP server | ||
class Connection | ||
CONNECTION = 'Connection'.freeze | ||
TRANSFER_ENCODING = 'Transfer-Encoding'.freeze | ||
KEEP_ALIVE = 'Keep-Alive'.freeze | ||
CLOSE = 'close'.freeze | ||
|
||
attr_reader :socket, :parser, :current_response | ||
attr_accessor :request_state, :response_state | ||
|
||
# Attempt to read this much data | ||
BUFFER_SIZE = 16384 | ||
attr_reader :buffer_size | ||
|
||
def initialize(socket, buffer_size = nil) | ||
@socket = socket | ||
@keepalive = true | ||
@buffer_size = buffer_size || BUFFER_SIZE | ||
@parser = Response::Parser.new(self) | ||
|
||
@request_state = :headers | ||
@response_state = :headers | ||
reset_response | ||
end | ||
|
||
# Is the connection still active? | ||
def alive?; @keepalive; end | ||
|
||
# Send a request to the server | ||
# Response can be a symbol indicating the status code or a HTTP::Response | ||
def respond(response, headers_or_body = {}, body = nil) | ||
raise StateError, "not in header state" if @response_state != :headers | ||
|
||
if headers_or_body.is_a? Hash | ||
headers = headers_or_body | ||
else | ||
headers = {} | ||
body = headers_or_body | ||
end | ||
|
||
if @keepalive | ||
headers[CONNECTION] = KEEP_ALIVE | ||
else | ||
headers[CONNECTION] = CLOSE | ||
end | ||
|
||
case response | ||
when Symbol | ||
response = Response.new(response, headers, body) | ||
when Response | ||
else raise TypeError, "invalid response: #{response.inspect}" | ||
end | ||
|
||
if current_request | ||
current_request.handle_response(response) | ||
else | ||
raise RequestError | ||
end | ||
|
||
# Enable streaming mode | ||
if response.chunked? and response.body.nil? | ||
@response_state = :chunked_body | ||
elsif @keepalive | ||
reset_request | ||
else | ||
@current_request = nil | ||
@parser.reset | ||
@request_state = :closed | ||
end | ||
rescue IOError, Errno::ECONNRESET, Errno::EPIPE, RequestError | ||
# The client disconnected early, or there is no request | ||
@keepalive = false | ||
@request_state = :closed | ||
end | ||
|
||
def readpartial(size = @buffer_size) | ||
unless @request_state == :headers || @request_state == :body | ||
raise StateError, "can't read in the '#{@request_fsm.state}' request state" | ||
end | ||
|
||
@parser.readpartial(size) | ||
end | ||
|
||
# Close the connection | ||
def close | ||
@keepalive = false | ||
@socket.close unless @socket.closed? | ||
end | ||
|
||
# Reset the current response state | ||
def reset_response | ||
@response_state = :headers | ||
@current_request = nil | ||
@parser.reset | ||
end | ||
private :reset_response | ||
|
||
# Set response state for the connection. | ||
def response_state=(state) | ||
reset_response if state == :headers | ||
@response_state = state | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is where the EOFError stuff may occur, see #46
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this
@parser
(HTTP::Response::Parser
) is fairly different from the original. Also, this doesn't initiate I/O directly, rather the parser itself does.