Class: URI::Generic
- Inherits:
-
Object
- Object
- URI::Generic
- Includes:
- URI
- Defined in:
- lib/uri/generic.rb,
lib/open-uri.rb
Overview
Base class for all URI classes. Implements generic URI syntax as per RFC 2396.
Constant Summary
- DEFAULT_PORT =
nil- COMPONENT =
[ :scheme, :userinfo, :host, :port, :registry, :path, :opaque, :query, :fragment ].freeze
- USE_REGISTRY =
false- @@to_s =
Kernel.instance_method(:to_s)
Constants included from URI
DEFAULT_PARSER, HTML5ASCIIINCOMPAT, TBLDECWWWCOMP_, TBLENCWWWCOMP_, VERSION, VERSION_CODE, WFKV_
Instance Attribute Summary (collapse)
-
- (Object) fragment
Returns the value of attribute fragment.
-
- (Object) host
Returns the value of attribute host.
-
- (Object) opaque
Returns the value of attribute opaque.
-
- (Object) path
Returns the value of attribute path.
-
- (Object) port
Returns the value of attribute port.
-
- (Object) query
Returns the value of attribute query.
-
- (Object) registry
Returns the value of attribute registry.
-
- (Object) scheme
Returns the value of attribute scheme.
Class Method Summary (collapse)
-
+ (Object) build(args)
Synopsis.
-
+ (Object) build2(args)
Synopsis.
-
+ (Object) component
Components of the URI in the order.
-
+ (Object) default_port
Returns default port.
-
+ (Object) use_registry
DOC: FIXME!.
Instance Method Summary (collapse)
-
- (Object) ==(oth)
Compares to URI's.
-
- (Boolean) absolute?
(also: #absolute)
Checks if URI is an absolute one.
- - (Object) coerce(oth)
- - (Object) component
- - (Object) default_port
- - (Boolean) eql?(oth)
-
- (Object) find_proxy
returns a proxy URI.
- - (Object) hash
-
- (Boolean) hierarchical?
Checks if URI has a path.
-
- (Generic) initialize(scheme, userinfo, host, port, registry, path, opaque, query, fragment, parser = DEFAULT_PARSER, arg_check = false)
constructor
Args.
- - (Object) inspect
-
- (Object) merge(oth)
(also: #+)
Args.
-
- (Object) merge!(oth)
Args.
-
- (Object) normalize
Returns normalized URI.
-
- (Object) normalize!
Destructive version of #normalize.
- - (Object) parser
- - (Object) password
- - (Object) password=(password)
-
- (Boolean) relative?
Checks if URI is relative.
-
- (Object) route_from(oth)
(also: #-)
Args.
-
- (Object) route_to(oth)
Args.
-
- (Object) select(*components)
Args.
-
- (Object) to_s
Constructs String from URI.
- - (Object) user
- - (Object) user=(user)
- - (Object) userinfo
-
- (Object) userinfo=(userinfo)
Sets userinfo, argument is string like 'name:pass'.
Methods included from URI
decode_www_form, decode_www_form_component, encode_www_form, encode_www_form_component, extract, join, parse, regexp, scheme_list, split
Methods included from Escape
Constructor Details
- (Generic) initialize(scheme, userinfo, host, port, registry, path, opaque, query, fragment, parser = DEFAULT_PARSER, arg_check = false)
Args
scheme |
Protocol scheme, i.e. 'http','ftp','mailto' and so on. |
userinfo |
User name and password, i.e. 'sdmitry:bla' |
host |
Server host name |
port |
Server port |
registry |
DOC: FIXME! |
path |
Path on server |
opaque |
DOC: FIXME! |
query |
Query data |
fragment |
A part of URI after '#' sign |
parser |
Parser for internal use [URI::DEFAULT_PARSER by default] |
arg_check |
Check arguments [false by default] |
Description
Creates a new URI::Generic instance from "generic'' components without check.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/uri/generic.rb', line 158 def initialize(scheme, userinfo, host, port, registry, path, opaque, query, fragment, parser = DEFAULT_PARSER, arg_check = false) @scheme = nil @user = nil @password = nil @host = nil @port = nil @path = nil @query = nil @opaque = nil @registry = nil @fragment = nil @parser = parser == DEFAULT_PARSER ? nil : parser if arg_check self.scheme = scheme self.userinfo = userinfo self.host = host self.port = port self.path = path self.query = query self.opaque = opaque self.registry = registry self.fragment = fragment else self.set_scheme(scheme) self.set_userinfo(userinfo) self.set_host(host) self.set_port(port) self.set_path(path) self.set_query(query) self.set_opaque(opaque) self.set_registry(registry) self.set_fragment(fragment) end if @registry && !self.class.use_registry raise InvalidURIError, "the scheme #{@scheme} does not accept registry part: #{@registry} (or bad hostname?)" end @scheme.freeze if @scheme self.set_path('') if !@path && !@opaque # (see RFC2396 Section 5.2) self.set_port(self.default_port) if self.default_port && !@port end |
Instance Attribute Details
- (Object) fragment
Returns the value of attribute fragment
214 215 216 |
# File 'lib/uri/generic.rb', line 214 def fragment @fragment end |
- (Object) host
Returns the value of attribute host
208 209 210 |
# File 'lib/uri/generic.rb', line 208 def host @host end |
- (Object) opaque
Returns the value of attribute opaque
213 214 215 |
# File 'lib/uri/generic.rb', line 213 def opaque @opaque end |
- (Object) path
Returns the value of attribute path
211 212 213 |
# File 'lib/uri/generic.rb', line 211 def path @path end |
- (Object) port
Returns the value of attribute port
209 210 211 |
# File 'lib/uri/generic.rb', line 209 def port @port end |
- (Object) query
Returns the value of attribute query
212 213 214 |
# File 'lib/uri/generic.rb', line 212 def query @query end |
- (Object) registry
Returns the value of attribute registry
210 211 212 |
# File 'lib/uri/generic.rb', line 210 def registry @registry end |
- (Object) scheme
Returns the value of attribute scheme
207 208 209 |
# File 'lib/uri/generic.rb', line 207 def scheme @scheme end |
Class Method Details
+ (Object) build(args)
Synopsis
See #new
Description
Creates a new URI::Generic instance from components of URI::Generic with check. Components are: scheme, userinfo, host, port, registry, path, opaque, query and fragment. You can provide arguments either by an Array or a Hash. See #new for hash keys to use or for order of array items.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/uri/generic.rb', line 107 def self.build(args) if args.kind_of?(Array) && args.size == ::URI::Generic::COMPONENT.size tmp = args elsif args.kind_of?(Hash) tmp = ::URI::Generic::COMPONENT.collect do |c| if args.include?(c) args[c] else nil end end else raise ArgumentError, "expected Array of or Hash of components of #{self.class} (#{self.class.component.join(', ')})" end tmp << nil tmp << true return self.new(*tmp) end |
+ (Object) build2(args)
Synopsis
See #new
Description
At first, tries to create a new URI::Generic instance using URI::Generic::build. But, if exception URI::InvalidComponentError is raised, then it URI::Escape.escape all URI components and tries again.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/uri/generic.rb', line 69 def self.build2(args) begin return self.build(args) rescue InvalidComponentError if args.kind_of?(Array) return self.build(args.collect{|x| if x parser.escape(x) else x end }) elsif args.kind_of?(Hash) tmp = {} args.each do |key, value| tmp[key] = if value parser.escape(value) else value end end return self.build(tmp) end end end |
+ (Object) component
Components of the URI in the order.
44 45 46 |
# File 'lib/uri/generic.rb', line 44 def self.component self::COMPONENT end |
+ (Object) default_port
Returns default port
25 26 27 |
# File 'lib/uri/generic.rb', line 25 def self.default_port self::DEFAULT_PORT end |
+ (Object) use_registry
DOC: FIXME!
53 54 55 |
# File 'lib/uri/generic.rb', line 53 def self.use_registry self::USE_REGISTRY end |
Instance Method Details
- (Object) ==(oth)
Compares to URI's
1061 1062 1063 1064 1065 1066 1067 |
# File 'lib/uri/generic.rb', line 1061 def ==(oth) if self.class == oth.class self.normalize.component_ary == oth.normalize.component_ary else false end end |
- (Boolean) absolute? Also known as: absolute
Checks if URI is an absolute one
611 612 613 614 615 616 617 |
# File 'lib/uri/generic.rb', line 611 def absolute? if @scheme true else false end end |
- (Object) coerce(oth)
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 |
# File 'lib/uri/generic.rb', line 1130 def coerce(oth) case oth when String oth = parser.parse(oth) else super end return oth, self end |
- (Object) component
236 237 238 |
# File 'lib/uri/generic.rb', line 236 def component self.class.component end |
- (Object) default_port
29 30 31 |
# File 'lib/uri/generic.rb', line 29 def default_port self.class.default_port end |
- (Boolean) eql?(oth)
1073 1074 1075 1076 1077 |
# File 'lib/uri/generic.rb', line 1073 def eql?(oth) self.class == oth.class && parser == oth.parser && self.component_ary.eql?(oth.component_ary) end |
- (Object) find_proxy
returns a proxy URI. The proxy URI is obtained from environment variables such as http_proxy, ftp_proxy, no_proxy, etc. If there is no proper proxy, nil is returned.
Note that capitalized variables (HTTP_PROXY, FTP_PROXY, NO_PROXY, etc.) are examined too.
But http_proxy and HTTP_PROXY is treated specially under CGI environment. It's because HTTP_PROXY may be set by Proxy: header. So HTTP_PROXY is not used. http_proxy is not used too if the variable is case insensitive. CGI_HTTP_PROXY can be used instead.
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 |
# File 'lib/open-uri.rb', line 703 def find_proxy name = self.scheme.downcase + '_proxy' proxy_uri = nil if name == 'http_proxy' && ENV.include?('REQUEST_METHOD') # CGI? # HTTP_PROXY conflicts with *_proxy for proxy settings and # HTTP_* for header information in CGI. # So it should be careful to use it. pairs = ENV.reject {|k, v| /\Ahttp_proxy\z/i !~ k } case pairs.length when 0 # no proxy setting anyway. proxy_uri = nil when 1 k, v = pairs.shift if k == 'http_proxy' && ENV[k.upcase] == nil # http_proxy is safe to use because ENV is case sensitive. proxy_uri = ENV[name] else proxy_uri = nil end else # http_proxy is safe to use because ENV is case sensitive. proxy_uri = ENV.to_hash[name] end if !proxy_uri # Use CGI_HTTP_PROXY. cf. libwww-perl. proxy_uri = ENV["CGI_#{name.upcase}"] end elsif name == 'http_proxy' unless proxy_uri = ENV[name] if proxy_uri = ENV[name.upcase] warn 'The environment variable HTTP_PROXY is discouraged. Use http_proxy.' end end else proxy_uri = ENV[name] || ENV[name.upcase] end if proxy_uri && self.host require 'socket' begin addr = IPSocket.getaddress(self.host) proxy_uri = nil if /\A127\.|\A::1\z/ =~ addr rescue SocketError end end if proxy_uri proxy_uri = URI.parse(proxy_uri) name = 'no_proxy' if no_proxy = ENV[name] || ENV[name.upcase] no_proxy.scan(/([^:,]*)(?::(\d+))?/) {|host, port| if /(\A|\.)#{Regexp.quote host}\z/i =~ self.host && (!port || self.port == port.to_i) proxy_uri = nil break end } end proxy_uri else nil end end |
- (Object) hash
1069 1070 1071 |
# File 'lib/uri/generic.rb', line 1069 def hash self.component_ary.hash end |
- (Boolean) hierarchical?
Checks if URI has a path
600 601 602 603 604 605 606 |
# File 'lib/uri/generic.rb', line 600 def hierarchical? if @path true else false end end |
- (Object) inspect
1126 1127 1128 |
# File 'lib/uri/generic.rb', line 1126 def inspect @@to_s.bind(self).call.sub!(/>\z/) {" URL:#{self}>"} end |
- (Object) merge(oth) Also known as: +
Args
oth |
URI or String |
Description
Merges two URI's.
Usage
require 'uri'
uri = URI.parse("http://my.example.com")
p uri.merge("/main.rbx?page=1")
# => #<URI::HTTP:0x2021f3b0 URL:http://my.example.com/main.rbx?page=1>
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 |
# File 'lib/uri/generic.rb', line 742 def merge(oth) begin base, rel = merge0(oth) rescue raise $!.class, $!. end if base == rel return base end = rel.userinfo || rel.host || rel.port # RFC2396, Section 5.2, 2) if (rel.path.nil? || rel.path.empty?) && ! && !rel.query base.set_fragment(rel.fragment) if rel.fragment return base end base.set_query(nil) base.set_fragment(nil) # RFC2396, Section 5.2, 4) if ! base.set_path(merge_path(base.path, rel.path)) if base.path && rel.path else # RFC2396, Section 5.2, 4) base.set_path(rel.path) if rel.path end # RFC2396, Section 5.2, 7) base.set_userinfo(rel.userinfo) if rel.userinfo base.set_host(rel.host) if rel.host base.set_port(rel.port) if rel.port base.set_query(rel.query) if rel.query base.set_fragment(rel.fragment) if rel.fragment return base end |
- (Object) merge!(oth)
Args
oth |
URI or String |
Description
Destructive form of #merge
Usage
require 'uri'
uri = URI.parse("http://my.example.com")
uri.merge!("/main.rbx?page=1")
p uri
# => #<URI::HTTP:0x2021f3b0 URL:http://my.example.com/main.rbx?page=1>
714 715 716 717 718 719 720 721 722 |
# File 'lib/uri/generic.rb', line 714 def merge!(oth) t = merge(oth) if self == t nil else replace!(t) self end end |
- (Object) normalize
Returns normalized URI
984 985 986 987 988 |
# File 'lib/uri/generic.rb', line 984 def normalize uri = dup uri.normalize! uri end |
- (Object) normalize!
Destructive version of #normalize
993 994 995 996 997 998 999 1000 1001 1002 1003 |
# File 'lib/uri/generic.rb', line 993 def normalize! if path && path == '' set_path('/') end if scheme && scheme != scheme.downcase set_scheme(self.scheme.downcase) end if host && host != host.downcase set_host(self.host.downcase) end end |
- (Object) parser
216 217 218 219 220 221 222 |
# File 'lib/uri/generic.rb', line 216 def parser if !defined?(@parser) || !@parser DEFAULT_PARSER else @parser || DEFAULT_PARSER end end |
- (Object) password
384 385 386 |
# File 'lib/uri/generic.rb', line 384 def password @password end |
- (Object) password=(password)
328 329 330 331 332 |
# File 'lib/uri/generic.rb', line 328 def password=(password) check_password(password) set_password(password) # returns password end |
- (Boolean) relative?
Checks if URI is relative
623 624 625 |
# File 'lib/uri/generic.rb', line 623 def relative? !absolute? end |
- (Object) route_from(oth) Also known as: -
Args
oth |
URI or String |
Description
Calculates relative path from oth to self
Usage
require 'uri'
uri = URI.parse('http://my.example.com/main.rbx?page=1')
p uri.route_from('http://my.example.com')
#=> #<URI::Generic:0x20218858 URL:/main.rbx?page=1>
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 |
# File 'lib/uri/generic.rb', line 928 def route_from(oth) # you can modify `rel', but can not `oth'. begin oth, rel = route_from0(oth) rescue raise $!.class, $!. end if oth == rel return rel end rel.set_path(route_from_path(oth.path, self.path)) if rel.path == './' && self.query # "./?foo" -> "?foo" rel.set_path('') end return rel end |
- (Object) route_to(oth)
Args
oth |
URI or String |
Description
Calculates relative path to oth from self
Usage
require 'uri'
uri = URI.parse('http://my.example.com')
p uri.route_to('http://my.example.com/main.rbx?page=1')
#=> #<URI::Generic:0x2020c2f6 URL:/main.rbx?page=1>
968 969 970 971 972 973 974 975 976 977 978 979 |
# File 'lib/uri/generic.rb', line 968 def route_to(oth) case oth when Generic when String oth = parser.parse(oth) else raise ArgumentError, "bad argument(expected URI object or URI string)" end oth.route_from(self) end |
- (Object) select(*components)
Args
components |
Multiple Symbol arguments defined in URI::HTTP |
Description
Selects specified components from URI
Usage
require 'uri'
uri = URI.parse('http://myuser:mypass@my.example.com/test.rbx')
p uri.select(:userinfo, :host, :path)
# => ["myuser:mypass", "my.example.com", "/test.rbx"]
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 |
# File 'lib/uri/generic.rb', line 1114 def select(*components) components.collect do |c| if component.include?(c) self.send(c) else raise ArgumentError, "expected of components of #{self.class} (#{self.class.component.join(', ')})" end end end |
- (Object) to_s
Constructs String from URI
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 |
# File 'lib/uri/generic.rb', line 1017 def to_s str = '' if @scheme str << @scheme str << ':' end if @opaque str << @opaque else if @registry str << @registry else if @host str << '//' end if self.userinfo str << self.userinfo str << '@' end if @host str << @host end if @port && @port != self.default_port str << ':' str << @port.to_s end end str << path_query end if @fragment str << '#' str << @fragment end str end |
- (Object) user
380 381 382 |
# File 'lib/uri/generic.rb', line 380 def user @user end |
- (Object) user=(user)
322 323 324 325 326 |
# File 'lib/uri/generic.rb', line 322 def user=(user) check_user(user) set_user(user) # returns user end |
- (Object) userinfo
370 371 372 373 374 375 376 377 378 |
# File 'lib/uri/generic.rb', line 370 def userinfo if @user.nil? nil elsif @password.nil? @user else @user + ':' + @password end end |
- (Object) userinfo=(userinfo)
Sets userinfo, argument is string like 'name:pass'
313 314 315 316 317 318 319 320 |
# File 'lib/uri/generic.rb', line 313 def userinfo=(userinfo) if userinfo.nil? return nil end check_userinfo(*userinfo) set_userinfo(*userinfo) # returns userinfo end |