Response Status Codes 3xx

あらや @arayaryoma

2020-01-17 HTTP/Tokyo #1

slides.araya.dev/http-tokyo-1/

参考資料

3xx 系の Status Codes

  • 300 Multiple Choices [RFC7231]
  • 301 Moved Permanently [RFC7231]
  • 302 Found [RFC7231]
  • 303 See Other [RFC7231]
  • 304 Not Modified [RFC7232]
  • 305 Use Proxy [RFC7231]
  • 306 (Unused) [RFC7231]
  • 307 Temporary Redirect [RFC7231]
  • 308 Permanent Redirect [RFC7538]
[RFC7231]より
------------

The 3xx (Redirection) class of status code indicates that further action needs to be taken by the user agent in order to fulfill the request.  If a Location header field (Section 7.1.2) is provided, the user agent MAY automatically redirect its request to the URI referenced by the Location field value, even if the specific status code is not understood.  Automatic redirection needs to done with care for methods not known to be safe, as defined in Section 4.2.1, since the user might not wish to redirect an unsafe request.

ステータスコード 3xx は、

  • リクエストを果たすためには UA によるさらなるアクションが必要 ということを指示する。
  • Location ヘッダが与えられていたら、UA はステータスコードを解釈できなくてもリダイレクトしてもよい。(MAY)

redirection の種類

[RFC7231]より
------------

1.  Redirects that indicate the resource might be available at a different URI, as provided by the Location field, as in the status codes 301 (Moved Permanently), 302 (Found), and 307 (Temporary Redirect).

2.  Redirection that offers a choice of matching resources, each capable of representing the original request target, as in the 300 (Multiple Choices) status code.

3.  Redirection to a different resource, identified by the Location field, that can represent an indirect response to the request, as in the 303 (See Other) status code.

4.  Redirection to a previously cached result, as in the 304 (Not Modified) status code.
  1. Locaion フィールドで提供される、異なる URI が利用できる可能性があることを指示する(301, 302, 307)
  2. 元のリクエストターゲットを表現する能力を持つような、マッチするリソースの選択肢を提示する(300)
  3. Location フィールドで特定される、間接的なレスポンスを表現可能な、異なるリソースを提示する(303)
  4. すでにキャッシュされた結果へのリダイレクトを提示する(304)

301 と 302 について

[RFC7231]より
------------

 Note: In HTTP/1.0, the status codes 301 (Moved Permanently) and 302 (Found) were defined for the first type of redirect ([RFC1945], Section 9.3).  Early user agents split on whether the method applied to the redirect target would be the same as the original request or would be rewritten as GET. Although HTTP originally defined the former semantics for 301 and 302 (to match its original implementation at CERN), and defined 303 (See Other) to match the latter semantics, prevailing practice gradually converged on the latter semantics for 301 and 302 as well.  The first revision of HTTP/1.1 added 307 (Temporary Redirect) to indicate the former semantics without being impacted by divergent practice.  Over 10 years later, most user agents still do method rewriting for 301 and 302; therefore, this specification makes that behavior conformant when the original request is POST.
  • 301 と 302 は HTTP/1.0 で定義された。
  • 初期の UA では、ステータスコードが 301, 302 だったとき、リクエストターゲットへの適用に差がある。
    • メソッドを元のリクエストと同じにする。
    • メソッドを GET に書き換える。
  • HTTP/1.1 ではこのような実際の差分に影響されない 307 が追加された。
  • 10 年以上が経過した現在(RFC の最終更新は 2014 年)においても、ほとんどの UA は Status code が 301/302 の場合にはメソッドを書き換えている。

無限リダイレクトループ

[RFC7231]より
------------

A client SHOULD detect and intervene in cyclical redirections (i.e., "infinite" redirection loops).

Note: An earlier version of this specification recommended a maximum of five redirections ([RFC2068], Section 10.3).  Content developers need to be aware that some clients might implement such a fixed limitation.
  • クライアントはリダイレクトの無限ループに対して介入するべきである(SHOULD)。
  • この仕様の以前のバージョンでは最大 5 回までが推奨されていた

300 Multiple Choices

[RFC7231]より
------------

The 300 (Multiple Choices) status code indicates that the target resource has more than one representation, each with its own more specific identifier, and information about the alternatives is being provided so that the user (or user agent) can select a preferred representation by redirecting its request to one or more of those identifiers.  In other words, the server desires that the user agent engage in reactive negotiation to select the most appropriate representation(s) for its needs (Section 3.4).

