1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

cookies: Check return values of parse_header_param.

Mostly this makes set_cookie more complex, as it now distinguishes
between HEADER_PARAM_NOT_FOUND and HEADER_PARAM_OUT_OF_MEMORY, and kills the cookie in the
latter case.  However, the cookie->secure check became simpler.
This commit is contained in:
Kalle Olavi Niemitalo 2006-05-30 09:32:01 +03:00 committed by Miciah Dashiel Butler Masters
parent 819b6fab80
commit 23a9a17827

View File

@ -283,7 +283,7 @@ is_domain_security_ok(unsigned char *domain, unsigned char *server, int server_l
void void
set_cookie(struct uri *uri, unsigned char *str) set_cookie(struct uri *uri, unsigned char *str)
{ {
unsigned char *secure, *path; unsigned char *path;
struct cookie *cookie; struct cookie *cookie;
struct cookie_str cstr; struct cookie_str cstr;
int max_age; int max_age;
@ -307,8 +307,9 @@ set_cookie(struct uri *uri, unsigned char *str)
cookie->name = memacpy(str, cstr.nam_end - str); cookie->name = memacpy(str, cstr.nam_end - str);
cookie->value = memacpy(cstr.val_start, cstr.val_end - cstr.val_start); cookie->value = memacpy(cstr.val_start, cstr.val_end - cstr.val_start);
cookie->server = get_cookie_server(uri->host, uri->hostlen); cookie->server = get_cookie_server(uri->host, uri->hostlen);
parse_header_param(str, "domain", &cookie->domain); if (parse_header_param(str, "domain", &cookie->domain)
if (!cookie->domain) cookie->domain = memacpy(uri->host, uri->hostlen); == HEADER_PARAM_NOT_FOUND)
cookie->domain = memacpy(uri->host, uri->hostlen);
/* Now check that all is well */ /* Now check that all is well */
if (!cookie->domain if (!cookie->domain
@ -347,10 +348,11 @@ set_cookie(struct uri *uri, unsigned char *str)
max_age = get_cookies_max_age(); max_age = get_cookies_max_age();
if (max_age) { if (max_age) {
unsigned char *date; unsigned char *date;
parse_header_param(str, "expires", &date); time_t expires;
if (date) { switch (parse_header_param(str, "expires", &date)) {
time_t expires = parse_date(&date, NULL, 0, 1); /* Convert date to seconds. */ case HEADER_PARAM_FOUND:
expires = parse_date(&date, NULL, 0, 1); /* Convert date to seconds. */
mem_free(date); mem_free(date);
@ -365,13 +367,33 @@ set_cookie(struct uri *uri, unsigned char *str)
cookie->expires = expires; cookie->expires = expires;
} }
break;
case HEADER_PARAM_NOT_FOUND:
break;
default: /* error */
done_cookie(cookie);
return;
} }
} }
parse_header_param(str, "path", &path); switch (parse_header_param(str, "path", &path)) {
if (!path) {
unsigned char *path_end; unsigned char *path_end;
case HEADER_PARAM_FOUND:
if (!path[0]
|| path[strlen(path) - 1] != '/')
add_to_strn(&path, "/");
if (path[0] != '/') {
add_to_strn(&path, "x");
memmove(path + 1, path, strlen(path) - 1);
path[0] = '/';
}
break;
case HEADER_PARAM_NOT_FOUND:
path = get_uri_string(uri, URI_PATH); path = get_uri_string(uri, URI_PATH);
if (!path) { if (!path) {
done_cookie(cookie); done_cookie(cookie);
@ -385,17 +407,11 @@ set_cookie(struct uri *uri, unsigned char *str)
break; break;
} }
} }
break;
} else { default: /* error */
if (!path[0] done_cookie(cookie);
|| path[strlen(path) - 1] != '/') return;
add_to_strn(&path, "/");
if (path[0] != '/') {
add_to_strn(&path, "x");
memmove(path + 1, path, strlen(path) - 1);
path[0] = '/';
}
} }
cookie->path = path; cookie->path = path;
@ -403,12 +419,8 @@ set_cookie(struct uri *uri, unsigned char *str)
memmove(cookie->domain, cookie->domain + 1, memmove(cookie->domain, cookie->domain + 1,
strlen(cookie->domain)); strlen(cookie->domain));
/* cookie->secure is set to 0 by default by calloc(). */ cookie->secure = (parse_header_param(str, "secure", NULL)
parse_header_param(str, "secure", &secure); == HEADER_PARAM_FOUND);
if (secure) {
cookie->secure = 1;
mem_free(secure);
}
#ifdef DEBUG_COOKIES #ifdef DEBUG_COOKIES
{ {