+ php_stream *stream;
+ zval *zfc = php_http_ztyp(IS_STRING, *zfile);
+
+ if (SUCCESS == zend_hash_find(ht, ZEND_STRS("data"), (void *) &zdata)) {
+ if (Z_TYPE_PP(zdata) == IS_RESOURCE) {
+ php_stream_from_zval_no_verify(stream, zdata);
+ } else {
+ zval *tmp = php_http_ztyp(IS_STRING, *zdata);
+
+ stream = php_stream_memory_open(TEMP_STREAM_READONLY, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
+ zval_ptr_dtor(&tmp);
+ }
+ } else {
+ stream = php_stream_open_wrapper(Z_STRVAL_P(zfc), "r", REPORT_ERRORS|USE_PATH, NULL);
+ }
+
+ if (!stream) {
+ zval_ptr_dtor(&zfc);
+ return FAILURE;
+ } else {
+ zval *znc = php_http_ztyp(IS_STRING, *zname), *ztc = php_http_ztyp(IS_STRING, *ztype);
+ char *key = format_key(HASH_KEY_IS_STRING, Z_STRVAL_P(znc), 0, name);
+ STATUS ret = php_http_message_body_add_form_file(body, key, Z_STRVAL_P(ztc), Z_STRVAL_P(zfc), stream);
+
+ efree(key);
+ zval_ptr_dtor(&znc);
+ zval_ptr_dtor(&ztc);
+ zval_ptr_dtor(&zfc);
+ if (!zdata || Z_TYPE_PP(zdata) != IS_RESOURCE) {
+ php_stream_close(stream);
+ }
+ return ret;
+ }
+
+ }
+}
+
+struct splitbody_arg {
+ php_http_buffer_t buf;
+ php_http_message_parser_t *parser;
+ char *boundary_str;
+ size_t boundary_len;
+ size_t consumed;
+};
+
+static size_t splitbody(void *opaque, char *buf, size_t len TSRMLS_DC)
+{
+ struct splitbody_arg *arg = opaque;
+ const char *boundary = NULL;
+ size_t consumed = 0;
+ int first_boundary;
+
+ do {
+ first_boundary = !(consumed || arg->consumed);
+
+ if ((boundary = php_http_locate_str(buf, len, arg->boundary_str + first_boundary, arg->boundary_len - first_boundary))) {
+ size_t real_boundary_len = arg->boundary_len - 1, cut;
+ const char *real_boundary = boundary + !first_boundary;
+ int eol_len = 0;
+
+ if (buf + len <= real_boundary + real_boundary_len) {
+ /* if we just have enough data for the boundary, it's just a byte too less */
+ arg->consumed += consumed;
+ return consumed;
+ }
+
+ if (!first_boundary) {
+ /* this is not the first boundary, read rest of this message */
+ php_http_buffer_append(&arg->buf, buf, real_boundary - buf);
+ php_http_message_parser_parse(arg->parser, &arg->buf, 0, &arg->parser->message);
+ }
+
+ /* move after the boundary */
+ cut = real_boundary - buf + real_boundary_len;
+ buf += cut;
+ len -= cut;
+ consumed += cut;
+
+ if (buf == php_http_locate_bin_eol(buf, len, &eol_len)) {
+ /* skip CRLF */
+ buf += eol_len;
+ len -= eol_len;
+ consumed += eol_len;
+
+ if (!first_boundary) {
+ /* advance messages */
+ php_http_message_t *msg;
+
+ msg = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
+ msg->parent = arg->parser->message;
+ arg->parser->message = msg;
+ }
+ } else {
+ /* is this the last boundary? */
+ if (*buf == '-') {
+ /* ignore the rest */
+ consumed += len;
+ len = 0;
+ } else {
+ /* let this be garbage */
+ php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_BODY, "Malformed multipart boundary at pos %zu", consumed);
+ return -1;
+ }
+ }
+ }
+ } while (boundary && len);
+
+ /* let there be room for the next boundary */
+ if (len > arg->boundary_len) {
+ consumed += len - arg->boundary_len;
+ php_http_buffer_append(&arg->buf, buf, len - arg->boundary_len);
+ php_http_message_parser_parse(arg->parser, &arg->buf, 0, &arg->parser->message);