Spec-Zone.ru › Ruby 3.3

модуль URI

Включенные модули:
URI::RFC2396_REGEXP

URI — это модуль, предоставляющий классы для обработки универсальных идентификаторов ресурсов (RFC2396).

Возможности

  • Единый способ обработки URI.

  • Гибкость для внедрения пользовательских схем URI.

  • Гибкость для использования альтернативного URI::Parser (или просто различных шаблонов и регулярных выражений).

Пример использования

require 'uri'

uri = URI("http://foo.com/posts?id=30&limit=5#time=1305298413")
#=> #<URI::HTTP http://foo.com/posts?id=30&limit=5#time=1305298413>

uri.scheme    #=> "http"
uri.host      #=> "foo.com"
uri.path      #=> "/posts"
uri.query     #=> "id=30&limit=5"
uri.fragment  #=> "time=1305298413"

uri.to_s      #=> "http://foo.com/posts?id=30&limit=5#time=1305298413"

Добавление пользовательских URI

module URI
  class RSYNC < Generic
    DEFAULT_PORT = 873
  end
  register_scheme 'RSYNC', RSYNC
end
#=> URI::RSYNC

URI.scheme_list
#=> {"FILE"=>URI::File, "FTP"=>URI::FTP, "HTTP"=>URI::HTTP,
#    "HTTPS"=>URI::HTTPS, "LDAP"=>URI::LDAP, "LDAPS"=>URI::LDAPS,
#    "MAILTO"=>URI::MailTo, "RSYNC"=>URI::RSYNC}

uri = URI("rsync://rsync.foo.com")
#=> #<URI::RSYNC rsync://rsync.foo.com>

Ссылки на RFC

Хорошее место для просмотра спецификаций RFC — www.ietf.org/rfc.html.

Вот список всех связанных RFC:

  • RFC822

  • RFC1738

  • RFC2255

  • RFC2368

  • RFC2373

  • RFC2396

  • RFC2732

  • RFC3986

Class дерево

  • URI::Generic (в uri/generic.rb)

    • URI::File — (в uri/file.rb)

    • URI::FTP — (в uri/ftp.rb)

    • URI::HTTP — (в uri/http.rb)

      • URI::HTTPS — (в uri/https.rb)

    • URI::LDAP — (в uri/ldap.rb)

      • URI::LDAPS — (в uri/ldaps.rb)

    • URI::MailTo — (в uri/mailto.rb)

  • URI::Parser — (в uri/common.rb)

  • URI::REGEXP — (в uri/common.rb)

    • URI::REGEXP::PATTERN — (в uri/common.rb)

  • URI::Util — (в uri/common.rb)

  • URI::Error — (в uri/common.rb)

    • URI::InvalidURIError — (в uri/common.rb)

    • URI::InvalidComponentError — (в uri/common.rb)

    • URI::BadURIError — (в uri/common.rb)

Информация о копирайте

Автор

Akira Yamada <akira@ruby-lang.org>

Документация

Akira Yamada <akira@ruby-lang.org> Dmitry V. Sabanin <sdmitry@lrn.ru> Vincent Batts <vbatts@hashbangbash.com>

Лицензия

Авторское право © 2001 akira yamada <akira@ruby-lang.org> Вы можете распространять и/или изменять его на тех же условиях, что и Ruby.

Константы

DEFAULT_PARSER

URI::Parser.new

INITIAL_SCHEMES
Parser
REGEXP
RFC3986_PARSER
TBLENCURICOMP_

Публичные методы класса

decode_uri_component(str, enc=Encoding::UTF_8) Показать исходный код
# File lib/uri/common.rb, line 379
def self.decode_uri_component(str, enc=Encoding::UTF_8)
  _decode_uri_component(/%\h\h/, str, enc)
end

Как URI.decode_www_form_component, за исключением того, что '+' сохраняется.

