lib/http/protocol/http1/connection.rb in http-protocol-0.10.1 vs lib/http/protocol/http1/connection.rb in http-protocol-0.11.0
- old
+ new
@@ -97,18 +97,11 @@
def write_response(version, status, headers, body = nil, head = false)
@stream.write("#{version} #{status}\r\n")
write_headers(headers)
write_persistent_header(version)
-
- if head
- write_body_head(body)
- else
- write_body(body, version == HTTP11)
- end
-
- @stream.flush
+ write_body(body, version == HTTP11, head)
end
def write_headers(headers)
headers.each do |name, value|
@stream.write("#{name}: #{value}\r\n")
@@ -190,29 +183,27 @@
crlf = @stream.read(2)
return chunk
end
- def write_chunk(chunk)
- if chunk.nil?
- @stream.write("0\r\n\r\n")
- elsif !chunk.empty?
- @stream.write("#{chunk.bytesize.to_s(16).upcase}\r\n")
- @stream.write(chunk)
- @stream.write(CRLF)
- @stream.flush
- end
- end
-
def write_empty_body(body)
@stream.write("content-length: 0\r\n\r\n")
- @stream.flush
+
+ if body
+ body.close if body.respond_to?(:close)
+ end
end
- def write_fixed_length_body(body, length)
+ def write_fixed_length_body(body, length, head)
@stream.write("content-length: #{length}\r\n\r\n")
+ if head
+ body.close if body.respond_to?(:close)
+
+ return
+ end
+
chunk_length = 0
body.each do |chunk|
chunk_length += chunk.bytesize
if chunk_length > length
@@ -227,59 +218,59 @@
if chunk_length != length
raise ArgumentError, "Wrote #{chunk_length} bytes, but content length was #{length} bytes!"
end
end
- def write_chunked_body(body)
+ def write_chunked_body(body, head)
@stream.write("transfer-encoding: chunked\r\n\r\n")
+ if head
+ body.close if body.respond_to?(:close)
+
+ return
+ end
+
body.each do |chunk|
next if chunk.size == 0
@stream.write("#{chunk.bytesize.to_s(16).upcase}\r\n")
@stream.write(chunk)
@stream.write(CRLF)
@stream.flush
end
@stream.write("0\r\n\r\n")
- @stream.flush
end
- def write_body_and_close(body)
+ def write_body_and_close(body, head)
# We can't be persistent because we don't know the data length:
@persistent = false
-
@stream.write("\r\n")
- body.each do |chunk|
- @stream.write(chunk)
- @stream.flush
+ if head
+ body.close if body.respond_to?(:close)
+ else
+ body.each do |chunk|
+ @stream.write(chunk)
+ @stream.flush
+ end
end
@stream.stream.close_write
end
- def write_body(body, chunked = true)
+ def write_body(body, chunked = true, head = false)
if body.nil? or body.empty?
write_empty_body(body)
elsif length = body.length
- write_fixed_length_body(body, length)
+ write_fixed_length_body(body, length, head)
elsif chunked
- write_chunked_body(body)
+ write_chunked_body(body, head)
else
- write_body_and_close(body)
+ write_body_and_close(body, head)
end
- end
-
- def write_body_head(body)
- if body.nil? or body.empty?
- @stream.write("content-length: 0\r\n\r\n")
- elsif length = body.length
- @stream.write("content-length: #{length}\r\n\r\n")
- else
- @stream.write("\r\n")
- end
+
+ @stream.flush
end
def read_chunked_body
buffer = String.new.b