If the server has a preferred choice, the server SHOULD generate a Location header field containing a preferred choice's URI reference. The user agent MAY use the Location field value for automatic redirection.
  • 対象のリソースが複数の表現をもち、それぞれが自身のより特定的な識別子をもつ。
  • それらの代替えについての情報が提供されていて、ユーザーもしくは UA が優先する表現を選択し、1 つ以上の識別子にリダイレクトする。
  • 言い換えると、サーバーは UA がリアクティブネゴシエーションに参加し、ニーズに合わせて最も最適な表現を選択することを求める。
  • サーバーが優先するべき選択を持っていたら、サーバーはその選択肢の URI 参照をLocaionヘッダーに含めるべき(SHOULD)。UA はその値を自動リダイレクトに利用してもよい(MAY)。

300 Multiple Choices

[RFC7231]より
------------

For request methods other than HEAD, the server SHOULD generate a payload in the 300 response containing a list of representation metadata and URI reference(s) from which the user or user agent can choose the one most preferred.  The user agent MAY make a selection from that list automatically if it understands the provided media type.  A specific format for automatic selection is not defined by this specification because HTTP tries to remain orthogonal to the definition of its payloads.  In practice, the representation is provided in some easily parsed format believed to be acceptable to the user agent, as determined by shared design or content negotiation, or in some commonly accepted hypertext format.

A 300 response is cacheable by default; i.e., unless otherwise indicated by the method definition or explicit cache controls (see Section 4.2.2 of [RFC7234]).
  • HEAD以外のリクエストメソッドに対して、サーバーは 300 レスポンス内に、ユーザーもしくは UA が最も優先スべきものを選択できる表現メタデータと URI 参照のリストを含むペイロードを生成するべき(SHOULD)。
  • UA は提供されたメディアタイプを理解できるなら、そのリストの中から自動的に選択してもよい(MAY)。
  • 特定のフォーマットについては仕様では定義されていない。
  • 実際には、コンテントネゴシエーションや共有デザインによって決定される、UA が容易に解析できるフォーマット若しくは、一般的なハイパーテキストフォーマット。
  • 300 レスポンスはデフォルトでキャッシュ可能。

301 Moved Permanently

[RFC7231]より
------------

The 301 (Moved Permanently) status code indicates that the target resource has been assigned a new permanent URI and any future references to this resource ought to use one of the enclosed URIs. Clients with link-editing capabilities ought to automatically re-link references to the effective request URI to one or more of the new references sent by the server, where possible.

The server SHOULD generate a Location header field in the response containing a preferred URI reference for the new permanent URI.  The user agent MAY use the Location field value for automatic redirection.  The server's response payload usually contains a short hypertext note with a hyperlink to the new URI(s).

Note: For historical reasons, a user agent MAY change the request method from POST to GET for the subsequent request.  If this behavior is undesired, the 307 (Temporary Redirect) status code can be used instead.

A 301 response is cacheable by default
  • 対象のリソースに新しい恒久的な URI が充てられていて、このリソースへの今後の参照は同封された URI のいずれかを利用するべきことを指示する。
  • リンク編集能力を備えたクライアントは、可能な場合、サーバーから送信された 1 つ以上の新しい参照に、有効的なリクエスト URI への参照を自動的にリンクし直すべき。
  • サーバーは新しい恒久的 URI への URI 参照を含むレスポンスでLocationヘッダーフィールドを生成するべき(SHOULD)。
  • UA はLocationフィールドの値をつかって自動的にリダイレクトしてもよい(MAY)。
  • サーバーからのレスポンスペイロードは、通常、新しい URI へのハイパーリンクを持つ短いハイパーテキストを含む。
  • 歴史的経緯で、UA は後続のリクエストのメソッドを POST から GET へ変更してもよい(MAY)。この挙動が不都合な場合は代わりに 307 が利用できる。
  • 301 レスポンスはデフォルトでキャッシュ可能。

302 Found

The 302 (Found) status code indicates that the target resource resides temporarily under a different URI.  Since the redirection might be altered on occasion, the client ought to continue to use the effective request URI for future requests.

The server SHOULD generate a Location header field in the response containing a URI reference for the different URI.  The user agent MAY use the Location field value for automatic redirection.  The server's response payload usually contains a short hypertext note with a hyperlink to the different URI(s).

Note: For historical reasons, a user agent MAY change the request method from POST to GET for the subsequent request.  If this behavior is undesired, the 307 (Temporary Redirect) status code can be used instead.
  • 対象のリソースが一時的に異なる URI のもとに配置されていることを指示する。
  • リダイレクトはその都度変更される可能性があるため、クライアントは今後のリクエストに有効的な URI を利用し続けるべき。
  • サーバーは異なる URI へのへの参照を含むレスポンス内に、Locationヘッダーフィールドを生成するべき(SHOULD)。
  • UA はLocationフィールドの値をつかって自動的にリダイレクトしてもよい(MAY)。
  • サーバーからのレスポンスペイロードは、通常、新しい URI へのハイパーリンクを持つ短いハイパーテキストを含む。
  • 歴史的経緯で、UA は後続のリクエストのメソッドを POST から GET へ変更してもよい(MAY)。この挙動が不都合な場合は代わりに 307 が利用できる。

