关于 HTTP 请求标准

4
GET http://stackoverflow.com/questions HTTP/1.1
Host: stackoverflow.com

HTTP标准是否要求GET请求使用绝对或相对地址?当请求在代理中时呢?
我问这个问题是因为我觉得它与Host信息重复了。
3个回答

5
GET / HTTP/1.1

这是一个有效的请求行。完整路径不是必需的。

5.1.2 Request-URI

The Request-URI is a Uniform Resource Identifier (section 3.2) and identifies the resource upon which to apply the request.

   Request-URI    = "*" | absoluteURI | abs_path | authority

The four options for Request-URI are dependent on the nature of the request. The asterisk "*" means that the request does not apply to a particular resource, but to the server itself, and is only allowed when the method used does not necessarily apply to a resource. One example would be

   OPTIONS * HTTP/1.1

The absoluteURI form is REQUIRED when the request is being made to a proxy. The proxy is requested to forward the request or service it from a valid cache, and return the response. Note that the proxy MAY forward the request on to another proxy or directly to the server specified by the absoluteURI. In order to avoid request loops, a proxy MUST be able to recognize all of its server names, including any aliases, local variations, and the numeric IP address. An example Request-Line would be:

   GET http://www.w3.org/pub/WWW/TheProject.html HTTP/1.1

To allow for transition to absoluteURIs in all requests in future versions of HTTP, all HTTP/1.1 servers MUST accept the absoluteURI form in requests, even though HTTP/1.1 clients will only generate them in requests to proxies.


然而,需要提供Host头。 - hammar
是的,我本意是说“请求行”,而不是“请求”。我已经更新了答案。 - SpliFF
从绝对URI中提取主机名还是使用主机头是标准做法? - DriverBoy
@DriverBoy:从上面的内容来看,如果你正在编写代理程序,那么你应该使用URI。但是,如果你正在编写终端点(HTTP服务器),那么你应该遵守“Host”头部。 - Matthew Scharley
小问题:除非向代理发出请求,否则不需要完整路径,但在这种情况下,它是必需的。 - james.garriss

0
你可以查阅HTTP RFC来了解这个。

3.2.1 一般语法

在 HTTP 中,URI 可以采用绝对形式或相对于某个已知基础 URI [11] 的形式表示,这取决于它们的使用上下文。


0
主机细节不是必需的。 相对路径就足够了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接