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
rubocop:disable ParameterLists.
-
#join_headers ⇒ Object
Joins the headers specified in the request into a correctly formatted http request header string.
- #send_request_body ⇒ Object
- #send_request_header ⇒ Object
-
#stream ⇒ Object
Stream the request to a socket.
Constructor Details
#initialize(socket, body, headers, headline) ⇒ Writer
rubocop:disable ParameterLists
21 22 23 24 25 26 27 28 |
# File 'lib/http/request/writer.rb', line 21 def initialize(socket, body, headers, headline) # rubocop:disable ParameterLists @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
51 52 53 54 55 56 57 |
# File 'lib/http/request/writer.rb', line 51 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
44 45 46 47 |
# File 'lib/http/request/writer.rb', line 44 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
61 62 63 64 65 |
# File 'lib/http/request/writer.rb', line 61 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_body ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/http/request/writer.rb', line 74 def send_request_body if @body.is_a?(String) write(@body) elsif @body.is_a?(Enumerable) @body.each do |chunk| write(chunk.bytesize.to_s(16) << CRLF) write(chunk << CRLF) end write(CHUNKED_END) end end |
#send_request_header ⇒ Object
67 68 69 70 71 72 |
# File 'lib/http/request/writer.rb', line 67 def send_request_header add_headers add_body_type_headers write(join_headers) end |
#stream ⇒ Object
Stream the request to a socket
38 39 40 41 |
# File 'lib/http/request/writer.rb', line 38 def stream send_request_header send_request_body end |