root/trunk/whisperlib/net/http/http_consts.h

Revision 7, 11.9 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Catalin Popescu
31 //
32 // Various HTTP specific constants
33
34
35 #ifndef __NET_HTTP_HTTP_CONSTS_H__
36 #define __NET_HTTP_HTTP_CONSTS_H__
37
38 #include <whisperlib/common/base/types.h>
39
40 namespace http {
41
42 //////////////////////////////////////////////////////////////////////
43 //
44 // HTTP Return codes
45 //
46
47 enum HttpReturnCode {
48   UNKNOWN                          = 0,
49   // Continuation codes
50   CONTINUE                         = 100,
51   SWITCHING_PROTOCOLS              = 101,
52
53   // OK codes
54   OK                               = 200,
55   CREATED                          = 201,
56   ACCEPTED                         = 202,
57   NON_AUTHORITATIVE_INFORMATION    = 203,
58   NO_CONTENT                       = 204,
59   RESET_CONTENT                    = 205,
60   PARTIAL_CONTENT                  = 206,
61
62   // Redirections / choices
63   MULTIPLE_CHOICES                 = 300,
64   MOVED_PERMANENTLY                = 301,
65   FOUND                            = 302,
66   SEE_OTHER                        = 303,
67   NOT_MODIFIED                     = 304,
68   USE_PROXY                        = 305,
69   TEMPORARY_REDIRECT               = 307,
70
71   // Client error codes
72   BAD_REQUEST                      = 400,
73   UNAUTHORIZED                     = 401,
74   PAYMENT_REQUIRED                 = 402,
75   FORBIDDEN                        = 403,
76   NOT_FOUND                        = 404,
77   METHOD_NOT_ALLOWED               = 405,
78   NOT_ACCEPTABLE                   = 406,
79   PROXY_AUTHENTICATION_REQUIRED    = 407,
80   REQUEST_TIME_OUT                 = 408,
81   CONFLICT                         = 409,
82   GONE                             = 410,
83   LENGTH_REQUIRED                  = 411,
84   PRECONDITION_FAILED              = 412,
85   REQUEST_ENTITY_TOO_LARGE         = 413,
86   REQUEST_URI_TOO_LARGE            = 414,
87   UNSUPPORTED_MEDIA_TYPE           = 415,
88   REQUESTED_RANGE_NOT_SATISFIABLE  = 416,
89   EXPECTATION_FAILED               = 417,
90
91   // Server error codes
92   INTERNAL_SERVER_ERROR            = 500,
93   NOT_IMPLEMENTED                  = 501,
94   BAD_GATEWAY                      = 502,
95   SERVICE_UNAVAILABLE              = 503,
96   GATEWAY_TIME_OUT                 = 504,
97   HTTP_VERSION_NOT_SUPPORTED       = 505,
98 };
99 const char* GetHttpReturnCodeDescription(HttpReturnCode code);
100 const char* GetHttpCodeDescription(int32 code);
101 const char* GetHttpReturnCodeName(HttpReturnCode code);
102
103 //////////////////////////////////////////////////////////////////////
104 //
105 // Various standard HTTP header names
106 //
107
108
109 // General headers
110 static const char kHeaderCacheControl[] = "Cache-Control";
111 static const char kHeaderConnection[] = "Connection";
112 static const char kHeaderDate[] = "Date";
113 static const char kHeaderPragma[] = "Pragma";
114 static const char kHeaderTrailer[] = "Trailer";
115 static const char kHeaderTransferEncoding[] = "Transfer-Encoding";
116 static const char kHeaderUpgrade[] = "Upgrade";
117 static const char kHeaderVia[] = "Via";
118 static const char kHeaderWarning[] = "Warning";
119
120 // Request headers
121 static const char kHeaderAccept[] = "Accept";
122 static const char kHeaderAcceptCharset[] = "Accept-Charset";
123 static const char kHeaderAcceptEncoding[] = "Accept-Encoding";
124 static const char kHeaderAcceptLanguage[] = "Accept-Language";
125 static const char kHeaderAuthorization[] = "Authorization";
126 static const char kHeaderExpect[] = "Expect";
127 static const char kHeaderFrom[] = "From";
128 static const char kHeaderHost[] = "Host";
129 static const char kHeaderIfMatch[] = "If-Match";
130 static const char kHeaderIfModifiedSince[] = "If-Modified-Since";
131 static const char kHeaderIfNoneMatch[] = "If-None-Match";
132 static const char kHeaderIfRange[] = "If-Range";
133 static const char kHeaderIfUnmodifiedSince[] = "If-Unmodified-Since";
134 static const char kHeaderMaxForwards[] = "Max-Forwards";
135 static const char kHeaderProxyAuthorization[] = "Proxy-Authorization";
136 static const char kHeaderRange[] = "Range";
137 static const char kHeaderReferer[] = "Referer";
138 static const char kHeaderTE[] = "TE";
139 static const char kHeaderUserAgent[] = "User-Agent";
140
141 // Response headers
142 static const char kHeaderAcceptRanges[] = "Accept-Ranges";
143 static const char kHeaderAge[] = "Age";
144 static const char kHeaderETag[] = "ETag";
145 static const char kHeaderKeepAlive[] = "Keep-Alive";
146 static const char kHeaderProxyAuthenticate[] = "Proxy-Authenticate";
147 static const char kHeaderRetryAfter[] = "Retry-After";
148 static const char kHeaderServer[] = "Server";
149 static const char kHeaderVary[] = "Vary";
150 static const char kHeaderWWWAuthenticate[] = "WWW-Authenticate";
151
152 // Entity header
153 static const char kHeaderAllow[] = "Allow";
154 static const char kHeaderContentEncoding[] = "Content-Encoding";
155 static const char kHeaderContentLanguage[] = "Content-Language";
156 static const char kHeaderContentLength[] = "Content-Length";
157 static const char kHeaderContentLocation[] = "Content-Location";
158 static const char kHeaderContentMD5[] = "Content-MD5";
159 static const char kHeaderContentRange[] = "Content-Range";
160 static const char kHeaderContentType[] = "Content-Type";
161 static const char kHeaderExpires[] = "Expires";
162 static const char kHeaderLastModified[] = "Last-Modified";
163
164 // Non standard
165 static const char kHeaderLocation[] = "Location";
166
167 // Cookie headers (non standard)
168 static const char kHeaderCookie[] = "Cookie";
169 static const char kHeaderSetCookie[] = "Set-Cookie";
170
171 // Nonstandard - whispercast use internal
172 static const char kHeaderXRequestId[] = "X-Request-Id";
173
174 //////////////////////////////////////////////////////////////////////
175 //
176 // HTTP request methods
177 //
178 static const char kMethodOptions[]       = "OPTIONS";
179 static const char kMethodGet[]           = "GET";
180 static const char kMethodHead[]          = "HEAD";
181 static const char kMethodPost[]          = "POST";
182 static const char kMethodPut[]           = "PUT";
183 static const char kMethodDelete[]        = "DELETE";
184 static const char kMethodTrace[]         = "TRACE";
185 static const char kMethodConnect[]       = "CONNECT";
186
187 enum HttpMethod {
188   METHOD_UNKNOWN,
189   METHOD_OPTIONS,
190   METHOD_GET,
191   METHOD_HEAD,
192   METHOD_POST,
193   METHOD_PUT,
194   METHOD_DELETE,
195   METHOD_TRACE,
196   METHOD_CONNECT,
197 };
198 HttpMethod GetHttpMethod(const char* methoda);
199 const char* GetHttpMethodName(HttpMethod method);
200
201
202 //////////////////////////////////////////////////////////////////////
203 //
204 // HTTP versions
205 //
206 static const char kHttpVersion0_9[]       = "HTTP/0.9";
207 static const char kHttpVersion1_0[]       = "HTTP/1.0";
208 static const char kHttpVersion1_1[]       = "HTTP/1.1";
209 enum HttpVersion {
210   VERSION_UNKNOWN = 0,
211   VERSION_0_9     = 1,
212   VERSION_1_0     = 2,
213   VERSION_1_1     = 3,
214 };
215 HttpVersion GetHttpVersion(const char* ver);
216 const char* GetHttpVersionName(HttpVersion ver);
217
218 //////////////////////////////////////////////////////////////////////
219 //
220 // NOTE: For the next piece of code (pulled from google-url library:)
221 //
222 // Copyright 2007, Google Inc.
223 // All rights reserved.
224 //
225 // Redistribution and use in source and binary forms, with or without
226 // modification, are permitted provided that the following conditions are
227 // met:
228 //
229 //     * Redistributions of source code must retain the above copyright
230 // notice, this list of conditions and the following disclaimer.
231 //     * Redistributions in binary form must reproduce the above
232 // copyright notice, this list of conditions and the following disclaimer
233 // in the documentation and/or other materials provided with the
234 // distribution.
235 //     * Neither the name of Google Inc. nor the names of its
236 // contributors may be used to endorse or promote products derived from
237 // this software without specific prior written permission.
238 //
239 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
240 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
241 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
242 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
243 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
244 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
248 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
249 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
250 //
251 //
252 // Character type discrimination functions
253 //
254 //
255 //   CTL            = <any US-ASCII control character
256 //                    (octets 0 - 31) and DEL (127)>
257 //   CHAR           = <any US-ASCII character (octets 0 - 127)>
258 //   CR             = <US-ASCII CR, carriage return (13)>
259 //   LF             = <US-ASCII LF, linefeed (10)>
260 //   SP             = <US-ASCII SP, space (32)>
261 //   HT             = <US-ASCII HT, horizontal-tab (9)>
262 //
263 //   CRLF           = CR LF
264 //   LWS            = [CRLF] 1*( SP | HT )
265 //
266 //   token          = 1*<any CHAR except CTLs or separators>
267 //   separators     = "(" | ")" | "<" | ">" | "@"
268 //                    | "," | ";" | ":" | "\" | <">
269 //                    | "/" | "[" | "]" | "?" | "="
270 //                    | "{" | "}" | SP | HT
271
272 // Bits that identify different character types. These types identify different
273 // bits that are set for each 8-bit character in the kSharedCharTypeTable.
274 enum SharedCharTypes {
275   // Characters can be HTTP separators
276   CHAR_SEPARATOR = 1,
277
278   // Characters can be in a LWF
279   CHAR_LWF = 2,
280
281   // Valid in an ASCII-representation of a hex digit (as in %-escaped).
282   CHAR_HEX = 4,
283
284   // Valid in an ASCII-representation of a decimal digit.
285   CHAR_DEC = 8,
286
287   // Valid in an ASCII-representation of an octal digit.
288   CHAR_OCT = 16,
289 };
290 extern const unsigned char kSharedCharTypeTable[0x100];
291
292 // More readable wrappers around the character type lookup table.
293 inline bool IsCharOfType(unsigned char c, SharedCharTypes type) {
294   return !!(kSharedCharTypeTable[c] & type);
295 }
296
297 inline bool IsCtlChar(unsigned char c) {
298   return c <= 31 || c == 127;
299 }
300 inline bool IsAsciiChar(unsigned char c) {
301   return c <= 127;
302 }
303 inline bool IsWhiteSpace(unsigned char c) {
304   return c == 32 || c == 9;
305 }
306 inline bool IsHexChar(unsigned char c) {
307   return IsCharOfType(c, CHAR_HEX);
308 }
309 inline bool IsSeparatorChar(unsigned char c) {
310   return IsCharOfType(c, CHAR_SEPARATOR);
311 }
312 inline bool IsLwfChar(unsigned char c) {
313   return IsCharOfType(c, CHAR_LWF);
314 }
315 inline bool IsTokenChar(unsigned char c) {
316   return IsAsciiChar(c) && !IsCtlChar(c) && !IsSeparatorChar(c);
317 }
318 //
319 // END NOTE (google-url library)
320 //
321 //////////////////////////////////////////////////////////////////////
322 }
323
324 #endif  // __NET_HTTP_HTTP_CONSTS_H__
Note: See TracBrowser for help on using the browser.