Class: HTTP::Request::Writer

### Home Classes Methods

### In Files

- lib/http/request/writer.rb

### Parent

Object

### Methods

- ::new
- #add_body_type_headers
- #add_headers
- #connect_through_proxy
- #join_headers
- #send_request_body
- #send_request_header
- #stream

### Class/Module Index

Quicksearch

- HTTP
- HTTP::Cache
- HTTP::Cache::Headers
- HTTP::Cache::NullCache
- HTTP::Cache::NullLogger
- HTTP::CacheError
- HTTP::Chainable
- HTTP::Client
- HTTP::Connection
- HTTP::Error
- HTTP::Headers
- HTTP::Headers::Mixin
- HTTP::InvalidHeaderNameError
- HTTP::MimeType
- HTTP::MimeType::Adapter
- HTTP::MimeType::JSON
- HTTP::Options
- HTTP::Redirector
- HTTP::Redirector::EndlessRedirectError
- HTTP::Redirector::TooManyRedirectsError
- HTTP::Request
- HTTP::Request::Caching
- HTTP::Request::UnsupportedMethodError
- HTTP::Request::UnsupportedSchemeError
- HTTP::Request::Writer
- HTTP::RequestError
- HTTP::Response
- HTTP::Response::Body
- HTTP::Response::Caching
- HTTP::Response::IoBody
- HTTP::Response::Parser
- HTTP::Response::Status
- HTTP::Response::StringBody
- HTTP::ResponseError
- HTTP::StateError
- HTTP::Timeout
- HTTP::Timeout::Global
- HTTP::Timeout::Null
- HTTP::Timeout::PerOperation
- HTTP::TimeoutError
- HTTP::URI
- Object

No matching classes.

# HTTP::Request::Writer

### Constants

CHUNKED

  Chunked transfer encoding

CRLF

  CRLF is the universal HTTP delimiter

VALID_BODY_TYPES

  Types valid to be used as body source

ZERO

  Chunked data termintaor.

### Public Class Methods

new(socket, body, headers, headline) click to toggle source

    # File lib/http/request/writer.rb, line 18
    def initialize(socket, body, headers, headline) # rubocop:disable ParameterLists
      @body           = body
      @socket         = socket
      @headers        = headers
      @request_header = [headline]

      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 48
    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() click to toggle source

Adds headers to the request header from the headers array

    # File lib/http/request/writer.rb, line 28
    def add_headers
      @headers.each do |field, value|
        @request_header << "#{field}: #{value}"
      end
    end

connect_through_proxy() click to toggle source

Send headers needed to connect through proxy

    # File lib/http/request/writer.rb, line 41
    def connect_through_proxy
      add_headers
      @socket << join_headers
    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 58
    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 71
    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 << ZERO << CRLF << CRLF
      end
    end

send_request_header() click to toggle source

    # File lib/http/request/writer.rb, line 64
    def send_request_header
      add_headers
      add_body_type_headers

      @socket << join_headers
    end

stream() click to toggle source

Stream the request to a socket

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.