Class: HTTP::Message::Headers

Inherits:
Object
  • Object
show all
Defined in:
lib/httpclient/http.rb

Overview

Represents HTTP message header.

Constant Summary collapse

STATUS_CODE_MAP =

HTTP response status code to reason phrase mapping definition.

{
  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',
}
CHARSET_MAP =

$KCODE to charset mapping definition.

{
  'NONE' => 'us-ascii',
  'EUC'  => 'euc-jp',
  'SJIS' => 'shift_jis',
  'UTF8' => 'utf-8',
}
NIL_URI =

Placeholder URI object for nil uri.

URI.parse('http://nil-uri-given/')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHeaders

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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# 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

Instance Attribute Details

#body_charsetObject

Used for dumping response.



122
123
124
# File 'lib/httpclient/http.rb', line 122

def body_charset
  @body_charset
end

#body_dateObject

Used for dumping response.



124
125
126
# File 'lib/httpclient/http.rb', line 124

def body_date
  @body_date
end

#body_sizeObject

Size of body. nil when size is unknown (e.g. chunked response).



101
102
103
# File 'lib/httpclient/http.rb', line 101

def body_size
  @body_size
end

#body_typeObject

Used for dumping response.



120
121
122
# File 'lib/httpclient/http.rb', line 120

def body_type
  @body_type
end

#chunkedObject

Request/Response is chunked or not.



103
104
105
# File 'lib/httpclient/http.rb', line 103

def chunked
  @chunked
end

#http_versionObject

HTTP version in a HTTP header. Float.



99
100
101
# File 'lib/httpclient/http.rb', line 99

def http_version
  @http_version
end

#reason_phraseObject

Response only. HTTP status reason phrase.



117
118
119
# File 'lib/httpclient/http.rb', line 117

def reason_phrase
  @reason_phrase
end

#request_methodObject (readonly)

Request only. Requested method.



106
107
108
# File 'lib/httpclient/http.rb', line 106

def request_method
  @request_method
end

#request_queryObject

Request only. Requested query.



110
111
112
# File 'lib/httpclient/http.rb', line 110

def request_query
  @request_query
end

#request_uriObject

Request only. Requested URI.



108
109
110
# File 'lib/httpclient/http.rb', line 108

def request_uri
  @request_uri
end

#request_via_proxyObject

Request only. Requested via proxy or not.



112
113
114
# File 'lib/httpclient/http.rb', line 112

def request_via_proxy
  @request_via_proxy
end

#status_codeObject

Response only. HTTP status



115
116
117
# File 'lib/httpclient/http.rb', line 115

def status_code
  @status_code
end

Instance Method Details

#[](key) ⇒ Object

Returns an Array of header values for the given key.



272
273
274
# File 'lib/httpclient/http.rb', line 272

def [](key)
  get(key).collect { |item| item[1] }
end

#[]=(key, value) ⇒ Object

Adds a header. See set.



267
268
269
# File 'lib/httpclient/http.rb', line 267

def []=(key, value)
  set(key, value)
end

#allObject

Returns an Array of all headers.



256
257
258
# File 'lib/httpclient/http.rb', line 256

def all
  @header_item
end

#contenttypeObject

Returns ‘Content-Type’ header value.



208
209
210
# File 'lib/httpclient/http.rb', line 208

def contenttype
  self['Content-Type'][0]
end

#contenttype=(contenttype) ⇒ Object

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



213
214
215
216
# File 'lib/httpclient/http.rb', line 213

def contenttype=(contenttype)
  delete('Content-Type')
  self['Content-Type'] = contenttype
end

#delete(key) ⇒ Object

Deletes headers of the given key.



261
262
263
264
# File 'lib/httpclient/http.rb', line 261

def delete(key)
  key = key.upcase
  @header_item.delete_if { |k, v| k.upcase == key }
end

#dumpObject

Dumps message header part and returns a dumped String.



225
226
227
228
229
230
231
232
233
234
235
236
# 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

#get(key = nil) ⇒ Object

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.



246
247
248
249
250
251
252
253
# File 'lib/httpclient/http.rb', line 246

def get(key = nil)
  if key.nil?
    all
  else
    key = key.upcase
    @header_item.find_all { |k, v| k.upcase == key }
  end
end

#init_connect_request(uri) ⇒ Object

Initialize this instance as a CONNECT request.



176
177
178
179
180
181
182
# 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

#init_request(method, uri, query = nil) ⇒ Object

Initialize this instance as a general request.



187
188
189
190
191
192
193
# 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

#init_response(status_code) ⇒ Object

Initialize this instance as a response.



196
197
198
199
# File 'lib/httpclient/http.rb', line 196

def init_response(status_code)
  @is_request = false
  self.status_code = status_code
end

#set(key, value) ⇒ Object

Adds a header. Addition order is preserved.



239
240
241
# File 'lib/httpclient/http.rb', line 239

def set(key, value)
  @header_item.push([key, value])
end