Skip to content

cURL: make possible to send file from buffer string #6456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions ext/curl/curl_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,33 @@ void curlfile_register_class(void)
curl_CURLFile_class->serialize = zend_class_serialize_deny;
curl_CURLFile_class->unserialize = zend_class_unserialize_deny;
}

PHP_CURL_API zend_class_entry *curl_CURLStringFile_class;

ZEND_METHOD(CURLStringFile, __construct)
{
zend_string *data, *postname, *mime = NULL;
zval *object;

object = ZEND_THIS;

ZEND_PARSE_PARAMETERS_START(2,3)
Z_PARAM_STR(data)
Z_PARAM_STR(postname)
Z_PARAM_OPTIONAL
Z_PARAM_STR(mime)
ZEND_PARSE_PARAMETERS_END();

zend_update_property_str(curl_CURLStringFile_class, Z_OBJ_P(object), "data", sizeof("data") - 1, data);
zend_update_property_str(curl_CURLStringFile_class, Z_OBJ_P(object), "postname", sizeof("postname")-1, postname);
if (mime) {
zend_update_property_str(curl_CURLStringFile_class, Z_OBJ_P(object), "mime", sizeof("mime")-1, mime);
} else {
zend_update_property_string(curl_CURLStringFile_class, Z_OBJ_P(object), "mime", sizeof("mime")-1, "application/octet-stream");
}
}

void curlstringfile_register_class(void)
{
curl_CURLStringFile_class = register_class_CURLStringFile();
}
9 changes: 9 additions & 0 deletions ext/curl/curl_file.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,12 @@ public function setMimeType(string $mime_type) {}
/** @return void */
public function setPostFilename(string $posted_filename) {}
}

class CURLStringFile
{
public string $data;
public string $postname;
public string $mime;

public function __construct(string $data, string $postname, string $mime = "application/octet-stream") {}
}
43 changes: 42 additions & 1 deletion ext/curl/curl_file_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 2bd78005380fd7f885618c4cb993bb21abe8cea9 */
* Stub hash: fdeef1c2a9e835b443d6e4cced23656ce21d8a30 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_CURLFile___construct, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
Expand All @@ -22,13 +22,20 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_CURLFile_setPostFilename, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, posted_filename, IS_STRING, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_CURLStringFile___construct, 0, 0, 2)
ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, postname, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, mime, IS_STRING, 0, "\"application/octet-stream\"")
ZEND_END_ARG_INFO()


ZEND_METHOD(CURLFile, __construct);
ZEND_METHOD(CURLFile, getFilename);
ZEND_METHOD(CURLFile, getMimeType);
ZEND_METHOD(CURLFile, getPostFilename);
ZEND_METHOD(CURLFile, setMimeType);
ZEND_METHOD(CURLFile, setPostFilename);
ZEND_METHOD(CURLStringFile, __construct);


