Class: Addressable::URI
- Inherits:
-
Object
- Object
- Addressable::URI
- Defined in:
- lib/addressable/uri.rb
Overview
This is an implementation of a URI parser based on <a href=“www.ietf.org/rfc/rfc3986.txt”>RFC 3986</a>, <a href=“www.ietf.org/rfc/rfc3987.txt”>RFC 3987</a>.
Defined Under Namespace
Modules: CharacterClasses Classes: InvalidURIError
Class Method Summary collapse
-
.convert_path(path) ⇒ Addressable::URI
Converts a path to a file scheme URI.
-
.encode(uri, returning = String) ⇒ String, Addressable::URI
(also: escape)
Percent encodes any special characters in the URI.
-
.heuristic_parse(uri, hints = {}) ⇒ Addressable::URI
Converts an input to a URI.
-
.ip_based_schemes ⇒ Object
Returns an array of known ip-based schemes.
-
.join(*uris) ⇒ Addressable::URI
Joins several URIs together.
-
.normalize_component(component, character_class = CharacterClasses::RESERVED + CharacterClasses::UNRESERVED) ⇒ String
Normalizes the encoding of a URI component.
-
.normalized_encode(uri, returning = String) ⇒ String, Addressable::URI
Normalizes the encoding of a URI.
-
.parse(uri) ⇒ Addressable::URI
Returns a URI object based on the parsed string.
-
.port_mapping ⇒ Object
Returns a hash of common IP-based schemes and their default port numbers.
-
.unencode(uri, returning = String) ⇒ String, Addressable::URI
(also: unescape, unencode_component, unescape_component)
Unencodes any percent encoded characters within a URI component.
Instance Method Summary collapse
-
#==(uri) ⇒ TrueClass, FalseClass
Returns
true
if the URI objects are equal. -
#===(uri) ⇒ TrueClass, FalseClass
Returns
true
if the URI objects are equal. -
#absolute? ⇒ TrueClass, FalseClass
Determines if the URI is absolute.
-
#authority ⇒ String
The authority component for this URI.
-
#authority=(new_authority) ⇒ Object
Sets the authority component for this URI.
-
#basename ⇒ String
The basename, if any, of the file in the path component.
-
#defer_validation(&block) ⇒ Object
This method allows you to make several changes to a URI simultaneously, which separately would cause validation errors, but in conjunction, are valid.
-
#display_uri ⇒ Addressable::URI
Creates a URI suitable for display to users.
-
#dup ⇒ Addressable::URI
Clones the URI object.
-
#eql?(uri) ⇒ TrueClass, FalseClass
Returns
true
if the URI objects are equal. -
#extname ⇒ String
The extname, if any, of the file in the path component.
-
#fragment ⇒ String
The fragment component for this URI.
-
#fragment=(new_fragment) ⇒ Object
Sets the fragment component for this URI.
-
#freeze ⇒ Addressable::URI
Freezes the URI object.
-
#frozen? ⇒ TrueClass, FalseClass
Determines if the URI is frozen.
-
#hash ⇒ Integer
A hash value that will make a URI equivalent to its normalized form.
-
#host ⇒ String
The host component for this URI.
-
#host=(new_host) ⇒ Object
Sets the host component for this URI.
-
#inferred_port ⇒ Integer
The inferred port component for this URI.
-
#initialize(options = {}) ⇒ Addressable::URI
constructor
Creates a new uri object from component parts.
-
#inspect ⇒ String
Returns a
String
representation of the URI object’s state. -
#ip_based? ⇒ TrueClass, FalseClass
Determines if the scheme indicates an IP-based protocol.
-
#join(uri) ⇒ Addressable::URI
(also: #+)
Joins two URIs together.
-
#join!(uri) ⇒ Addressable::URI
Destructive form of
join
. -
#merge(hash) ⇒ Addressable::URI
Merges a URI with a
Hash
of components. -
#merge!(uri) ⇒ Addressable::URI
Destructive form of
merge
. -
#normalize ⇒ Addressable::URI
Returns a normalized URI object.
-
#normalize! ⇒ Addressable::URI
Destructively normalizes this URI object.
-
#normalized_authority ⇒ String
The authority component for this URI, normalized.
-
#normalized_fragment ⇒ String
The fragment component for this URI, normalized.
-
#normalized_host ⇒ String
The host component for this URI, normalized.
-
#normalized_password ⇒ String
The password component for this URI, normalized.
-
#normalized_path ⇒ String
The path component for this URI, normalized.
-
#normalized_port ⇒ Integer
The port component for this URI, normalized.
-
#normalized_query ⇒ String
The query component for this URI, normalized.
-
#normalized_scheme ⇒ String
The scheme component for this URI, normalized.
-
#normalized_user ⇒ String
The user component for this URI, normalized.
-
#normalized_userinfo ⇒ String
The userinfo component for this URI, normalized.
-
#omit(*components) ⇒ Addressable::URI
Omits components from a URI.
-
#omit!(*components) ⇒ Addressable::URI
Destructive form of omit.
-
#password ⇒ String
The password component for this URI.
-
#password=(new_password) ⇒ Object
Sets the password component for this URI.
-
#path ⇒ String
The path component for this URI.
-
#path=(new_path) ⇒ Object
Sets the path component for this URI.
-
#port ⇒ Integer
The port component for this URI.
-
#port=(new_port) ⇒ Object
Sets the port component for this URI.
-
#query ⇒ String
The query component for this URI.
-
#query=(new_query) ⇒ Object
Sets the query component for this URI.
-
#query_values(options = {}) ⇒ Hash
Converts the query component to a Hash value.
-
#query_values=(new_query_values) ⇒ Object
Sets the query component for this URI from a Hash object.
-
#relative? ⇒ TrueClass, FalseClass
Determines if the URI is relative.
-
#request_uri ⇒ String
The HTTP request URI for this URI.
-
#request_uri=(new_request_uri) ⇒ Object
Sets the HTTP request URI for this URI.
-
#route_from(uri) ⇒ Addressable::URI
Returns the shortest normalized relative form of this URI that uses the supplied URI as a base for resolution.
-
#route_to(uri) ⇒ Addressable::URI
Returns the shortest normalized relative form of the supplied URI that uses this URI as a base for resolution.
-
#scheme ⇒ String
The scheme component for this URI.
-
#scheme=(new_scheme) ⇒ Object
Sets the scheme component for this URI.
-
#to_hash ⇒ Hash
Returns a Hash of the URI components.
-
#to_s ⇒ String
(also: #to_str)
Converts the URI to a
String
. -
#user ⇒ String
The user component for this URI.
-
#user=(new_user) ⇒ Object
Sets the user component for this URI.
-
#userinfo ⇒ String
The userinfo component for this URI.
-
#userinfo=(new_userinfo) ⇒ Object
Sets the userinfo component for this URI.
Constructor Details
#initialize(options = {}) ⇒ Addressable::URI
Creates a new uri object from component parts.
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
# File 'lib/addressable/uri.rb', line 580 def initialize(={}) if .has_key?(:authority) if (.keys & [:userinfo, :user, :password, :host, :port]).any? raise ArgumentError, "Cannot specify both an authority and any of the components " + "within the authority." end end if .has_key?(:userinfo) if (.keys & [:user, :password]).any? raise ArgumentError, "Cannot specify both a userinfo and either the user or password." end end self.defer_validation do # Bunch of crazy logic required because of the composite components # like userinfo and authority. self.scheme = [:scheme] if [:scheme] self.user = [:user] if [:user] self.password = [:password] if [:password] self.userinfo = [:userinfo] if [:userinfo] self.host = [:host] if [:host] self.port = [:port] if [:port] self. = [:authority] if [:authority] self.path = [:path] if [:path] self.query = [:query] if [:query] self.fragment = [:fragment] if [:fragment] end end |
Class Method Details
.convert_path(path) ⇒ Addressable::URI
Converts a path to a file scheme URI. If the path supplied is relative, it will be returned as a relative URI. If the path supplied is actually a non-file URI, it will parse the URI as if it had been parsed with Addressable::URI.parse
. Handles all of the various Microsoft-specific formats for specifying paths.
Typically a String
path to a file or directory, but will return a sensible return value if an absolute URI is supplied instead.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/addressable/uri.rb', line 209 def self.convert_path(path) # If we were given nil, return nil. return nil unless path # If a URI object is passed, just return itself. return path if path.kind_of?(self) if !path.respond_to?(:to_str) raise TypeError, "Can't convert #{path.class} into String." end # Otherwise, convert to a String path = path.to_str.strip path.gsub!(/^file:\/?\/?/, "") if path =~ /^file:\/?\/?/ path = "/" + path if path =~ /^([a-zA-Z])[\|:]/ uri = self.parse(path) if uri.scheme == nil # Adjust windows-style uris uri.path.gsub!(/^\/?([a-zA-Z])[\|:][\\\/]/) do "/#{$1.downcase}:/" end uri.path.gsub!(/\\/, "/") if File.exists?(uri.path) && File.stat(uri.path).directory? uri.path.gsub!(/\/$/, "") uri.path = uri.path + '/' end # If the path is absolute, set the scheme and host. if uri.path =~ /^\// uri.scheme = "file" uri.host = "" end uri.normalize! end return uri end |
.encode(uri, returning = String) ⇒ String, Addressable::URI Also known as: escape
Percent encodes any special characters in the URI.
This value may only be set to String
or Addressable::URI
. All other values are invalid. Defaults to String
.
The return type is determined by the returning
parameter.
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/addressable/uri.rb', line 457 def self.encode(uri, returning=String) return nil if uri.nil? if !uri.respond_to?(:to_str) raise TypeError, "Can't convert #{uri.class} into String." end if ![String, ::Addressable::URI].include?(returning) raise TypeError, "Expected Class (String or Addressable::URI), " + "got #{returning.inspect}" end uri_object = uri.kind_of?(self) ? uri : self.parse(uri.to_str) encoded_uri = Addressable::URI.new( :scheme => self.encode_component(uri_object.scheme, Addressable::URI::CharacterClasses::SCHEME), :authority => self.encode_component(uri_object., Addressable::URI::CharacterClasses::AUTHORITY), :path => self.encode_component(uri_object.path, Addressable::URI::CharacterClasses::PATH), :query => self.encode_component(uri_object.query, Addressable::URI::CharacterClasses::QUERY), :fragment => self.encode_component(uri_object.fragment, Addressable::URI::CharacterClasses::FRAGMENT) ) if returning == String return encoded_uri.to_s elsif returning == ::Addressable::URI return encoded_uri end end |
.heuristic_parse(uri, hints = {}) ⇒ Addressable::URI
Converts an input to a URI. The input does not have to be a valid URI — the method will use heuristics to guess what URI was intended. This is not standards-compliant, merely user-friendly.
No parsing is performed if the object is already an Addressable::URI
. Defaults to {:scheme => "http"}
.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/addressable/uri.rb', line 140 def self.heuristic_parse(uri, hints={}) # If we were given nil, return nil. return nil unless uri # If a URI object is passed, just return itself. return uri if uri.kind_of?(self) if !uri.respond_to?(:to_str) raise TypeError, "Can't convert #{uri.class} into String." end # Otherwise, convert to a String uri = uri.to_str.dup hints = { :scheme => "http" }.merge(hints) case uri when /^http:\/+/ uri.gsub!(/^http:\/+/, "http://") when /^feed:\/+http:\/+/ uri.gsub!(/^feed:\/+http:\/+/, "feed:http://") when /^feed:\/+/ uri.gsub!(/^feed:\/+/, "feed://") when /^file:\/+/ uri.gsub!(/^file:\/+/, "file:///") end parsed = self.parse(uri) if parsed.scheme =~ /^[^\/?#\.]+\.[^\/?#]+$/ parsed = self.parse(hints[:scheme] + "://" + uri) end if parsed.path.include?(".") new_host = parsed.path[/^([^\/]+\.[^\/]*)/, 1] if new_host new_path = parsed.path.gsub( Regexp.new("^" + Regexp.escape(new_host)), "") parsed.host = new_host parsed.path = new_path parsed.scheme = hints[:scheme] unless parsed.scheme end end return parsed end |
.ip_based_schemes ⇒ Object
Returns an array of known ip-based schemes. These schemes typically use a similar URI form: //<user>:<password>@<host>:<port>/<url-path>
995 996 997 |
# File 'lib/addressable/uri.rb', line 995 def self.ip_based_schemes return self.port_mapping.keys end |
.join(*uris) ⇒ Addressable::URI
Joins several URIs together.
260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/addressable/uri.rb', line 260 def self.join(*uris) uri_objects = uris.collect do |uri| if !uri.respond_to?(:to_str) raise TypeError, "Can't convert #{uri.class} into String." end uri.kind_of?(self) ? uri : self.parse(uri.to_str) end result = uri_objects.shift.dup for uri in uri_objects result.join!(uri) end return result end |
.normalize_component(component, character_class = CharacterClasses::RESERVED + CharacterClasses::UNRESERVED) ⇒ String
Normalizes the encoding of a URI component.
The characters which are not percent encoded. If a String
is passed, the String
must be formatted as a regular expression character class. (Do not include the surrounding square brackets.) For example, "b-zB-Z0-9"
would cause everything but the letters ‘b’ through ‘z’ and the numbers ‘0’ through ‘9’ to be percent encoded. If a Regexp
is passed, the value /[^b-zB-Z0-9]/
would have the same effect. A set of useful String
values may be found in the Addressable::URI::CharacterClasses
module. The default value is the reserved plus unreserved character classes specified in <a href=“www.ietf.org/rfc/rfc3986.txt”>RFC 3986</a>.
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/addressable/uri.rb', line 410 def self.normalize_component(component, character_class= CharacterClasses::RESERVED + CharacterClasses::UNRESERVED) return nil if component.nil? if !component.respond_to?(:to_str) raise TypeError, "Can't convert #{component.class} into String." end component = component.to_str if ![String, Regexp].include?(character_class.class) raise TypeError, "Expected String or Regexp, got #{character_class.inspect}" end if character_class.kind_of?(String) character_class = /[^#{character_class}]/ end if component.respond_to?(:force_encoding) # We can't perform regexps on invalid UTF sequences, but # here we need to, so switch to ASCII. component = component.dup component.force_encoding(Encoding::ASCII_8BIT) end unencoded = self.unencode_component(component) begin encoded = self.encode_component( Addressable::IDNA.unicode_normalize_kc(unencoded), character_class ) rescue ArgumentError encoded = self.encode_component(unencoded) end return encoded end |
.normalized_encode(uri, returning = String) ⇒ String, Addressable::URI
Normalizes the encoding of a URI. Characters within a hostname are not percent encoded to allow for internationalized domain names.
This value may only be set to String
or Addressable::URI
. All other values are invalid. Defaults to String
.
The return type is determined by the returning
parameter.
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 |
# File 'lib/addressable/uri.rb', line 507 def self.normalized_encode(uri, returning=String) if !uri.respond_to?(:to_str) raise TypeError, "Can't convert #{uri.class} into String." end if ![String, ::Addressable::URI].include?(returning) raise TypeError, "Expected Class (String or Addressable::URI), " + "got #{returning.inspect}" end uri_object = uri.kind_of?(self) ? uri : self.parse(uri.to_str) components = { :scheme => self.unencode_component(uri_object.scheme), :user => self.unencode_component(uri_object.user), :password => self.unencode_component(uri_object.password), :host => self.unencode_component(uri_object.host), :port => uri_object.port, :path => self.unencode_component(uri_object.path), :query => self.unencode_component(uri_object.query), :fragment => self.unencode_component(uri_object.fragment) } components.each do |key, value| if value != nil begin components[key] = Addressable::IDNA.unicode_normalize_kc(value.to_str) rescue ArgumentError # Likely a malformed UTF-8 character, skip unicode normalization components[key] = value.to_str end end end encoded_uri = Addressable::URI.new( :scheme => self.encode_component(components[:scheme], Addressable::URI::CharacterClasses::SCHEME), :user => self.encode_component(components[:user], Addressable::URI::CharacterClasses::UNRESERVED), :password => self.encode_component(components[:password], Addressable::URI::CharacterClasses::UNRESERVED), :host => components[:host], :port => components[:port], :path => self.encode_component(components[:path], Addressable::URI::CharacterClasses::PATH), :query => self.encode_component(components[:query], Addressable::URI::CharacterClasses::QUERY), :fragment => self.encode_component(components[:fragment], Addressable::URI::CharacterClasses::FRAGMENT) ) if returning == String return encoded_uri.to_s elsif returning == ::Addressable::URI return encoded_uri end end |
.parse(uri) ⇒ Addressable::URI
Returns a URI object based on the parsed string.
No parsing is performed if the object is already an Addressable::URI
.
66 67 68 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/addressable/uri.rb', line 66 def self.parse(uri) # If we were given nil, return nil. return nil unless uri # If a URI object is passed, just return itself. return uri if uri.kind_of?(self) # If a URI object of the Ruby standard library variety is passed, # convert it to a string, then parse the string. # We do the check this way because we don't want to accidentally # cause a missing constant exception to be thrown. if uri.class.name =~ /^URI\b/ uri = uri.to_s end if !uri.respond_to?(:to_str) raise TypeError, "Can't convert #{uri.class} into String." end # Otherwise, convert to a String uri = uri.to_str # This Regexp supplied as an example in RFC 3986, and it works great. uri_regex = /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/ scan = uri.scan(uri_regex) fragments = scan[0] scheme = fragments[1] = fragments[3] path = fragments[4] query = fragments[6] fragment = fragments[8] user = nil password = nil host = nil port = nil if != nil # The Regexp above doesn't split apart the authority. userinfo = [/^([^\[\]]*)@/, 1] if userinfo != nil user = userinfo.strip[/^([^:]*):?/, 1] password = userinfo.strip[/:(.*)$/, 1] end host = .gsub(/^([^\[\]]*)@/, "").gsub(/:([^:@\[\]]*?)$/, "") port = [/:([^:@\[\]]*?)$/, 1] end if port == "" port = nil end return Addressable::URI.new( :scheme => scheme, :user => user, :password => password, :host => host, :port => port, :path => path, :query => query, :fragment => fragment ) end |
.port_mapping ⇒ Object
Returns a hash of common IP-based schemes and their default port numbers. Adding new schemes to this hash, as necessary, will allow for better URI normalization.
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 |
# File 'lib/addressable/uri.rb', line 1002 def self.port_mapping @port_mapping ||= { "http" => 80, "https" => 443, "ftp" => 21, "tftp" => 69, "sftp" => 22, "ssh" => 22, "svn+ssh" => 22, "telnet" => 23, "nntp" => 119, "gopher" => 70, "wais" => 210, "ldap" => 389, "prospero" => 1525 } end |
.unencode(uri, returning = String) ⇒ String, Addressable::URI Also known as: unescape, unencode_component, unescape_component
Unencodes any percent encoded characters within a URI component. This method may be used for unencoding either components or full URIs, however, it is recommended to use the unencode_component
alias when unencoding components.
This value may only be set to String
or Addressable::URI
. All other values are invalid. Defaults to String
.
The return type is determined by the returning
parameter.
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/addressable/uri.rb', line 350 def self.unencode(uri, returning=String) return nil if uri.nil? if !uri.respond_to?(:to_str) raise TypeError, "Can't convert #{uri.class} into String." end if ![String, ::Addressable::URI].include?(returning) raise TypeError, "Expected Class (String or Addressable::URI), " + "got #{returning.inspect}" end result = uri.to_str.gsub(/%[0-9a-f]{2}/i) do |sequence| sequence[1..3].to_i(16).chr end result.force_encoding("utf-8") if result.respond_to?(:force_encoding) if returning == String return result elsif returning == ::Addressable::URI return ::Addressable::URI.parse(result) end end |
Instance Method Details
#==(uri) ⇒ TrueClass, FalseClass
Returns true
if the URI objects are equal. This method normalizes both URIs before doing the comparison.
1816 1817 1818 1819 |
# File 'lib/addressable/uri.rb', line 1816 def ==(uri) return false unless uri.kind_of?(self.class) return self.normalize.to_s == uri.normalize.to_s end |
#===(uri) ⇒ TrueClass, FalseClass
Returns true
if the URI objects are equal. This method normalizes both URIs before doing the comparison, and allows comparison against Strings
.
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 |
# File 'lib/addressable/uri.rb', line 1795 def ===(uri) if uri.respond_to?(:normalize) uri_string = uri.normalize.to_s else begin uri_string = ::Addressable::URI.parse(uri).normalize.to_s rescue InvalidURIError, TypeError return false end end return self.normalize.to_s == uri_string end |
#absolute? ⇒ TrueClass, FalseClass
Determines if the URI is absolute.
true
if the URI is absolute. false
otherwise.
1465 1466 1467 |
# File 'lib/addressable/uri.rb', line 1465 def absolute? return !relative? end |
#authority ⇒ String
The authority component for this URI. Combines the user, password, host, and port components.
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 |
# File 'lib/addressable/uri.rb', line 912 def @authority ||= (begin if self.host.nil? nil else = "" if self.userinfo != nil << "#{self.userinfo}@" end << self.host if self.port != nil << ":#{self.port}" end end end) end |
#authority=(new_authority) ⇒ Object
Sets the authority component for this URI.
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 |
# File 'lib/addressable/uri.rb', line 956 def () # Check for frozenness raise TypeError, "Can't modify frozen URI." if self.frozen? if if !.respond_to?(:to_str) raise TypeError, "Can't convert #{.class} into String." end = .to_str new_userinfo = [/^([^\[\]]*)@/, 1] if new_userinfo new_user = new_userinfo.strip[/^([^:]*):?/, 1] new_password = new_userinfo.strip[/:(.*)$/, 1] end new_host = .gsub(/^([^\[\]]*)@/, "").gsub(/:([^:@\[\]]*?)$/, "") new_port = [/:([^:@\[\]]*?)$/, 1] end # Password assigned first to ensure validity in case of nil self.password = defined?(new_password) ? new_password : nil self.user = defined?(new_user) ? new_user : nil self.host = defined?(new_host) ? new_host : nil self.port = defined?(new_port) ? new_port : nil # Reset dependant values @inferred_port = nil @userinfo = nil @normalized_userinfo = nil @uri_string = nil # Ensure we haven't created an invalid URI validate() end |
#basename ⇒ String
The basename, if any, of the file in the path component.
1150 1151 1152 1153 |
# File 'lib/addressable/uri.rb', line 1150 def basename # Path cannot be nil return File.basename(self.path).gsub(/;[^\/]*$/, "") end |
#defer_validation(&block) ⇒ Object
This method allows you to make several changes to a URI simultaneously, which separately would cause validation errors, but in conjunction, are valid. The URI will be revalidated as soon as the entire block has been executed.
1982 1983 1984 1985 1986 1987 1988 1989 |
# File 'lib/addressable/uri.rb', line 1982 def defer_validation(&block) raise LocalJumpError, "No block given." unless block @validation_deferred = true block.call() @validation_deferred = false validate return nil end |
#display_uri ⇒ Addressable::URI
Creates a URI suitable for display to users. If semantic attacks are likely, the application should try to detect these and warn the user. See <a href=“www.ietf.org/rfc/rfc3986.txt”>RFC 3986</a>, section 7.6 for more information.
1780 1781 1782 1783 1784 |
# File 'lib/addressable/uri.rb', line 1780 def display_uri display_uri = self.normalize display_uri.host = ::Addressable::IDNA.to_unicode(display_uri.host) return display_uri end |
#dup ⇒ Addressable::URI
Clones the URI object.
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 |
# File 'lib/addressable/uri.rb', line 1847 def dup duplicated_uri = Addressable::URI.new( :scheme => self.scheme ? self.scheme.dup : nil, :user => self.user ? self.user.dup : nil, :password => self.password ? self.password.dup : nil, :host => self.host ? self.host.dup : nil, :port => self.port, :path => self.path ? self.path.dup : nil, :query => self.query ? self.query.dup : nil, :fragment => self.fragment ? self.fragment.dup : nil ) return duplicated_uri end |
#eql?(uri) ⇒ TrueClass, FalseClass
Returns true
if the URI objects are equal. This method does NOT normalize either URI before doing the comparison.
1829 1830 1831 1832 |
# File 'lib/addressable/uri.rb', line 1829 def eql?(uri) return false unless uri.kind_of?(self.class) return self.to_s == uri.to_s end |
#extname ⇒ String
The extname, if any, of the file in the path component. Empty string if there is no extension.
1160 1161 1162 1163 |
# File 'lib/addressable/uri.rb', line 1160 def extname return nil unless self.path return File.extname(self.basename) end |
#fragment ⇒ String
The fragment component for this URI.
1395 1396 1397 |
# File 'lib/addressable/uri.rb', line 1395 def fragment return @fragment ||= nil end |
#fragment=(new_fragment) ⇒ Object
Sets the fragment component for this URI.
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 |
# File 'lib/addressable/uri.rb', line 1420 def fragment=(new_fragment) # Check for frozenness raise TypeError, "Can't modify frozen URI." if self.frozen? if new_fragment && !new_fragment.respond_to?(:to_str) raise TypeError, "Can't convert #{new_fragment.class} into String." end @fragment = new_fragment ? new_fragment.to_str : nil # Reset dependant values @normalized_fragment = nil @uri_string = nil # Ensure we haven't created an invalid URI validate() end |
#freeze ⇒ Addressable::URI
Freezes the URI object.
1865 1866 1867 1868 1869 1870 1871 1872 |
# File 'lib/addressable/uri.rb', line 1865 def freeze # Unfortunately, because of the memoized implementation of many of the # URI methods, the default freeze method will cause unexpected errors. # As an alternative, we freeze the string representation of the URI # instead. This should generally produce the desired effect. self.to_s.freeze return self end |
#frozen? ⇒ TrueClass, FalseClass
Determines if the URI is frozen.
1879 1880 1881 |
# File 'lib/addressable/uri.rb', line 1879 def frozen? self.to_s.frozen? end |
#hash ⇒ Integer
A hash value that will make a URI equivalent to its normalized form.
1839 1840 1841 |
# File 'lib/addressable/uri.rb', line 1839 def hash return @hash ||= (self.to_s.hash * -1) end |
#host ⇒ String
The host component for this URI.
856 857 858 |
# File 'lib/addressable/uri.rb', line 856 def host return @host ||= nil end |
#host=(new_host) ⇒ Object
Sets the host component for this URI.
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 |
# File 'lib/addressable/uri.rb', line 889 def host=(new_host) # Check for frozenness raise TypeError, "Can't modify frozen URI." if self.frozen? if new_host && !new_host.respond_to?(:to_str) raise TypeError, "Can't convert #{new_host.class} into String." end @host = new_host ? new_host.to_str : nil # Reset dependant values @authority = nil @normalized_host = nil @uri_string = nil # Ensure we haven't created an invalid URI validate() end |
#inferred_port ⇒ Integer
The inferred port component for this URI. This method will normalize to the default port for the URI’s scheme if the port isn’t explicitly specified in the URI.
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 |
# File 'lib/addressable/uri.rb', line 1079 def inferred_port @inferred_port ||= (begin if port.to_i == 0 if scheme self.class.port_mapping[scheme.strip.downcase] else nil end else port.to_i end end) end |
#inspect ⇒ String
Returns a String
representation of the URI object’s state.
1970 1971 1972 |
# File 'lib/addressable/uri.rb', line 1970 def inspect sprintf("#<%s:%#0x URI:%s>", self.class.to_s, self.object_id, self.to_s) end |
#ip_based? ⇒ TrueClass, FalseClass
Determines if the scheme indicates an IP-based protocol.
true
if the scheme indicates an IP-based protocol. false
otherwise.
1443 1444 1445 1446 1447 1448 1449 |
# File 'lib/addressable/uri.rb', line 1443 def ip_based? if self.scheme return self.class.ip_based_schemes.include?( self.scheme.strip.downcase) end return false end |
#join(uri) ⇒ Addressable::URI Also known as: +
Joins two URIs together.
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 |
# File 'lib/addressable/uri.rb', line 1475 def join(uri) if !uri.respond_to?(:to_str) raise TypeError, "Can't convert #{uri.class} into String." end if !uri.kind_of?(self.class) # Otherwise, convert to a String, then parse. uri = self.class.parse(uri.to_str) end if uri.to_s == "" return self.dup end joined_scheme = nil joined_user = nil joined_password = nil joined_host = nil joined_port = nil joined_path = nil joined_query = nil joined_fragment = nil # Section 5.2.2 of RFC 3986 if uri.scheme != nil joined_scheme = uri.scheme joined_user = uri.user joined_password = uri.password joined_host = uri.host joined_port = uri.port joined_path = self.class.normalize_path(uri.path) joined_query = uri.query else if uri. != nil joined_user = uri.user joined_password = uri.password joined_host = uri.host joined_port = uri.port joined_path = self.class.normalize_path(uri.path) joined_query = uri.query else if uri.path == nil || uri.path == "" joined_path = self.path if uri.query != nil joined_query = uri.query else joined_query = self.query end else if uri.path[0..0] == "/" joined_path = self.class.normalize_path(uri.path) else base_path = self.path.dup base_path = "" if base_path == nil base_path = self.class.normalize_path(base_path) # Section 5.2.3 of RFC 3986 # # Removes the right-most path segment from the base path. if base_path =~ /\// base_path.gsub!(/\/[^\/]+$/, "/") else base_path = "" end # If the base path is empty and an authority segment has been # defined, use a base path of "/" if base_path == "" && self. != nil base_path = "/" end joined_path = self.class.normalize_path(base_path + uri.path) end joined_query = uri.query end joined_user = self.user joined_password = self.password joined_host = self.host joined_port = self.port end joined_scheme = self.scheme end joined_fragment = uri.fragment return Addressable::URI.new( :scheme => joined_scheme, :user => joined_user, :password => joined_password, :host => joined_host, :port => joined_port, :path => joined_path, :query => joined_query, :fragment => joined_fragment ) end |
#join!(uri) ⇒ Addressable::URI
Destructive form of join
.
1578 1579 1580 |
# File 'lib/addressable/uri.rb', line 1578 def join!(uri) replace_self(self.join(uri)) end |
#merge(hash) ⇒ Addressable::URI
Merges a URI with a Hash
of components. This method has different behavior from join
. Any components present in the hash
parameter will override the original components. The path component is not treated specially.
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 |
# File 'lib/addressable/uri.rb', line 1593 def merge(hash) if !hash.respond_to?(:to_hash) raise TypeError, "Can't convert #{hash.class} into Hash." end hash = hash.to_hash if hash.has_key?(:authority) if (hash.keys & [:userinfo, :user, :password, :host, :port]).any? raise ArgumentError, "Cannot specify both an authority and any of the components " + "within the authority." end end if hash.has_key?(:userinfo) if (hash.keys & [:user, :password]).any? raise ArgumentError, "Cannot specify both a userinfo and either the user or password." end end uri = Addressable::URI.new uri.defer_validation do # Bunch of crazy logic required because of the composite components # like userinfo and authority. uri.scheme = hash.has_key?(:scheme) ? hash[:scheme] : self.scheme if hash.has_key?(:authority) uri. = hash.has_key?(:authority) ? hash[:authority] : self. end if hash.has_key?(:userinfo) uri.userinfo = hash.has_key?(:userinfo) ? hash[:userinfo] : self.userinfo end if !hash.has_key?(:userinfo) && !hash.has_key?(:authority) uri.user = hash.has_key?(:user) ? hash[:user] : self.user uri.password = hash.has_key?(:password) ? hash[:password] : self.password end if !hash.has_key?(:authority) uri.host = hash.has_key?(:host) ? hash[:host] : self.host uri.port = hash.has_key?(:port) ? hash[:port] : self.port end uri.path = hash.has_key?(:path) ? hash[:path] : self.path uri.query = hash.has_key?(:query) ? hash[:query] : self.query uri.fragment = hash.has_key?(:fragment) ? hash[:fragment] : self.fragment end return uri end |
#merge!(uri) ⇒ Addressable::URI
Destructive form of merge
.
1658 1659 1660 |
# File 'lib/addressable/uri.rb', line 1658 def merge!(uri) replace_self(self.merge(uri)) end |
#normalize ⇒ Addressable::URI
Returns a normalized URI object.
NOTE: This method does not attempt to fully conform to specifications. It exists largely to correct other people’s failures to read the specifications, and also to deal with caching issues since several different URIs may represent the same resource and should not be cached multiple times.
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 |
# File 'lib/addressable/uri.rb', line 1743 def normalize # This is a special exception for the frequently misused feed # URI scheme. if normalized_scheme == "feed" if self.to_s =~ /^feed:\/*http:\/*/ return self.class.parse( self.to_s[/^feed:\/*(http:\/*.*)/, 1] ).normalize end end return Addressable::URI.new( :scheme => normalized_scheme, :authority => , :path => normalized_path, :query => normalized_query, :fragment => normalized_fragment ) end |
#normalize! ⇒ Addressable::URI
Destructively normalizes this URI object.
1769 1770 1771 |
# File 'lib/addressable/uri.rb', line 1769 def normalize! replace_self(self.normalize) end |
#normalized_authority ⇒ String
The authority component for this URI, normalized.
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 |
# File 'lib/addressable/uri.rb', line 934 def @normalized_authority ||= (begin if self.normalized_host.nil? nil else = "" if self.normalized_userinfo != nil << "#{self.normalized_userinfo}@" end << self.normalized_host if self.normalized_port != nil << ":#{self.normalized_port}" end end end) end |
#normalized_fragment ⇒ String
The fragment component for this URI, normalized.
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 |
# File 'lib/addressable/uri.rb', line 1403 def normalized_fragment @normalized_fragment ||= (begin if self.fragment Addressable::URI.normalize_component( self.fragment.strip, Addressable::URI::CharacterClasses::FRAGMENT ) else nil end end) end |
#normalized_host ⇒ String
The host component for this URI, normalized.
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 |
# File 'lib/addressable/uri.rb', line 864 def normalized_host @normalized_host ||= (begin if self.host != nil if self.host.strip != "" result = ::Addressable::IDNA.to_ascii( self.class.unencode_component(self.host.strip.downcase) ) if result[-1..-1] == "." # Trailing dots are unnecessary result = result[0...-1] end result else "" end else nil end end) end |
#normalized_password ⇒ String
The password component for this URI, normalized.
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 |
# File 'lib/addressable/uri.rb', line 734 def normalized_password @normalized_password ||= (begin if self.password if normalized_scheme =~ /https?/ && self.password.strip == "" && (!self.user || self.user.strip == "") nil else Addressable::URI.normalize_component( self.password.strip, Addressable::URI::CharacterClasses::UNRESERVED ) end else nil end end) end |
#normalized_path ⇒ String
The path component for this URI, normalized.
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 |
# File 'lib/addressable/uri.rb', line 1106 def normalized_path @normalized_path ||= (begin # String#split(delimeter, -1) uses the more strict splitting behavior # found in Python. result = (self.path.strip.split("/", -1).map do |segment| Addressable::URI.normalize_component( segment, Addressable::URI::CharacterClasses::PCHAR ) end).join("/") result = self.class.normalize_path(result) if result == "" && ["http", "https", "ftp", "tftp"].include?(self.normalized_scheme) result = "/" end result end) end |
#normalized_port ⇒ Integer
The port component for this URI, normalized.
1034 1035 1036 1037 1038 1039 1040 1041 1042 |
# File 'lib/addressable/uri.rb', line 1034 def normalized_port @normalized_port ||= (begin if self.class.port_mapping[normalized_scheme] == self.port nil else self.port end end) end |
#normalized_query ⇒ String
The query component for this URI, normalized.
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 |
# File 'lib/addressable/uri.rb', line 1177 def normalized_query @normalized_query ||= (begin if self.query Addressable::URI.normalize_component( self.query.strip, Addressable::URI::CharacterClasses::QUERY ) else nil end end) end |
#normalized_scheme ⇒ String
The scheme component for this URI, normalized.
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
# File 'lib/addressable/uri.rb', line 623 def normalized_scheme @normalized_scheme ||= (begin if self.scheme != nil if self.scheme =~ /^\s*ssh\+svn\s*$/i "svn+ssh" else Addressable::URI.normalize_component( self.scheme.strip.downcase, Addressable::URI::CharacterClasses::SCHEME ) end else nil end end) end |
#normalized_user ⇒ String
The user component for this URI, normalized.
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 |
# File 'lib/addressable/uri.rb', line 674 def normalized_user @normalized_user ||= (begin if self.user if normalized_scheme =~ /https?/ && self.user.strip == "" && (!self.password || self.password.strip == "") nil else Addressable::URI.normalize_component( self.user.strip, Addressable::URI::CharacterClasses::UNRESERVED ) end else nil end end) end |
#normalized_userinfo ⇒ String
The userinfo component for this URI, normalized.
806 807 808 809 810 811 812 813 814 815 816 817 818 |
# File 'lib/addressable/uri.rb', line 806 def normalized_userinfo @normalized_userinfo ||= (begin current_user = self.normalized_user current_password = self.normalized_password if !current_user && !current_password nil elsif current_user && current_password "#{current_user}:#{current_password}" elsif current_user && !current_password "#{current_user}" end end) end |
#omit(*components) ⇒ Addressable::URI
Omits components from a URI.
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 |
# File 'lib/addressable/uri.rb', line 1895 def omit(*components) invalid_components = components - [ :scheme, :user, :password, :userinfo, :host, :port, :authority, :path, :query, :fragment ] unless invalid_components.empty? raise ArgumentError, "Invalid component names: #{invalid_components.inspect}." end duplicated_uri = self.dup duplicated_uri.defer_validation do components.each do |component| duplicated_uri.send((component.to_s + "=").to_sym, nil) end duplicated_uri.user = duplicated_uri.normalized_user end duplicated_uri end |
#omit!(*components) ⇒ Addressable::URI
Destructive form of omit.
1922 1923 1924 |
# File 'lib/addressable/uri.rb', line 1922 def omit!(*components) replace_self(self.omit(*components)) end |
#password ⇒ String
The password component for this URI.
726 727 728 |
# File 'lib/addressable/uri.rb', line 726 def password return @password ||= nil end |
#password=(new_password) ⇒ Object
Sets the password component for this URI.
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 781 |
# File 'lib/addressable/uri.rb', line 756 def password=(new_password) # Check for frozenness raise TypeError, "Can't modify frozen URI." if self.frozen? if new_password && !new_password.respond_to?(:to_str) raise TypeError, "Can't convert #{new_password.class} into String." end @password = new_password ? new_password.to_str : nil # You can't have a nil user with a non-nil password @password ||= nil @user ||= nil if @password != nil @user = "" if @user.nil? end # Reset dependant values @userinfo = nil @normalized_userinfo = nil @authority = nil @normalized_password = nil @uri_string = nil # Ensure we haven't created an invalid URI validate() end |
#path ⇒ String
The path component for this URI.
1097 1098 1099 1100 |
# File 'lib/addressable/uri.rb', line 1097 def path @path ||= "" return @path end |
#path=(new_path) ⇒ Object
Sets the path component for this URI.
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 |
# File 'lib/addressable/uri.rb', line 1129 def path=(new_path) # Check for frozenness raise TypeError, "Can't modify frozen URI." if self.frozen? if new_path && !new_path.respond_to?(:to_str) raise TypeError, "Can't convert #{new_path.class} into String." end @path = (new_path || "").to_str if @path != "" && @path[0..0] != "/" && host != nil @path = "/#{@path}" end # Reset dependant values @normalized_path = nil @uri_string = nil end |
#port ⇒ Integer
The port component for this URI. This is the port number actually given in the URI. This does not infer port numbers from default values.
1026 1027 1028 |
# File 'lib/addressable/uri.rb', line 1026 def port return @port ||= nil end |
#port=(new_port) ⇒ Object
Sets the port component for this URI.
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 |
# File 'lib/addressable/uri.rb', line 1048 def port=(new_port) # Check for frozenness raise TypeError, "Can't modify frozen URI." if self.frozen? if new_port != nil && new_port.respond_to?(:to_str) new_port = Addressable::URI.unencode_component(new_port.to_str) end if new_port != nil && !(new_port.to_s =~ /^\d+$/) raise InvalidURIError, "Invalid port number: #{new_port.inspect}" end @port = new_port.to_s.to_i @port = nil if @port == 0 # Reset dependant values @authority = nil @inferred_port = nil @normalized_port = nil @uri_string = nil # Ensure we haven't created an invalid URI validate() end |
#query ⇒ String
The query component for this URI.
1169 1170 1171 |
# File 'lib/addressable/uri.rb', line 1169 def query return @query ||= nil end |
#query=(new_query) ⇒ Object
Sets the query component for this URI.
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 |
# File 'lib/addressable/uri.rb', line 1194 def query=(new_query) # Check for frozenness raise TypeError, "Can't modify frozen URI." if self.frozen? if new_query && !new_query.respond_to?(:to_str) raise TypeError, "Can't convert #{new_query.class} into String." end @query = new_query ? new_query.to_str : nil # Reset dependant values @normalized_query = nil @uri_string = nil end |
#query_values(options = {}) ⇒ Hash
Converts the query component to a Hash value.
The :dot
notation is not supported for assignment. Default value is :subscript
.
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 |
# File 'lib/addressable/uri.rb', line 1239 def query_values(={}) defaults = {:notation => :subscript} = defaults.merge() if ![:flat, :dot, :subscript].include?([:notation]) raise ArgumentError, "Invalid notation. Must be one of: [:flat, :dot, :subscript]." end dehash = lambda do |hash| hash.each do |(key, value)| if value.kind_of?(Hash) hash[key] = dehash.call(value) end end if hash != {} && hash.keys.all? { |key| key =~ /^\d+$/ } hash.sort.inject([]) do |accu, (key, value)| accu << value; accu end else hash end end return nil if self.query == nil return ((self.query.split("&").map do |pair| pair.split("=") end).inject({}) do |accumulator, (key, value)| value = true if value.nil? key = self.class.unencode_component(key) if value != true value = self.class.unencode_component(value).gsub(/\+/, " ") end if [:notation] == :flat if accumulator[key] raise ArgumentError, "Key was repeated: #{key.inspect}" end accumulator[key] = value else if [:notation] == :dot array_value = false subkeys = key.split(".") elsif [:notation] == :subscript array_value = !!(key =~ /\[\]$/) subkeys = key ? key.split(/[\[\]]+/) : [] end current_hash = accumulator for i in 0...(subkeys.size - 1) subkey = subkeys[i] current_hash[subkey] = {} unless current_hash[subkey] current_hash = current_hash[subkey] end if array_value current_hash[subkeys.last] = [] unless current_hash[subkeys.last] current_hash[subkeys.last] << value else current_hash[subkeys.last] = value end end accumulator end).inject({}) do |accumulator, (key, value)| accumulator[key] = value.kind_of?(Hash) ? dehash.call(value) : value accumulator end end |
#query_values=(new_query_values) ⇒ Object
Sets the query component for this URI from a Hash object. This method produces a query string using the :subscript notation.
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 |
# File 'lib/addressable/uri.rb', line 1307 def query_values=(new_query_values) # Check for frozenness raise TypeError, "Can't modify frozen URI." if self.frozen? if !new_query_values.respond_to?(:to_hash) raise TypeError, "Can't convert #{new_query_values.class} into Hash." end new_query_values = new_query_values.to_hash # Algorithm shamelessly stolen from Julien Genestoux, slightly modified buffer = "" stack = [] e = lambda do |component| component = component.to_s if component.kind_of?(Symbol) self.class.encode_component(component, CharacterClasses::UNRESERVED) end new_query_values.each do |key, value| if value.kind_of?(Hash) stack << [key, value] elsif value.kind_of?(Array) stack << [ key, value.inject({}) { |accu, x| accu[accu.size.to_s] = x; accu } ] elsif value == true buffer << "#{e.call(key)}&" else buffer << "#{e.call(key)}=#{e.call(value)}&" end end stack.each do |(parent, hash)| (hash.sort_by { |key| key.to_s }).each do |(key, value)| if value.kind_of?(Hash) stack << ["#{parent}[#{key}]", value] elsif value == true buffer << "#{parent}[#{e.call(key)}]&" else buffer << "#{parent}[#{e.call(key)}]=#{e.call(value)}&" end end end @query = buffer.chop # Reset dependant values @normalized_query = nil @uri_string = nil end |
#relative? ⇒ TrueClass, FalseClass
Determines if the URI is relative.
true
if the URI is relative. false
otherwise.
1456 1457 1458 |
# File 'lib/addressable/uri.rb', line 1456 def relative? return self.scheme.nil? end |
#request_uri ⇒ String
The HTTP request URI for this URI. This is the path and the query string.
1359 1360 1361 1362 1363 1364 1365 |
# File 'lib/addressable/uri.rb', line 1359 def request_uri return nil if self.absolute? && self.scheme !~ /^https?$/ return ( (self.path != "" ? self.path : "/") + (self.query ? "?#{self.query}" : "") ) end |
#request_uri=(new_request_uri) ⇒ Object
Sets the HTTP request URI for this URI.
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 |
# File 'lib/addressable/uri.rb', line 1371 def request_uri=(new_request_uri) if !new_request_uri.respond_to?(:to_str) raise TypeError, "Can't convert #{new_request_uri.class} into String." end if self.absolute? && self.scheme !~ /^https?$/ raise InvalidURIError, "Cannot set an HTTP request URI for a non-HTTP URI." end new_request_uri = new_request_uri.to_str path_component = new_request_uri[/^([^\?]*)\?(?:.*)$/, 1] query_component = new_request_uri[/^(?:[^\?]*)\?(.*)$/, 1] path_component = path_component.to_s path_component = (path_component != "" ? path_component : "/") self.path = path_component self.query = query_component # Reset dependant values @uri_string = nil end |
#route_from(uri) ⇒ Addressable::URI
Returns the shortest normalized relative form of this URI that uses the supplied URI as a base for resolution. Returns an absolute URI if necessary. This is effectively the opposite of route_to
.
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 |
# File 'lib/addressable/uri.rb', line 1671 def route_from(uri) uri = self.class.parse(uri).normalize normalized_self = self.normalize if normalized_self.relative? raise ArgumentError, "Expected absolute URI, got: #{self.to_s}" end if uri.relative? raise ArgumentError, "Expected absolute URI, got: #{uri.to_s}" end if normalized_self == uri return Addressable::URI.parse("##{normalized_self.fragment}") end components = normalized_self.to_hash if normalized_self.scheme == uri.scheme components[:scheme] = nil if normalized_self. == uri. components[:user] = nil components[:password] = nil components[:host] = nil components[:port] = nil if normalized_self.path == uri.path components[:path] = nil if normalized_self.query == uri.query components[:query] = nil end else if uri.path != "/" components[:path].gsub!( Regexp.new("^" + Regexp.escape(uri.path)), "") end end end end # Avoid network-path references. if components[:host] != nil components[:scheme] = normalized_self.scheme end return Addressable::URI.new( :scheme => components[:scheme], :user => components[:user], :password => components[:password], :host => components[:host], :port => components[:port], :path => components[:path], :query => components[:query], :fragment => components[:fragment] ) end |
#route_to(uri) ⇒ Addressable::URI
Returns the shortest normalized relative form of the supplied URI that uses this URI as a base for resolution. Returns an absolute URI if necessary. This is effectively the opposite of route_from
.
1729 1730 1731 |
# File 'lib/addressable/uri.rb', line 1729 def route_to(uri) return self.class.parse(uri).route_from(self) end |
#scheme ⇒ String
The scheme component for this URI.
615 616 617 |
# File 'lib/addressable/uri.rb', line 615 def scheme return @scheme ||= nil end |
#scheme=(new_scheme) ⇒ Object
Sets the scheme component for this URI.
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 |
# File 'lib/addressable/uri.rb', line 644 def scheme=(new_scheme) # Check for frozenness raise TypeError, "Can't modify frozen URI." if self.frozen? if new_scheme && !new_scheme.respond_to?(:to_str) raise TypeError, "Can't convert #{new_scheme.class} into String." end @scheme = new_scheme ? new_scheme.to_str : nil @scheme = nil if @scheme.to_s.strip == "" # Reset dependant values @normalized_scheme = nil @uri_string = nil # Ensure we haven't created an invalid URI validate() end |
#to_hash ⇒ Hash
Returns a Hash of the URI components.
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 |
# File 'lib/addressable/uri.rb', line 1953 def to_hash return { :scheme => self.scheme, :user => self.user, :password => self.password, :host => self.host, :port => self.port, :path => self.path, :query => self.query, :fragment => self.fragment } end |
#to_s ⇒ String Also known as: to_str
Converts the URI to a String
.
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 |
# File 'lib/addressable/uri.rb', line 1930 def to_s @uri_string ||= (begin uri_string = "" uri_string << "#{self.scheme}:" if self.scheme != nil uri_string << "//#{self.}" if self. != nil uri_string << self.path.to_s uri_string << "?#{self.query}" if self.query != nil uri_string << "##{self.fragment}" if self.fragment != nil if uri_string.respond_to?(:force_encoding) uri_string.force_encoding(Encoding::UTF_8) end uri_string end) end |
#user ⇒ String
The user component for this URI.
666 667 668 |
# File 'lib/addressable/uri.rb', line 666 def user return @user ||= nil end |
#user=(new_user) ⇒ Object
Sets the user component for this URI.
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 |
# File 'lib/addressable/uri.rb', line 696 def user=(new_user) # Check for frozenness raise TypeError, "Can't modify frozen URI." if self.frozen? if new_user && !new_user.respond_to?(:to_str) raise TypeError, "Can't convert #{new_user.class} into String." end @user = new_user ? new_user.to_str : nil # You can't have a nil user with a non-nil password @password ||= nil if @password != nil @user = "" if @user.nil? end # Reset dependant values @userinfo = nil @normalized_userinfo = nil @authority = nil @normalized_user = nil @uri_string = nil # Ensure we haven't created an invalid URI validate() end |
#userinfo ⇒ String
The userinfo component for this URI. Combines the user and password components.
788 789 790 791 792 793 794 795 796 797 798 799 800 |
# File 'lib/addressable/uri.rb', line 788 def userinfo @userinfo ||= (begin current_user = self.user current_password = self.password if !current_user && !current_password nil elsif current_user && current_password "#{current_user}:#{current_password}" elsif current_user && !current_password "#{current_user}" end end) end |
#userinfo=(new_userinfo) ⇒ Object
Sets the userinfo component for this URI.
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 |
# File 'lib/addressable/uri.rb', line 824 def userinfo=(new_userinfo) # Check for frozenness raise TypeError, "Can't modify frozen URI." if self.frozen? if new_userinfo && !new_userinfo.respond_to?(:to_str) raise TypeError, "Can't convert #{new_userinfo.class} into String." end new_user, new_password = if new_userinfo [ new_userinfo.to_str.strip[/^(.*):/, 1], new_userinfo.to_str.strip[/:(.*)$/, 1] ] else [nil, nil] end # Password assigned first to ensure validity in case of nil self.password = new_password self.user = new_user # Reset dependant values @authority = nil @uri_string = nil # Ensure we haven't created an invalid URI validate() end |