首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

URI::HTTP

Parent:URI::GenericIncluded modules:OpenURI::OpenRead

HTTP URI的语法在RFC1738第3.3节中定义。

请注意,Ruby URI库允许包含用户名和密码的HTTP URL。根据RFC,这是不合法的,但在MS04-004安全更新之前,在Internet Explorer 5和6中受支持。请参阅URL:[support.microsoft.com/kb/834489](http://support.microsoft.com/kb/834489)。

常量

COMPONENT

URI::HTTP的可用组件的数组

DEFAULT_PORT

URI::HTTP的默认端口为80

公共类方法

build(args) Show source

描述

从组件创建一个新的URI::HTTP对象,并进行语法检查。

接受的组件是用户信息,主机,端口,路径,查询和片段。

这些组件应该以数组的形式提供,或者以组件名称前面用冒号形成的哈希形式提供。

如果使用数组,则必须按照userinfo,host,port,path,query,fragment的顺序传递组件。

示例:

代码语言:javascript
复制
newuri = URI::HTTP.build(host: 'www.example.com', path: '/foo/bar')

newuri = URI::HTTP.build([nil, "www.example.com", nil, "/path",
  "query", 'fragment'])

目前,如果传递userinfo组件,则此方法根据RFC 1738生成无效的HTTP URI。

调用超类方法URI::Generic.build

代码语言:javascript
复制
# File lib/uri/http.rb, line 59
def self.build(args)
  tmp = Util.make_components_hash(self, args)
  super(tmp)
end

公共实例方法

request_uri() Show source

代码语言:javascript
复制
#
# == Description
#
# Create a new URI::HTTP object from generic URI components as per
# RFC 2396. No HTTP-specific syntax checking (as per RFC 1738) is
# performed.
#
# Arguments are +scheme+, +userinfo+, +host+, +port+, +registry+, +path+,
# +opaque+, +query+ and +fragment+, in that order.
#
# Example:
#
#     uri = URI::HTTP.new("http", nil, "www.example.com", nil, nil,
#                         "/path", nil, "query", "fragment")
#
#
# See also URI::Generic.new
#
def initialize(*arg)
  super(*arg)
end

# == Description

#

# Returns the full path for an HTTP request, as required by Net::HTTP::Get.

#

# If the URI contains a query, the full path is URI#path + '?' + URI#query.

# Otherwise, the path is simply URI#path.

#

# Example:

#

# newuri = ::build(path: '/foo/bar', query: 'test=true')

# newuri.request_uri

# => “/foo/bar?test=true”

代码语言:javascript
复制
# File lib/uri/http.rb, line 101
def request_uri
  return unless @path

  url = @query ? "#@path?#@query" : @path.dup
  url.start_with?(?/.freeze) ? url : ?/ + url
end

扫码关注腾讯云开发者

领取腾讯云代金券