@@ -12,6 +12,9 @@ class Writer
12
12
# Chunked transfer encoding
13
13
CHUNKED = "chunked" . freeze
14
14
15
+ # End of a chunked transfer
16
+ CHUNKED_END = "#{ ZERO } #{ CRLF } #{ CRLF } " . freeze
17
+
15
18
# Types valid to be used as body source
16
19
VALID_BODY_TYPES = [ String , NilClass , Enumerable ]
17
20
@@ -40,7 +43,7 @@ def stream
40
43
# Send headers needed to connect through proxy
41
44
def connect_through_proxy
42
45
add_headers
43
- @socket << join_headers
46
+ write ( join_headers )
44
47
end
45
48
46
49
# Adds the headers to the header array for the given request body we are working
@@ -65,24 +68,35 @@ def send_request_header
65
68
add_headers
66
69
add_body_type_headers
67
70
68
- @socket << join_headers
71
+ write ( join_headers )
69
72
end
70
73
71
74
def send_request_body
72
75
if @body . is_a? ( String )
73
- @socket << @ body
76
+ write ( @ body)
74
77
elsif @body . is_a? ( Enumerable )
75
78
@body . each do |chunk |
76
- @socket << chunk . bytesize . to_s ( 16 ) << CRLF
77
- @socket << chunk << CRLF
79
+ write ( chunk . bytesize . to_s ( 16 ) << CRLF )
80
+ write ( chunk << CRLF )
78
81
end
79
82
80
- @socket << ZERO << CRLF << CRLF
83
+ write ( CHUNKED_END )
81
84
end
82
85
end
83
86
84
87
private
85
88
89
+ def write ( data )
90
+ while data . present?
91
+ length = @socket . write ( data )
92
+ if data . length > length
93
+ data = data [ length ..-1 ]
94
+ else
95
+ break
96
+ end
97
+ end
98
+ end
99
+
86
100
def validate_body_type!
87
101
return if VALID_BODY_TYPES . any? { |type | @body . is_a? type }
88
102
fail RequestError , "body of wrong type: #{ @body . class } "
0 commit comments