Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby27/share/ruby/uri
File: generic.rb
# frozen_string_literal: true
[0] Fix | Delete
[1] Fix | Delete
# = uri/generic.rb
[2] Fix | Delete
#
[3] Fix | Delete
# Author:: Akira Yamada <akira@ruby-lang.org>
[4] Fix | Delete
# License:: You can redistribute it and/or modify it under the same term as Ruby.
[5] Fix | Delete
# Revision:: $Id$
[6] Fix | Delete
#
[7] Fix | Delete
# See URI for general documentation
[8] Fix | Delete
#
[9] Fix | Delete
[10] Fix | Delete
require_relative 'common'
[11] Fix | Delete
autoload :IPSocket, 'socket'
[12] Fix | Delete
autoload :IPAddr, 'ipaddr'
[13] Fix | Delete
[14] Fix | Delete
module URI
[15] Fix | Delete
[16] Fix | Delete
#
[17] Fix | Delete
# Base class for all URI classes.
[18] Fix | Delete
# Implements generic URI syntax as per RFC 2396.
[19] Fix | Delete
#
[20] Fix | Delete
class Generic
[21] Fix | Delete
include URI
[22] Fix | Delete
[23] Fix | Delete
#
[24] Fix | Delete
# A Default port of nil for URI::Generic.
[25] Fix | Delete
#
[26] Fix | Delete
DEFAULT_PORT = nil
[27] Fix | Delete
[28] Fix | Delete
#
[29] Fix | Delete
# Returns default port.
[30] Fix | Delete
#
[31] Fix | Delete
def self.default_port
[32] Fix | Delete
self::DEFAULT_PORT
[33] Fix | Delete
end
[34] Fix | Delete
[35] Fix | Delete
#
[36] Fix | Delete
# Returns default port.
[37] Fix | Delete
#
[38] Fix | Delete
def default_port
[39] Fix | Delete
self.class.default_port
[40] Fix | Delete
end
[41] Fix | Delete
[42] Fix | Delete
#
[43] Fix | Delete
# An Array of the available components for URI::Generic.
[44] Fix | Delete
#
[45] Fix | Delete
COMPONENT = [
[46] Fix | Delete
:scheme,
[47] Fix | Delete
:userinfo, :host, :port, :registry,
[48] Fix | Delete
:path, :opaque,
[49] Fix | Delete
:query,
[50] Fix | Delete
:fragment
[51] Fix | Delete
].freeze
[52] Fix | Delete
[53] Fix | Delete
#
[54] Fix | Delete
# Components of the URI in the order.
[55] Fix | Delete
#
[56] Fix | Delete
def self.component
[57] Fix | Delete
self::COMPONENT
[58] Fix | Delete
end
[59] Fix | Delete
[60] Fix | Delete
USE_REGISTRY = false # :nodoc:
[61] Fix | Delete
[62] Fix | Delete
def self.use_registry # :nodoc:
[63] Fix | Delete
self::USE_REGISTRY
[64] Fix | Delete
end
[65] Fix | Delete
[66] Fix | Delete
#
[67] Fix | Delete
# == Synopsis
[68] Fix | Delete
#
[69] Fix | Delete
# See ::new.
[70] Fix | Delete
#
[71] Fix | Delete
# == Description
[72] Fix | Delete
#
[73] Fix | Delete
# At first, tries to create a new URI::Generic instance using
[74] Fix | Delete
# URI::Generic::build. But, if exception URI::InvalidComponentError is raised,
[75] Fix | Delete
# then it does URI::Escape.escape all URI components and tries again.
[76] Fix | Delete
#
[77] Fix | Delete
def self.build2(args)
[78] Fix | Delete
begin
[79] Fix | Delete
return self.build(args)
[80] Fix | Delete
rescue InvalidComponentError
[81] Fix | Delete
if args.kind_of?(Array)
[82] Fix | Delete
return self.build(args.collect{|x|
[83] Fix | Delete
if x.is_a?(String)
[84] Fix | Delete
DEFAULT_PARSER.escape(x)
[85] Fix | Delete
else
[86] Fix | Delete
x
[87] Fix | Delete
end
[88] Fix | Delete
})
[89] Fix | Delete
elsif args.kind_of?(Hash)
[90] Fix | Delete
tmp = {}
[91] Fix | Delete
args.each do |key, value|
[92] Fix | Delete
tmp[key] = if value
[93] Fix | Delete
DEFAULT_PARSER.escape(value)
[94] Fix | Delete
else
[95] Fix | Delete
value
[96] Fix | Delete
end
[97] Fix | Delete
end
[98] Fix | Delete
return self.build(tmp)
[99] Fix | Delete
end
[100] Fix | Delete
end
[101] Fix | Delete
end
[102] Fix | Delete
[103] Fix | Delete
#
[104] Fix | Delete
# == Synopsis
[105] Fix | Delete
#
[106] Fix | Delete
# See ::new.
[107] Fix | Delete
#
[108] Fix | Delete
# == Description
[109] Fix | Delete
#
[110] Fix | Delete
# Creates a new URI::Generic instance from components of URI::Generic
[111] Fix | Delete
# with check. Components are: scheme, userinfo, host, port, registry, path,
[112] Fix | Delete
# opaque, query, and fragment. You can provide arguments either by an Array or a Hash.
[113] Fix | Delete
# See ::new for hash keys to use or for order of array items.
[114] Fix | Delete
#
[115] Fix | Delete
def self.build(args)
[116] Fix | Delete
if args.kind_of?(Array) &&
[117] Fix | Delete
args.size == ::URI::Generic::COMPONENT.size
[118] Fix | Delete
tmp = args.dup
[119] Fix | Delete
elsif args.kind_of?(Hash)
[120] Fix | Delete
tmp = ::URI::Generic::COMPONENT.collect do |c|
[121] Fix | Delete
if args.include?(c)
[122] Fix | Delete
args[c]
[123] Fix | Delete
else
[124] Fix | Delete
nil
[125] Fix | Delete
end
[126] Fix | Delete
end
[127] Fix | Delete
else
[128] Fix | Delete
component = self.class.component rescue ::URI::Generic::COMPONENT
[129] Fix | Delete
raise ArgumentError,
[130] Fix | Delete
"expected Array of or Hash of components of #{self.class} (#{component.join(', ')})"
[131] Fix | Delete
end
[132] Fix | Delete
[133] Fix | Delete
tmp << nil
[134] Fix | Delete
tmp << true
[135] Fix | Delete
return self.new(*tmp)
[136] Fix | Delete
end
[137] Fix | Delete
[138] Fix | Delete
#
[139] Fix | Delete
# == Args
[140] Fix | Delete
#
[141] Fix | Delete
# +scheme+::
[142] Fix | Delete
# Protocol scheme, i.e. 'http','ftp','mailto' and so on.
[143] Fix | Delete
# +userinfo+::
[144] Fix | Delete
# User name and password, i.e. 'sdmitry:bla'.
[145] Fix | Delete
# +host+::
[146] Fix | Delete
# Server host name.
[147] Fix | Delete
# +port+::
[148] Fix | Delete
# Server port.
[149] Fix | Delete
# +registry+::
[150] Fix | Delete
# Registry of naming authorities.
[151] Fix | Delete
# +path+::
[152] Fix | Delete
# Path on server.
[153] Fix | Delete
# +opaque+::
[154] Fix | Delete
# Opaque part.
[155] Fix | Delete
# +query+::
[156] Fix | Delete
# Query data.
[157] Fix | Delete
# +fragment+::
[158] Fix | Delete
# Part of the URI after '#' character.
[159] Fix | Delete
# +parser+::
[160] Fix | Delete
# Parser for internal use [URI::DEFAULT_PARSER by default].
[161] Fix | Delete
# +arg_check+::
[162] Fix | Delete
# Check arguments [false by default].
[163] Fix | Delete
#
[164] Fix | Delete
# == Description
[165] Fix | Delete
#
[166] Fix | Delete
# Creates a new URI::Generic instance from ``generic'' components without check.
[167] Fix | Delete
#
[168] Fix | Delete
def initialize(scheme,
[169] Fix | Delete
userinfo, host, port, registry,
[170] Fix | Delete
path, opaque,
[171] Fix | Delete
query,
[172] Fix | Delete
fragment,
[173] Fix | Delete
parser = DEFAULT_PARSER,
[174] Fix | Delete
arg_check = false)
[175] Fix | Delete
@scheme = nil
[176] Fix | Delete
@user = nil
[177] Fix | Delete
@password = nil
[178] Fix | Delete
@host = nil
[179] Fix | Delete
@port = nil
[180] Fix | Delete
@path = nil
[181] Fix | Delete
@query = nil
[182] Fix | Delete
@opaque = nil
[183] Fix | Delete
@fragment = nil
[184] Fix | Delete
@parser = parser == DEFAULT_PARSER ? nil : parser
[185] Fix | Delete
[186] Fix | Delete
if arg_check
[187] Fix | Delete
self.scheme = scheme
[188] Fix | Delete
self.userinfo = userinfo
[189] Fix | Delete
self.hostname = host
[190] Fix | Delete
self.port = port
[191] Fix | Delete
self.path = path
[192] Fix | Delete
self.query = query
[193] Fix | Delete
self.opaque = opaque
[194] Fix | Delete
self.fragment = fragment
[195] Fix | Delete
else
[196] Fix | Delete
self.set_scheme(scheme)
[197] Fix | Delete
self.set_userinfo(userinfo)
[198] Fix | Delete
self.set_host(host)
[199] Fix | Delete
self.set_port(port)
[200] Fix | Delete
self.set_path(path)
[201] Fix | Delete
self.query = query
[202] Fix | Delete
self.set_opaque(opaque)
[203] Fix | Delete
self.fragment=(fragment)
[204] Fix | Delete
end
[205] Fix | Delete
if registry
[206] Fix | Delete
raise InvalidURIError,
[207] Fix | Delete
"the scheme #{@scheme} does not accept registry part: #{registry} (or bad hostname?)"
[208] Fix | Delete
end
[209] Fix | Delete
[210] Fix | Delete
@scheme&.freeze
[211] Fix | Delete
self.set_path('') if !@path && !@opaque # (see RFC2396 Section 5.2)
[212] Fix | Delete
self.set_port(self.default_port) if self.default_port && !@port
[213] Fix | Delete
end
[214] Fix | Delete
[215] Fix | Delete
#
[216] Fix | Delete
# Returns the scheme component of the URI.
[217] Fix | Delete
#
[218] Fix | Delete
# URI("http://foo/bar/baz").scheme #=> "http"
[219] Fix | Delete
#
[220] Fix | Delete
attr_reader :scheme
[221] Fix | Delete
[222] Fix | Delete
# Returns the host component of the URI.
[223] Fix | Delete
#
[224] Fix | Delete
# URI("http://foo/bar/baz").host #=> "foo"
[225] Fix | Delete
#
[226] Fix | Delete
# It returns nil if no host component exists.
[227] Fix | Delete
#
[228] Fix | Delete
# URI("mailto:foo@example.org").host #=> nil
[229] Fix | Delete
#
[230] Fix | Delete
# The component does not contain the port number.
[231] Fix | Delete
#
[232] Fix | Delete
# URI("http://foo:8080/bar/baz").host #=> "foo"
[233] Fix | Delete
#
[234] Fix | Delete
# Since IPv6 addresses are wrapped with brackets in URIs,
[235] Fix | Delete
# this method returns IPv6 addresses wrapped with brackets.
[236] Fix | Delete
# This form is not appropriate to pass to socket methods such as TCPSocket.open.
[237] Fix | Delete
# If unwrapped host names are required, use the #hostname method.
[238] Fix | Delete
#
[239] Fix | Delete
# URI("http://[::1]/bar/baz").host #=> "[::1]"
[240] Fix | Delete
# URI("http://[::1]/bar/baz").hostname #=> "::1"
[241] Fix | Delete
#
[242] Fix | Delete
attr_reader :host
[243] Fix | Delete
[244] Fix | Delete
# Returns the port component of the URI.
[245] Fix | Delete
#
[246] Fix | Delete
# URI("http://foo/bar/baz").port #=> 80
[247] Fix | Delete
# URI("http://foo:8080/bar/baz").port #=> 8080
[248] Fix | Delete
#
[249] Fix | Delete
attr_reader :port
[250] Fix | Delete
[251] Fix | Delete
def registry # :nodoc:
[252] Fix | Delete
nil
[253] Fix | Delete
end
[254] Fix | Delete
[255] Fix | Delete
# Returns the path component of the URI.
[256] Fix | Delete
#
[257] Fix | Delete
# URI("http://foo/bar/baz").path #=> "/bar/baz"
[258] Fix | Delete
#
[259] Fix | Delete
attr_reader :path
[260] Fix | Delete
[261] Fix | Delete
# Returns the query component of the URI.
[262] Fix | Delete
#
[263] Fix | Delete
# URI("http://foo/bar/baz?search=FooBar").query #=> "search=FooBar"
[264] Fix | Delete
#
[265] Fix | Delete
attr_reader :query
[266] Fix | Delete
[267] Fix | Delete
# Returns the opaque part of the URI.
[268] Fix | Delete
#
[269] Fix | Delete
# URI("mailto:foo@example.org").opaque #=> "foo@example.org"
[270] Fix | Delete
# URI("http://foo/bar/baz").opaque #=> nil
[271] Fix | Delete
#
[272] Fix | Delete
# The portion of the path that does not make use of the slash '/'.
[273] Fix | Delete
# The path typically refers to an absolute path or an opaque part.
[274] Fix | Delete
# (See RFC2396 Section 3 and 5.2.)
[275] Fix | Delete
#
[276] Fix | Delete
attr_reader :opaque
[277] Fix | Delete
[278] Fix | Delete
# Returns the fragment component of the URI.
[279] Fix | Delete
#
[280] Fix | Delete
# URI("http://foo/bar/baz?search=FooBar#ponies").fragment #=> "ponies"
[281] Fix | Delete
#
[282] Fix | Delete
attr_reader :fragment
[283] Fix | Delete
[284] Fix | Delete
# Returns the parser to be used.
[285] Fix | Delete
#
[286] Fix | Delete
# Unless a URI::Parser is defined, DEFAULT_PARSER is used.
[287] Fix | Delete
#
[288] Fix | Delete
def parser
[289] Fix | Delete
if !defined?(@parser) || !@parser
[290] Fix | Delete
DEFAULT_PARSER
[291] Fix | Delete
else
[292] Fix | Delete
@parser || DEFAULT_PARSER
[293] Fix | Delete
end
[294] Fix | Delete
end
[295] Fix | Delete
[296] Fix | Delete
# Replaces self by other URI object.
[297] Fix | Delete
#
[298] Fix | Delete
def replace!(oth)
[299] Fix | Delete
if self.class != oth.class
[300] Fix | Delete
raise ArgumentError, "expected #{self.class} object"
[301] Fix | Delete
end
[302] Fix | Delete
[303] Fix | Delete
component.each do |c|
[304] Fix | Delete
self.__send__("#{c}=", oth.__send__(c))
[305] Fix | Delete
end
[306] Fix | Delete
end
[307] Fix | Delete
private :replace!
[308] Fix | Delete
[309] Fix | Delete
#
[310] Fix | Delete
# Components of the URI in the order.
[311] Fix | Delete
#
[312] Fix | Delete
def component
[313] Fix | Delete
self.class.component
[314] Fix | Delete
end
[315] Fix | Delete
[316] Fix | Delete
#
[317] Fix | Delete
# Checks the scheme +v+ component against the URI::Parser Regexp for :SCHEME.
[318] Fix | Delete
#
[319] Fix | Delete
def check_scheme(v)
[320] Fix | Delete
if v && parser.regexp[:SCHEME] !~ v
[321] Fix | Delete
raise InvalidComponentError,
[322] Fix | Delete
"bad component(expected scheme component): #{v}"
[323] Fix | Delete
end
[324] Fix | Delete
[325] Fix | Delete
return true
[326] Fix | Delete
end
[327] Fix | Delete
private :check_scheme
[328] Fix | Delete
[329] Fix | Delete
# Protected setter for the scheme component +v+.
[330] Fix | Delete
#
[331] Fix | Delete
# See also URI::Generic.scheme=.
[332] Fix | Delete
#
[333] Fix | Delete
def set_scheme(v)
[334] Fix | Delete
@scheme = v&.downcase
[335] Fix | Delete
end
[336] Fix | Delete
protected :set_scheme
[337] Fix | Delete
[338] Fix | Delete
#
[339] Fix | Delete
# == Args
[340] Fix | Delete
#
[341] Fix | Delete
# +v+::
[342] Fix | Delete
# String
[343] Fix | Delete
#
[344] Fix | Delete
# == Description
[345] Fix | Delete
#
[346] Fix | Delete
# Public setter for the scheme component +v+
[347] Fix | Delete
# (with validation).
[348] Fix | Delete
#
[349] Fix | Delete
# See also URI::Generic.check_scheme.
[350] Fix | Delete
#
[351] Fix | Delete
# == Usage
[352] Fix | Delete
#
[353] Fix | Delete
# require 'uri'
[354] Fix | Delete
#
[355] Fix | Delete
# uri = URI.parse("http://my.example.com")
[356] Fix | Delete
# uri.scheme = "https"
[357] Fix | Delete
# uri.to_s #=> "https://my.example.com"
[358] Fix | Delete
#
[359] Fix | Delete
def scheme=(v)
[360] Fix | Delete
check_scheme(v)
[361] Fix | Delete
set_scheme(v)
[362] Fix | Delete
v
[363] Fix | Delete
end
[364] Fix | Delete
[365] Fix | Delete
#
[366] Fix | Delete
# Checks the +user+ and +password+.
[367] Fix | Delete
#
[368] Fix | Delete
# If +password+ is not provided, then +user+ is
[369] Fix | Delete
# split, using URI::Generic.split_userinfo, to
[370] Fix | Delete
# pull +user+ and +password.
[371] Fix | Delete
#
[372] Fix | Delete
# See also URI::Generic.check_user, URI::Generic.check_password.
[373] Fix | Delete
#
[374] Fix | Delete
def check_userinfo(user, password = nil)
[375] Fix | Delete
if !password
[376] Fix | Delete
user, password = split_userinfo(user)
[377] Fix | Delete
end
[378] Fix | Delete
check_user(user)
[379] Fix | Delete
check_password(password, user)
[380] Fix | Delete
[381] Fix | Delete
return true
[382] Fix | Delete
end
[383] Fix | Delete
private :check_userinfo
[384] Fix | Delete
[385] Fix | Delete
#
[386] Fix | Delete
# Checks the user +v+ component for RFC2396 compliance
[387] Fix | Delete
# and against the URI::Parser Regexp for :USERINFO.
[388] Fix | Delete
#
[389] Fix | Delete
# Can not have a registry or opaque component defined,
[390] Fix | Delete
# with a user component defined.
[391] Fix | Delete
#
[392] Fix | Delete
def check_user(v)
[393] Fix | Delete
if @opaque
[394] Fix | Delete
raise InvalidURIError,
[395] Fix | Delete
"can not set user with opaque"
[396] Fix | Delete
end
[397] Fix | Delete
[398] Fix | Delete
return v unless v
[399] Fix | Delete
[400] Fix | Delete
if parser.regexp[:USERINFO] !~ v
[401] Fix | Delete
raise InvalidComponentError,
[402] Fix | Delete
"bad component(expected userinfo component or user component): #{v}"
[403] Fix | Delete
end
[404] Fix | Delete
[405] Fix | Delete
return true
[406] Fix | Delete
end
[407] Fix | Delete
private :check_user
[408] Fix | Delete
[409] Fix | Delete
#
[410] Fix | Delete
# Checks the password +v+ component for RFC2396 compliance
[411] Fix | Delete
# and against the URI::Parser Regexp for :USERINFO.
[412] Fix | Delete
#
[413] Fix | Delete
# Can not have a registry or opaque component defined,
[414] Fix | Delete
# with a user component defined.
[415] Fix | Delete
#
[416] Fix | Delete
def check_password(v, user = @user)
[417] Fix | Delete
if @opaque
[418] Fix | Delete
raise InvalidURIError,
[419] Fix | Delete
"can not set password with opaque"
[420] Fix | Delete
end
[421] Fix | Delete
return v unless v
[422] Fix | Delete
[423] Fix | Delete
if !user
[424] Fix | Delete
raise InvalidURIError,
[425] Fix | Delete
"password component depends user component"
[426] Fix | Delete
end
[427] Fix | Delete
[428] Fix | Delete
if parser.regexp[:USERINFO] !~ v
[429] Fix | Delete
raise InvalidComponentError,
[430] Fix | Delete
"bad password component"
[431] Fix | Delete
end
[432] Fix | Delete
[433] Fix | Delete
return true
[434] Fix | Delete
end
[435] Fix | Delete
private :check_password
[436] Fix | Delete
[437] Fix | Delete
#
[438] Fix | Delete
# Sets userinfo, argument is string like 'name:pass'.
[439] Fix | Delete
#
[440] Fix | Delete
def userinfo=(userinfo)
[441] Fix | Delete
if userinfo.nil?
[442] Fix | Delete
return nil
[443] Fix | Delete
end
[444] Fix | Delete
check_userinfo(*userinfo)
[445] Fix | Delete
set_userinfo(*userinfo)
[446] Fix | Delete
# returns userinfo
[447] Fix | Delete
end
[448] Fix | Delete
[449] Fix | Delete
#
[450] Fix | Delete
# == Args
[451] Fix | Delete
#
[452] Fix | Delete
# +v+::
[453] Fix | Delete
# String
[454] Fix | Delete
#
[455] Fix | Delete
# == Description
[456] Fix | Delete
#
[457] Fix | Delete
# Public setter for the +user+ component
[458] Fix | Delete
# (with validation).
[459] Fix | Delete
#
[460] Fix | Delete
# See also URI::Generic.check_user.
[461] Fix | Delete
#
[462] Fix | Delete
# == Usage
[463] Fix | Delete
#
[464] Fix | Delete
# require 'uri'
[465] Fix | Delete
#
[466] Fix | Delete
# uri = URI.parse("http://john:S3nsit1ve@my.example.com")
[467] Fix | Delete
# uri.user = "sam"
[468] Fix | Delete
# uri.to_s #=> "http://sam:V3ry_S3nsit1ve@my.example.com"
[469] Fix | Delete
#
[470] Fix | Delete
def user=(user)
[471] Fix | Delete
check_user(user)
[472] Fix | Delete
set_user(user)
[473] Fix | Delete
# returns user
[474] Fix | Delete
end
[475] Fix | Delete
[476] Fix | Delete
#
[477] Fix | Delete
# == Args
[478] Fix | Delete
#
[479] Fix | Delete
# +v+::
[480] Fix | Delete
# String
[481] Fix | Delete
#
[482] Fix | Delete
# == Description
[483] Fix | Delete
#
[484] Fix | Delete
# Public setter for the +password+ component
[485] Fix | Delete
# (with validation).
[486] Fix | Delete
#
[487] Fix | Delete
# See also URI::Generic.check_password.
[488] Fix | Delete
#
[489] Fix | Delete
# == Usage
[490] Fix | Delete
#
[491] Fix | Delete
# require 'uri'
[492] Fix | Delete
#
[493] Fix | Delete
# uri = URI.parse("http://john:S3nsit1ve@my.example.com")
[494] Fix | Delete
# uri.password = "V3ry_S3nsit1ve"
[495] Fix | Delete
# uri.to_s #=> "http://john:V3ry_S3nsit1ve@my.example.com"
[496] Fix | Delete
#
[497] Fix | Delete
def password=(password)
[498] Fix | Delete
check_password(password)
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function