39 #define MAX_COOKIES 32 40 #define MAX_STORE_SIZE 8192 43 #define SECONDS_IN_DAY (24 * HOUR) 68 static U32BIT cookie_count = 0;
69 static U32BIT cookiejar_size = 0;
74 static U32BIT SkipWhitespace(
void *data, U32BIT offset, U32BIT data_len);
80 static MHEG5Int FindChrInStr(
MHEG5String source, MHEG5Int offset,
char *search);
82 static MHEG5Bool StrCmpChr(
MHEG5String cmp_str,
char *cmp_chrs);
84 static U8BIT* AddHeader(U8BIT *header,
cookiestore_t *cookie);
86 static MHEG5Int ParseDate(
MHEG5String date_string, MHEG5Int *date, MHEG5Int *time);
88 static void SetMaxAge(
MHEG5String duration_string, MHEG5Int *date, MHEG5Int *time);
90 static MHEG5Bool GetDomainFromUrl(
char *full_url,
MHEG5String *domain_string);
114 data_handle.data = data;
115 data_handle.len = data_len;
117 if (data_len > 11 && memcmp(data_handle.data,
"Set-Cookie:", 11) == 0)
119 offset = strlen(
"Set-Cookie:");
123 new_cookie->secure = MHEG5FALSE;
124 new_cookie->expire_date = 0;
125 new_cookie->expire_time = 0;
126 new_cookie->domain.data = NULL;
127 new_cookie->domain.len = 0;
128 new_cookie->name.len = 0;
129 new_cookie->name.data = NULL;
130 new_cookie->path.len = 0;
131 new_cookie->path.data = NULL;
133 while (new_cookie && offset < data_len)
137 offset = SkipWhitespace(data, offset, data_len);
139 pos = FindChrInStr(data_handle, offset,
"=");
142 name.len = pos - offset;
143 name.data = (MHEG5Byte *)data + offset;
146 if (data_handle.data[offset] ==
'\"')
152 pos = FindChrInStr(data_handle, offset,
";");
155 pos = FindChrInStr(data_handle, offset,
"\r\n");
162 value.data = (MHEG5Byte *)data + offset;
165 value.len = pos - offset;
175 value.len = data_handle.len - offset;
176 offset = data_handle.len;
180 if (StrCmpChr(name,
"path") == MHEG5TRUE || StrCmpChr(name,
"Path") == MHEG5TRUE)
184 else if (StrCmpChr(name,
"domain") == MHEG5TRUE)
188 else if (StrCmpChr(name,
"expires") == MHEG5TRUE)
190 ParseDate(value, &new_cookie->expire_date, &new_cookie->expire_time);
192 else if (StrCmpChr(name,
"max-age") == MHEG5TRUE)
194 SetMaxAge(value, &new_cookie->expire_date, &new_cookie->expire_time);
196 else if (StrCmpChr(name,
"Version") == MHEG5TRUE)
209 if (new_cookie->domain.len == 0)
211 GetDomainFromUrl(url, &new_cookie->domain);
213 new_cookie->identity.len = new_cookie->name.len + new_cookie->domain.len +
214 new_cookie->path.len + 1;
215 new_cookie->identity.data = STR_DataAlloc(new_cookie->identity.len);
216 if (new_cookie->identity.data)
219 memcpy(new_cookie->identity.data + offset, new_cookie->name.data, new_cookie->name.len);
220 offset += new_cookie->name.len;
222 *(new_cookie->identity.data + offset) =
',';
225 memcpy(new_cookie->identity.data + offset, new_cookie->domain.data, new_cookie->domain.len);
226 offset += new_cookie->domain.len;
227 memcpy(new_cookie->identity.data + offset, new_cookie->path.data, new_cookie->path.len);
228 PushCookie(new_cookie);
245 U8BIT *cookie_header = NULL;
251 S32BIT current_time, current_date;
260 if (!memcmp(url,
"http://", 7))
265 else if (!memcmp(url,
"https://", 8))
277 location.len = strlen((
char *)url) - offset;
278 location.data = url + offset;
285 if (cursor->expire_date == 0 ||
286 cursor->expire_date > current_date ||
287 (cursor->expire_date == current_date && cursor->expire_time > current_time))
289 if ((cursor->secure == MHEG5FALSE || secure == MHEG5TRUE) &&
290 !memcmp(location.data, cursor->domain.data, cursor->domain.len))
292 offset = cursor->domain.len;
293 if (!memcmp(location.data + offset, cursor->path.data, cursor->path.len))
295 cookie_header = AddHeader(cookie_header, cursor);
307 return cookie_header;
320 MHEG5Bool success = MHEG5FALSE;
322 if (cookie->value.len > MAX_STORE_SIZE)
330 success = ParseIdentity(new_cookie);
331 if (success == MHEG5TRUE)
335 new_cookie->secure = cookie->secure;
336 new_cookie->expire_date = cookie->expires;
337 new_cookie->expire_time = 0;
338 PushCookie(new_cookie);
342 MHEG5freeMem(new_cookie);
357 MHEG5freeMem(header);
372 S32BIT current_time, current_date;
383 if (CookieEqual(&cursor->identity, &cookie->identity) == MHEG5TRUE)
385 cookie->expires = cursor->expire_date;
386 cookie->secure = cursor->secure;
409 DeleteCookie(cookiejar);
421 static U32BIT SkipWhitespace(
void *data, U32BIT offset, U32BIT data_len)
423 U8BIT *handle = data;
425 while (offset < data_len)
427 if (handle[offset] !=
' ' && handle[offset] !=
'\t' && handle[offset] !=
'\r' && handle[offset] !=
'\n')
447 if (cookie->name.data != cookie->identity.data)
449 if (cookie->name.len)
451 if (cookie->path.len)
453 if (cookie->domain.len)
457 if (cookie->value.len)
459 cookiejar_size -= cookie->value.len;
462 if (cookie->identity.len)
466 if (cookie->previous)
468 cookie->previous->next = cookie->next;
472 assert(cookie == cookiejar);
473 cookiejar = cookie->next;
478 cookie->next->previous = cookie->previous;
481 if (cookie == oldest_cookie)
483 oldest_cookie = cookie->previous;
486 MHEG5freeMem(cookie);
498 MHEG5Bool success = MHEG5FALSE;
499 MHEG5Int domain_pos, path_pos;
502 domain_pos = FindChrInStr(cookie->identity, 0,
",");
503 if (domain_pos != -1)
505 path_pos = FindChrInStr(cookie->identity, domain_pos,
"/");
508 cookie->name.len = domain_pos;
509 cookie->name.data = cookie->identity.data;
511 cookie->domain.len = path_pos - domain_pos - 1;
512 offset = domain_pos + 1;
513 cookie->domain.data = cookie->identity.data + offset;
515 cookie->path.len = cookie->identity.len - path_pos;
517 cookie->path.data = cookie->identity.data + offset;
534 if (cookiejar == NULL)
536 cookiejar = new_cookie;
537 new_cookie->previous = NULL;
538 new_cookie->next = NULL;
539 oldest_cookie = new_cookie;
543 cookiejar->previous = new_cookie;
544 new_cookie->next = cookiejar;
545 new_cookie->previous = NULL;
546 cookiejar = new_cookie;
551 cookiejar_size += new_cookie->value.len;
553 if (cookie_count > MAX_COOKIES)
555 DeleteCookie(oldest_cookie);
558 while (cookiejar_size > MAX_STORE_SIZE)
560 DeleteCookie(oldest_cookie);
573 static MHEG5Int FindChrInStr(
MHEG5String source, MHEG5Int offset,
char *search)
576 register int rc = -1, i, j = 0;
578 if (offset >= 0 && offset < source.len && search && source.data)
580 len = strlen(search);
581 for (i = offset; i != source.len; i++)
583 if ((source.data[i] | 0x20) == (search[j] | 0x20))
609 static MHEG5Bool StrCmpChr(
MHEG5String cmp_str,
char *cmp_chrs)
611 if (cmp_str.len != strlen(cmp_chrs))
616 if (!memcmp(cmp_chrs, cmp_str.data, cmp_str.len))
626 static U8BIT* AddHeader(U8BIT *header,
cookiestore_t *cookie)
628 U8BIT *header_string;
633 header = MHEG5getMem(23);
635 memcpy(header,
"Cookie: $Version=\"0\"; \0", 23);
638 len = strlen((
char *)header);
639 len += cookie->name.len;
640 len += cookie->value.len;
641 len += cookie->path.len;
643 header_string = MHEG5getMem(len);
645 memcpy(header_string, header, strlen((
char *)header));
646 offset = strlen((
char *)header);
648 MHEG5freeMem(header);
650 memcpy(header_string + offset, cookie->name.data, cookie->name.len);
651 offset += cookie->name.len;
653 memcpy(header_string + offset,
"=", 1);
656 memcpy(header_string + offset, cookie->value.data, cookie->value.len);
657 offset += cookie->value.len;
659 memcpy(header_string + offset,
"; $Path=", 8);
662 memcpy(header_string + offset, cookie->path.data, cookie->path.len);
663 offset += cookie->path.len;
665 memcpy(header_string + offset,
"; \0", 3);
668 return header_string;
671 static S32BIT ParseMonthString(U8BIT *input_string)
673 S32BIT month_index = 0;
674 switch (input_string[0])
677 if (!memcmp(input_string,
"Jan", 3))
681 if (!month_index && !memcmp(input_string,
"Jun", 3))
685 if (!month_index && !memcmp(input_string,
"Jul", 3))
691 if (!memcmp(input_string,
"Feb", 3))
697 if (!memcmp(input_string,
"Mar", 3))
701 if (!month_index && !memcmp(input_string,
"May", 3))
707 if (!memcmp(input_string,
"Apr", 3))
711 if (!month_index && !memcmp(input_string,
"Aug", 3))
717 if (!memcmp(input_string,
"Sep", 3))
723 if (!memcmp(input_string,
"Oct", 3))
729 if (!memcmp(input_string,
"Nov", 3))
735 if (!memcmp(input_string,
"Dec", 3))
745 static S32BIT ParseTimeString(U8BIT *input_string)
748 time = (input_string[0] -
'0') * HOUR * 10;
749 time += (input_string[1] -
'0') * HOUR;
750 time += (input_string[3] -
'0') * MINUTE * 10;
751 time += (input_string[4] -
'0') * MINUTE;
752 time += (input_string[6] -
'0') * 10;
753 time += (input_string[7] -
'0');
757 static MHEG5Int ParseDate(
MHEG5String date_string, MHEG5Int *date, MHEG5Int *time)
762 if (date_string.len == 29)
764 day = (date_string.data[5] -
'0') * 10;
765 day += date_string.data[6] -
'0';
766 month = ParseMonthString(date_string.data + 8);
767 year = (date_string.data[12] -
'0') * 1000;
768 year += (date_string.data[13] -
'0') * 100;
769 year += (date_string.data[14] -
'0') * 10;
770 year += date_string.data[15] -
'0';
772 *time = ParseTimeString(date_string.data + 17);
778 static void SetMaxAge(
MHEG5String duration_string, MHEG5Int *date, MHEG5Int *time)
781 S32BIT current_date, current_time;
787 *date = current_date + (max_age / SECONDS_IN_DAY);
788 *time = current_time + (max_age % SECONDS_IN_DAY);
791 static MHEG5Bool GetDomainFromUrl(
char *full_url,
MHEG5String *domain_string)
795 full_len = strlen(full_url);
797 if (full_len > 7 && full_url[7] !=
'/')
801 else if (full_len > 8 && full_url[8] !=
'/')
809 for (end = offset; end != full_len; end++)
811 if (full_url[end] ==
'/')
821 domain_string->data = STR_DataAlloc(end);
822 if (domain_string->data == NULL)
826 domain_string->len = end;
827 memcpy(domain_string->data,(U8BIT *)full_url + offset,end);
842 enum { NAME, DOMAIN, PATH } state = NAME;
847 if (stored->len == requested->len)
851 for (i = 0; equal && i < len; i++)
853 if ((state == DOMAIN) && (stored->data[i] ==
'/'))
861 if (toupper(stored->data[i]) != toupper(requested->data[i]))
868 if (stored->data[i] != requested->data[i])
874 if ((state == NAME) && (stored->data[i] ==
','))
void MHEG5getDate(S32BIT *day, S32BIT *sec)
Modified Julian Date - see Davic 9.2.12.1.
U8BIT * MHEG5CookieGenerateHeader(U8BIT *url)
Generate a "Cookie" header for the required http request, containing all valid cookies.
MHEG5Int MHEG5strToInt(MHEG5String string)
Convert a MHEG5String to a MHEG5Integer.
void MHEG5CookieClearStore(void)
Delete all cookies from the temporary store.
void MHEG5CookieParse(void *data, U32BIT data_len, char *url)
Parse a Set-cookie header and add the new cookie to the store. If the header is not a set-cookie...
MHEG5String MHEG5stringCopy(MHEG5String source)
<Function description>="">
void MHEG5stringDestruct(MHEG5String *item)
Destruct a MHEG5String.
MHEG5Bool MHEG5CookieRetrieve(MHEG5cookie_t *cookie)
Retrieve a specific cookie from the store, referenced by the identity string.
MHEG5Bool MHEG5CookieAdd(MHEG5cookie_t *cookie)
Directly add a cookie to the store.
System Wide Global Technical Data Type Definitions.
Functions relating to HTTP Cookie Store.
U32BIT JulianDate(S32BIT day, S32BIT month, S32BIT year)
The function JulianDate() calculates the julian day number for the specified day, month and year...
void MHEG5CookieAckHeader(U8BIT *header)
This function should be called once the header string returned by MHEG5CookieGenerateHeader has been ...