Commit 8faf6f7b authored by nullable.type's avatar nullable.type

Made behavior of mg_get_cookie(..) on errors the same as mg_get_var(..)....

Made behavior of mg_get_cookie(..) on errors the same as mg_get_var(..). Separated the two cases a.) problems with destination buffer and b.) cookie header or cookie parameter not existing.
parent ca9212c3
...@@ -1698,15 +1698,17 @@ int mg_get_cookie(const struct mg_connection *conn, const char *cookie_name, ...@@ -1698,15 +1698,17 @@ int mg_get_cookie(const struct mg_connection *conn, const char *cookie_name,
const char *s, *p, *end; const char *s, *p, *end;
int name_len, len = -1; int name_len, len = -1;
if (dst == NULL || dst_size == 0) {
len = -2;
} else if (cookie_name == NULL || (s = mg_get_header(conn, "Cookie")) == NULL) {
len = -1;
dst[0] = '\0'; dst[0] = '\0';
if ((s = mg_get_header(conn, "Cookie")) == NULL) { } else {
return -1;
}
name_len = (int) strlen(cookie_name); name_len = (int) strlen(cookie_name);
end = s + strlen(s); end = s + strlen(s);
dst[0] = '\0';
for (; (s = strstr(s, cookie_name)) != NULL; s += name_len) for (; (s = strstr(s, cookie_name)) != NULL; s += name_len) {
if (s[name_len] == '=') { if (s[name_len] == '=') {
s += name_len + 1; s += name_len + 1;
if ((p = strchr(s, ' ')) == NULL) if ((p = strchr(s, ' ')) == NULL)
...@@ -1720,10 +1722,13 @@ int mg_get_cookie(const struct mg_connection *conn, const char *cookie_name, ...@@ -1720,10 +1722,13 @@ int mg_get_cookie(const struct mg_connection *conn, const char *cookie_name,
if ((size_t) (p - s) < dst_size) { if ((size_t) (p - s) < dst_size) {
len = p - s; len = p - s;
mg_strlcpy(dst, s, (size_t) len + 1); mg_strlcpy(dst, s, (size_t) len + 1);
} else {
len = -2;
} }
break; break;
} }
}
}
return len; return len;
} }
......
...@@ -308,9 +308,9 @@ int mg_get_var(const char *data, size_t data_len, ...@@ -308,9 +308,9 @@ int mg_get_var(const char *data, size_t data_len,
// //
// Return: // Return:
// On success, value length. // On success, value length.
// On error, -1 (either "Cookie:" header is not present at all, or the // On error:
// requested parameter is not found, or destination buffer is too small // -1 (either "Cookie:" header is not present at all or the requested parameter is not found).
// to hold the value). // -2 (destination buffer is NULL, zero length or too small to hold the value).
int mg_get_cookie(const struct mg_connection *, int mg_get_cookie(const struct mg_connection *,
const char *cookie_name, char *buf, size_t buf_len); const char *cookie_name, char *buf, size_t buf_len);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment