+/* {{{ php_stream *http_get_request_body_stream(void) */
+PHP_HTTP_API php_stream *_http_get_request_body_stream(TSRMLS_D)
+{
+ php_stream *s = NULL;
+
+ if (SG(request_info).raw_post_data) {
+ s = php_stream_open_wrapper("php://input", "rb", 0, NULL);
+ } else if (sapi_module.read_post && !HTTP_G->read_post_data) {
+ HTTP_G->read_post_data = 1;
+
+ if ((s = php_stream_temp_new())) {
+ char buf[4096];
+ int len;
+
+ while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
+ php_stream_write(s, buf, len);
+ if (len < (int) sizeof(buf)) {
+ break;
+ }
+ }
+
+ if (len < 0) {
+ php_stream_close(s);
+ s = NULL;
+ } else {
+ php_stream_rewind(s);
+ }
+ }
+ }
+
+ return s;
+}
+/* }}} */
+
+/* {{{ void http_parse_params_default_callback(...) */
+PHP_HTTP_API void _http_parse_params_default_callback(void *arg, const char *key, int keylen, const char *val, int vallen TSRMLS_DC)
+{
+ char *kdup;
+ zval tmp, *entry;
+ HashTable *ht = (HashTable *) arg;
+
+ if (ht) {
+ INIT_ZARR(tmp, ht);
+
+ if (vallen) {
+ MAKE_STD_ZVAL(entry);
+ array_init(entry);
+ if (keylen) {
+ kdup = estrndup(key, keylen);
+ add_assoc_stringl_ex(entry, kdup, keylen + 1, (char *) val, vallen, 1);
+ efree(kdup);
+ } else {
+ add_next_index_stringl(entry, (char *) val, vallen, 1);
+ }
+ add_next_index_zval(&tmp, entry);
+ } else {
+ add_next_index_stringl(&tmp, (char *) key, keylen, 1);
+ }
+ }
+}
+/* }}} */
+
+/* {{{ STATUS http_parse_params(const char *, HashTable *) */
+PHP_HTTP_API STATUS _http_parse_params_ex(const char *param, int flags, http_parse_params_callback cb, void *cb_arg TSRMLS_DC)
+{
+#define ST_QUOTE 1
+#define ST_VALUE 2
+#define ST_KEY 3
+#define ST_ASSIGN 4
+#define ST_ADD 5
+
+ int st = ST_KEY, keylen = 0, vallen = 0;
+ char *s, *c, *key = NULL, *val = NULL;
+
+ for(c = s = estrdup(param);;) {
+ continued:
+#if 0
+ {
+ 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?*c:'0', tk, tv
+ );
+ STR_FREE(tk); STR_FREE(tv);
+ }
+#endif
+ switch (st) {
+ case ST_QUOTE:
+ quote:
+ if (*c == '"') {
+ if (*(c-1) == '\\') {
+ memmove(c-1, c, strlen(c)+1);
+ goto quote;
+ } else {
+ goto add;
+ }
+ } 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 ';':
+ case '\0':
+ goto add;
+ break;
+ case ',':
+ if (flags & HTTP_PARAMS_ALLOW_COMMA) {
+ goto add;
+ }
+ default:
+ if (!val) {
+ val = c;
+ }
+ break;
+ }
+ break;
+
+ case ST_KEY:
+ switch (*c) {
+ case ',':
+ if (flags & HTTP_PARAMS_ALLOW_COMMA) {
+ goto allow_comma;
+ }
+ case '\r':
+ case '\n':
+ case '\t':
+ case '\013':
+ case '\014':
+ goto failure;
+ break;
+
+ case ' ':
+ if (key) {
+ keylen = c - key;
+ st = ST_ASSIGN;
+ }
+ break;
+
+ case ';':
+ case '\0':
+ allow_comma:
+ if (key) {
+ keylen = c-- - key;
+ st = ST_ADD;
+ }
+ break;
+
+ case ':':
+ if (!(flags & HTTP_PARAMS_COLON_SEPARATOR)) {
+ goto not_separator;
+ }
+ if (key) {
+ keylen = c - key;
+ st = ST_VALUE;
+ } else {
+ goto failure;
+ }
+ break;
+
+ case '=':
+ if (flags & HTTP_PARAMS_COLON_SEPARATOR) {
+ goto not_separator;
+ }
+ if (key) {
+ keylen = c - key;
+ st = ST_VALUE;
+ } else {
+ goto failure;
+ }
+ break;
+
+ default:
+ not_separator:
+ if (!key) {
+ key = c;
+ }
+ break;
+ }
+ break;
+
+ case ST_ASSIGN:
+ if (*c == '=') {
+ st = ST_VALUE;
+ } else if (!*c || *c == ';' || ((flags & HTTP_PARAMS_ALLOW_COMMA) && *c == ',')) {
+ st = ST_ADD;
+ } else if (*c != ' ') {
+ goto failure;
+ }
+ break;
+
+ case ST_ADD:
+ add:
+ if (val) {
+ vallen = c - val;
+ if (st != ST_QUOTE) {
+ while (val[vallen-1] == ' ') --vallen;
+ }
+ } else {
+ val = "";
+ vallen = 0;
+ }
+
+ cb(cb_arg, key, keylen, val, vallen TSRMLS_CC);
+
+ 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:
+ if (flags & HTTP_PARAMS_RAISE_ERROR) {
+ http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Unexpected character (%c) at pos %tu of %zu", *c, c-s, strlen(s));
+ }
+ if (flags & HTTP_PARAMS_ALLOW_FAILURE) {
+ if (st == ST_KEY) {
+ if (key) {
+ keylen = c - key;
+ } else {
+ key = c;
+ }
+ } else {
+ --c;
+ }
+ st = ST_ADD;
+ goto continued;
+ }
+ efree(s);
+ return FAILURE;
+}
+/* }}} */
+
+/* {{{ array_join */
+int apply_array_append_func(void *pDest HTTP_ZAPI_HASH_TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
+{
+ int flags;
+ char *key = NULL;
+ HashTable *dst;
+ zval **data = NULL, **value = (zval **) pDest;
+
+ dst = va_arg(args, HashTable *);
+ flags = va_arg(args, int);
+
+ if ((!(flags & ARRAY_JOIN_STRONLY)) || hash_key->nKeyLength) {
+ if ((flags & ARRAY_JOIN_PRETTIFY) && hash_key->nKeyLength) {
+ key = pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 1);
+ zend_hash_find(dst, key, hash_key->nKeyLength, (void *) &data);
+ } else {
+ zend_hash_quick_find(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) &data);
+ }
+
+ ZVAL_ADDREF(*value);
+ if (data) {
+ add_next_index_zval(http_zset(IS_ARRAY, *data), *value);
+ } else if (key) {
+ zend_hash_add(dst, key, hash_key->nKeyLength, value, sizeof(zval *), NULL);
+ } else {
+ zend_hash_quick_add(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, value, sizeof(zval *), NULL);
+ }
+
+ if (key) {
+ efree(key);
+ }
+ }
+
+ return ZEND_HASH_APPLY_KEEP;
+}
+
+int apply_array_merge_func(void *pDest HTTP_ZAPI_HASH_TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
+{
+ int flags;
+ char *key = NULL;
+ HashTable *dst;
+ zval **value = (zval **) pDest;
+
+ dst = va_arg(args, HashTable *);
+ flags = va_arg(args, int);
+
+ if ((!(flags & ARRAY_JOIN_STRONLY)) || hash_key->nKeyLength) {
+ ZVAL_ADDREF(*value);
+ if ((flags & ARRAY_JOIN_PRETTIFY) && hash_key->nKeyLength) {
+ key = pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 1);
+ zend_hash_update(dst, key, hash_key->nKeyLength, (void *) value, sizeof(zval *), NULL);
+ efree(key);
+ } else {
+ zend_hash_quick_update(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) value, sizeof(zval *), NULL);
+ }
+ }
+
+ return ZEND_HASH_APPLY_KEEP;
+}
+/* }}} */