303 See Other

[RFC7231]より
------------

The 303 (See Other) status code indicates that the server is redirecting the user agent to a different resource, as indicated by a URI in the Location header field, which is intended to provide an indirect response to the original request.  A user agent can perform a retrieval request targeting that URI (a GET or HEAD request if using HTTP), which might also be redirected, and present the eventual result as an answer to the original request.  Note that the new URI in the Location header field is not considered equivalent to the effective request URI.

This status code is applicable to any HTTP method.  It is primarily used to allow the output of a POST action to redirect the user agent to a selected resource, since doing so provides the information corresponding to the POST response in a form that can be separately identified, bookmarked, and cached, independent of the original request.
  • UA に対し、異なるリソースへリダイレクトすることを指示する。
  • UA は受け取ったLocationヘッダーフィールドの URI をターゲットとするリクエストを発行できる。
    • このリクエストはさらにリダイレクトされる可能性があり、最終的な結果が元のリクエストに対し提示できる。
  • Locationヘッダーの新しい URI は有効なリクエスト URI と同等とみなされない。
  • すべての HTTP メソッドに適用できる。
  • 主に、POST アクションの出力が UA を選択されたリソースに対してリダイレクトさせるために使われる。
  • form の POST レスポンスの情報が、元のリクエストから独立して個別に識別、ブックマーク、キャッシュできる形で提供できる。

303 See Other

[RFC7231]より
------------

A 303 response to a GET request indicates that the origin server does not have a representation of the target resource that can be transferred by the server over HTTP.  However, the Location field value refers to a resource that is descriptive of the target resource, such that making a retrieval request on that other resource might result in a representation that is useful to recipients without implying that it represents the original target resource.  Note that answers to the questions of what can be represented, what representations are adequate, and what might be a useful description are outside the scope of HTTP.

Except for responses to a HEAD request, the representation of a 303 response ought to contain a short hypertext note with a hyperlink to the same URI reference provided in the Location header field.
  • GET リクエストに対する 303 レスポンスは、オリジンサーバーが HTTP 経由で転送できるリソース表現を持ってないことを示す。
  • Locationフィールドの値は元の対象リソースを記述するものである別のリソースを示す。
  • そのため、他のリソースへのリクエスト結果は、元のリソースを表すことを暗示せずに、受信者にとって有用な表現になる。
  • 何を表現可能か、どのような表現が必要十分か、どのような記述が有用かという質問に対する答えは HTTP のスコープ外。

304 Not Modified ([RFC7232] Conditional Requests)

[RFC7232]より
------------

The 304 (Not Modified) status code indicates that a conditional GET or HEAD request has been received and would have resulted in a 200 (OK) response if it were not for the fact that the condition evaluated to false.  In other words, there is no need for the server to transfer a representation of the target resource because the request indicates that the client, which made the request conditional, already has a valid representation; the server is therefore redirecting the client to make use of that stored representation as if it were the payload of a 200 (OK) response.

The server generating a 304 response MUST generate any of the following header fields that would have been sent in a 200 (OK) response to the same request: Cache-Control, Content-Location, Date, ETag, Expires, and Vary.
  • 条件付きの GET/HEAD リクエストが受け取られ、仮に条件が false と評価されていなければ 200 で応答することになることを示す。
  • 言い換えると、リクエストに条件をつけたクライアントがすでに有効な表現を持っていることをリクエストが示しているため、サーバーが対象リソースの表現を転送する必要がない。
  • よってサーバーはクライアントをリダイレクトして、ストアされた表現を 200 レスポンスのペイロードであるかのように使用させる。
  • 304 レスポンスを生成するサーバーは、次のうち同じリクエストに対する 200 レスポンスで送信されるヘッダーフィールドのすべてを生成しなければならない(MUST)。
    • Cache-Control, Content-Location, Date, Etag, Expires, Vary

304 Not Modified ([RFC7232])

[RFC7232]より
------------

Since the goal of a 304 response is to minimize information transfer when the recipient already has one or more cached representations, a sender SHOULD NOT generate representation metadata other than the above listed fields unless said metadata exists for the purpose of guiding cache updates (e.g., Last-Modified might be useful if the response does not have an ETag field).

Requirements on a cache that receives a 304 response are defined in Section 4.3.4 of [RFC7234].  If the conditional request originated with an outbound client, such as a user agent with its own cache sending a conditional GET to a shared proxy, then the proxy SHOULD forward the 304 response to that client.

