• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_HTTP_HTTP_STATUS_CODE_H_
6 #define NET_HTTP_HTTP_STATUS_CODE_H_
7 
8 #include <optional>
9 
10 #include "net/base/net_export.h"
11 
12 namespace net {
13 
14 // These values are persisted to logs. Entries should not be renumbered and
15 // numeric values should never be reused.
16 //
17 // HTTP status codes.
18 enum HttpStatusCode {
19 
20 #define HTTP_STATUS_ENUM_VALUE(label, code, reason) HTTP_##label = code,
21 #include "net/http/http_status_code_list.h"
22 #undef HTTP_STATUS_ENUM_VALUE
23 
24 };
25 
26 // Returns the corresponding HTTP status description to use in the Reason-Phrase
27 // field in an HTTP response for given |code|. It's based on the IANA HTTP
28 // Status Code Registry.
29 // http://www.iana.org/assignments/http-status-codes/http-status-codes.xml
30 //
31 // This function may not cover all codes defined in the IANA registry. It
32 // returns an empty string (or crash in debug build) for status codes which are
33 // not yet covered or just invalid. Please extend it when needed.
34 NET_EXPORT const char* GetHttpReasonPhrase(HttpStatusCode code);
35 
36 // Similar to the one above, but returns a nullptr in case code does not map
37 // to a known reasone phrase, which allows a gentle recovery in case the code
38 // was obtained from a non-trusted party.
39 NET_EXPORT const char* TryToGetHttpReasonPhrase(HttpStatusCode code);
40 
41 // Returns the corresponding HTTP status code enum value for a given
42 // |response_code|. Returns std::nullopt if the status code is not in the IANA
43 // HTTP Status Code Registry.
44 NET_EXPORT const std::optional<HttpStatusCode> TryToGetHttpStatusCode(
45     int response_code);
46 
47 }  // namespace net
48 
49 #endif  // NET_HTTP_HTTP_STATUS_CODE_H_
50