static const zend_function_entry class_CURLFile_methods[] = {
Expand All @@ -41,6 +48,12 @@ static const zend_function_entry class_CURLFile_methods[] = {
ZEND_FE_END
};


static const zend_function_entry class_CURLStringFile_methods[] = {
ZEND_ME(CURLStringFile, __construct, arginfo_class_CURLStringFile___construct, ZEND_ACC_PUBLIC)
ZEND_FE_END
};

zend_class_entry *register_class_CURLFile()
{
zend_class_entry ce, *class_entry;
Expand Down Expand Up @@ -69,3 +82,31 @@ zend_class_entry *register_class_CURLFile()
return class_entry;
}

zend_class_entry *register_class_CURLStringFile()
{
zend_class_entry ce, *class_entry;

INIT_CLASS_ENTRY(ce, "CURLStringFile", class_CURLStringFile_methods);
class_entry = zend_register_internal_class_ex(&ce, NULL);

zval property_data_default_value;
ZVAL_UNDEF(&property_data_default_value);
zend_string *property_data_name = zend_string_init("data", sizeof("data") - 1, 1);
zend_declare_typed_property(class_entry, property_data_name, &property_data_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
zend_string_release(property_data_name);

zval property_postname_default_value;
ZVAL_UNDEF(&property_postname_default_value);
zend_string *property_postname_name = zend_string_init("postname", sizeof("postname") - 1, 1);
zend_declare_typed_property(class_entry, property_postname_name, &property_postname_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
zend_string_release(property_postname_name);

zval property_mime_default_value;
ZVAL_UNDEF(&property_mime_default_value);
zend_string *property_mime_name = zend_string_init("mime", sizeof("mime") - 1, 1);
zend_declare_typed_property(class_entry, property_mime_name, &property_mime_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
zend_string_release(property_mime_name);

return class_entry;
}

4 changes: 4 additions & 0 deletions ext/curl/curl_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ struct _php_curl_send_headers {
struct _php_curl_free {
zend_llist post;
zend_llist stream;
#if LIBCURL_VERSION_NUM < 0x073800 /* 7.56.0 */
zend_llist buffers;
#endif
HashTable *slist;
};

Expand Down Expand Up @@ -155,6 +158,7 @@ static inline php_curlsh *curl_share_from_obj(zend_object *obj) {
void curl_multi_register_handlers(void);
void curl_share_register_handlers(void);
void curlfile_register_class(void);
void curlstringfile_register_class(void);
int curl_cast_object(zend_object *obj, zval *result, int type);

#endif /* _PHP_CURL_PRIVATE_H */
91 changes: 91 additions & 0 deletions ext/curl/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,7 @@ PHP_MINIT_FUNCTION(curl)
curl_share_ce = register_class_CurlShareHandle();
curl_share_register_handlers();
curlfile_register_class();
curlstringfile_register_class();

return SUCCESS;
}
Expand Down Expand Up @@ -1654,6 +1655,15 @@ static void curl_free_cb_arg(void **cb_arg_p)
}
/* }}} */

#if LIBCURL_VERSION_NUM < 0x073800 /* 7.56.0 */
/* {{{ curl_free_buffers */
static void curl_free_buffers(void **buffer)
{
zend_string_release((zend_string *) *buffer);
}
/* }}} */
#endif

/* {{{ curl_free_slist */
static void curl_free_slist(zval *el)
{
Expand Down Expand Up @@ -1744,6 +1754,10 @@ void init_curl_handle(php_curl *ch)
zend_llist_init(&ch->to_free->post, sizeof(struct HttpPost *), (llist_dtor_func_t)curl_free_post, 0);
zend_llist_init(&ch->to_free->stream, sizeof(struct mime_data_cb_arg *), (llist_dtor_func_t)curl_free_cb_arg, 0);

#if LIBCURL_VERSION_NUM < 0x073800 /* 7.56.0 */
zend_llist_init(&ch->to_free->buffers, sizeof(zend_string *), (llist_dtor_func_t)curl_free_buffers, 0);
#endif

ch->to_free->slist = emalloc(sizeof(HashTable));
zend_hash_init(ch->to_free->slist, 4, NULL, curl_free_slist, 0);
ZVAL_UNDEF(&ch->postfields);
Expand Down Expand Up @@ -2086,6 +2100,78 @@ static inline int build_mime_structure_from_hash(php_curl *ch, zval *zpostfields
continue;
}

if (Z_TYPE_P(current) == IS_OBJECT && instanceof_function(Z_OBJCE_P(current), curl_CURLStringFile_class)) {
/* new-style file upload from string */
zval *prop, rv;
char *type = NULL, *filename = NULL;

prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "postname", sizeof("postname")-1, 0, &rv);
if (EG(exception)) {
zend_string_release_ex(string_key, 0);
return FAILURE;
}
ZVAL_DEREF(prop);
ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);

filename = Z_STRVAL_P(prop);

prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "mime", sizeof("mime")-1, 0, &rv);
if (EG(exception)) {
zend_string_release_ex(string_key, 0);
return FAILURE;
}
ZVAL_DEREF(prop);
ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);

type = Z_STRVAL_P(prop);

prop = zend_read_property(curl_CURLStringFile_class, Z_OBJ_P(current), "data", sizeof("data")-1, 0, &rv);
if (EG(exception)) {
zend_string_release_ex(string_key, 0);
return FAILURE;
}
ZVAL_DEREF(prop);
ZEND_ASSERT(Z_TYPE_P(prop) == IS_STRING);

postval = Z_STR_P(prop);

#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
zval_ptr_dtor(&ch->postfields);
ZVAL_COPY(&ch->postfields, zpostfields);

part = curl_mime_addpart(mime);
if (part == NULL) {
zend_string_release_ex(string_key, 0);
return FAILURE;
}
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
|| (form_error = curl_mime_data(part, ZSTR_VAL(postval), ZSTR_LEN(postval))) != CURLE_OK
|| (form_error = curl_mime_filename(part, filename)) != CURLE_OK
|| (form_error = curl_mime_type(part, type)) != CURLE_OK) {
error = form_error;
}
#else
postval = zend_string_copy(postval);
zend_llist_add_element(&ch->to_free->buffers, &postval);

form_error = curl_formadd(&first, &last,
CURLFORM_COPYNAME, ZSTR_VAL(string_key),
CURLFORM_NAMELENGTH, ZSTR_LEN(string_key),
CURLFORM_BUFFER, filename,
CURLFORM_CONTENTTYPE, type,
CURLFORM_BUFFERPTR, ZSTR_VAL(postval),
CURLFORM_BUFFERLENGTH, ZSTR_LEN(postval),
CURLFORM_END);
if (form_error != CURL_FORMADD_OK) {
/* Not nice to convert between enums but we only have place for one error type */
error = (CURLcode)form_error;
}
#endif

zend_string_release_ex(string_key, 0);
continue;
}

postval = zval_get_tmp_string(current, &tmp_postval);

