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

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 // Various HTTP specific constants
31
32 #include <strings.h>
33 #include "net/http/http_consts.h"
34
35 namespace http {
36
37 const char* GetHttpReturnCodeName(HttpReturnCode code) {
38   switch ( code ) {
39     CONSIDER(UNKNOWN);
40     CONSIDER(CONTINUE);
41     CONSIDER(SWITCHING_PROTOCOLS);
42     CONSIDER(OK);
43     CONSIDER(CREATED);
44     CONSIDER(ACCEPTED);
45     CONSIDER(NON_AUTHORITATIVE_INFORMATION);
46     CONSIDER(NO_CONTENT);
47     CONSIDER(RESET_CONTENT);
48     CONSIDER(PARTIAL_CONTENT);
49     CONSIDER(MULTIPLE_CHOICES);
50     CONSIDER(MOVED_PERMANENTLY);
51     CONSIDER(FOUND);
52     CONSIDER(SEE_OTHER);
53     CONSIDER(NOT_MODIFIED);
54     CONSIDER(USE_PROXY);
55     CONSIDER(TEMPORARY_REDIRECT);
56     CONSIDER(BAD_REQUEST);
57     CONSIDER(UNAUTHORIZED);
58     CONSIDER(PAYMENT_REQUIRED);
59     CONSIDER(FORBIDDEN);
60     CONSIDER(NOT_FOUND);
61     CONSIDER(METHOD_NOT_ALLOWED);
62     CONSIDER(NOT_ACCEPTABLE);
63     CONSIDER(PROXY_AUTHENTICATION_REQUIRED);
64     CONSIDER(REQUEST_TIME_OUT);
65     CONSIDER(CONFLICT);
66     CONSIDER(GONE);
67     CONSIDER(LENGTH_REQUIRED);
68     CONSIDER(PRECONDITION_FAILED);
69     CONSIDER(REQUEST_ENTITY_TOO_LARGE);
70     CONSIDER(REQUEST_URI_TOO_LARGE);
71     CONSIDER(UNSUPPORTED_MEDIA_TYPE);
72     CONSIDER(REQUESTED_RANGE_NOT_SATISFIABLE);
73     CONSIDER(EXPECTATION_FAILED);
74     CONSIDER(INTERNAL_SERVER_ERROR);
75     CONSIDER(NOT_IMPLEMENTED);
76     CONSIDER(BAD_GATEWAY);
77     CONSIDER(SERVICE_UNAVAILABLE);
78     CONSIDER(GATEWAY_TIME_OUT);
79     CONSIDER(HTTP_VERSION_NOT_SUPPORTED);
80   }
81   return "UNKNOWN";
82 }
83
84 const char* GetHttpCodeDescription(int32 code) {
85   switch ( code ) {
86   case 100: return "Continue";
87   case 101: return "Switching Protocols";
88   case 200: return "OK";
89   case 201: return "Created";
90   case 202: return "Accepted";
91   case 203: return "Non-Authoritative Information";
92   case 204: return "No Content";
93   case 205: return "Reset Content";
94   case 206: return "Partial Content";
95   case 300: return "Multiple Choices";
96   case 301: return "Moved Permanently";
97   case 302: return "Found";
98   case 303: return "See Other";
99   case 304: return "Not Modified";
100   case 305: return "Use Proxy";
101   case 307: return "Temporary Redirect";
102   case 400: return "Bad Request";
103   case 401: return "Unauthorized";
104   case 402: return "Payment Required";
105   case 403: return "Forbidden";
106   case 404: return "Not Found";
107   case 405: return "Method Not Allowed";
108   case 406: return "Not Acceptable";
109   case 407: return "Proxy Authentication Required";
110   case 408: return "Request Time-out";
111   case 409: return "Conflict";
112   case 410: return "Gone";
113   case 411: return "Length Required";
114   case 412: return "Precondition Failed";
115   case 413: return "Request Entity Too Large";
116   case 414: return "Request-URI Too Large";
117   case 415: return "Unsupported Media Type";
118   case 416: return "Requested range not satisfiable";
119   case 417: return "Expectation Failed";
120   case 500: return "Internal Server Error";
121   case 501: return "Not Implemented";
122   case 502: return "Bad Gateway";
123   case 503: return "Service Unavailable";
124   case 504: return "Gateway Time-out";
125   case 505: return "HTTP Version not supported";
126   }
127   return "Unknown";
128 }
129
130 const char* GetHttpReturnCodeDescription(HttpReturnCode code) {
131   return GetHttpCodeDescription(static_cast<int32>(code));
132 }
133
134
135 HttpVersion GetHttpVersion(const char* ver) {
136   if ( !strcasecmp(ver, kHttpVersion1_1) ) return VERSION_1_1;
137   if ( !strcasecmp(ver, kHttpVersion1_0) ) return VERSION_1_0;
138   if ( !strcasecmp(ver, kHttpVersion0_9) ) return VERSION_0_9;
139   return VERSION_UNKNOWN;
140 }
141
142 const char* GetHttpVersionName(HttpVersion ver) {
143   switch ( ver ) {
144   case VERSION_0_9: return kHttpVersion0_9;
145   case VERSION_1_0: return kHttpVersion1_0;
146   case VERSION_1_1: return kHttpVersion1_1;
147   case VERSION_UNKNOWN: break;
148   }
149   return "HTTP/unknown";
150 }
151
152 HttpMethod GetHttpMethod(const char* method) {
153   switch ( toupper(*method) ) {
154   case 'O':
155     if ( !strcasecmp(method + 1, "PTIONS") ) return METHOD_OPTIONS;
156     break;
157   case 'G':
158     if ( !strcasecmp(method + 1, "ET") )     return METHOD_GET;
159     break;
160   case 'H':
161     if ( !strcasecmp(method + 1, "EAD") )    return METHOD_HEAD;
162     break;
163   case 'P':
164     if ( !strcasecmp(method + 1, "OST") )    return METHOD_POST;
165     if ( !strcasecmp(method + 1, "UT") )     return METHOD_PUT;
166     break;
167   case 'D':
168     if ( !strcasecmp(method + 1, "ELETE") )  return METHOD_DELETE;
169     break;
170   case 'T':
171     if ( !strcasecmp(method + 1, "RACE") )   return METHOD_TRACE;
172     break;
173   case 'C':
174     if ( !strcasecmp(method + 1, "ONNECT") ) return METHOD_CONNECT;
175     break;
176   }
177   return METHOD_UNKNOWN;
178 }
179 const char* GetHttpMethodName(HttpMethod method) {
180   switch ( method ) {
181   case METHOD_UNKNOWN: return "UNKNOWN";
182   case METHOD_OPTIONS: return "OPTIONS";
183   case METHOD_GET: return "GET";
184   case METHOD_HEAD: return "HEAD";
185   case METHOD_POST: return "POST";
186   case METHOD_PUT: return "PUT";
187   case METHOD_DELETE: return "DELETE";
188   case METHOD_TRACE: return "TRACE";
189   case METHOD_CONNECT: return "CONNECT";
190   }
191   return "UNKNOWN";
192 }
193
194
195 // See the header file for this array's declaration.
196 const unsigned char kSharedCharTypeTable[0x100] = {
197     0, 0, 0, 0, 0, 0, 0, 0, 0,       // 0x00 - 0x08
198     CHAR_LWF | CHAR_SEPARATOR,       // 0x08 \t
199     CHAR_LWF,                        // 0x0a \n
200     0,                               // 0x0b
201     0,                               // 0x0c
202     CHAR_LWF,                        // 0x0d \r
203     0,                               // 0x0e
204     0,                               // 0x0f
205     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0x10 - 0x1f
206     CHAR_LWF | CHAR_SEPARATOR,       // 0x20  ' '
207     0,                               // 0x21  !
208     CHAR_SEPARATOR,                  // 0x22  "
209     0,                               // 0x23  #
210     0,                               // 0x24  $
211     0,                               // 0x25  %
212     0,                               // 0x26  &
213     0,                               // 0x27  '
214     CHAR_SEPARATOR,                  // 0x28  (
215     CHAR_SEPARATOR,                  // 0x29  )
216     0,                               // 0x2a  *
217     0,                               // 0x2b  +
218     CHAR_SEPARATOR,                  // 0x2c  ,
219     0,                               // 0x2d  -
220     0,                               // 0x2e  .
221     CHAR_SEPARATOR,                  // 0x2f  /
222     CHAR_HEX | CHAR_DEC | CHAR_OCT,  // 0x30  0
223     CHAR_HEX | CHAR_DEC | CHAR_OCT,  // 0x31  1
224     CHAR_HEX | CHAR_DEC | CHAR_OCT,  // 0x32  2
225     CHAR_HEX | CHAR_DEC | CHAR_OCT,  // 0x33  3
226     CHAR_HEX | CHAR_DEC | CHAR_OCT,  // 0x34  4
227     CHAR_HEX | CHAR_DEC | CHAR_OCT,  // 0x35  5
228     CHAR_HEX | CHAR_DEC | CHAR_OCT,  // 0x36  6
229     CHAR_HEX | CHAR_DEC | CHAR_OCT,  // 0x37  7
230     CHAR_HEX | CHAR_DEC,             // 0x38  8
231     CHAR_HEX | CHAR_DEC,             // 0x39  9
232     CHAR_SEPARATOR,                  // 0x3a  :
233     CHAR_SEPARATOR,                  // 0x3b  ;
234     CHAR_SEPARATOR,                  // 0x3c  <
235     CHAR_SEPARATOR,                  // 0x3d  =
236     CHAR_SEPARATOR,                  // 0x3e  >
237     CHAR_SEPARATOR,                  // 0x3f  ?
238     CHAR_SEPARATOR,                  // 0x40  @
239     CHAR_HEX,                        // 0x41  A
240     CHAR_HEX,                        // 0x42  B
241     CHAR_HEX,                        // 0x43  C
242     CHAR_HEX,                        // 0x44  D
243     CHAR_HEX,                        // 0x45  E
244     CHAR_HEX,                        // 0x46  F
245     0,                               // 0x47  G
246     0,                               // 0x48  H
247     0,                               // 0x49  I
248     0,                               // 0x4a  J
249     0,                               // 0x4b  K
250     0,                               // 0x4c  L
251     0,                               // 0x4d  M
252     0,                               // 0x4e  N
253     0,                               // 0x4f  O
254     0,                               // 0x50  P
255     0,                               // 0x51  Q
256     0,                               // 0x52  R
257     0,                               // 0x53  S
258     0,                               // 0x54  T
259     0,                               // 0x55  U
260     0,                               // 0x56  V
261     0,                               // 0x57  W
262     0,                               // 0x58  X
263     0,                               // 0x59  Y
264     0,                               // 0x5a  Z
265     CHAR_SEPARATOR,  // 0x5b  [
266     CHAR_SEPARATOR,  // 0x5c  '\'
267     CHAR_SEPARATOR,  // 0x5d  ]
268     0,                               // 0x5e  ^
269     0,                               // 0x5f  _
270     0,                               // 0x60  `
271     CHAR_HEX,                        // 0x61  a
272     CHAR_HEX,                        // 0x62  b
273     CHAR_HEX,                        // 0x63  c
274     CHAR_HEX,                        // 0x64  d
275     CHAR_HEX,                        // 0x65  e
276     CHAR_HEX,                        // 0x66  f
277     0,                               // 0x67  g
278     0,                               // 0x68  h
279     0,                               // 0x69  i
280     0,                               // 0x6a  j
281     0,                               // 0x6b  k
282     0,                               // 0x6c  l
283     0,                               // 0x6d  m
284     0,                               // 0x6e  n
285     0,                               // 0x6f  o
286     0,                               // 0x70  p
287     0,                               // 0x71  q
288     0,                               // 0x72  r
289     0,                               // 0x73  s
290     0,                               // 0x74  t
291     0,                               // 0x75  u
292     0,                               // 0x76  v
293     0,                               // 0x77  w
294     0,                               // 0x78  x
295     0,                               // 0x79  y
296     0,                               // 0x7a  z
297     CHAR_SEPARATOR,                  // 0x7b  {
298     CHAR_SEPARATOR,                  // 0x7c  |
299     0,                               // 0x7d  }
300     0,                               // 0x7e  ~
301     0,                               // 0x7f
302     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0x80 - 0x8f
303     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0x90 - 0x9f
304     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0xa0 - 0xaf
305     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0xb0 - 0xbf
306     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0xc0 - 0xcf
307     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0xd0 - 0xdf
308     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0xe0 - 0xef
309     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0xf0 - 0xff
310 };
311 }
Note: See TracBrowser for help on using the browser.