decode_www_form(str, enc=Encoding::UTF_8, separator: '&', use__charset_: false, isindex: false) Показать исходный код
# File lib/uri/common.rb, line 554
def self.decode_www_form(str, enc=Encoding::UTF_8, separator: '&', use__charset_: false, isindex: false)
  raise ArgumentError, "the input of #{self.name}.#{__method__} must be ASCII only string" unless str.ascii_only?
  ary = []
  return ary if str.empty?
  enc = Encoding.find(enc)
  str.b.each_line(separator) do |string|
    string.chomp!(separator)
    key, sep, val = string.partition('=')
    if isindex
      if sep.empty?
        val = key
        key = +''
      end
      isindex = false
    end

    if use__charset_ and key == '_charset_' and e = get_encoding(val)
      enc = e
      use__charset_ = false
    end

    key.gsub!(/\+|%\h\h/, TBLDECWWWCOMP_)
    if val
      val.gsub!(/\+|%\h\h/, TBLDECWWWCOMP_)
    else
      val = +''
    end

    ary << [key, val]
  end
  ary.each do |k, v|
    k.force_encoding(enc)
    k.scrub!
    v.force_encoding(enc)
    v.scrub!
  end
  ary
end

Возвращает пары имя/значение, полученные из заданной строки str, которая должна быть строкой ASCII.

Метод может использоваться для декодирования тела объекта Net::HTTPResponse res, для которого res['Content-Type'] является 'application/x-www-form-urlencoded'.

Возвращаемые данные представляют собой массив 2-элементных подмассивов; каждый подмассив — это пара имя/значение (оба являются строками). Каждая возвращаемая строка имеет кодировку enc, и из нее были удалены недопустимые символы с помощью String#scrub.

Простой пример:

URI.decode_www_form('foo=0&bar=1&baz')
# => [["foo", "0"], ["bar", "1"], ["baz", ""]]

Возвращаемые строки имеют определенные преобразования, аналогичные тем, которые выполняются в URI.decode_www_form_component:

URI.decode_www_form('f%23o=%2F&b-r=%24&b+z=%40')
# => [["f#o", "/"], ["b-r", "$"], ["b z", "@"]]

Заданная строка может содержать последовательные разделители:

URI.decode_www_form('foo=0&&bar=1&&baz=2')
# => [["foo", "0"], ["", ""], ["bar", "1"], ["", ""], ["baz", "2"]]

Можно указать другой разделитель:

URI.decode_www_form('foo=0--bar=1--baz', separator: '--')
# => [["foo", "0"], ["bar", "1"], ["baz", ""]]
decode_www_form_component(str, enc=Encoding::UTF_8) Показать исходный код
# File lib/uri/common.rb, line 368
def self.decode_www_form_component(str, enc=Encoding::UTF_8)
  _decode_uri_component(/\+|%\h\h/, str, enc)
end

Возвращает строку, декодированную из заданной строки, закодированной в URL str.

