aboutsummaryrefslogtreecommitdiffstats
path: root/lib/plum
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2015-11-08 22:43:14 +0900
committerKazuki Yamaguchi <k@rhe.jp>2015-11-08 22:43:14 +0900
commit256e0a5133861568e52c499da8074dd211b650ff (patch)
tree8c12989d2f2a6d2e7377f7a0e2fae6676ce89d0c /lib/plum
parent458ac001132a810d83c24baefdf3337bf278d567 (diff)
downloadplum-256e0a5133861568e52c499da8074dd211b650ff.tar.gz
client/legacy_client_session: set transfer-encoding: chunked if content-length is not specified
Diffstat (limited to 'lib/plum')
-rw-r--r--lib/plum/client/legacy_client_session.rb46
1 files changed, 38 insertions, 8 deletions
diff --git a/lib/plum/client/legacy_client_session.rb b/lib/plum/client/legacy_client_session.rb
index 2e52be2..087c643 100644
--- a/lib/plum/client/legacy_client_session.rb
+++ b/lib/plum/client/legacy_client_session.rb
@@ -30,8 +30,18 @@ module Plum
end
def request(headers, body, options, &headers_cb)
+ headers["host"] = headers[":authority"] || headers["host"] || @config[:hostname]
+ if body
+ if headers["content-length"] || headers["transfer-encoding"]
+ chunked = false
+ else
+ chunked = true
+ headers["transfer-encoding"] = "chunked"
+ end
+ end
+
response = Response.new
- @requests << [response, headers, body, headers_cb]
+ @requests << [response, headers, body, chunked, headers_cb]
consume_queue
response
end
@@ -45,20 +55,40 @@ module Plum
def consume_queue
return if @response || @requests.empty?
- response, headers, body, cb = @requests.shift
- headers["host"] = headers[":authority"] || headers["host"] || @config[:hostname]
+ response, headers, body, chunked, cb = @requests.shift
@response = response
@headers_callback = cb
- @socket << "%s %s HTTP/1.1\r\n" % [headers[":method"], headers[":path"]]
+ @socket << construct_request(headers)
+
+ if body
+ if chunked
+ read_object(body) { |chunk|
+ @socket << chunk.bytesize.to_s(16) << "\r\n" << chunk << "\r\n"
+ }
+ else
+ read_object(body) { |chunk| @socket << chunk }
+ end
+ end
+ end
+
+ def construct_request(headers)
+ out = String.new
+ out << "%s %s HTTP/1.1\r\n" % [headers[":method"], headers[":path"]]
headers.each { |key, value|
next if key.start_with?(":") # HTTP/2 psuedo headers
- @socket << "%s: %s\r\n" % [key, value]
+ out << "%s: %s\r\n" % [key, value]
}
- @socket << "\r\n"
+ out << "\r\n"
+ end
- if body
- @socket << body
+ def read_object(body)
+ if body.is_a?(String)
+ yield body
+ else # IO
+ until body.eof?
+ yield body.readpartial(1024)
+ end
end
end