Commit 4120b9bc authored by Sergey Lyubka's avatar Sergey Lyubka

mg_get_cookie() signature change

parent f7725f2e
......@@ -1709,23 +1709,22 @@ int mg_get_var(const char *data, size_t data_len, const char *name,
return len;
}
int mg_get_cookie(const struct mg_connection *conn, const char *cookie_name,
int mg_get_cookie(const char *cookie_header, const char *var_name,
char *dst, size_t dst_size) {
const char *s, *p, *end;
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) {
} else if (var_name == NULL || (s = cookie_header) == NULL) {
len = -1;
dst[0] = '\0';
} else {
name_len = (int) strlen(cookie_name);
name_len = (int) strlen(var_name);
end = s + strlen(s);
dst[0] = '\0';
for (; (s = mg_strcasestr(s, cookie_name)) != NULL; s += name_len) {
for (; (s = mg_strcasestr(s, var_name)) != NULL; s += name_len) {
if (s[name_len] == '=') {
s += name_len + 1;
if ((p = strchr(s, ' ')) == NULL)
......@@ -1740,7 +1739,7 @@ int mg_get_cookie(const struct mg_connection *conn, const char *cookie_name,
len = p - s;
mg_strlcpy(dst, s, (size_t) len + 1);
} else {
len = -2;
len = -3;
}
break;
}
......
......@@ -285,8 +285,8 @@ int mg_get_var(const char *data, size_t data_len,
// parameter is not found).
// -2 (destination buffer is NULL, zero length or too small to hold the
// value).
int mg_get_cookie(const struct mg_connection *,
const char *cookie_name, char *buf, size_t buf_len);
int mg_get_cookie(const char *cookie, const char *var_name,
char *buf, size_t buf_len);
// Download data from the remote web server.
......
......@@ -609,6 +609,22 @@ static void test_mg_strcasestr(void) {
ASSERT(mg_strcasestr("aa", "AAB") == NULL);
}
static void test_mg_get_cookie(void) {
char buf[20];
ASSERT(mg_get_cookie("", "foo", NULL, sizeof(buf)) == -2);
ASSERT(mg_get_cookie("", "foo", buf, 0) == -2);
ASSERT(mg_get_cookie("", "foo", buf, sizeof(buf)) == -1);
ASSERT(mg_get_cookie("", NULL, buf, sizeof(buf)) == -1);
ASSERT(mg_get_cookie("a=1; b=2; c; d", "a", buf, sizeof(buf)) == 1);
ASSERT(strcmp(buf, "1") == 0);
ASSERT(mg_get_cookie("a=1; b=2; c; d", "b", buf, sizeof(buf)) == 1);
ASSERT(strcmp(buf, "2") == 0);
ASSERT(mg_get_cookie("a=1; b=123", "b", buf, sizeof(buf)) == 3);
ASSERT(strcmp(buf, "123") == 0);
ASSERT(mg_get_cookie("a=1; b=2; c; d", "c", buf, sizeof(buf)) == -1);
}
int __cdecl main(void) {
test_mg_strcasestr();
test_alloc_vprintf();
......@@ -627,6 +643,7 @@ int __cdecl main(void) {
test_request_replies();
test_api_calls();
test_url_decode();
test_mg_get_cookie();
#ifdef USE_LUA
test_lua();
#endif
......
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