1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
// $Id$
// HTTP_Helpers.cpp -- Helper utilities for both server and client
#include "HTTP_Helpers.h"
#include "ace/Log_Msg.h"
#include "ace/OS_NS_string.h"
#include "ace/Guard_T.h"
#include "ace/OS_NS_time.h"
#include "ace/OS_NS_stdio.h"
// = Static initialization.
const char *const
HTTP_Helper::months_[12]=
{
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
char const *HTTP_Helper::alphabet_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char * HTTP_Helper::date_string_ = 0;
ACE_SYNCH_MUTEX HTTP_Helper::mutex_;
ACE_SYNCH_MUTEX HTTP_Status_Code::lock_;
int HTTP_Status_Code::instance_ = 0;
const char *HTTP_Status_Code::Reason[HTTP_Status_Code::MAX_STATUS_CODE + 1];
time_t
HTTP_Helper::HTTP_mktime (const char *httpdate)
{
char *buf;
ACE_NEW_RETURN (buf, char[ACE_OS::strlen (httpdate) + 1], (time_t) -1);
// Make spaces in the date be semi-colons so we can parse robustly
// with sscanf.
const char *ptr1 = httpdate;
char *ptr2 = buf;
do
{
if (*ptr1 == ' ')
*ptr2++ = ';';
else
*ptr2++ = *ptr1;
}
while (*ptr1++ != '\0');
// In HTTP/1.0, there are three versions of an HTTP_date.
// rfc1123-date = wkday "," SP dd month yyyy SP hh:mm:ss SP "GMT"
// rfc850-date = weekday "," SP dd-month-yy SP hh:mm:ss SP "GMT"
// asctime-date = wkday SP month dd SP hh:mm:ss SP yyyy
// static const char rfc1123_date[] = "%3s,;%2d;%3s;%4d;%2d:%2d:%2d;GMT";
// static const char rfc850_date[] = "%s,;%2d-%3s-%2d;%2d:%2d:%2d;GMT";
// static const char asctime_date[] = "%3s;%3s;%2d;%2d:%2d:%2d;%4d";
// Should also support other versions (such as from NNTP and SMTP)
// for robustness, but it should be clear how to extend this.
struct tm tms;
char month[4];
char weekday[10];
if (::sscanf(buf, "%3s,;%2d;%3s;%4d;%2d:%2d:%2d;GMT", // RFC-1123 date format
weekday,
&tms.tm_mday,
month,
&tms.tm_year,
&tms.tm_hour,
&tms.tm_min,
&tms.tm_sec) == 7)
;
else if (::sscanf(buf, "%s,;%2d-%3s-%2d;%2d:%2d:%2d;GMT", // RFC-850 date format
weekday,
&tms.tm_mday, month, &tms.tm_year,
&tms.tm_hour, &tms.tm_min, &tms.tm_sec) == 7)
{
weekday[3] = '\0';
}
else if (::sscanf(buf, "%3s;%3s;%2d;%2d:%2d:%2d;%4d", // ASCTIME date format.
weekday,
month, &tms.tm_mday,
&tms.tm_hour, &tms.tm_min, &tms.tm_sec,
&tms.tm_year) == 7)
{
}
delete [] buf;
tms.tm_year = HTTP_Helper::fixyear (tms.tm_year);
tms.tm_mon = HTTP_Helper::HTTP_month (month);
if (tms.tm_mon == -1)
return (time_t) -1;
// mktime is a Standard C function.
{
#if !defined (ACE_HAS_REENTRANT_LIBC)
ACE_MT (ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, g, HTTP_Helper::mutex_, -1));
#endif /* NOT ACE_HAS_REENTRANT_LIBC */
return ACE_OS::mktime (&tms);
}
}
const char *
HTTP_Helper::HTTP_date (void)
{
if (HTTP_Helper::date_string_ == 0)
{
ACE_MT (ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, m, HTTP_Helper::mutex_, 0));
if (HTTP_Helper::date_string_ == 0)
{
// 40 bytes is all I need.
ACE_NEW_RETURN (HTTP_Helper::date_string_, char[40], 0);
if (!HTTP_Helper::HTTP_date (HTTP_Helper::date_string_))
{
delete [] HTTP_Helper::date_string_;
HTTP_Helper::date_string_ = 0;
}
}
}
return HTTP_Helper::date_string_;
}
const char *
HTTP_Helper::HTTP_date (char *s)
{
// Return the date-string formatted per HTTP standards. Time must
// be in UTC, so using the 'strftime' call (which obeys the locale)
// isn't correct.
static const char* months[] = {"Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"};
static const char* days[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t tloc;
struct tm tms;
char * date_string = s;
if (ACE_OS::time (&tloc) != (time_t) -1
&& ACE_OS::gmtime_r (&tloc, &tms) != 0)
{
ACE_OS::sprintf (date_string,
"%s, %2.2d %s %4.4d %2.2d:%2.2d:%2.2d GMT",
days[tms.tm_wday], tms.tm_mday, months[tms.tm_mon],
tms.tm_year + 1900, tms.tm_hour, tms.tm_min, tms.tm_sec);
}
else
date_string = 0;
return date_string;
}
int
HTTP_Helper::HTTP_month (const char *month)
{
for (size_t i = 0; i < 12; i++)
if (ACE_OS::strcmp(month, HTTP_Helper::months_[i]) == 0)
return i;
return -1;
}
const char *
HTTP_Helper::HTTP_month (int month)
{
if (month < 0 || month >= 12)
return 0;
return HTTP_Helper::months_[month];
}
// Fix the path if it needs fixing/is fixable.
char *
HTTP_Helper::HTTP_decode_string (char *path)
{
// replace the percentcodes with the actual character
int i, j;
char percentcode[3];
for (i = j = 0; path[i] != '\0'; i++, j++)
{
if (path[i] == '%')
{
percentcode[0] = path[++i];
percentcode[1] = path[++i];
percentcode[2] = '\0';
path[j] = (char) ACE_OS::strtol (percentcode, (char **) 0, 16);
}
else
path[j] = path[i];
}
path[j] = path[i];
return path;
}
char *
HTTP_Helper::HTTP_decode_base64 (char *data)
{
char inalphabet[256], decoder[256];
ACE_OS::memset (inalphabet, 0, sizeof (inalphabet));
ACE_OS::memset (decoder, 0, sizeof (decoder));
for (int i = ACE_OS::strlen (HTTP_Helper::alphabet_) - 1;
i >= 0;
i--)
{
inalphabet[(unsigned int) HTTP_Helper::alphabet_[i]] = 1;
decoder[(unsigned int) HTTP_Helper::alphabet_[i]] = i;
}
char *indata = data;
char *outdata = data;
int bits = 0;
int c;
int char_count = 0;
int errors = 0;
while ((c = *indata++) != '\0')
{
if (c == '=')
break;
if (c > 255 || ! inalphabet[c])
continue;
bits += decoder[c];
char_count++;
if (char_count == 4)
{
*outdata++ = (bits >> 16);
*outdata++ = ((bits >> 8) & 0xff);
*outdata++ = (bits & 0xff);
bits = 0;
char_count = 0;
}
else
bits <<= 6;
}
if (c == '\0')
{
if (char_count)
{
ACE_DEBUG ((LM_DEBUG,
"base64 encoding incomplete: at least %d bits truncated\n",
((4 - char_count) * 6)));
errors++;
}
}
else
{
// c == '='
switch (char_count)
{
case 1:
ACE_DEBUG ((LM_DEBUG,
"base64 encoding incomplete: at least 2 bits missing\n"));
errors++;
break;
case 2:
*outdata++ = (bits >> 10);
break;
case 3:
*outdata++ = (bits >> 16);
*outdata++ = ((bits >> 8) & 0xff);
break;
}
}
*outdata = '\0';
return errors ? 0 : data;
}
char *
HTTP_Helper::HTTP_encode_base64 (char *data)
{
char buf[BUFSIZ];
int c;
int error;
int char_count = 0;
int bits = 0;
error = 0;
char *indata = data;
char *outdata = buf;
const unsigned char ASCII_MAX = ~0;
while ((c = *indata++) != '\0')
{
if (c > (int)ASCII_MAX)
{
ACE_DEBUG ((LM_DEBUG, "encountered char > 255 (decimal %d)\n", c));
error++;
break;
}
bits += c;
char_count++;
if (char_count == 3)
{
*outdata++ = HTTP_Helper::alphabet_[bits >> 18];
*outdata++ = HTTP_Helper::alphabet_[(bits >> 12) & 0x3f];
*outdata++ = HTTP_Helper::alphabet_[(bits >> 6) & 0x3f];
*outdata++ = HTTP_Helper::alphabet_[bits & 0x3f];
bits = 0;
char_count = 0;
}
else
bits <<= 8;
}
if (!error)
{
if (char_count != 0)
{
bits <<= 16 - (8 * char_count);
*outdata++ = HTTP_Helper::alphabet_[bits >> 18];
*outdata++ = HTTP_Helper::alphabet_[(bits >> 12) & 0x3f];
if (char_count == 1)
{
*outdata++ = '=';
*outdata++ = '=';
}
else
{
*outdata++ = HTTP_Helper::alphabet_[(bits >> 6) & 0x3f];
*outdata++ = '=';
}
}
*outdata = '\0';
ACE_OS::strcpy (data, buf);
}
return (error ? 0 : data);
}
int
HTTP_Helper::fixyear (int year)
{
// Fix the year 2000 problem
if (year > 1000)
year -= 1900;
else if (year < 100)
{
struct tm tms;
time_t tloc;
if (ACE_OS::time (&tloc) != (time_t) -1)
{
ACE_OS::gmtime_r (&tloc, &tms);
if (tms.tm_year % 100 == year)
year = tms.tm_year;
// The last two cases check boundary conditions, in case the
// year just changed at the moment we checked to see if we
// need to fix it.
if ((year+1) % 100 == tms.tm_year % 100)
year = tms.tm_year - 1;
if (year == (tms.tm_year + 1) % 100)
year = tms.tm_year + 1;
// What to do if none of the above?
}
}
return year;
}
const char **
HTTP_Status_Code::instance (void)
{
if (HTTP_Status_Code::instance_ == 0)
{
ACE_MT (ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, g, lock_, 0));
if (HTTP_Status_Code::instance_ == 0)
{
for (size_t i = 0;
i < HTTP_Status_Code::MAX_STATUS_CODE + 1;
i++)
{
switch (i)
{
case STATUS_OK:
HTTP_Status_Code::Reason[i] = "OK"; break;
case STATUS_CREATED:
HTTP_Status_Code::Reason[i] = "Created"; break;
case STATUS_ACCEPTED:
HTTP_Status_Code::Reason[i] = "Accepted"; break;
case STATUS_NO_CONTENT:
HTTP_Status_Code::Reason[i] = "No Content"; break;
case STATUS_MOVED_PERMANENTLY:
HTTP_Status_Code::Reason[i] = "Moved Permanently"; break;
case STATUS_MOVED_TEMPORARILY:
HTTP_Status_Code::Reason[i] = "Moved Temporarily"; break;
case STATUS_NOT_MODIFIED:
HTTP_Status_Code::Reason[i] = "Not Modified"; break;
case STATUS_BAD_REQUEST:
HTTP_Status_Code::Reason[i] = "Bad Request"; break;
case STATUS_UNAUTHORIZED:
HTTP_Status_Code::Reason[i] = "Unauthorized"; break;
case STATUS_FORBIDDEN:
HTTP_Status_Code::Reason[i] = "Forbidden"; break;
case STATUS_NOT_FOUND:
HTTP_Status_Code::Reason[i] = "Not Found"; break;
case STATUS_INTERNAL_SERVER_ERROR:
HTTP_Status_Code::Reason[i] = "Internal Server Error"; break;
case STATUS_NOT_IMPLEMENTED:
HTTP_Status_Code::Reason[i] = "Not Implemented"; break;
case STATUS_BAD_GATEWAY:
HTTP_Status_Code::Reason[i] = "Bad Gateway"; break;
case STATUS_SERVICE_UNAVAILABLE:
HTTP_Status_Code::Reason[i] = "Service Unavailable"; break;
default:
HTTP_Status_Code::Reason[i] = "Unknown";
}
}
HTTP_Status_Code::instance_ = 1;
}
// GUARD released
}
return HTTP_Status_Code::Reason;
}
|