Upgrade - HTTP 编辑

The HTTP 1.1 (only) Upgrade header can be used to upgrade an already established client/server connection to a different protocol (over the same transport protocol). For example, it can be used by a client to upgrade a connection from HTTP 1.1 to HTTP 2.0, or an HTTP or HTTPS connection into a WebSocket.

HTTP/2 explicitly disallows the use of this mechanism/header; it is specific to HTTP/1.1.

Header typeGeneral header
Forbidden header nameyes

Overview

The Upgrade header field may be used by clients to invite a server to switch to one (or more) of the listed protocols, in descending preference order.

For example, the client might send a GET request as shown, listing the preferred protocols to switch to (in this case "example/1" and "foo/2"):

GET /index.html HTTP/1.1
Host: www.example.com
Connection: upgrade
Upgrade: example/1, foo/2

Note: Connection: upgrade must be set whenever Upgrade is sent.

The server can choose to ignore the request, for any reason, in which case it should just respond as though the Upgrade header had not been sent (for example, with a  200 OK).

If the server decides to upgrade the connection, it must:

  1. Send back a 101 Switching Protocols response status with an Upgrade header that specifies the protocol(s) being switched to. For example:
    HTTP/1.1 101 Switching Protocols
    Upgrade: foo/2
    Connection: Upgrade
  2. Send a response to the original request using the new protocol (the server may only switch to a protocol with which it can complete the original request).

A server may also send the header as part of a 426 Upgrade Required response, to indicate that the server won't perform the request using the current protocol, but might do so if the protocol is changed. The client can then request a protocol change using the process above.

More detail and examples are provided in the topic Protocol upgrade mechanism.

Syntax

Connection: upgrade
Upgrade: protocol_name[/protocol_version]

Notes:

  • The Connection header with type upgrade must always be sent with the Upgrade header (as shown above).
  • Protocols are listed, comma-separated, in order of descending preference. Protocol version is optional. For example:
      Connection: upgrade
      Upgrade: a_protocol/1, example ,another_protocol/2.2
    

Directives

any comma-separated list protocol names (each with optional protocol version)
One or more protocol names with optional version ("/" separated). The protocols are listed in order of descending preference.

Examples

Connection: upgrade
Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11
Connection: Upgrade
Upgrade: websocket

Specifications

SpecificationStatusComment
RFC 7230, section 6.7: Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and RoutingIETF RFC
RFC 7231, section 6.5.15: 426 Upgrade RequiredIETF RFC
RFC 7540, section 8.1.1: Hypertext Transfer Protocol Version 2 (HTTP/2)IETF RFC

Browser compatibility

BCD tables only load in the browser

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:142 次

字数:5907

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文