Method: HTTPTools::Encoding.transfer_encoding_chunked_encode

Defined in:
lib/http_tools/encoding.rb

.transfer_encoding_chunked_encode(string) ⇒ Object

:call-seq: Encoding.transfer_encoding_chunked_encode(string) -> encoded_string

Returns string as a ‘chunked’ transfer encoding encoded string, suitable for a streaming response from a HTTP server, eg “foo” becomes “3\r\nfoo\r\n”

chunked responses should be terminted with a empty chunk, eg “0\r\n”, passing an empty string or nil will generate the empty chunk.



97
98
99
100
101
102
103
# File 'lib/http_tools/encoding.rb', line 97

def transfer_encoding_chunked_encode(string)
  if string && (length = string.length) > 0
    "#{length.to_s(16)}\r\n#{string}\r\n"
  else
    "0\r\n"
  end
end