Skip to main content

6. Response (响应)

在接收和解释请求消息后,服务器用 HTTP 响应消息进行响应。

Response      = Status-Line               ; Section 6.1
*(( general-header ; Section 4.5
| response-header ; Section 6.2
| entity-header ) CRLF) ; Section 7.1
CRLF
[ message-body ] ; Section 7.2

6.1 Status-Line (状态行)

响应消息的第一行是 Status-Line,由协议版本后跟数字状态码及其关联的文本短语组成,每个元素由 SP 字符分隔。除了最终的 CRLF 序列外,不允许 CR 或 LF。

Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF

6.1.1 Status Code and Reason Phrase (状态码和原因短语)

Status-Code 元素是一个 3 位数字的整数结果代码,表示理解和满足请求的尝试结果。这些代码在第 10 节中完整定义。Reason-Phrase 旨在提供 Status-Code 的简短文本描述。Status-Code 旨在供自动机使用,而 Reason-Phrase 旨在供人类用户使用。客户端不需要检查或显示 Reason-Phrase。

Status-Code 的第一位数字定义响应的类别。最后两位数字没有任何分类作用。第一位数字有 5 个值:

  • 1xx: Informational (信息性) - 请求已接收,继续处理

  • 2xx: Success (成功) - 操作已成功接收、理解和接受

  • 3xx: Redirection (重定向) - 必须采取进一步操作才能完成请求

  • 4xx: Client Error (客户端错误) - 请求包含错误的语法或无法完成

  • 5xx: Server Error (服务器错误) - 服务器未能完成明显有效的请求

为 HTTP/1.1 定义的数字状态码的各个值以及一组相应的 Reason-Phrase 示例如下所示。此处列出的原因短语仅是建议——它们可以 (MAY) 被本地等效项替换而不影响协议。

Status-Code    =
"100" ; Section 10.1.1: Continue
| "101" ; Section 10.1.2: Switching Protocols
| "200" ; Section 10.2.1: OK
| "201" ; Section 10.2.2: Created
| "202" ; Section 10.2.3: Accepted
| "203" ; Section 10.2.4: Non-Authoritative Information
| "204" ; Section 10.2.5: No Content
| "205" ; Section 10.2.6: Reset Content
| "206" ; Section 10.2.7: Partial Content
| "300" ; Section 10.3.1: Multiple Choices
| "301" ; Section 10.3.2: Moved Permanently
| "302" ; Section 10.3.3: Found
| "303" ; Section 10.3.4: See Other
| "304" ; Section 10.3.5: Not Modified
| "305" ; Section 10.3.6: Use Proxy
| "307" ; Section 10.3.8: Temporary Redirect
| "400" ; Section 10.4.1: Bad Request
| "401" ; Section 10.4.2: Unauthorized
| "402" ; Section 10.4.3: Payment Required
| "403" ; Section 10.4.4: Forbidden
| "404" ; Section 10.4.5: Not Found
| "405" ; Section 10.4.6: Method Not Allowed
| "406" ; Section 10.4.7: Not Acceptable
| "407" ; Section 10.4.8: Proxy Authentication Required
| "408" ; Section 10.4.9: Request Time-out
| "409" ; Section 10.4.10: Conflict
| "410" ; Section 10.4.11: Gone
| "411" ; Section 10.4.12: Length Required
| "412" ; Section 10.4.13: Precondition Failed
| "413" ; Section 10.4.14: Request Entity Too Large
| "414" ; Section 10.4.15: Request-URI Too Large
| "415" ; Section 10.4.16: Unsupported Media Type
| "416" ; Section 10.4.17: Requested range not satisfiable
| "417" ; Section 10.4.18: Expectation Failed
| "500" ; Section 10.5.1: Internal Server Error
| "501" ; Section 10.5.2: Not Implemented
| "502" ; Section 10.5.3: Bad Gateway
| "503" ; Section 10.5.4: Service Unavailable
| "504" ; Section 10.5.5: Gateway Time-out
| "505" ; Section 10.5.6: HTTP Version not supported
| extension-code

extension-code = 3DIGIT
Reason-Phrase = *<TEXT, excluding CR, LF>

HTTP 状态码是可扩展的。HTTP 应用程序不需要理解所有已注册状态码的含义,尽管这种理解显然是可取的。但是,应用程序必须 (MUST) 理解任何状态码的类别(如第一位数字所示),并将任何无法识别的响应视为等同于该类别的 x00 状态码,但无法识别的响应禁止 (MUST NOT) 被缓存。例如,如果客户端接收到无法识别的状态码 431,它可以安全地假设其请求存在问题,并将响应视为已接收到 400 状态码。在这种情况下,用户代理应该 (SHOULD) 向用户呈现响应中包含的实体,因为该实体可能包含人类可读的信息以解释异常状态。

6.2 Response Header Fields (响应头字段)

响应头字段允许服务器传递关于响应的附加信息,这些信息无法放在 Status-Line 中。这些头字段提供关于服务器的信息以及关于通过 Request-URI 标识的资源的进一步访问的信息。

response-header = Accept-Ranges           ; Section 14.5
| Age ; Section 14.6
| ETag ; Section 14.19
| Location ; Section 14.30
| Proxy-Authenticate ; Section 14.33
| Retry-After ; Section 14.37
| Server ; Section 14.38
| Vary ; Section 14.44
| WWW-Authenticate ; Section 14.47

响应头字段名称只能通过更改协议版本来可靠地扩展。但是,可以在不更改协议版本的情况下为字段值赋予新的或实验性的头字段。无法识别的头字段应该 (SHOULD) 被接收方视为实体头字段。