HTTP 路径必须以斜杠开头吗? [英] Do HTTP paths have to start with a slash?

查看:41
本文介绍了HTTP 路径必须以斜杠开头吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 HTTP 格式的问题.HTTP 请求的第一行如下所示:

GET/path/to/resource.txt HTTP/1.1

或者换句话说:

METHOD URI HTTP-VERSION

是否要求 URI/ 字符开头,以便 /path/to/resource.txtpath/to/resource.txt 并且两者都有效?

如果不需要前导 /GET/HTTP/1.1GET HTTP/1.1 是否也一样?(GETHTTP/1.1 之间的两个空格)

解决方案

参见 RFC 7230,第 5.3 节请求目标:

<块引用>

一旦获得入站连接,客户端发送一个 HTTP带有请求目标的请求消息(第 3 节)目标 URI.请求目标有四种不同的格式,取决于请求的方法以及请求是否是代理.

<块引用>

request-target = origin-form

/绝对形式/权限形式/星号形式

您说的是起源形式,在 第 5.3 节中描述.1:

<块引用>

直接向源服务器发出请求时,除了CONNECT 或服务器范围的 OPTIONS 请求(如下详述),一个客户端必须只发送目标的绝对路径和查询组件URI 作为请求目标.如果目标 URI 的路径组件是为空,客户端必须发送/";作为起源形式中的路径request-target. 还会发送 Host 标头字段,如定义第 5.4 节.

<块引用>

例如,客户端希望检索资源标识为

<块引用>

http://www.example.org/where?q=now

<块引用>

直接从源服务器打开(或重用)一个 TCP连接到主机www.example.org"的端口 80;并发送行:

<块引用>

GET/where?q=now HTTP/1.1

 主机:www.example.org

还有一条绝对路径"之前在 第 2.7 节 中定义为

<块引用>

 absolute-path = 1*( "/" 段)

所以是的,some 目标标识符是必需的,路径总是以斜线为前缀,如果未知、为空或不适用,则为 /.

I have a question regarding the HTTP format. The first line of a HTTP request looks something like this:

GET /path/to/resource.txt HTTP/1.1

or in other words:

METHOD URI HTTP-VERSION

Is it required that the URI starts with the / character so that /path/to/resource.txt would be the same as path/to/resource.txt and both are valid?

And if a leading / is not required, are GET / HTTP/1.1 and GET HTTP/1.1 the same too? (two spaces between GET and HTTP/1.1)

解决方案

See RFC 7230, section 5.3 Request target:

Once an inbound connection is obtained, the client sends an HTTP request message (Section 3) with a request-target derived from the target URI. There are four distinct formats for the request-target, depending on both the method being requested and whether the request is to a proxy.

request-target = origin-form

                / absolute-form
                / authority-form
                / asterisk-form

You're talking about origin-form, described in subsection 5.3.1:

When making a request directly to an origin server, other than a CONNECT or server-wide OPTIONS request (as detailed below), a client MUST send only the absolute path and query components of the target URI as the request-target. If the target URI's path component is empty, the client MUST send "/" as the path within the origin-form of request-target. A Host header field is also sent, as defined in Section 5.4.

For example, a client wishing to retrieve a representation of the resource identified as

http://www.example.org/where?q=now

directly from the origin server would open (or reuse) a TCP connection to port 80 of the host "www.example.org" and send the lines:

GET /where?q=now HTTP/1.1

 Host: www.example.org

And an "absolute path" is defined earlier in section 2.7 as

 absolute-path = 1*( "/" segment )

So yes, some target identifier is required, the path is always prefixed with a slash, and if unknown, empty or inapplicable, it is /.

这篇关于HTTP 路径必须以斜杠开头吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