Заданная строка сначала кодируется как Encoding::ASCII-8BIT (с использованием String#b), затем декодируется (как показано ниже) и, наконец, принудительно кодируется в заданную кодировку enc.

Возвращаемая строка:

  • Сохраняет:

    • Символы '*', '.', '-' и '_'.

    • Символы в диапазонах 'a'..'z', 'A'..'Z' и '0'..'9'.

    Пример:

    URI.decode_www_form_component('*.-_azAZ09')
    # => "*.-_azAZ09"
    
  • Преобразует:

    • Символ '+' в символ ' '.

    • Каждое «процентное обозначение» в символ ASCII.

    Пример:

    URI.decode_www_form_component('Here+are+some+punctuation+characters%3A+%2C%3B%3F%3A')
    # => "Here are some punctuation characters: ,;?:"
    

Связанное: URI.decode_uri_component (сохраняет '+').

encode_uri_component(str, enc=nil) Показать исходный код
# File lib/uri/common.rb, line 374
def self.encode_uri_component(str, enc=nil)
  _encode_uri_component(/[^*\-.0-9A-Z_a-z]/, TBLENCURICOMP_, str, enc)
end

Как URI.encode_www_form_component, за исключением того, что ' ' (пробел) кодируется как '%20' (вместо '+').

encode_www_form(enum, enc=nil) Показать исходный код
# File lib/uri/common.rb, line 501
def self.encode_www_form(enum, enc=nil)
  enum.map do |k,v|
    if v.nil?
      encode_www_form_component(k, enc)
    elsif v.respond_to?(:to_ary)
      v.to_ary.map do |w|
        str = encode_www_form_component(k, enc)
        unless w.nil?
          str << '='
          str << encode_www_form_component(w, enc)
        end
      end.join('&')
    else
      str = encode_www_form_component(k, enc)
      str << '='
      str << encode_www_form_component(v, enc)
    end
  end.join('&')
end

Возвращает строку, закодированную в URL, полученную из заданного Enumerable enum.

Результат подходит для использования в качестве данных формы для HTTP-запроса, Content-Type которого 'application/x-www-form-urlencoded'.

Возвращаемая строка состоит из элементов enum, каждый из которых преобразуется в одну или несколько строк, закодированных в URL, и все они соединяются символом '&'.

Простые примеры:

URI.encode_www_form([['foo', 0], ['bar', 1], ['baz', 2]])
# => "foo=0&bar=1&baz=2"
URI.encode_www_form({foo: 0, bar: 1, baz: 2})
# => "foo=0&bar=1&baz=2"

Возвращаемая строка формируется с помощью метода URI.encode_www_form_component, который преобразует определенные символы:

URI.encode_www_form('f#o': '/', 'b-r': '$', 'b z': '@')
# => "f%23o=%2F&b-r=%24&b+z=%40"

Когда enum является похожим на массив, каждый элемент ele преобразуется в поле:

  • Если ele является массивом из двух или более элементов, поле формируется из его первых двух элементов (а все дополнительные элементы игнорируются):

    name = URI.encode_www_form_component(ele[0], enc)
    value = URI.encode_www_form_component(ele[1], enc)
    "#{name}=#{value}"
    

    Примеры:

    URI.encode_www_form([%w[foo bar], %w[baz bat bah]])
    # => "foo=bar&baz=bat"
    URI.encode_www_form([['foo', 0], ['bar', :baz, 'bat']])
    # => "foo=0&bar=baz"
    
  • Если ele является массивом из одного элемента, поле формируется из ele[0]:

    URI.encode_www_form_component(ele[0])
    

    Пример:

    URI.encode_www_form([['foo'], [:bar], [0]])
    # => "foo&bar&0"
    
  • В противном случае поле формируется из ele:

    URI.encode_www_form_component(ele)
    

    Пример:

    URI.encode_www_form(['foo', :bar, 0])
    # => "foo&bar&0"
    

Элементы массивоподобного enum могут быть смешанными:

URI.encode_www_form([['foo', 0], ['bar', 1, 2], ['baz'], :bat])
# => "foo=0&bar=1&baz&bat"

Когда enum является похожим на хэш, каждая пара key/value преобразуется в одно или несколько полей:

  • Если value является преобразуемым в массив, каждый элемент ele в value объединяется с key для формирования поля:

    name = URI.encode_www_form_component(key, enc)
    value = URI.encode_www_form_component(ele, enc)
    "#{name}=#{value}"
    

    Пример:

    URI.encode_www_form({foo: [:bar, 1], baz: [:bat, :bam, 2]})
    # => "foo=bar&foo=1&baz=bat&baz=bam&baz=2"
    
  • В противном случае key и value объединяются для формирования поля:

    name = URI.encode_www_form_component(key, enc)
    value = URI.encode_www_form_component(value, enc)
    "#{name}=#{value}"
    

    Пример:

    URI.encode_www_form({foo: 0, bar: 1, baz: 2})
    # => "foo=0&bar=1&baz=2"
    

Элементы хэшоподобного enum могут быть смешанными:

URI.encode_www_form({foo: [0, 1], bar: 2})
# => "foo=0&foo=1&bar=2"
encode_www_form_component(str, enc=nil) Показать исходный код
# File lib/uri/common.rb, line 335
def self.encode_www_form_component(str, enc=nil)
  _encode_uri_component(/[^*\-.0-9A-Z_a-z]/, TBLENCWWWCOMP_, str, enc)
end

Возвращает строку, закодированную в URL, полученную из заданной строки str.

Возвращаемая строка:

  • Сохраняет:

    • Символы '*', '.', '-' и '_'.

    • Символы в диапазонах 'a'..'z', 'A'..'Z' и '0'..'9'.

    Пример:

    URI.encode_www_form_component('*.-_azAZ09')
    # => "*.-_azAZ09"
    
  • Преобразует:

    • Символ ' ' в символ '+'.

    • Любой другой символ в «процентное обозначение»; процентное обозначение для символа c — это '%%%X' % c.ord.

    Пример:

    URI.encode_www_form_component('Here are some punctuation characters: ,;?:')
    # => "Here+are+some+punctuation+characters%3A+%2C%3B%3F%3A"
    

Кодировка:

  • Если str имеет кодировку Encoding::ASCII_8BIT, аргумент enc игнорируется.

  • В противном случае str сначала преобразуется в Encoding::UTF_8 (с соответствующими заменами символов), а затем в кодировку enc.

В любом случае, возвращаемая строка имеет принудительную кодировку Encoding::US_ASCII.

Связанное: URI.encode_uri_component (кодирует ' ' как '%20').

for(scheme, *arguments, default: Generic) Показать исходный код
# File lib/uri/common.rb, line 123
def self.for(scheme, *arguments, default: Generic)
  const_name = scheme.to_s.upcase

  uri_class = INITIAL_SCHEMES[const_name]
  uri_class ||= if /\A[A-Z]\w*\z/.match?(const_name) && Schemes.const_defined?(const_name, false)
    Schemes.const_get(const_name, false)
  end
  uri_class ||= default

  return uri_class.new(scheme, *arguments)
end

Возвращает новый объект, созданный из заданных scheme, arguments и default:

  • Новый объект является экземпляром URI.scheme_list[scheme.upcase].

  • Объект инициализируется вызовом инициализатора класса с использованием scheme и arguments. См. URI::Generic.new.

Примеры:

values = ['john.doe', 'www.example.com', '123', nil, '/forum/questions/', nil, 'tag=networking&order=newest', 'top']
URI.for('https', *values)
# => #<URI::HTTPS https://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top>
URI.for('foo', *values, default: URI::HTTP)
# => #<URI::HTTP foo://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top>
join(*str) Показать исходный код
# File lib/uri/common.rb, line 211
def self.join(*str)
  RFC3986_PARSER.join(*str)
end

Объединяет заданные строки URI str согласно RFC 2396.

Каждая строка в str преобразуется в RFC3986 URI перед объединением.

Примеры:

URI.join("http://example.com/","main.rbx")
# => #<URI::HTTP http://example.com/main.rbx>

URI.join('http://example.com', 'foo')
# => #<URI::HTTP http://example.com/foo>

URI.join('http://example.com', '/foo', '/bar')
# => #<URI::HTTP http://example.com/bar>

URI.join('http://example.com', '/foo', 'bar')
# => #<URI::HTTP http://example.com/bar>

URI.join('http://example.com', '/foo/', 'bar')
# => #<URI::HTTP http://example.com/foo/bar>
open(name, *rest, &block) Показать исходный код
# File lib/open-uri.rb, line 23
def self.open(name, *rest, &block)
  if name.respond_to?(:open)
    name.open(*rest, &block)
  elsif name.respond_to?(:to_str) &&
        %r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ name &&
        (uri = URI.parse(name)).respond_to?(:open)
    uri.open(*rest, &block)
  else
    super
  end
end

Позволяет открывать различные ресурсы, включая URI.

Если первый аргумент отвечает методу «open», вызывается «open» с остальными аргументами.

Если первый аргумент является строкой, начинающейся с (protocol)://, она анализируется с помощью URI.parse. Если проанализированный объект отвечает методу «open», вызывается «open» с остальными аргументами.

В противном случае вызывается Kernel#open.

OpenURI::OpenRead#open предоставляет URI::HTTP#open, URI::HTTPS#open и URI::FTP#open, Kernel#open.

Мы можем принимать URI и строки, начинающиеся с http://, https:// и ftp://. В этих случаях открытый объект файла дополняется OpenURI::Meta.

Вызов метода суперкласса
parse(uri) Показать исходный код
# File lib/uri/common.rb, line 184
def self.parse(uri)
  RFC3986_PARSER.parse(uri)
end

Возвращает новый объект URI, построенный из заданной строки uri:

URI.parse('https://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top')
# => #<URI::HTTPS https://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top>
URI.parse('http://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top')
# => #<URI::HTTP http://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top>

Рекомендуется сначала ::escape строку uri если она может содержать недопустимые URI символы.

register_scheme(scheme, klass) Показать исходный код
# File lib/uri/common.rb, line 79
def self.register_scheme(scheme, klass)
  Schemes.const_set(scheme.to_s.upcase, klass)
end

Регистрирует заданный klass как класс, который будет создан при разборе URI с заданным scheme:

URI.register_scheme('MS_SEARCH', URI::Generic) # => URI::Generic
URI.scheme_list['MS_SEARCH']                   # => URI::Generic

Обратите внимание, что после вызова String#upcase на scheme, он должен быть допустимым именем константы.

scheme_list() Показать исходный код
# File lib/uri/common.rb, line 97
def self.scheme_list
  Schemes.constants.map { |name|
    [name.to_s.upcase, Schemes.const_get(name)]
  }.to_h
end

Возвращает хеш определённых схем:

URI.scheme_list
# =>
{"MAILTO"=>URI::MailTo,
 "LDAPS"=>URI::LDAPS,
 "WS"=>URI::WS,
 "HTTP"=>URI::HTTP,
 "HTTPS"=>URI::HTTPS,
 "LDAP"=>URI::LDAP,
 "FILE"=>URI::File,
 "FTP"=>URI::FTP}

Связанно с: URI.register_scheme.

split(uri) Показать исходный код
# File lib/uri/common.rb, line 170
def self.split(uri)
  RFC3986_PARSER.split(uri)
end

Возвращает массив из 9 элементов, представляющий части URI, сформированные из строки uri; каждый элемент массива — строка или nil:

names = %w[scheme userinfo host port registry path opaque query fragment]
values = URI.split('https://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top')
names.zip(values)
# =>
[["scheme", "https"],
 ["userinfo", "john.doe"],
 ["host", "www.example.com"],
 ["port", "123"],
 ["registry", nil],
 ["path", "/forum/questions/"],
 ["opaque", nil],
 ["query", "tag=networking&order=newest"],
 ["fragment", "top"]]

Методы класса (приватные)

_decode_uri_component(regexp, str, enc) Показать исходный код
# File lib/uri/common.rb, line 397
def self._decode_uri_component(regexp, str, enc)
  raise ArgumentError, "invalid %-encoding (#{str})" if /%(?!\h\h)/.match?(str)
  str.b.gsub(regexp, TBLDECWWWCOMP_).force_encoding(enc)
end
_encode_uri_component(regexp, table, str, enc) Показать исходный код
# File lib/uri/common.rb, line 383
def self._encode_uri_component(regexp, table, str, enc)
  str = str.to_s.dup
  if str.encoding != Encoding::ASCII_8BIT
    if enc && enc != Encoding::ASCII_8BIT
      str.encode!(Encoding::UTF_8, invalid: :replace, undef: :replace)
      str.encode!(enc, fallback: ->(x){"&##{x.ord};"})
    end
    str.force_encoding(Encoding::ASCII_8BIT)
  end
  str.gsub!(regexp, table)
  str.force_encoding(Encoding::US_ASCII)
end

Ruby Core © 1993–2022 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API