Class: HTTP::Request::Writer
- Inherits:
-
Object
- Object
- HTTP::Request::Writer
- Defined in:
- lib/http/request/writer.rb
Constant Summary collapse
- CRLF =
CRLF is the universal HTTP delimiter
"\r\n".freeze
- ZERO =
Chunked data termintaor.
"0".freeze
- CHUNKED =
Chunked transfer encoding
"chunked".freeze
- CHUNKED_END =
End of a chunked transfer
"#{ZERO}#{CRLF}#{CRLF}".freeze
- VALID_BODY_TYPES =
Types valid to be used as body source
[String, NilClass, Enumerable]
Instance Method Summary collapse
-
#add_body_type_headers ⇒ Object
Adds the headers to the header array for the given request body we are working with.
-
#add_headers ⇒ Object
Adds headers to the request header from the headers array.
-
#connect_through_proxy ⇒ Object
Send headers needed to connect through proxy.
-
#initialize(socket, body, headers, headline) ⇒ Writer
constructor
A new instance of Writer.
-
#join_headers ⇒ Object
Joins the headers specified in the request into a correctly formatted http request header string.
- #send_request ⇒ Object
-
#stream ⇒ Object
Stream the request to a socket.
Constructor Details
#initialize(socket, body, headers, headline) ⇒ Writer
Returns a new instance of Writer.
21 22 23 24 25 26 27 28 |
# File 'lib/http/request/writer.rb', line 21 def initialize(socket, body, headers, headline) @body = body @socket = socket @headers = headers @request_header = [headline] validate_body_type! end |
Instance Method Details
#add_body_type_headers ⇒ Object
Adds the headers to the header array for the given request body we are working with
52 53 54 55 56 57 58 |
# File 'lib/http/request/writer.rb', line 52 def add_body_type_headers if @body.is_a?(String) && !@headers[Headers::CONTENT_LENGTH] @request_header << "#{Headers::CONTENT_LENGTH}: #{@body.bytesize}" elsif @body.is_a?(Enumerable) && CHUNKED != @headers[Headers::TRANSFER_ENCODING] fail(RequestError, "invalid transfer encoding") end end |
#add_headers ⇒ Object
Adds headers to the request header from the headers array
31 32 33 34 35 |
# File 'lib/http/request/writer.rb', line 31 def add_headers @headers.each do |field, value| @request_header << "#{field}: #{value}" end end |
#connect_through_proxy ⇒ Object
Send headers needed to connect through proxy
45 46 47 48 |
# File 'lib/http/request/writer.rb', line 45 def connect_through_proxy add_headers write(join_headers) end |
#join_headers ⇒ Object
Joins the headers specified in the request into a correctly formatted http request header string
62 63 64 65 66 |
# File 'lib/http/request/writer.rb', line 62 def join_headers # join the headers array with crlfs, stick two on the end because # that ends the request header @request_header.join(CRLF) + (CRLF) * 2 end |
#send_request ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/http/request/writer.rb', line 68 def send_request headers = join_headers # It's important to send the request in a single write call when # possible in order to play nicely with Nagle's algorithm. Making # two writes in a row triggers a pathological case where Nagle is # expecting a third write that never happens. case @body when NilClass write(headers) when String write(headers << @body) when Enumerable write(headers) @body.each do |chunk| write(chunk.bytesize.to_s(16) << CRLF << chunk << CRLF) end write(CHUNKED_END) else fail TypeError, "invalid body type: #{@body.class}" end end |
#stream ⇒ Object
Stream the request to a socket
38 39 40 41 42 |
# File 'lib/http/request/writer.rb', line 38 def stream add_headers add_body_type_headers send_request end |