1 /******************************************************************************
2 
3     HTTP status codes and reason phrases
4 
5     @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
6 
7     Copyright:
8         Copyright (c) 2009-2016 dunnhumby Germany GmbH.
9         All rights reserved.
10 
11     License:
12         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
13         Alternatively, this file may be distributed under the terms of the Tango
14         3-Clause BSD License (see LICENSE_BSD.txt for details).
15 
16  ******************************************************************************/
17 
18 module ocean.net.http.consts.StatusCodes;
19 
20 
21 import ocean.meta.types.Qualifiers;
22 
23 import ocean.net.http.HttpConst: HttpHeader, HttpResponseCode;
24 
25 /******************************************************************************
26 
27     Status phrase string definitions and code association
28 
29  ******************************************************************************/
30 
31 struct StatusPhrases
32 {
33     struct HttpStatusPhrase
34     {
35         HttpResponseCode status_code;
36         istring          reason_phrase;
37     }
38 
39     /**************************************************************************
40 
41         The officially recommended reason phrases for the status codes
42 
43         @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1.1
44 
45      **************************************************************************/
46 
47     enum HttpStatusPhrase[] StatusReasonPhrases =
48     [
49         HttpStatusPhrase(HttpResponseCode.Continue,                     "Continue"),
50         HttpStatusPhrase(HttpResponseCode.SwitchingProtocols,           "Switching Protocols"),
51         HttpStatusPhrase(HttpResponseCode.OK,                           "OK"),
52         HttpStatusPhrase(HttpResponseCode.Created,                      "Created"),
53         HttpStatusPhrase(HttpResponseCode.Accepted,                     "Accepted"),
54         HttpStatusPhrase(HttpResponseCode.NonAuthoritativeInformation,  "Non-Authoritative Information"),
55         HttpStatusPhrase(HttpResponseCode.NoContent,                    "No Content"),
56         HttpStatusPhrase(HttpResponseCode.ResetContent,                 "Reset Content"),
57         HttpStatusPhrase(HttpResponseCode.PartialContent,               "Partial Content"),
58         HttpStatusPhrase(HttpResponseCode.MultipleChoices,              "Multiple Choices"),
59         HttpStatusPhrase(HttpResponseCode.MovedPermanently,             "Moved Permanently"),
60         HttpStatusPhrase(HttpResponseCode.Found,                        "Found"),
61         HttpStatusPhrase(HttpResponseCode.SeeOther,                     "See Other"),
62         HttpStatusPhrase(HttpResponseCode.NotModified,                  "Not Modified"),
63         HttpStatusPhrase(HttpResponseCode.UseProxy,                     "Use Proxy"),
64         HttpStatusPhrase(HttpResponseCode.TemporaryRedirect,            "Temporary Redirect"),
65         HttpStatusPhrase(HttpResponseCode.BadRequest,                   "Bad Request"),
66         HttpStatusPhrase(HttpResponseCode.Unauthorized,                 "Unauthorized"),
67         HttpStatusPhrase(HttpResponseCode.PaymentRequired,              "$$$ Payment Required $$$"),
68         HttpStatusPhrase(HttpResponseCode.Forbidden,                    "Forbidden"),
69         HttpStatusPhrase(HttpResponseCode.NotFound,                     "Not Found"),
70         HttpStatusPhrase(HttpResponseCode.MethodNotAllowed,             "Method Not Allowed"),
71         HttpStatusPhrase(HttpResponseCode.NotAcceptable,                "Not Acceptable"),
72         HttpStatusPhrase(HttpResponseCode.ProxyAuthenticationRequired,  "Proxy Authentication Requred"),
73         HttpStatusPhrase(HttpResponseCode.RequestTimeout,               "Request Timeout"),
74         HttpStatusPhrase(HttpResponseCode.Conflict,                     "Conflict"),
75         HttpStatusPhrase(HttpResponseCode.Gone,                         "Gone"),
76         HttpStatusPhrase(HttpResponseCode.LengthRequired,               "Length Required"),
77         HttpStatusPhrase(HttpResponseCode.PreconditionFailed,           "Precondition Failed"),
78         HttpStatusPhrase(HttpResponseCode.RequestEntityTooLarge,        "Request Entity Too Large"),
79         HttpStatusPhrase(HttpResponseCode.RequestURITooLarge,           "Request-URI Too Long"),
80         HttpStatusPhrase(HttpResponseCode.UnsupportedMediaType,         "Unsupported Media Type"),
81         HttpStatusPhrase(HttpResponseCode.RequestedRangeNotSatisfiable, "Request Range Not satisfiable"),
82         HttpStatusPhrase(HttpResponseCode.ExpectationFailed,            "Expectation Failed"),
83         HttpStatusPhrase(HttpResponseCode.InternalServerError,          "Internal server Error"),
84         HttpStatusPhrase(HttpResponseCode.NotImplemented,               "Not Implemented"),
85         HttpStatusPhrase(HttpResponseCode.BadGateway,                   "Bad Gateway"),
86         HttpStatusPhrase(HttpResponseCode.ServiceUnavailable,           "Service Unavailable"),
87         HttpStatusPhrase(HttpResponseCode.GatewayTimeout,               "Gateway Timeout"),
88         HttpStatusPhrase(HttpResponseCode.VersionNotSupported,          "version Not supported")
89     ];
90 
91     /**************************************************************************
92 
93         HTTP status phrases by status code
94 
95      **************************************************************************/
96 
97     private static istring[HttpResponseCode] reason_phrases;
98 
99     /**************************************************************************
100 
101         Obtains the HTTP status phrase for status_code
102 
103         Params:
104             status_code = HTTP status code
105 
106         Returns:
107             HTTP status phrase for status_code
108 
109         Throws:
110             behaves like indexing an associative array
111 
112      **************************************************************************/
113 
114     static istring opIndex ( HttpResponseCode status_code )
115     {
116         return reason_phrases[status_code];
117     }
118 
119     /**************************************************************************
120 
121         Static constructor; populates reason_phrases
122 
123      **************************************************************************/
124 
125     static this ( )
126     {
127         foreach (srp; StatusReasonPhrases)
128         {
129             reason_phrases[srp.status_code] = srp.reason_phrase;
130         }
131 
132         reason_phrases.rehash;
133     }
134 }