跳到主要内容

6. 响应 (Response)

6 Response

server 在接收并解释 request message 后, 会以 HTTP response message 响应.

   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

Response message 的第一行是 Status-Line, 由 protocol version 后跟数字 status code 及其关联文本短语组成, 每个元素由 SP 字符分隔. 除最后的 CRLF 序列外, 不允许出现 CR 或 LF.

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

6.1.1 Status Code 和 Reason Phrase

Status-Code 元素是一个 3 位整数结果码, 表示理解并满足 request 的尝试结果. 这些代码在第 10 节中完整定义. Reason-Phrase 用于给出 Status-Code 的简短文本说明. Status-Code 供自动机使用, Reason-Phrase 供人类用户使用. client 不要求检查或显示 Reason-Phrase.

Status-Code 的第一位数字定义 response 类别. 最后两位数字没有分类作用. 第一位数字有 5 个取值:

  - 1xx: Informational - Request received, continuing process

- 2xx: Success - The action was successfully received,
understood, and accepted

- 3xx: Redirection - Further action must be taken in order to
complete the request

- 4xx: Client Error - The request contains bad syntax or cannot
be fulfilled

- 5xx: Server Error - The server failed to fulfill an apparently
valid request

下面列出 HTTP/1.1 定义的各个数字 status code 值, 以及一组对应 Reason-Phrase 示例. 此处列出的 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 status code 是可扩展的. HTTP 应用不要求理解所有已注册 status code 的含义, 尽管这种理解显然是理想的. 但是, 应用 MUST 根据第一位数字理解任何 status code 的类别, 并把任何无法识别的 response 视为等价于该类别的 x00 status code, 但有一个例外: 无法识别的 response MUST NOT 被缓存. 例如, 如果 client 收到无法识别的 status code 431, 它可以安全地假定自己的 request 有问题, 并把 response 当作收到 400 status code 处理. 在这些情况下, user agent SHOULD 向用户呈现随 response 返回的 entity, 因为该 entity 很可能包含解释异常 status 的人类可读信息.

6.2 Response Header Field

response-header field 允许 server 传递无法放在 Status-Line 中的关于 response 的额外信息. 这些 header field 提供关于 server 以及进一步访问 Request-URI 所标识 resource 的信息.

   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

response-header field name 只有结合协议版本变化才能可靠扩展. 但是, 如果通信各方都认识到新的或实验性 header field 是 response-header field, 则 MAY 赋予它们 response-header field 的语义. 未识别的 header field 被当作 entity-header field 处理.