Class: HTTP::Message::Headers

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

Overview

HTTP::Message::Headers – HTTP message header.

DESCRIPTION

A class that describes header part of HTTP message.

Constant Summary collapse

StatusCodeMap =
{
  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',
}
CharsetMap =
{
  'NONE' => 'us-ascii',
  'EUC'  => 'euc-jp',
  'SJIS' => 'shift_jis',
  'UTF8' => 'utf-8',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHeaders

SYNOPSIS

HTTP::Message.new

ARGS

N/A

DESCRIPTION

Create a instance of HTTP request or HTTP response.  Specify
status_code for HTTP response.


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/httpclient/http.rb', line 137

def initialize
  @is_request = nil	# true, false and nil
  @http_version = 'HTTP/1.1'
  @body_type = nil
  @body_charset = nil
  @body_size = nil
  @body_date = nil
  @header_item = []
  @chunked = false
  @response_status_code = nil
  @reason_phrase = nil
  @request_method = nil
  @request_uri = nil
  @request_query = nil
  @request_via_proxy = nil
end

Instance Attribute Details

#body_charsetObject

Charset.



90
91
92
# File 'lib/httpclient/http.rb', line 90

def body_charset
  @body_charset
end

#body_dateObject

A milestone of body.



94
95
96
# File 'lib/httpclient/http.rb', line 94

def body_date
  @body_date
end

#body_sizeObject

Size of body.



92
93
94
# File 'lib/httpclient/http.rb', line 92

def body_size
  @body_size
end

#body_typeObject

Content-type.



88
89
90
# File 'lib/httpclient/http.rb', line 88

def body_type
  @body_type
end

#chunkedObject (readonly)

Chunked or not.



96
97
98
# File 'lib/httpclient/http.rb', line 96

def chunked
  @chunked
end

#http_versionObject

HTTP version string in a HTTP header.



86
87
88
# File 'lib/httpclient/http.rb', line 86

def http_version
  @http_version
end

#reason_phraseObject

HTTP status reason phrase.



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

def reason_phrase
  @reason_phrase
end

#request_methodObject (readonly)

Request method.



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

def request_method
  @request_method
end

#request_uriObject (readonly)

Requested URI.



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

def request_uri
  @request_uri
end

#request_via_proxyObject

Returns the value of attribute request_via_proxy.



171
172
173
# File 'lib/httpclient/http.rb', line 171

def request_via_proxy
  @request_via_proxy
end

#response_status_codeObject

Returns the value of attribute response_status_code.



173
174
175
# File 'lib/httpclient/http.rb', line 173

def response_status_code
  @response_status_code
end

Instance Method Details

#[](key) ⇒ Object



233
234
235
# File 'lib/httpclient/http.rb', line 233

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

#[]=(key, value) ⇒ Object



229
230
231
# File 'lib/httpclient/http.rb', line 229

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

#contenttypeObject



179
180
181
# File 'lib/httpclient/http.rb', line 179

def contenttype
  self['content-type'][0]
end

#contenttype=(contenttype) ⇒ Object



183
184
185
# File 'lib/httpclient/http.rb', line 183

def contenttype=(contenttype)
  self['content-type'] = contenttype
end

#delete(key) ⇒ Object



224
225
226
227
# File 'lib/httpclient/http.rb', line 224

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

#dump(dev = '') ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/httpclient/http.rb', line 197

def dump(dev = '')
  set_header
  str = nil
  if @is_request
	str = request_line
  else
	str = response_status_line
  end
  str += @header_item.collect { |key, value|
	  dump_line("#{ key }: #{ value }")
	}.join
  dev << str
  dev
end

#get(key = nil) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/httpclient/http.rb', line 216

def get(key = nil)
  if !key
	@header_item
  else
	@header_item.find_all { |pair| pair[0].upcase == key.upcase }
  end
end

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



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/httpclient/http.rb', line 154

def init_request(method, uri, query = nil, via_proxy = nil)
  @is_request = true
  @request_method = method
  @request_uri = if uri.is_a?(URI)
	  uri
	else
	  URI.parse(uri.to_s)
	end
  @request_query = create_query_uri(@request_uri, query)
  @request_via_proxy = via_proxy
end

#init_response(status_code) ⇒ Object



166
167
168
169
# File 'lib/httpclient/http.rb', line 166

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

#set(key, value) ⇒ Object



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

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