From e4ad3010bea598b10e89c89d7995622f5e4ee6c2 Mon Sep 17 00:00:00 2001
From: Sergey Lyubka <valenok@gmail.com>
Date: Tue, 30 Dec 2014 03:56:17 +0000
Subject: [PATCH] Fix #416

---
 mongoose.c       | 10 +++++-----
 test/unit_test.c |  5 +++--
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/mongoose.c b/mongoose.c
index 09a3dc9b4..0cacdfed4 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -4105,23 +4105,23 @@ static int is_dav_request(const struct connection *conn) {
 
 static int parse_header(const char *str, int str_len, const char *var_name,
                         char *buf, size_t buf_size) {
-  int ch = ' ', len = 0, n = strlen(var_name);
+  int ch = ' ', ch1 = ',', len = 0, n = strlen(var_name);
   const char *p, *end = str + str_len, *s = NULL;
 
   if (buf != NULL && buf_size > 0) buf[0] = '\0';
 
   // Find where variable starts
   for (s = str; s != NULL && s + n < end; s++) {
-    if ((s == str || s[-1] == ' ' || s[-1] == ',') && s[n] == '=' &&
+    if ((s == str || s[-1] == ch || s[-1] == ch1) && s[n] == '=' &&
         !memcmp(s, var_name, n)) break;
   }
 
   if (s != NULL && &s[n + 1] < end) {
     s += n + 1;
-    if (*s == '"' || *s == '\'') ch = *s++;
+    if (*s == '"' || *s == '\'') ch = ch1 = *s++;
     p = s;
-    while (p < end && p[0] != ch && p[0] != ',' && len < (int) buf_size) {
-      if (p[0] == '\\' && p[1] == ch) p++;
+    while (p < end && p[0] != ch && p[0] != ch1 && len < (int) buf_size) {
+      if (ch == ch1 && p[0] == '\\' && p[1] == ch) p++;
       buf[len++] = *p++;
     }
     if (len >= (int) buf_size || (ch != ' ' && *p != ch)) {
diff --git a/test/unit_test.c b/test/unit_test.c
index 18dfcaa6e..39e36ae02 100644
--- a/test/unit_test.c
+++ b/test/unit_test.c
@@ -306,9 +306,9 @@ static const char *test_base64_encode(void) {
 }
 
 static const char *test_mg_parse_header(void) {
-  const char *str = "xx=1 kl yy, ert=234 kl=123, "
+  const char *str = "xx=1 kl yy, ert=234 kl=123, uri=\"/?name=x,y\", "
     "ii=\"12\\\"34\" zz='aa bb',tt=2,gf=\"xx d=1234";
-  char buf[10];
+  char buf[20];
   ASSERT(mg_parse_header(str, "yy", buf, sizeof(buf)) == 0);
   ASSERT(mg_parse_header(str, "ert", buf, sizeof(buf)) == 3);
   ASSERT(strcmp(buf, "234") == 0);
@@ -331,6 +331,7 @@ static const char *test_mg_parse_header(void) {
   ASSERT(strcmp(buf, "12\"34") == 0);
   ASSERT(mg_parse_header(str, "tt", buf, sizeof(buf)) == 1);
   ASSERT(strcmp(buf, "2") == 0);
+  ASSERT(mg_parse_header(str, "uri", buf, sizeof(buf)) == 10);
   return NULL;
 }
 
-- 
GitLab