Class: HTTP::Message::Headers
- Inherits:
-
Object
- Object
- HTTP::Message::Headers
- 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.
HTTPClient::Util.urify('http://nil-uri-given/')
Instance Attribute Summary collapse
-
#body_charset ⇒ Object
Used for dumping response.
-
#body_date ⇒ Object
Used for dumping response.
-
#body_encoding ⇒ Object
readonly
Used for keeping content encoding.
-
#body_size ⇒ Object
Size of body.
-
#body_type ⇒ Object
Used for dumping response.
-
#chunked ⇒ Object
Request/Response is chunked or not.
-
#http_version ⇒ Object
HTTP version in a HTTP header.
-
#reason_phrase ⇒ Object
Response only.
-
#request_absolute_uri ⇒ Object
Request only.
-
#request_method ⇒ Object
readonly
Request only.
-
#request_query ⇒ Object
Request only.
-
#request_uri ⇒ Object
Request only.
-
#status_code ⇒ Object
Response only.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns an Array of header values for the given key.
-
#[]=(key, value) ⇒ Object
Adds a header.
-
#add(key, value) ⇒ Object
Adds a header.
-
#all ⇒ Object
Returns an Array of all headers.
-
#content_type ⇒ Object
(also: #contenttype)
Returns ‘Content-Type’ header value.
-
#content_type=(content_type) ⇒ Object
(also: #contenttype=)
Sets ‘Content-Type’ header value.
- #create_query_part ⇒ Object
- #create_query_uri ⇒ Object
-
#delete(key) ⇒ Object
Deletes headers of the given key.
-
#dump ⇒ Object
Dumps message header part and returns a dumped String.
-
#get(key = nil) ⇒ Object
Returns an Array of headers for the given key.
-
#init_connect_request(uri) ⇒ Object
Initialize this instance as a CONNECT request.
-
#init_request(method, uri, query = nil) ⇒ Object
Initialize this instance as a general request.
-
#init_response(status_code, req = nil) ⇒ Object
Initialize this instance as a response.
-
#initialize ⇒ Headers
constructor
Creates a Message::Headers.
-
#set(key, value) ⇒ Object
Sets a header.
- #set_body_encoding ⇒ Object
-
#set_date_header ⇒ Object
Set Date header.
- #set_headers(headers) ⇒ Object
Constructor Details
#initialize ⇒ Headers
Creates a Message::Headers. Use init_request, init_response, or init_connect_request for acutual initialize.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/httpclient/http.rb', line 160 def initialize @http_version = '1.1' @body_size = nil @chunked = false @request_method = nil @request_uri = nil @request_query = nil @request_absolute_uri = nil @status_code = nil @reason_phrase = nil @body_type = nil @body_charset = nil @body_date = nil @body_encoding = nil @is_request = nil @header_item = [] @dumped = false end |
Instance Attribute Details
#body_charset ⇒ Object
Used for dumping response.
127 128 129 |
# File 'lib/httpclient/http.rb', line 127 def body_charset @body_charset end |
#body_date ⇒ Object
Used for dumping response.
129 130 131 |
# File 'lib/httpclient/http.rb', line 129 def body_date @body_date end |
#body_encoding ⇒ Object (readonly)
Used for keeping content encoding.
131 132 133 |
# File 'lib/httpclient/http.rb', line 131 def body_encoding @body_encoding end |
#body_size ⇒ Object
Size of body. nil when size is unknown (e.g. chunked response).
106 107 108 |
# File 'lib/httpclient/http.rb', line 106 def body_size @body_size end |
#body_type ⇒ Object
Used for dumping response.
125 126 127 |
# File 'lib/httpclient/http.rb', line 125 def body_type @body_type end |
#chunked ⇒ Object
Request/Response is chunked or not.
108 109 110 |
# File 'lib/httpclient/http.rb', line 108 def chunked @chunked end |
#http_version ⇒ Object
HTTP version in a HTTP header. String.
104 105 106 |
# File 'lib/httpclient/http.rb', line 104 def http_version @http_version end |
#reason_phrase ⇒ Object
Response only. HTTP status reason phrase.
122 123 124 |
# File 'lib/httpclient/http.rb', line 122 def reason_phrase @reason_phrase end |
#request_absolute_uri ⇒ Object
Request only. Requested via proxy or not.
117 118 119 |
# File 'lib/httpclient/http.rb', line 117 def request_absolute_uri @request_absolute_uri end |
#request_method ⇒ Object (readonly)
Request only. Requested method.
111 112 113 |
# File 'lib/httpclient/http.rb', line 111 def request_method @request_method end |
#request_query ⇒ Object
Request only. Requested query.
115 116 117 |
# File 'lib/httpclient/http.rb', line 115 def request_query @request_query end |
#request_uri ⇒ Object
Request only. Requested URI.
113 114 115 |
# File 'lib/httpclient/http.rb', line 113 def request_uri @request_uri end |
#status_code ⇒ Object
Response only. HTTP status
120 121 122 |
# File 'lib/httpclient/http.rb', line 120 def status_code @status_code end |
Instance Method Details
#[](key) ⇒ Object
Returns an Array of header values for the given key.
319 320 321 |
# File 'lib/httpclient/http.rb', line 319 def [](key) get(key).collect { |item| item[1] } end |
#[]=(key, value) ⇒ Object
Adds a header. See set.
314 315 316 |
# File 'lib/httpclient/http.rb', line 314 def []=(key, value) set(key, value) end |
#add(key, value) ⇒ Object
Adds a header. Addition order is preserved.
274 275 276 277 278 279 280 281 282 |
# File 'lib/httpclient/http.rb', line 274 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 |
#all ⇒ Object
Returns an Array of all headers.
303 304 305 |
# File 'lib/httpclient/http.rb', line 303 def all @header_item end |
#content_type ⇒ Object Also known as: contenttype
Returns ‘Content-Type’ header value.
221 222 223 |
# File 'lib/httpclient/http.rb', line 221 def content_type self['Content-Type'][0] end |
#content_type=(content_type) ⇒ Object Also known as: contenttype=
Sets ‘Content-Type’ header value. Overrides if already exists.
226 227 228 229 |
# File 'lib/httpclient/http.rb', line 226 def content_type=(content_type) delete('Content-Type') self['Content-Type'] = content_type end |
#create_query_part ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/httpclient/http.rb', line 342 def create_query_part() query_str = nil if @request_uri.query query_str = @request_uri.query end if @request_query if query_str query_str += "&#{Message.create_query_part_str(@request_query)}" else query_str = Message.create_query_part_str(@request_query) end end query_str end |
#create_query_uri ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/httpclient/http.rb', line 330 def create_query_uri() if @request_method == 'CONNECT' return "#{@request_uri.host}:#{@request_uri.port}" end path = @request_uri.path path = '/' if path.nil? or path.empty? if query_str = create_query_part() path += "?#{query_str}" end path end |
#delete(key) ⇒ Object
Deletes headers of the given key.
308 309 310 311 |
# File 'lib/httpclient/http.rb', line 308 def delete(key) key = key.upcase @header_item.delete_if { |k, v| k.upcase == key } end |
#dump ⇒ Object
Dumps message header part and returns a dumped String.
255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/httpclient/http.rb', line 255 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.
293 294 295 296 297 298 299 300 |
# File 'lib/httpclient/http.rb', line 293 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.
184 185 186 187 188 189 190 |
# File 'lib/httpclient/http.rb', line 184 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.
195 196 197 198 199 200 201 |
# File 'lib/httpclient/http.rb', line 195 def init_request(method, uri, query = nil) @is_request = true @request_method = method @request_uri = uri || NIL_URI @request_query = query @request_absolute_uri = false end |
#init_response(status_code, req = nil) ⇒ Object
Initialize this instance as a response.
204 205 206 207 208 209 210 211 212 |
# File 'lib/httpclient/http.rb', line 204 def init_response(status_code, req = nil) @is_request = false self.status_code = status_code if req @request_method = req.request_method @request_uri = req.request_uri @request_query = req.request_query end end |
#set(key, value) ⇒ Object
Sets a header.
285 286 287 288 |
# File 'lib/httpclient/http.rb', line 285 def set(key, value) delete(key) add(key, value) end |
#set_body_encoding ⇒ Object
235 236 237 238 239 240 241 |
# File 'lib/httpclient/http.rb', line 235 def set_body_encoding if type = self.content_type OpenURI::Meta.init(o = '') o.('content-type', type) @body_encoding = o.encoding end end |
#set_date_header ⇒ Object
Set Date header
269 270 271 |
# File 'lib/httpclient/http.rb', line 269 def set_date_header set('Date', Time.now.httpdate) end |
#set_headers(headers) ⇒ Object
323 324 325 326 327 328 |
# File 'lib/httpclient/http.rb', line 323 def set_headers(headers) headers.each do |key, value| add(key, value) end set_body_encoding end |