+/* {{{ STATUS http_parse_cookie(char *, HashTable *) */
+PHP_HTTP_API STATUS _http_parse_cookie(const char *list, HashTable *items TSRMLS_DC)
+{
+#define ST_QUOTE 1
+#define ST_VALUE 2
+#define ST_KEY 3
+#define ST_ASSIGN 4
+#define ST_ADD 5
+
+ zval array;
+ int first = 1, st = ST_KEY, keylen = 0, vallen = 0;
+ char *s, *c, *key = NULL, *val = NULL;
+
+ INIT_ZARR(array, items);
+
+ c = s = estrdup(list);
+ for(;;) {
+#if
+ char *tk = NULL, *tv = NULL;
+
+ if (key) {
+ if (keylen) {
+ tk= estrndup(key, keylen);
+ } else {
+ tk = ecalloc(1, 7);
+ memcpy(tk, key, 3);
+ tk[3]='.'; tk[4]='.'; tk[5]='.';
+ }
+ }
+ if (val) {
+ if (vallen) {
+ tv = estrndup(val, vallen);
+ } else {
+ tv = ecalloc(1, 7);
+ memcpy(tv, val, 3);
+ tv[3]='.'; tv[4]='.'; tv[5]='.';
+ }
+ }
+ fprintf(stderr, "[%6s] %c \"%s=%s\"\n",
+ (
+ st == ST_QUOTE ? "QUOTE" :
+ st == ST_VALUE ? "VALUE" :
+ st == ST_KEY ? "KEY" :
+ st == ST_ASSIGN ? "ASSIGN" :
+ st == ST_ADD ? "ADD":
+ "HUH?"
+ ), *c, tk, tv
+ );
+ STR_FREE(tk); STR_FREE(tv);
+#endif
+ switch (st)
+ {
+ case ST_QUOTE:
+ if (*c == '"') {
+ if (*(c-1) != '\\') {
+ st = ST_ADD;
+ } else {
+ memmove(c-1, c, strlen(c)+1);
+ }
+ } else {
+ if (!val) {
+ val = c;
+ }
+ if (!*c) {
+ --val;
+ st = ST_ADD;
+ }
+ }
+ break;
+
+ case ST_VALUE:
+ switch (*c)
+ {
+ case '"':
+ if (!val) {
+ st = ST_QUOTE;
+ }
+ break;
+
+ case ' ':
+ break;
+
+ case ';':
+ goto add;
+ break;
+
+ case '\0':
+ st = ST_ADD;
+ break;
+
+ default:
+ if (!val) {
+ val = c;
+ }
+ break;
+ }
+ break;
+
+ case ST_KEY:
+ switch (*c)
+ {
+ default:
+ if (!isalnum(*c)) {
+ goto failure;
+ }
+ case '.':
+ case '_':
+ case '$':
+ case '@':
+ if (!key) {
+ key = c;
+ }
+ break;
+
+ case ' ':
+ if (key) {
+ keylen = c - key;
+ st = ST_ASSIGN;
+ }
+ break;
+
+ case '=':
+ if (key) {
+ keylen = c - key;
+ st = ST_VALUE;
+ } else {
+ goto failure;
+ }
+ break;
+
+ case '\0':
+ if (key) {
+ keylen = c - key;
+ st = ST_ADD;
+ }
+ break;
+ }
+ break;
+
+ case ST_ASSIGN:
+ if (*c == '=') {
+ st = ST_VALUE;
+ } else if (*c == ';') {
+ st = ST_ADD;
+ } else if (*c != ' ') {
+ goto failure;
+ }
+ break;
+
+ case ST_ADD:
+ add:
+ if (val) {
+ vallen = c - val - (*c?1:0);
+ while (val[vallen-1] == ' ') --vallen;
+ } else {
+ val = "";
+ vallen = 0;
+ }
+ if (first) {
+ first = 0;
+ add_assoc_stringl(&array, "name", key, keylen, 1);
+ add_assoc_stringl(&array, "value", val, vallen, 1);
+ } else {
+ key = estrndup(key, keylen);
+ add_assoc_stringl_ex(&array, key, keylen+1, val, vallen, 1);
+ efree(key);
+ }
+ st = ST_KEY;
+ key = val = NULL;
+ keylen = vallen = 0;
+ break;
+ }
+
+ if (*c) {
+ ++c;
+ } else if (st == ST_ADD) {
+ goto add;
+ } else {
+ break;
+ }
+ }
+
+ efree(s);
+ return SUCCESS;
+
+failure:
+ http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Unexpected character (%c) at pos %tu of %zu", *c, c-s, strlen(s));
+ efree(s);
+ return FAILURE;
+}
+/* }}} */
+