Do HTTP paths have to start with a slash?

前端 未结 1 591
情书的邮戳
情书的邮戳 2021-01-13 08:50

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

1条回答
  •  抹茶落季
    2021-01-13 09:35

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

    0 讨论(0)
提交回复
热议问题