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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
|
//
// HtCookie.cc
//
// HtCookie: This class represents a HTTP cookie.
//
// HtCookie.cc
//
// by Robert La Ferla. Started 12/5/2000.
// Reviewed by G.Bartolini - since 24 Feb 2001
// Cookies input file by G.Bartolini - since 27 Jan 2003
//
////////////////////////////////////////////////////////////
//
// The HtCookie class represents a single HTTP cookie.
//
// See "PERSISTENT CLIENT STATE HTTP COOKIES" Specification
// at http://www.netscape.com/newsref/std/cookie_spec.html
// Modified according to RFC2109 (max age and version attributes)
//
// This class also manages the creation of a cookie from a line
// of a cookie file format, which is a text file as proposed by Netscape;
// each line contains a name-value pair for a cookie.
// Fields within a single line are separated by the 'tab' character;
//
///////
//
// Part of the ht://Dig package <http://www.htdig.org/>
// Part of the ht://Check package <http://htcheck.sourceforge.net/>
// Copyright (c) 2001-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: HtCookie.cc,v 1.14 2004/05/28 13:15:22 lha Exp $
//
#ifdef HAVE_CONFIG_H
#include "htconfig.h"
#endif /* HAVE_CONFIG_H */
#include "HtCookie.h"
#include <stdlib.h>
#include <ctype.h>
#ifdef HAVE_STD
#include <iostream>
#ifdef HAVE_NAMESPACES
using namespace std;
#endif
#else
#include <iostream.h>
#endif /* HAVE_STD */
///////
// Static variables initialization
///////
// Debug level
int HtCookie::debug = 0;
// Precompiled constants regarding the cookies file format (field order)
#define COOKIES_FILE_DOMAIN 0
#define COOKIES_FILE_FLAG 1
#define COOKIES_FILE_PATH 2
#define COOKIES_FILE_SECURE 3
#define COOKIES_FILE_EXPIRES 4
#define COOKIES_FILE_NAME 5
#define COOKIES_FILE_VALUE 6
// Default constructor
HtCookie::HtCookie()
: name(0),
value(0),
path(0),
domain(0),
expires(0),
isSecure(false),
isDomainValid(true),
srcURL(0),
issue_time(),
max_age(-1),
rfc_version(0)
{
}
// Constructor that accepts a name and a value
// and the calling URL
HtCookie::HtCookie(const String &aName, const String &aValue,
const String& aURL)
: name(aName),
value(aValue),
path(0),
domain(0),
expires(0),
isSecure(false),
isDomainValid(true),
srcURL(aURL),
issue_time(),
max_age(-1),
rfc_version(0)
{
}
// Constructor from a server response header
HtCookie::HtCookie(const String &setCookieLine, const String& aURL)
: name(0),
value(0),
path(0),
domain(0),
expires(0),
isSecure(false),
isDomainValid(true),
srcURL(aURL),
issue_time(),
max_age(-1),
rfc_version(0)
{
String cookieLineStr(setCookieLine);
char * token;
const char * str;
if (debug > 5)
cout << "Creating cookie from response header: " << cookieLineStr << endl;
// Parse the cookie line
token = strtok(cookieLineStr, "=");
if (token != NULL)
{
SetName(token);
token = strtok(NULL, ";");
SetValue(token);
}
// Get all the fields returned by the server
while ((str = strtok(NULL, "=")))
{
const char * ctoken;
token = stripAllWhitespace(str);
if (mystrcasecmp(token, "path") == 0)
{
// Let's grab the path
ctoken = strtok(NULL, ";");
SetPath(ctoken);
}
else if (mystrcasecmp(token, "expires") == 0)
{
// Let's grab the expiration date
HtDateTime dt;
ctoken = strtok(NULL, ";");
if (ctoken && SetDate(ctoken, dt))
SetExpires(&dt);
else
SetExpires(0);
} else if (mystrcasecmp(token, "secure") == 0)
SetIsSecure(true);
else if (mystrcasecmp(token, "domain") == 0)
{
ctoken = strtok(NULL, ";");
SetDomain(ctoken);
}
else if (mystrcasecmp(token, "max-age") == 0)
{
ctoken = strtok(NULL, ";");
SetMaxAge(atoi(ctoken));
}
else if (mystrcasecmp(token, "version") == 0)
{
ctoken = strtok(NULL, ";");
SetVersion(atoi(ctoken));
}
if (token)
delete[](token);
}
if (debug>3)
printDebug();
}
// Constructor from a line of a cookie file (according to Netscape format)
HtCookie::HtCookie(const String &CookieFileLine)
: name(0),
value(0),
path(0),
domain(0),
expires(0),
isSecure(false),
isDomainValid(true),
srcURL(0),
issue_time(),
max_age(-1),
rfc_version(0)
{
String cookieLineStr(CookieFileLine);
char * token;
const char * str;
if (debug > 5)
cout << "Creating cookie from a cookie file line: " << cookieLineStr << endl;
// Parse the cookie line
if ((str = strtok(cookieLineStr, "\t")))
{
int num_field = 0;
int expires_value; // Holds the expires value that will be read
// According to the field number, set the appropriate object member's value
do
{
token = stripAllWhitespace(str);
switch(num_field)
{
case COOKIES_FILE_DOMAIN:
SetDomain(token);
break;
case COOKIES_FILE_FLAG:
// Ignored
break;
case COOKIES_FILE_PATH:
SetPath(token);
break;
case COOKIES_FILE_SECURE:
if (mystrcasecmp(token, "false"))
SetIsSecure(true);
else
SetIsSecure(false);
break;
case COOKIES_FILE_EXPIRES:
if ((expires_value = atoi(token) > 0)) // Sets the expires value only if > 0
{
time_t tmp = atoi(token); // avoid ambiguous arg list
expires = new HtDateTime(tmp);
}
break;
case COOKIES_FILE_NAME:
SetName(token);
break;
case COOKIES_FILE_VALUE:
SetValue(token);
break;
}
++num_field;
} while((str = strtok(NULL, "\t")));
}
if (debug>3)
printDebug();
}
// Copy constructor
HtCookie::HtCookie(const HtCookie& rhs)
: name(rhs.name),
value(rhs.value),
path(rhs.path),
domain(rhs.domain),
expires(0),
isSecure(rhs.isSecure),
isDomainValid(rhs.isDomainValid),
srcURL(rhs.srcURL),
issue_time(rhs.issue_time),
max_age(rhs.max_age),
rfc_version(rhs.rfc_version)
{
if (rhs.expires)
expires = new HtDateTime(*rhs.expires);
}
// Destructor
HtCookie::~HtCookie()
{
// Delete the DateTime info
if (expires)
delete expires;
}
// Set the expires datetime
void HtCookie::SetExpires(const HtDateTime *aDateTime)
{
//
// If expires has not yet been set,
// we just copy the reference
// otherwise, we just change the contents
// of our internal attribute
//
// We don't have a valid datetime, it's null
if (!aDateTime)
{
if (expires)
delete expires;
expires=0;
}
else
{
// We do have a valid datetime
// Let's check whether expires has already been created
if (!expires)
expires = new HtDateTime(*aDateTime); // No ... let's create it and copy it
}
}
// Strip all the whitespaces
char * HtCookie::stripAllWhitespace(const char * str)
{
int len;
int i;
int j;
char * newstr;
len = strlen(str);
newstr = new char[len + 1];
j = 0;
for (i = 0; i < len; i++) {
char c;
c = str[i];
if (isspace(c) == 0)
newstr[j++] = c;
}
newstr[j++] = (char)0;
return newstr;
}
// Copy operator overload
const HtCookie &HtCookie::operator = (const HtCookie &rhs)
{
// Prevent from copying itself
if (this == &rhs)
return *this;
// Copy all the values
name = rhs.name;
value = rhs.value;
path = rhs.path;
domain = rhs.domain;
srcURL = rhs.srcURL;
// Set the expiration time
SetExpires(rhs.expires);
isSecure = rhs.isSecure;
isDomainValid = rhs.isDomainValid;
issue_time = rhs.issue_time;
max_age = rhs.max_age;
return *this;
}
// Print a debug message
ostream& HtCookie::printDebug(ostream &out)
{
out << " - ";
out << "NAME=" << name << " VALUE=" << value << " PATH=" << path;
if (expires)
out << " EXPIRES=" << expires->GetRFC850();
if (domain.length())
out << " DOMAIN=" << domain << " ("
<< (isDomainValid?"VALID":"INVALID")
<< ")";
if (max_age >= 0)
out << " MAX-AGE=" << max_age;
if (isSecure)
out << " SECURE";
if (srcURL.length() > 0)
out << " - Issued by: " << srcURL;
out << endl;
return out;
}
//
// Set the date time value of a cookie's expires
// Given an HtDateTime object and a datestring
// It returns true if everything goes ok
// and false otherwise.
//
int HtCookie::SetDate(const char *datestring, HtDateTime &dt)
{
if (!datestring) // for any reason we don't have a string for the date
return 0; // and we exit
DateFormat df;
while (*datestring && isspace(*datestring))
datestring++; // skip initial spaces
df = RecognizeDateFormat(datestring);
if (df == DateFormat_NotRecognized)
{
// Not recognized
if (debug > 0)
cout << "Cookie '" << name
<< "' date format not recognized: " << datestring << endl;
return false;
}
dt.ToGMTime(); // Set to GM time
switch(df)
{
// Asc Time format
case DateFormat_AscTime:
dt.SetAscTime((char *)datestring);
break;
// RFC 1123
case DateFormat_RFC1123:
dt.SetRFC1123((char *)datestring);
break;
// RFC 850
case DateFormat_RFC850:
dt.SetRFC850((char *)datestring);
break;
default:
if (debug > 0)
cout << "Cookie '" << name
<< "' date format not handled: " << (int)df << endl;
break;
}
return !(df==DateFormat_NotRecognized);
}
// Recognize the date sent by the server
//
// The expires attribute specifies a date string that defines the valid life time
// of that cookie. Once the expiration date has been reached, the cookie will no
// longer be stored or given out.
//
// The date string is formatted as:
// Wdy, DD-Mon-YYYY HH:MM:SS GMT
// This is based on RFC 822, RFC 850, RFC 1036, and RFC 1123, with the variations
// that the only legal time zone is GMT and the separators between the elements
// of the date must be dashes.
//
HtCookie::DateFormat HtCookie::RecognizeDateFormat(const char *datestring)
{
register const char *s;
if (datestring)
{
if ((s=strchr(datestring, ',')))
{
// A comma is present.
// Two chances: RFC1123 or RFC850
if(strchr(s, '-'))
return DateFormat_RFC850; // RFC 850 recognized
else return DateFormat_RFC1123; // RFC 1123 recognized
}
else
{
// No comma present
// Let's try C Asctime: Sun Nov 6 08:49:37 1994
if (strlen(datestring) == 24)
{
return DateFormat_AscTime;
}
}
}
return DateFormat_NotRecognized;
}
|