A 304 response cannot contain a message-body; it is always terminated by the first empty line after the header fields.
  • 304 レスポンスの目的は、受信者が 1 つ以上のキャッシュされた表現をすでに持っている場合に情報の転送量を最小にすること。
  • キャッシュ更新をガイドする目的でのメタデータが存在しない限り、送信者は上記のフィールド以外の表現メタデータを生成するべきでない(SHOULD NOT)。
  • (たとえばLast-ModifiedはレスポンスにEtagフィールドがない場合に便利。)
  • 304 レスポンスを受け取るキャッシュの要件は[RFC7234]セクション 4.3.4 で定義されている。
  • 共有プロキシに条件付き GET を送信する自前のキャッシュを持つ UA のような、外部クライアントから条件付きリクエストが発行された場合、プロキシは 304 レスポンスをそのクライアントに転送する必要がある。
  • 304 レスポンスにメッセージボディを含めることはできない。ヘッダーフィールドの後の最初の空行で常に終了する。

305 Use Proxy

[RFC7231]より
------------

The 305 (Use Proxy) status code was defined in a previous version of this specification and is now deprecated (Appendix B).
  • 以前のバージョンで定義されていたが非推奨。
  • https://tools.ietf.org/html/rfc2616#section-10.3.6
    • Locationフィールドで指定されたプロキシにアクセスさせる。
    • オリジンサーバーによってのみ生成されなければならない。

306 (Unused)

[RFC7231]より
------------

The 306 status code was defined in a previous version of this specification, is no longer used, and the code is reserved.
  • 以前のバージョンの仕様で定義されていたが、もう使用されない。ステータスコードは予約されている。
  • かつて 306 Switch Proxy として提案されていた。

307 Temporary Redirect

[RFC7231]より
------------

The 307 (Temporary Redirect) status code indicates that the target resource resides temporarily under a different URI and the user agent MUST NOT change the request method if it performs an automatic redirection to that URI.  Since the redirection can change over time, the client ought to continue using the original effective request URI for future requests.

The server SHOULD generate a Location header field in the response containing a URI reference for the different URI.  The user agent MAY use the Location field value for automatic redirection.  The server's response payload usually contains a short hypertext note with a hyperlink to the different URI(s).

Note: This status code is similar to 302 (Found), except that it does not allow changing the request method from POST to GET.  This specification defines no equivalent counterpart for 301 (Moved Permanently) ([RFC7238], however, defines the status code 308 (Permanent Redirect) for this purpose).

  • 対象リソースが別の URI の下に一時的に存在し、UA がその URI への自動リダイレクトを実行する場合、リクエストメソッドを変更してはならない(MUST NOT)ことを示す。
  • 他は 302 と同等。
  • RFC7231 には 301 に相当するものは定義されていないが、RFC7238 で 308 を定義している。(RFC7238 は experimental で、標準仕様は RFC7538)

308 Permanent Redirect ([RFC7538])

[RFC7538]より
------------

The 308 (Permanent Redirect) status code indicates that the target resource has been assigned a new permanent URI and any future references to this resource ought to use one of the enclosed URIs. Clients with link editing capabilities ought to automatically re-link references to the effective request URI (Section 5.5 of [RFC7230]) to one or more of the new references sent by the server, where possible.

The server SHOULD generate a Location header field ([RFC7231], Section 7.1.2) in the response containing a preferred URI reference for the new permanent URI.  The user agent MAY use the Location field value for automatic redirection.  The server's response payload usually contains a short hypertext note with a hyperlink to the new URI(s).

  • 対象のリソースに新しい恒久的な URI が充てられていて、このリソースへの今後の参照は同封された URI のいずれかを利用するべきことを指示する。
  • リンク編集能力を備えたクライアントは、可能な場合、サーバーから送信された 1 つ以上の新しい参照に、有効的なリクエスト URI への参照を自動的にリンクし直すべき。
  • サーバーは新しい恒久的 URI への優先 URI 参照を含むレスポンスでLocationヘッダーフィールドを生成するべき(SHOULD)。
  • UA はLocationフィールドの値をつかって自動的にリダイレクトしてもよい(MAY)。
  • サーバーからのレスポンスペイロードは、通常、新しい URI へのハイパーリンクを持つ短いハイパーテキストを含む。

308 Permanent Redirect ([RFC7538])

[RFC7538]より
------------

A 308 response is cacheable by default; i.e., unless otherwise indicated by the method definition or explicit cache controls (see [RFC7234], Section 4.2.2).

Note: This status code is similar to 301 (Moved Permanently) ([RFC7231], Section 6.4.2), except that it does not allow changing the request method from POST to GET.
  • 明示的なキャッシュ制御の指示がなければ、デフォルトでキャッシュ可能。
  • 301 に似ているがリクエストメソッドを POST から GET に変更できない。

301, 302, 307, 308まとめ

永続的一時的
リクエストメソッドの変更が可能301302
リクエストメソッドの変更を許可しない308307

Demo