From 4120b9bcc0382cedf58d63c997c3b916b7ae9edf Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Thu, 11 Apr 2013 12:49:41 -0700 Subject: [PATCH] mg_get_cookie() signature change --- mongoose.c | 11 +++++------ mongoose.h | 4 ++-- test/unit_test.c | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/mongoose.c b/mongoose.c index 6e743303..1b7e6cdf 100644 --- a/mongoose.c +++ b/mongoose.c @@ -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; } diff --git a/mongoose.h b/mongoose.h index dee018b4..d3ce2b7b 100644 --- a/mongoose.h +++ b/mongoose.h @@ -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. diff --git a/test/unit_test.c b/test/unit_test.c index 575cf372..be714db3 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -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