Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do HTTP paths have to start with a slash?

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)

like image 523
MinecraftShamrock Avatar asked Dec 24 '14 14:12

MinecraftShamrock


Video Answer


1 Answers

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

like image 164
CodeCaster Avatar answered Oct 22 '22 18:10

CodeCaster