diff --git a/mongoose.c b/mongoose.c
index a7a3d11619dedfc4d7546bf71af104f78c3f2138..fde9ea94120618529e68238e7746ed2d592cc02c 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -3042,7 +3042,7 @@ int parse_header(const char *str, int str_len, const char *var_name, char *buf,
 
   // Find where variable starts
   for (s = str; s != NULL && s + n < end; s++) {
-    if ((s == str || s[-1] == ' ') && s[n] == '=' &&
+    if ((s == str || s[-1] == ' ' || s[-1] == ',') && s[n] == '=' &&
         !memcmp(s, var_name, n)) break;
   }
 
@@ -3050,7 +3050,7 @@ int parse_header(const char *str, int str_len, const char *var_name, char *buf,
     s += n + 1;
     if (*s == '"' || *s == '\'') ch = *s++;
     p = s;
-    while (p < end && p[0] != ch && len < (int) buf_size) {
+    while (p < end && p[0] != ch && p[0] != ',' && len < (int) buf_size) {
       if (p[0] == '\\' && p[1] == ch) p++;
       buf[len++] = *p++;
     }
diff --git a/unit_test.c b/unit_test.c
index 13ce3f795d1c30f3e6a423ac0f6e147bfca1f550..b3d034492bae3347bc9f91cfa2fdfdbdc87878f4 100644
--- a/unit_test.c
+++ b/unit_test.c
@@ -322,7 +322,7 @@ 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, "
-    "ii=\"12\\\"34\" zz='aa bb', gf=\"xx d=1234";
+    "ii=\"12\\\"34\" zz='aa bb',tt=2,gf=\"xx d=1234";
   char buf[10];
   ASSERT(mg_parse_header(str, "yy", buf, sizeof(buf)) == 0);
   ASSERT(mg_parse_header(str, "ert", buf, sizeof(buf)) == 3);
@@ -344,6 +344,8 @@ static const char *test_mg_parse_header(void) {
   ASSERT(strcmp(buf, "1") == 0);
   ASSERT(mg_parse_header(str, "ii", buf, sizeof(buf)) == 5);
   ASSERT(strcmp(buf, "12\"34") == 0);
+  ASSERT(mg_parse_header(str, "tt", buf, sizeof(buf)) == 1);
+  ASSERT(strcmp(buf, "2") == 0);
   return NULL;
 }