+--------------------------------------------------------------------+
*/
-#include "php_http.h"
+#include "php_http_api.h"
-#include <libgen.h>
#include <ext/standard/php_lcg.h>
-#include <ext/standard/php_string.h>
#define BOUNDARY_OPEN(body) \
do {\
const php_stream_statbuf *ssb = php_http_message_body_stat(body);
/* real file or temp buffer ? */
- if (body->ssb.sb.st_mtime) {
+ if (ssb && ssb->sb.st_mtime) {
char *etag;
spprintf(&etag, 0, "%lx-%lx-%lx", ssb->sb.st_ino, ssb->sb.st_mtime, ssb->sb.st_size);
} else {
php_http_etag_t *etag = php_http_etag_init(PHP_HTTP_G->env.etag_mode TSRMLS_CC);
- php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_etag_update, etag, 0, 0);
- return php_http_etag_finish(etag);
+ if (etag) {
+ php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_etag_update, etag, 0, 0);
+ return php_http_etag_finish(etag);
+ } else {
+ return NULL;
+ }
}
}
return print_len;
}
-PHP_HTTP_API STATUS php_http_message_body_add(php_http_message_body_t *body, HashTable *fields, HashTable *files)
+PHP_HTTP_API STATUS php_http_message_body_add_form(php_http_message_body_t *body, HashTable *fields, HashTable *files)
{
zval tmp;
return SUCCESS;
}
+PHP_HTTP_API void php_http_message_body_add_part(php_http_message_body_t *body, php_http_message_t *part)
+{
+ TSRMLS_FETCH_FROM_CTX(body->ts);
+
+ BOUNDARY_OPEN(body);
+ php_http_message_to_callback(part, (php_http_pass_callback_t) php_http_message_body_append, body);
+ BOUNDARY_CLOSE(body);
+}
+
-PHP_HTTP_API STATUS php_http_message_body_add_field(php_http_message_body_t *body, const char *name, const char *value_str, size_t value_len)
+PHP_HTTP_API STATUS php_http_message_body_add_form_field(php_http_message_body_t *body, const char *name, const char *value_str, size_t value_len)
{
char *safe_name;
TSRMLS_FETCH_FROM_CTX(body->ts);
BOUNDARY_OPEN(body);
php_http_message_body_appendf(
body,
- "Content-Disposition: form-data; name=\"%s\"" PHP_HTTP_CRLF
- "" PHP_HTTP_CRLF,
- safe_name);
+ "Content-Disposition: form-data; name=\"%s\"" PHP_HTTP_CRLF
+ "" PHP_HTTP_CRLF,
+ safe_name
+ );
php_http_message_body_append(body, value_str, value_len);
BOUNDARY_CLOSE(body);
return SUCCESS;
}
-PHP_HTTP_API STATUS php_http_message_body_add_file(php_http_message_body_t *body, const char *name, const char *path, const char *ctype)
+PHP_HTTP_API STATUS php_http_message_body_add_form_file(php_http_message_body_t *body, const char *name, const char *ctype, const char *path, php_stream *in)
{
- php_stream *in;
- char *path_dup = estrdup(path);
+ char *safe_name, *path_dup = estrdup(path);
TSRMLS_FETCH_FROM_CTX(body->ts);
- if ((in = php_stream_open_wrapper(path_dup, "r", REPORT_ERRORS|USE_PATH|STREAM_MUST_SEEK, NULL))) {
- php_stream_statbuf ssb = {{0}};
-
- if (SUCCESS == php_stream_stat(in, &ssb) && S_ISREG(ssb.sb.st_mode)) {
- char *safe_name = php_addslashes(estrdup(name), strlen(name), NULL, 1 TSRMLS_CC);
-
- BOUNDARY_OPEN(body);
- php_http_message_body_appendf(
- body,
- "Content-Disposition: attachment; name=\"%s\"; filename=\"%s\"" PHP_HTTP_CRLF
- "Content-Type: %s" PHP_HTTP_CRLF
- "Content-Length: %zu" PHP_HTTP_CRLF
- "" PHP_HTTP_CRLF,
- safe_name, basename(path_dup), ctype, ssb.sb.st_size);
- php_stream_copy_to_stream_ex(in, php_http_message_body_stream(body), PHP_STREAM_COPY_ALL, NULL);
- BOUNDARY_CLOSE(body);
-
- efree(safe_name);
- efree(path_dup);
- php_stream_close(in);
- return SUCCESS;
- } else {
- efree(path_dup);
- php_stream_close(in);
- php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_BODY, "Not a valid regular file: %s", path);
- return FAILURE;
- }
- } else {
- efree(path_dup);
- return FAILURE;
- }
+ safe_name = php_addslashes(estrdup(name), strlen(name), NULL, 1 TSRMLS_CC);
+ BOUNDARY_OPEN(body);
+ php_http_message_body_appendf(
+ body,
+ "Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"" PHP_HTTP_CRLF
+ "Content-Transfer-Encoding: binary" PHP_HTTP_CRLF
+ "Content-Type: %s" PHP_HTTP_CRLF
+ PHP_HTTP_CRLF,
+ safe_name, basename(path_dup), ctype
+ );
+ php_stream_copy_to_stream_ex(in, php_http_message_body_stream(body), PHP_STREAM_COPY_ALL, NULL);
+ BOUNDARY_CLOSE(body);
+
+ efree(safe_name);
+ efree(path_dup);
+
+ return SUCCESS;
}
static inline char *format_key(uint type, char *str, ulong num, const char *prefix) {
} else if (type == HASH_KEY_IS_STRING) {
new_key = estrdup(str);
} else {
- spprintf(&new_key, 0, "%lu", num);
+ new_key = estrdup("");
}
return new_key;
{
if (Z_TYPE_P(value) == IS_ARRAY || Z_TYPE_P(value) == IS_OBJECT) {
zval **val;
+ HashTable *ht;
HashPosition pos;
php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
TSRMLS_FETCH_FROM_CTX(body->ts);
- if (!HASH_OF(value)->nApplyCount) {
- ++HASH_OF(value)->nApplyCount;
+ ht = HASH_OF(value);
+ if (!ht->nApplyCount) {
+ ++ht->nApplyCount;
FOREACH_KEYVAL(pos, value, key, val) {
char *str = format_key(key.type, key.str, key.num, name);
if (SUCCESS != add_recursive_fields(body, str, *val)) {
efree(str);
- HASH_OF(value)->nApplyCount--;
+ ht->nApplyCount--;
return FAILURE;
}
efree(str);
}
- --HASH_OF(value)->nApplyCount;
+ --ht->nApplyCount;
}
} else {
zval *cpy = php_http_ztyp(IS_STRING, value);
- php_http_message_body_add_field(body, name, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy));
+ php_http_message_body_add_form_field(body, name, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy));
zval_ptr_dtor(&cpy);
}
static STATUS add_recursive_files(php_http_message_body_t *body, const char *name, zval *value)
{
- if (Z_TYPE_P(value) == IS_ARRAY || Z_TYPE_P(value) == IS_OBJECT) {
- zval **zfile, **zname, **ztype;
- TSRMLS_FETCH_FROM_CTX(body->ts);
+ zval **zdata = NULL, **zfile, **zname, **ztype;
+ HashTable *ht;
+ TSRMLS_FETCH_FROM_CTX(body->ts);
- if ((SUCCESS == zend_hash_find(HASH_OF(value), ZEND_STRS("name"), (void *) &zname))
- && (SUCCESS == zend_hash_find(HASH_OF(value), ZEND_STRS("file"), (void *) &zfile))
- && (SUCCESS == zend_hash_find(HASH_OF(value), ZEND_STRS("type"), (void *) &ztype))
- ) {
- zval *zfc = php_http_ztyp(IS_STRING, *zfile), *znc = php_http_ztyp(IS_STRING, *zname), *ztc = php_http_ztyp(IS_STRING, *ztype);
- char *str = format_key(HASH_KEY_IS_STRING, Z_STRVAL_P(znc), 0, name);
- STATUS ret = php_http_message_body_add_file(body, str, Z_STRVAL_P(zfc), Z_STRVAL_P(ztc));
+ if (Z_TYPE_P(value) != IS_ARRAY && Z_TYPE_P(value) != IS_OBJECT) {
+ php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_BODY, "Expected array or object (name, type, file) for message body file to add");
+ return FAILURE;
+ }
- efree(str);
- zval_ptr_dtor(&znc);
- zval_ptr_dtor(&zfc);
- zval_ptr_dtor(&ztc);
+ ht = HASH_OF(value);
- if (ret != SUCCESS) {
- return ret;
- }
- } else {
- zval **val;
- HashPosition pos;
- php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
+ if ((SUCCESS != zend_hash_find(ht, ZEND_STRS("name"), (void *) &zname))
+ || (SUCCESS != zend_hash_find(ht, ZEND_STRS("type"), (void *) &ztype))
+ || (SUCCESS != zend_hash_find(ht, ZEND_STRS("file"), (void *) &zfile))
+ ) {
+ zval **val;
+ HashPosition pos;
+ php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
- if (!HASH_OF(value)->nApplyCount) {
- ++HASH_OF(value)->nApplyCount;
- FOREACH_KEYVAL(pos, value, key, val) {
+ if (!ht->nApplyCount) {
+ ++ht->nApplyCount;
+ FOREACH_HASH_KEYVAL(pos, ht, key, val) {
+ if (Z_TYPE_PP(val) == IS_ARRAY || Z_TYPE_PP(val) == IS_OBJECT) {
char *str = format_key(key.type, key.str, key.num, name);
+
if (SUCCESS != add_recursive_files(body, str, *val)) {
efree(str);
- --HASH_OF(value)->nApplyCount;
+ --ht->nApplyCount;
return FAILURE;
}
efree(str);
}
- --HASH_OF(value)->nApplyCount;
}
+ --ht->nApplyCount;
}
+ return SUCCESS;
} else {
- TSRMLS_FETCH_FROM_CTX(body->ts);
- php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_BODY, "Unrecognized array format for message body file to add");
- return FAILURE;
+ 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;
+ }
+
}
+}
- return SUCCESS;
+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 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);
+ }
+
+ arg->consumed += consumed;
+ return consumed;
+}
+
+PHP_HTTP_API php_http_message_t *php_http_message_body_split(php_http_message_body_t *body, const char *boundary)
+{
+ php_stream *s = php_http_message_body_stream(body);
+ php_http_buffer_t *tmp = NULL;
+ php_http_message_t *msg = NULL;
+ struct splitbody_arg arg;
+ TSRMLS_FETCH_FROM_CTX(body->ts);
+
+ php_http_buffer_init(&arg.buf);
+ arg.parser = php_http_message_parser_init(NULL TSRMLS_CC);
+ arg.boundary_len = spprintf(&arg.boundary_str, 0, "\n--%s", boundary);
+ arg.consumed = 0;
+
+ php_stream_rewind(s);
+ while (!php_stream_eof(s)) {
+ php_http_buffer_passthru(&tmp, 0x1000, (php_http_buffer_pass_func_t) _php_stream_read, s, splitbody, &arg TSRMLS_CC);
+ }
+
+ msg = arg.parser->message;
+ arg.parser->message = NULL;
+
+ php_http_buffer_free(&tmp);
+ php_http_message_parser_free(&arg.parser);
+ php_http_buffer_dtor(&arg.buf);
+ STR_FREE(arg.boundary_str);
+
+ return msg;
}
/* PHP */
PHP_HTTP_ARG_VAL(string, 0)
PHP_HTTP_END_ARGS;
-PHP_HTTP_BEGIN_ARGS(add, 0)
- PHP_HTTP_ARG_VAL(fields, 0)
- PHP_HTTP_ARG_VAL(files, 0)
+PHP_HTTP_BEGIN_ARGS(addForm, 0)
+ PHP_HTTP_ARG_ARR(fields, 1, 0)
+ PHP_HTTP_ARG_ARR(files, 1, 0)
+PHP_HTTP_END_ARGS;
+
+PHP_HTTP_BEGIN_ARGS(addPart, 1)
+ PHP_HTTP_ARG_OBJ(http\\Message, message, 0)
PHP_HTTP_END_ARGS;
+PHP_HTTP_EMPTY_ARGS(etag);
+
+PHP_HTTP_BEGIN_ARGS(stat, 0)
+ PHP_HTTP_ARG_VAL(what, 0)
+PHP_HTTP_END_ARGS;
zend_class_entry *php_http_message_body_class_entry;
zend_function_entry php_http_message_body_method_entry[] = {
PHP_HTTP_MESSAGE_BODY_ME(toStream, ZEND_ACC_PUBLIC)
PHP_HTTP_MESSAGE_BODY_ME(toCallback, ZEND_ACC_PUBLIC)
PHP_HTTP_MESSAGE_BODY_ME(append, ZEND_ACC_PUBLIC)
- PHP_HTTP_MESSAGE_BODY_ME(add, ZEND_ACC_PUBLIC)
+ PHP_HTTP_MESSAGE_BODY_ME(addForm, ZEND_ACC_PUBLIC)
+ PHP_HTTP_MESSAGE_BODY_ME(addPart, ZEND_ACC_PUBLIC)
+ PHP_HTTP_MESSAGE_BODY_ME(etag, ZEND_ACC_PUBLIC)
+ PHP_HTTP_MESSAGE_BODY_ME(stat, ZEND_ACC_PUBLIC)
EMPTY_FUNCTION_ENTRY
};
static zend_object_handlers php_http_message_body_object_handlers;
RETURN_FALSE;
}
-struct fcd {
- zval *fcz;
- zend_fcall_info *fci;
- zend_fcall_info_cache *fcc;
-#ifdef ZTS
- void ***ts;
-#endif
-};
-
-static size_t pass(void *cb_arg, const char *str, size_t len)
-{
- struct fcd *fcd = cb_arg;
- zval *zdata;
- TSRMLS_FETCH_FROM_CTX(fcd->ts);
-
- MAKE_STD_ZVAL(zdata);
- ZVAL_STRINGL(zdata, str, len, 1);
- if (SUCCESS == zend_fcall_info_argn(fcd->fci TSRMLS_CC, 2, fcd->fcz, zdata)) {
- zend_fcall_info_call(fcd->fci, fcd->fcc, NULL, NULL TSRMLS_CC);
- zend_fcall_info_args_clear(fcd->fci, 0);
- }
- zval_ptr_dtor(&zdata);
- return len;
-}
PHP_METHOD(HttpMessageBody, toCallback)
{
- struct fcd fcd;
+ php_http_pass_fcall_arg_t fcd;
long offset = 0, forlen = 0;
if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f|ll", &fcd.fci, &fcd.fcc, &offset, &forlen)) {
php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
fcd.fcz = getThis();
Z_ADDREF_P(fcd.fcz);
- php_http_message_body_to_callback(obj->body, pass, &fcd, offset, forlen);
+ TSRMLS_SET_CTX(fcd.ts);
+
+ php_http_message_body_to_callback(obj->body, php_http_pass_fcall_callback, &fcd, offset, forlen);
+ zend_fcall_info_args_clear(&fcd.fci, 1);
+
zval_ptr_dtor(&fcd.fcz);
RETURN_TRUE;
}
RETURN_FALSE;
}
-PHP_METHOD(HttpMessageBody, add)
+PHP_METHOD(HttpMessageBody, addForm)
{
HashTable *fields = NULL, *files = NULL;
- if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|hh", &fields, &files)) {
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|h!h!", &fields, &files)) {
+ php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ RETURN_SUCCESS(php_http_message_body_add_form(obj->body, fields, files));
+ }
+ RETURN_FALSE;
+}
+
+PHP_METHOD(HttpMessageBody, addPart)
+{
+ zval *zobj;
+
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zobj, php_http_message_class_entry)) {
php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+ php_http_message_object_t *mobj = zend_object_store_get_object(zobj TSRMLS_CC);
- RETURN_SUCCESS(php_http_message_body_add(obj->body, fields, files));
+ php_http_message_body_add_part(obj->body, mobj->message);
+ RETURN_TRUE;
}
RETURN_FALSE;
}
+
+PHP_METHOD(HttpMessageBody, etag)
+{
+ if (SUCCESS == zend_parse_parameters_none()) {
+ php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+ char *etag = php_http_message_body_etag(obj->body);
+
+ if (etag) {
+ RETURN_STRING(etag, 0);
+ }
+ }
+ RETURN_FALSE;
+}
+
+PHP_METHOD(HttpMessageBody, stat)
+{
+ char *field_str = NULL;
+ int field_len = 0;
+
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &field_str, &field_len)) {
+ php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+ const php_stream_statbuf *sb = php_http_message_body_stat(obj->body);
+
+ if (sb) {
+ if (field_str && field_len) {
+ switch (*field_str) {
+ case 's':
+ case 'S':
+ RETURN_LONG(sb->sb.st_size);
+ break;
+ case 'a':
+ case 'A':
+ RETURN_LONG(sb->sb.st_atime);
+ break;
+ case 'm':
+ case 'M':
+ RETURN_LONG(sb->sb.st_mtime);
+ break;
+ case 'c':
+ case 'C':
+ RETURN_LONG(sb->sb.st_ctime);
+ break;
+ default:
+ php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_BODY, "unknown stat field: '%s' (should be one of [s]ize, [a]time, [m]time or [c]time)", field_str);
+ break;
+ }
+ } else {
+ array_init(return_value);
+ add_assoc_long_ex(return_value, ZEND_STRS("size"), sb->sb.st_size);
+ add_assoc_long_ex(return_value, ZEND_STRS("atime"), sb->sb.st_atime);
+ add_assoc_long_ex(return_value, ZEND_STRS("mtime"), sb->sb.st_mtime);
+ add_assoc_long_ex(return_value, ZEND_STRS("ctime"), sb->sb.st_ctime);
+ return;
+ }
+ }
+ }
+ RETURN_FALSE;
+}
+
/*
* Local variables:
* tab-width: 4