class URI::HTTP
RFC1738 section 3.3 中定义了 HTTP URI 的语法。
请注意,Ruby 的 URI 库允许包含用户名和密码的 HTTP URL。根据 RFC,这不合法,但在 MS04-004 安全更新之前,Internet Explorer 5 和 6 支持此功能。请参阅 <URL:support.microsoft.com/kb/834489>。
Constants
Public Class Methods
Source
# File lib/uri/http.rb, line 59 def self.build(args) tmp = Util.make_components_hash(self, args) super(tmp) end
描述
根据组件创建新的 URI::HTTP 对象,并进行语法检查。
接受的组件包括 userinfo、host、port、path、query 和 fragment。
组件应以 Array 的形式提供,或者以 Hash 的形式提供,其中键通过在组件名称前加上冒号形成。
如果使用 Array,组件必须按 [userinfo, host, port, path, query, fragment] 的顺序传递。
示例
uri = URI::HTTP.build(host: 'www.example.com', path: '/foo/bar') uri = URI::HTTP.build([nil, "www.example.com", nil, "/path", "query", 'fragment'])
目前,如果传递了 userinfo 组件,此方法会生成不符合 RFC 1738 的无效 HTTP URI。
调用超类方法
URI::Generic::buildPublic Instance Methods
Source
# File lib/uri/http.rb, line 65 def check_host(v) ret = super if ret && v.empty? raise InvalidComponentError, "bad component(expected host component): #{v}" end ret end
不允许空主机名,因为 RFC 3986 不允许。
调用超类方法
URI::Generic#check_hostSource
# File lib/uri/http.rb, line 131 def origin "#{scheme}://#{authority}" end
描述
返回 HTTP URI 的 origin,定义在 www.rfc-editor.org/rfc/rfc6454。
示例
URI::HTTP.build(host: 'www.example.com', path: '/foo/bar').origin #=> "http://www.example.com" URI::HTTP.build(host: 'www.example.com', port: 8000, path: '/foo/bar').origin #=> "http://www.example.com:8000" URI::HTTP.build(host: 'www.example.com', port: 80, path: '/foo/bar').origin #=> "http://www.example.com" URI::HTTPS.build(host: 'www.example.com', path: '/foo/bar').origin #=> "https://www.example.com"
Source
# File lib/uri/http.rb, line 89 def request_uri return unless @path url = @query ? "#@path?#@query" : @path.dup url.start_with?(?/.freeze) ? url : ?/ + url end
描述
返回 HTTP 请求的完整路径,如 Net::HTTP::Get 所需。
如果 URI 包含查询,则完整路径为 URI#path + ‘?’ + URI#query。否则,路径就是 URI#path。
示例
uri = URI::HTTP.build(path: '/foo/bar', query: 'test=true') uri.request_uri # => "/foo/bar?test=true"