Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break Request#stream into a method object RequestStream
Signed-off-by: Sam Phippen <samphippen@googlemail.com>
- Loading branch information
Sam Phippen
committed
Sep 5, 2012
1 parent
346b84d
commit 45180a2
Showing
3 changed files
with
50 additions
and
31 deletions.
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,47 @@ | ||
module Http | ||
class RequestStream | ||
def initialize(socket, body, headers, headerstart) | ||
@socket = socket | ||
@body = body | ||
@headers = headers | ||
@request_header = headerstart | ||
end | ||
|
||
# Stream the request to a socket | ||
def stream | ||
socket = @socket | ||
request_header = @request_header | ||
@headers.each do |field, value| | ||
request_header << "#{field}: #{value}#{CRLF}" | ||
end | ||
|
||
case @body | ||
when NilClass | ||
socket << request_header << CRLF | ||
return | ||
when String | ||
request_header << "Content-Length: #{@body.length}#{CRLF}" unless @headers['Content-Length'] | ||
request_header << CRLF | ||
|
||
socket << request_header | ||
socket << @body.to_s | ||
when Enumerable | ||
if encoding = @headers['Transfer-Encoding'] | ||
raise ArgumentError, "invalid transfer encoding" unless encoding == "chunked" | ||
request_header << CRLF | ||
else | ||
request_header << "Transfer-Encoding: chunked#{CRLF * 2}" | ||
end | ||
|
||
socket << request_header | ||
@body.each do |chunk| | ||
socket << chunk.bytesize.to_s(16) << CRLF | ||
socket << chunk | ||
end | ||
|
||
socket << "0" << CRLF * 2 | ||
else raise TypeError, "invalid @body type: #{@body.class}" | ||
end | ||
end | ||
end | ||
end |