Parent

HTTP::Request::Writer

Constants

CRLF

CRLF is the universal HTTP delimiter

VALID_BODY_TYPES

Types valid to be used as body source

Public Class Methods

new(socket, body, headers, headerstart) click to toggle source
# File lib/http/request/writer.rb, line 10
def initialize(socket, body, headers, headerstart) # rubocop:disable ParameterLists
  @body           = body
  @socket         = socket
  @headers        = headers
  @request_header = [headerstart]

  validate_body_type!
end

Public Instance Methods

add_body_type_headers() click to toggle source

Adds the headers to the header array for the given request body we are working with

# File lib/http/request/writer.rb, line 34
def add_body_type_headers
  if @body.is_a?(String) && !@headers['Content-Length']
    @request_header << "Content-Length: #{@body.bytesize}"
  elsif @body.is_a?(Enumerable)
    encoding = @headers['Transfer-Encoding']
    if encoding == 'chunked'
      @request_header << 'Transfer-Encoding: chunked'
    else
      fail(RequestError, 'invalid transfer encoding')
    end
  end
end
add_headers() click to toggle source

Adds headers to the request header from the headers array

# File lib/http/request/writer.rb, line 20
def add_headers
  @headers.each do |field, value|
    @request_header << "#{field}: #{value}"
  end
end
join_headers() click to toggle source

Joins the headers specified in the request into a correctly formatted http request header string

# File lib/http/request/writer.rb, line 49
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() click to toggle source
# File lib/http/request/writer.rb, line 63
def send_request_body
  if @body.is_a?(String)
    @socket << @body
  elsif @body.is_a?(Enumerable)
    @body.each do |chunk|
      @socket << chunk.bytesize.to_s(16) << CRLF
      @socket << chunk << CRLF
    end

    @socket << '0' << CRLF * 2
  end
end
send_request_header() click to toggle source
# File lib/http/request/writer.rb, line 55
def send_request_header
  add_headers
  add_body_type_headers
  header = join_headers

  @socket << header
end
stream() click to toggle source

Stream the request to a socket

# File lib/http/request/writer.rb, line 27
def stream
  send_request_header
  send_request_body
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.