Skip to content

Commit

Permalink
Refactor Request::Writer#send_request
Browse files Browse the repository at this point in the history
  • Loading branch information
janko committed May 24, 2018
1 parent 8529ffa commit 8f9081c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
5 changes: 0 additions & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ Layout/SpaceAroundOperators:
Layout/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space

## Lint ########################################################################

Lint/HandleExceptions:
Enabled: false

## Metrics #####################################################################

Metrics/AbcSize:
Expand Down
28 changes: 18 additions & 10 deletions lib/http/request/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,32 @@ def join_headers
@request_header.join(CRLF) + CRLF * 2
end

# Writes HTTP request data into the socket.
def send_request
# It's important to send the request in a single write call when
# possible in order to play nicely with Nagle's algorithm. Making
# two writes in a row triggers a pathological case where Nagle is
# expecting a third write that never happens.
each_chunk { |chunk| write chunk }
rescue Errno::EPIPE
# server doesn't need any more data
nil
end

# Yields chunks of request data that should be sent to the socket.
#
# It's important to send the request in a single write call when possible
# in order to play nicely with Nagle's algorithm. Making two writes in a
# row triggers a pathological case where Nagle is expecting a third write
# that never happens.
def each_chunk
data = join_headers

@body.each do |chunk|
data << encode_chunk(chunk)
write(data)
yield data
data.clear
end

write(data) unless data.empty?
yield data unless data.empty?

write(CHUNKED_END) if chunked?
rescue Errno::EPIPE
# server doesn't need any more data
yield CHUNKED_END if chunked?
end

# Returns the chunk encoded for to the specified "Transfer-Encoding" header.
Expand All @@ -103,7 +111,7 @@ def write(data)
data = data.byteslice(length..-1)
end
rescue Errno::EPIPE
raise # re-raise Errno::EPIPE
raise
rescue IOError, SocketError, SystemCallError => ex
raise ConnectionError, "error writing to socket: #{ex}", ex.backtrace
end
Expand Down

0 comments on commit 8f9081c

Please sign in to comment.