Class HTTP::Message::Headers
In: lib/httpclient/http.rb
Parent: Object

Represents HTTP message header.

Methods

[]   []=   add   all   body_size=   contenttype   contenttype=   delete   dump   get   init_connect_request   init_request   init_response   new   set   status_code=  

Constants

STATUS_CODE_MAP = { Status::OK => 'OK', Status::CREATED => "Created", Status::NON_AUTHORITATIVE_INFORMATION => "Non-Authoritative Information", Status::NO_CONTENT => "No Content", Status::RESET_CONTENT => "Reset Content", Status::PARTIAL_CONTENT => "Partial Content", Status::MOVED_PERMANENTLY => 'Moved Permanently', Status::FOUND => 'Found', Status::SEE_OTHER => 'See Other', Status::TEMPORARY_REDIRECT => 'Temporary Redirect', Status::MOVED_TEMPORARILY => 'Temporary Redirect', Status::BAD_REQUEST => 'Bad Request', Status::INTERNAL => 'Internal Server Error', }   HTTP response status code to reason phrase mapping definition.
CHARSET_MAP = { 'NONE' => 'us-ascii', 'EUC' => 'euc-jp', 'SJIS' => 'shift_jis', 'UTF8' => 'utf-8', }   $KCODE to charset mapping definition.
NIL_URI = URI.parse('http://nil-uri-given/')   Placeholder URI object for nil uri.

Attributes

body_size  [R]  Size of body. nil when size is unknown (e.g. chunked response).
chunked  [RW]  Request/Response is chunked or not.
http_version  [RW]  HTTP version in a HTTP header. Float.
reason_phrase  [RW]  Response only. HTTP status reason phrase.
request_method  [R]  Request only. Requested method.
request_query  [RW]  Request only. Requested query.
request_uri  [RW]  Request only. Requested URI.
request_via_proxy  [RW]  Request only. Requested via proxy or not.
status_code  [R]  Response only. HTTP status

Public Class methods

Creates a Message::Headers. Use init_request, init_response, or init_connect_request for acutual initialize.

[Source]

# File lib/httpclient/http.rb, line 153
      def initialize
        @http_version = 1.1
        @body_size = nil
        @chunked = false

        @request_method = nil
        @request_uri = nil
        @request_query = nil
        @request_via_proxy = nil

        @status_code = nil
        @reason_phrase = nil

        @body_type = nil
        @body_charset = nil
        @body_date = nil

        @is_request = nil
        @header_item = []
        @dumped = false
      end

Public Instance methods

Returns an Array of header values for the given key.

[Source]

# File lib/httpclient/http.rb, line 284
      def [](key)
        get(key).collect { |item| item[1] }
      end

Adds a header. See set.

[Source]

# File lib/httpclient/http.rb, line 279
      def []=(key, value)
        set(key, value)
      end

Adds a header. Addition order is preserved.

[Source]

# File lib/httpclient/http.rb, line 239
      def add(key, value)
        if value.is_a?(Array)
          value.each do |v|
            @header_item.push([key, v])
          end
        else
          @header_item.push([key, value])
        end
      end

Returns an Array of all headers.

[Source]

# File lib/httpclient/http.rb, line 268
      def all
        @header_item
      end

Sets byte size of message body. body_size == nil means that the body is_a? IO

[Source]

# File lib/httpclient/http.rb, line 220
      def body_size=(body_size)
        @body_size = body_size
      end

Returns ‘Content-Type’ header value.

[Source]

# File lib/httpclient/http.rb, line 208
      def contenttype
        self['Content-Type'][0]
      end

Sets ‘Content-Type’ header value. Overrides if already exists.

[Source]

# File lib/httpclient/http.rb, line 213
      def contenttype=(contenttype)
        delete('Content-Type')
        self['Content-Type'] = contenttype
      end

Deletes headers of the given key.

[Source]

# File lib/httpclient/http.rb, line 273
      def delete(key)
        key = key.upcase
        @header_item.delete_if { |k, v| k.upcase == key }
      end

Dumps message header part and returns a dumped String.

[Source]

# File lib/httpclient/http.rb, line 225
      def dump
        set_header
        str = nil
        if @is_request
          str = request_line
        else
          str = response_status_line
        end
        str + @header_item.collect { |key, value|
          "#{ key }: #{ value }#{ CRLF }"
        }.join
      end

Returns an Array of headers for the given key. Each element is a pair of key and value. It returns an single element Array even if the only one header exists. If nil key given, it returns all headers.

[Source]

# File lib/httpclient/http.rb, line 258
      def get(key = nil)
        if key.nil?
          all
        else
          key = key.upcase
          @header_item.find_all { |k, v| k.upcase == key }
        end
      end

Initialize this instance as a CONNECT request.

[Source]

# File lib/httpclient/http.rb, line 176
      def init_connect_request(uri)
        @is_request = true
        @request_method = 'CONNECT'
        @request_uri = uri
        @request_query = nil
        @http_version = 1.0
      end

Initialize this instance as a general request.

[Source]

# File lib/httpclient/http.rb, line 187
      def init_request(method, uri, query = nil)
        @is_request = true
        @request_method = method
        @request_uri = uri || NIL_URI
        @request_query = query
        @request_via_proxy = false
      end

Initialize this instance as a response.

[Source]

# File lib/httpclient/http.rb, line 196
      def init_response(status_code)
        @is_request = false
        self.status_code = status_code
      end

Sets a header.

[Source]

# File lib/httpclient/http.rb, line 250
      def set(key, value)
        delete(key)
        add(key, value)
      end

Sets status code and reason phrase.

[Source]

# File lib/httpclient/http.rb, line 202
      def status_code=(status_code)
        @status_code = status_code
        @reason_phrase = STATUS_CODE_MAP[@status_code]
      end

[Validate]