Object
Represents HTTP message header.
HTTP response status code to reason phrase mapping definition.
$KCODE to charset mapping definition.
Placeholder URI object for nil uri.
Creates a Message::Headers. Use init_request, init_response, or init_connect_request for acutual initialize.
# File lib/httpclient/http.rb, line 153
153: def initialize
154: @http_version = 1.1
155: @body_size = nil
156: @chunked = false
157:
158: @request_method = nil
159: @request_uri = nil
160: @request_query = nil
161: @request_via_proxy = nil
162:
163: @status_code = nil
164: @reason_phrase = nil
165:
166: @body_type = nil
167: @body_charset = nil
168: @body_date = nil
169:
170: @is_request = nil
171: @header_item = []
172: @dumped = false
173: end
Returns an Array of header values for the given key.
# File lib/httpclient/http.rb, line 284
284: def [](key)
285: get(key).collect { |item| item[1] }
286: end
Adds a header. See set.
# File lib/httpclient/http.rb, line 279
279: def []=(key, value)
280: set(key, value)
281: end
Adds a header. Addition order is preserved.
# File lib/httpclient/http.rb, line 239
239: def add(key, value)
240: if value.is_a?(Array)
241: value.each do |v|
242: @header_item.push([key, v])
243: end
244: else
245: @header_item.push([key, value])
246: end
247: end
Returns an Array of all headers.
# File lib/httpclient/http.rb, line 268
268: def all
269: @header_item
270: end
Sets byte size of message body. body_size == nil means that the body is_a? IO
# File lib/httpclient/http.rb, line 220
220: def body_size=(body_size)
221: @body_size = body_size
222: end
Returns ‘Content-Type’ header value.
# File lib/httpclient/http.rb, line 208
208: def contenttype
209: self['Content-Type'][0]
210: end
Sets ‘Content-Type’ header value. Overrides if already exists.
# File lib/httpclient/http.rb, line 213
213: def contenttype=(contenttype)
214: delete('Content-Type')
215: self['Content-Type'] = contenttype
216: end
Deletes headers of the given key.
# File lib/httpclient/http.rb, line 273
273: def delete(key)
274: key = key.upcase
275: @header_item.delete_if { |k, v| k.upcase == key }
276: end
Dumps message header part and returns a dumped String.
# File lib/httpclient/http.rb, line 225
225: def dump
226: set_header
227: str = nil
228: if @is_request
229: str = request_line
230: else
231: str = response_status_line
232: end
233: str + @header_item.collect { |key, value|
234: "#{ key }: #{ value }#{ CRLF }"
235: }.join
236: 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.
# File lib/httpclient/http.rb, line 258
258: def get(key = nil)
259: if key.nil?
260: all
261: else
262: key = key.upcase
263: @header_item.find_all { |k, v| k.upcase == key }
264: end
265: end
Initialize this instance as a CONNECT request.
# File lib/httpclient/http.rb, line 176
176: def init_connect_request(uri)
177: @is_request = true
178: @request_method = 'CONNECT'
179: @request_uri = uri
180: @request_query = nil
181: @http_version = 1.0
182: end
Initialize this instance as a general request.
# File lib/httpclient/http.rb, line 187
187: def init_request(method, uri, query = nil)
188: @is_request = true
189: @request_method = method
190: @request_uri = uri || NIL_URI
191: @request_query = query
192: @request_via_proxy = false
193: end
Initialize this instance as a response.
# File lib/httpclient/http.rb, line 196
196: def init_response(status_code)
197: @is_request = false
198: self.status_code = status_code
199: end
# File lib/httpclient/http.rb, line 358
358: def charset_label(charset)
359: CHARSET_MAP[charset] || 'us-ascii'
360: end
# File lib/httpclient/http.rb, line 362
362: def create_query_uri(uri, query)
363: if @request_method == 'CONNECT'
364: return "#{uri.host}:#{uri.port}"
365: end
366: path = uri.path
367: path = '/' if path.nil? or path.empty?
368: query_str = nil
369: if uri.query
370: query_str = uri.query
371: end
372: if query
373: if query_str
374: query_str += "&#{Message.create_query_part_str(query)}"
375: else
376: query_str = Message.create_query_part_str(query)
377: end
378: end
379: if query_str
380: path += "?#{query_str}"
381: end
382: path
383: end
# File lib/httpclient/http.rb, line 290
290: def request_line
291: path = create_query_uri(@request_uri, @request_query)
292: if @request_via_proxy
293: path = "#{ @request_uri.scheme }://#{ @request_uri.host }:#{ @request_uri.port }#{ path }"
294: end
295: "#{ @request_method } #{ path } HTTP/#{ @http_version }#{ CRLF }"
296: end
# File lib/httpclient/http.rb, line 298
298: def response_status_line
299: if defined?(Apache)
300: "HTTP/#{ @http_version } #{ @status_code } #{ @reason_phrase }#{ CRLF }"
301: else
302: "Status: #{ @status_code } #{ @reason_phrase }#{ CRLF }"
303: end
304: end
# File lib/httpclient/http.rb, line 306
306: def set_header
307: if @is_request
308: set_request_header
309: else
310: set_response_header
311: end
312: end
# File lib/httpclient/http.rb, line 314
314: def set_request_header
315: return if @dumped
316: @dumped = true
317: keep_alive = Message.keep_alive_enabled?(@http_version)
318: if !keep_alive and @request_method != 'CONNECT'
319: set('Connection', 'close')
320: end
321: if @chunked
322: set('Transfer-Encoding', 'chunked')
323: elsif @body_size and (keep_alive or @body_size != 0)
324: set('Content-Length', @body_size.to_s)
325: end
326: if @http_version >= 1.1
327: if @request_uri.port == @request_uri.default_port
328: # GFE/1.3 dislikes default port number (returns 404)
329: set('Host', "#{@request_uri.host}")
330: else
331: set('Host', "#{@request_uri.host}:#{@request_uri.port}")
332: end
333: end
334: end
# File lib/httpclient/http.rb, line 336
336: def set_response_header
337: return if @dumped
338: @dumped = true
339: if defined?(Apache) && self['Date'].empty?
340: set('Date', Time.now.httpdate)
341: end
342: keep_alive = Message.keep_alive_enabled?(@http_version)
343: if @chunked
344: set('Transfer-Encoding', 'chunked')
345: else
346: if keep_alive or @body_size != 0
347: set('Content-Length', @body_size.to_s)
348: end
349: end
350: if @body_date
351: set('Last-Modified', @body_date.httpdate)
352: end
353: if self['Content-Type'].empty?
354: set('Content-Type', "#{ @body_type || 'text/html' }; charset=#{ charset_label(@body_charset || $KCODE) }")
355: end
356: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.