#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
Expand Down Expand Up @@ -3330,6 +3416,11 @@ static void curl_free_obj(zend_object *object)
if (--(*ch->clone) == 0) {
zend_llist_clean(&ch->to_free->post);
zend_llist_clean(&ch->to_free->stream);

#if LIBCURL_VERSION_NUM < 0x073800 /* 7.56.0 */
zend_llist_clean(&ch->to_free->buffers);
#endif

zend_hash_destroy(ch->to_free->slist);
efree(ch->to_free->slist);
efree(ch->to_free);
Expand Down
1 change: 1 addition & 0 deletions ext/curl/php_curl.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ PHP_CURL_API extern zend_class_entry *curl_ce;
PHP_CURL_API extern zend_class_entry *curl_share_ce;
PHP_CURL_API extern zend_class_entry *curl_multi_ce;
PHP_CURL_API extern zend_class_entry *curl_CURLFile_class;
PHP_CURL_API extern zend_class_entry *curl_CURLStringFile_class;

#endif /* _PHP_CURL_H */
86 changes: 86 additions & 0 deletions ext/curl/tests/curl_string_file_upload.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
--TEST--
CURL file uploading from string
--SKIPIF--
<?php include 'skipif.inc'; ?>
--FILE--
<?php

function testcurl($ch, $postname, $data, $mime = null)
{
if (is_null($mime)) {
// for default mime value
$file = new CURLStringFile($data, $postname);
} else {
$file = new CURLStringFile($data, $postname, $mime);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => $file));
var_dump(curl_exec($ch));
}

include 'server.inc';
$host = curl_cli_server_start();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "{$host}/get.php?test=string_file");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$data = "test\0test";
var_dump(md5($data));
testcurl($ch, 'foo.txt', $data);
testcurl($ch, 'foo.txt', $data, 'text/plain');
testcurl($ch, '', $data);
testcurl($ch, 'foo.txt', '');
testcurl($ch, "foo.txt\0broken_string", $data, "text/plain\0broken_string");

// properties
$file = new CURLStringFile($data, 'foo.txt');
$file->mime = 'text/plain';
var_dump($file->mime);
var_dump($file->postname);
var_dump(md5($file->data));
curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => $file));
var_dump(curl_exec($ch));

// serialization / deserialization
$old = new CURLStringFile($data, 'foo.txt', 'text/plain');
$serialized = serialize($old);
$new = unserialize($serialized);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => $new));
var_dump(curl_exec($ch));

// destroy object before send request
$file = new CURLStringFile($data, 'foo.txt', 'text/plain');
curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => $file));
unset($file);
var_dump(curl_exec($ch));

// clone curl handler
$file = new CURLStringFile($data, 'foo.txt', 'text/plain');
curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => $file));
$ch2 = clone $ch;
var_dump(curl_exec($ch2));

// properties are references

$file = new CURLStringFile($data, 'foo.txt', 'text/plain');
$data =& $file->data;
$postname =& $file->postname;
$mime =& $file->mime;
curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => $file));
var_dump(curl_exec($ch));

?>
--EXPECTF--
string(%d) "62942c05ed0d1b501c4afe6dc1c4db1b"
string(%d) "foo.txt|application/octet-stream|62942c05ed0d1b501c4afe6dc1c4db1b"
string(%d) "foo.txt|text/plain|62942c05ed0d1b501c4afe6dc1c4db1b"
string(%d) "error:4"
string(%d) "foo.txt|application/octet-stream|d41d8cd98f00b204e9800998ecf8427e"
string(%d) "foo.txt|text/plain|62942c05ed0d1b501c4afe6dc1c4db1b"
string(%d) "text/plain"
string(%d) "foo.txt"
string(%d) "62942c05ed0d1b501c4afe6dc1c4db1b"
string(%d) "foo.txt|text/plain|62942c05ed0d1b501c4afe6dc1c4db1b"
string(%d) "foo.txt|text/plain|62942c05ed0d1b501c4afe6dc1c4db1b"
string(%d) "foo.txt|text/plain|62942c05ed0d1b501c4afe6dc1c4db1b"
string(%d) "foo.txt|text/plain|62942c05ed0d1b501c4afe6dc1c4db1b"
string(%d) "foo.txt|text/plain|62942c05ed0d1b501c4afe6dc1c4db1b"
9 changes: 9 additions & 0 deletions ext/curl/tests/responder/get.inc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
echo $_FILES['file']['name'] . '|' . $_FILES['file']['type'] . '|' . $_FILES['file']['size'];
}
break;
case 'string_file':
if (isset($_FILES['file'])) {
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
echo $_FILES['file']['name'] . '|' . $_FILES['file']['type'] . '|' . md5_file($_FILES['file']['tmp_name']);
} else {
echo 'error:' . $_FILES['file']['error'];
}
}
break;
case 'method':
echo $_SERVER['REQUEST_METHOD'];
break;
Expand Down