Do HTTP paths have to start with a slash?

2020-03-25 03:15发布

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)

1条回答
男人必须洒脱
2楼-- · 2020-03-25 03:37

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 /.

查看更多
登录 后发表回答