在接收和解析请求信息后,服务器用 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)
响应信息的第一行是状态行,由协议版本和数字状态代码及其相关的文本短语组成,每个元素之间用 SP 字符分隔。除了最后的 CRLF 序列,不允许有 CR 或 LF。
Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
6.1.1 状态码和原因短语
状态代码 (Status-Code) 元素是一个 3 位数的整数结果代码,是理解和满足请求的尝试。这些代码在第 10 节有完整的定义。Reason-Phrase 的目的是对 Status-Code 做一个简短的文本描述。状态代码是为自动机使用的,而原因短语是为人类用户准备的。客户端不需要检查或显示 “原因句”。
状态代码的第一个数字定义了响应的类别。后面两个数字不具有任何分类作用。第一个数字有 5 个值:
- 1xx: 信息性 - 收到请求,继续处理
- 2xx: 成功 - 该行动被成功接收、理解和接受
- 3xx: 重定向 - 必须采取进一步行动以完成请求。
- 4xx: 客户端错误 - 该请求包含错误的语法或无法完成
- 5xx: 服务器错误 - 服务器未能满足一个明显有效的请求
下面是为 HTTP/1.1 定义的数字状态代码的单个值,以及一组相应的原因短语的例子。这里列出的原因短语只是建议—它们可以被本地的等价物取代而不影响协议。
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 应用程序不需要理解所有注册状态码的含义,尽管这种理解显然是可取的。然而,应用程序必须理解任何状态代码的类别,如第一个数字所示,并将任何未识别的响应视为等同于该类别的x00 状态代码,但未识别的响应不得被缓存。例如,如果客户端收到 431 的未识别状态码,它可以安全地认为它的请求有问题,并把响应当作它收到 400 的状态码。在这种情况下,用户代理应该向用户展示与响应一起返回的实体,因为该实体可能包括人类可读的信息,这将解释不寻常的状态。
6.2 响应头字段
响应头字段允许服务器传递关于响应的额外信息,这些信息不能放在状态行中。这些头字段提供了关于服务器和进一步访问由 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
响应头字段的名称只有在协议版本改变的情况下才能被可靠地扩展。然而,如果通信中的所有各方都承认它们是响应头字段,那么新的或试验性的头字段可以被赋予响应头字段的语义。未被识别的头字段被当作实体头字段处理。