1# = uri/http.rb 2# 3# Author:: Akira Yamada <akira@ruby-lang.org> 4# License:: You can redistribute it and/or modify it under the same term as Ruby. 5# Revision:: $Id: http.rb 37472 2012-11-05 01:19:09Z zzak $ 6# 7# See URI for general documentation 8# 9 10require 'uri/generic' 11 12module URI 13 14 # 15 # The syntax of HTTP URIs is defined in RFC1738 section 3.3. 16 # 17 # Note that the Ruby URI library allows HTTP URLs containing usernames and 18 # passwords. This is not legal as per the RFC, but used to be 19 # supported in Internet Explorer 5 and 6, before the MS04-004 security 20 # update. See <URL:http://support.microsoft.com/kb/834489>. 21 # 22 class HTTP < Generic 23 # A Default port of 80 for URI::HTTP 24 DEFAULT_PORT = 80 25 26 # An Array of the available components for URI::HTTP 27 COMPONENT = [ 28 :scheme, 29 :userinfo, :host, :port, 30 :path, 31 :query, 32 :fragment 33 ].freeze 34 35 # 36 # == Description 37 # 38 # Create a new URI::HTTP object from components, with syntax checking. 39 # 40 # The components accepted are userinfo, host, port, path, query and 41 # fragment. 42 # 43 # The components should be provided either as an Array, or as a Hash 44 # with keys formed by preceding the component names with a colon. 45 # 46 # If an Array is used, the components must be passed in the order 47 # [userinfo, host, port, path, query, fragment]. 48 # 49 # Example: 50 # 51 # newuri = URI::HTTP.build({:host => 'www.example.com', 52 # :path => '/foo/bar'}) 53 # 54 # newuri = URI::HTTP.build([nil, "www.example.com", nil, "/path", 55 # "query", 'fragment']) 56 # 57 # Currently, if passed userinfo components this method generates 58 # invalid HTTP URIs as per RFC 1738. 59 # 60 def self.build(args) 61 tmp = Util::make_components_hash(self, args) 62 return super(tmp) 63 end 64 65 # 66 # == Description 67 # 68 # Create a new URI::HTTP object from generic URI components as per 69 # RFC 2396. No HTTP-specific syntax checking (as per RFC 1738) is 70 # performed. 71 # 72 # Arguments are +scheme+, +userinfo+, +host+, +port+, +registry+, +path+, 73 # +opaque+, +query+ and +fragment+, in that order. 74 # 75 # Example: 76 # 77 # uri = URI::HTTP.new('http', nil, "www.example.com", nil, "/path", 78 # "query", 'fragment') 79 # 80 # 81 # See also URI::Generic.new 82 # 83 def initialize(*arg) 84 super(*arg) 85 end 86 87 # 88 # == Description 89 # 90 # Returns the full path for an HTTP request, as required by Net::HTTP::Get. 91 # 92 # If the URI contains a query, the full path is URI#path + '?' + URI#query. 93 # Otherwise, the path is simply URI#path. 94 # 95 def request_uri 96 r = path_query 97 if r && r[0] != ?/ 98 r = '/' + r 99 end 100 101 r 102 end 103 end 104 105 @@schemes['HTTP'] = HTTP 106end 107