-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add support for upload files from buffer string in curl extenion #1283
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
Changes from all commits
6319f95
88e1ae0
17ea5ca
4e4fec6
e59e9de
afbe03b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,44 +29,75 @@ | |
|
||
PHP_CURL_API zend_class_entry *curl_CURLFile_class; | ||
|
||
static void curlfile_ctor(INTERNAL_FUNCTION_PARAMETERS) | ||
static void curlfile_ctor(char *fname, size_t fname_len, char *mime, size_t mime_len, char *postname, size_t postname_len, char *buffer, size_t buffer_len, zval *return_value) | ||
{ | ||
char *fname = NULL, *mime = NULL, *postname = NULL; | ||
size_t fname_len, mime_len, postname_len; | ||
zval *cf = return_value; | ||
|
||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ss", &fname, &fname_len, &mime, &mime_len, &postname, &postname_len) == FAILURE) { | ||
return; | ||
} | ||
|
||
if (fname) { | ||
zend_update_property_string(curl_CURLFile_class, cf, "name", sizeof("name")-1, fname); | ||
zend_update_property_stringl(curl_CURLFile_class, cf, "name", sizeof("name")-1, fname, fname_len); | ||
} | ||
|
||
if (mime) { | ||
zend_update_property_string(curl_CURLFile_class, cf, "mime", sizeof("mime")-1, mime); | ||
zend_update_property_stringl(curl_CURLFile_class, cf, "mime", sizeof("mime")-1, mime, mime_len); | ||
} | ||
|
||
if (postname) { | ||
zend_update_property_string(curl_CURLFile_class, cf, "postname", sizeof("postname")-1, postname); | ||
zend_update_property_stringl(curl_CURLFile_class, cf, "postname", sizeof("postname")-1, postname, postname_len); | ||
} | ||
|
||
if (buffer) { | ||
zend_update_property_stringl(curl_CURLFile_class, cf, "buffer", sizeof("buffer")-1, buffer, buffer_len); | ||
} | ||
} | ||
|
||
/* {{{ proto void CURLFile::__construct(string $name, [string $mimetype [, string $postfilename]]) | ||
/* {{{ proto void CURLFile::__construct([string $name, [string $mimetype [, string $postfilename]]]) | ||
Create the CURLFile object */ | ||
ZEND_METHOD(CURLFile, __construct) | ||
{ | ||
char *fname = NULL, *mime = NULL, *postname = NULL, *buffer = NULL; | ||
size_t fname_len, mime_len, postname_len, buffer_len; | ||
|
||
return_value = getThis(); | ||
curlfile_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU); | ||
|
||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sss", &fname, &fname_len, &mime, &mime_len, &postname, &postname_len) == FAILURE) { | ||
return; | ||
} | ||
|
||
curlfile_ctor(fname, fname_len, mime, mime_len, postname, postname_len, buffer, buffer_len, return_value); | ||
} | ||
/* }}} */ | ||
|
||
/* {{{ proto CURLFile curl_file_create(string $name, [string $mimetype [, string $postfilename]]) | ||
Create the CURLFile object */ | ||
Create the CURLFile object from file */ | ||
PHP_FUNCTION(curl_file_create) | ||
{ | ||
object_init_ex( return_value, curl_CURLFile_class ); | ||
curlfile_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU); | ||
char *fname = NULL, *mime = NULL, *postname = NULL, *buffer = NULL; | ||
size_t fname_len, mime_len, postname_len, buffer_len; | ||
|
||
object_init_ex( return_value, curl_CURLFile_class ); | ||
|
||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ss", &fname, &fname_len, &mime, &mime_len, &postname, &postname_len) == FAILURE) { | ||
return; | ||
} | ||
|
||
curlfile_ctor(fname, fname_len, mime, mime_len, postname, postname_len, buffer, buffer_len, return_value); | ||
} | ||
/* }}} */ | ||
|
||
/* {{{ proto CURLFile curl_buffer_file_create(string $buffer, string $postfilename, [string $mimetype]) | ||
Create the CURLFile object from string buffer */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is kind of confusing that curl_file_create takes name, mimetype, filename but curl_buffer_file_create takes buffer, filename, mimetype - two last arguments are kind of switched. I understand why it may be but I foresee people complaining about it. Not sure what to do here :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. We have only two variants:
|
||
PHP_FUNCTION(curl_buffer_file_create) | ||
{ | ||
char *fname = NULL, *mime = NULL, *postname = NULL, *buffer = NULL; | ||
size_t fname_len, mime_len, postname_len, buffer_len; | ||
|
||
object_init_ex( return_value, curl_CURLFile_class ); | ||
|
||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|s", &buffer, &buffer_len, &postname, &postname_len, &mime, &mime_len) == FAILURE) { | ||
return; | ||
} | ||
|
||
curlfile_ctor(fname, fname_len, mime, mime_len, postname, postname_len, buffer, buffer_len, return_value); | ||
} | ||
/* }}} */ | ||
|
||
|
@@ -90,7 +121,7 @@ static void curlfile_set_property(char *name, INTERNAL_FUNCTION_PARAMETERS) | |
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) { | ||
return; | ||
} | ||
zend_update_property_string(curl_CURLFile_class, getThis(), name, strlen(name), arg); | ||
zend_update_property_stringl(curl_CURLFile_class, getThis(), name, strlen(name), arg, arg_len); | ||
} | ||
|
||
/* {{{ proto string CURLFile::getFilename() | ||
|
@@ -101,6 +132,14 @@ ZEND_METHOD(CURLFile, getFilename) | |
} | ||
/* }}} */ | ||
|
||
/* {{{ proto string CURLFile::getBuffer() | ||
Get buffer */ | ||
ZEND_METHOD(CURLFile, getBuffer) | ||
{ | ||
curlfile_get_property("buffer", INTERNAL_FUNCTION_PARAM_PASSTHRU); | ||
} | ||
/* }}} */ | ||
|
||
/* {{{ proto string CURLFile::getMimeType() | ||
Get MIME type */ | ||
ZEND_METHOD(CURLFile, getMimeType) | ||
|
@@ -117,6 +156,22 @@ ZEND_METHOD(CURLFile, getPostFilename) | |
} | ||
/* }}} */ | ||
|
||
/* {{{ proto void CURLFile::setFilename(string $name) | ||
Set file name */ | ||
ZEND_METHOD(CURLFile, setFilename) | ||
{ | ||
curlfile_set_property("name", INTERNAL_FUNCTION_PARAM_PASSTHRU); | ||
} | ||
/* }}} */ | ||
|
||
/* {{{ proto void CURLFile::setBuffer(string $buffer) | ||
Set buffer */ | ||
ZEND_METHOD(CURLFile, setBuffer) | ||
{ | ||
curlfile_set_property("buffer", INTERNAL_FUNCTION_PARAM_PASSTHRU); | ||
} | ||
/* }}} */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if somebody sets both buffer and filename? Not clear what happens in this case - would the last one win? Would filename/buffer always win? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Filename always win.. Are you mean make more clear documentation or make more clear API? For example throw error if we setup both params. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if filename always wins then maybe setting buffer while filename is already set should produce an exception. Though then it is not clear why these methods are needed at all - can't you just construct the proper one via factory method? Why one would need to change it on existing object? I'd much rather prefer it to be immutable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I fully agree with you! Immutable objects will be perfect for CURLFile. But it greatly breaks backward compatibility. So if we try to make CURLFile immutable object we required RFC, and many chance that it can't be approved by community. |
||
|
||
/* {{{ proto void CURLFile::setMimeType(string $mime) | ||
Set MIME type */ | ||
ZEND_METHOD(CURLFile, setMimeType) | ||
|
@@ -137,12 +192,17 @@ ZEND_METHOD(CURLFile, setPostFilename) | |
Unserialization handler */ | ||
ZEND_METHOD(CURLFile, __wakeup) | ||
{ | ||
zend_update_property_string(curl_CURLFile_class, getThis(), "name", sizeof("name")-1, ""); | ||
zend_throw_exception(NULL, "Unserialization of CURLFile instances is not allowed", 0); | ||
zval *fname, rv; | ||
|
||
fname = zend_read_property(curl_CURLFile_class, getThis(), "name", strlen("name"), 1, &rv); | ||
if (zval_is_true(fname)) { | ||
zend_update_property_string(curl_CURLFile_class, getThis(), "name", sizeof("name")-1, ""); | ||
zend_throw_exception(NULL, "Unserialization of CURLFile instances with file name is not allowed", 0); | ||
} | ||
} | ||
/* }}} */ | ||
|
||
ZEND_BEGIN_ARG_INFO_EX(arginfo_curlfile_create, 0, 0, 1) | ||
ZEND_BEGIN_ARG_INFO_EX(arginfo_curlfile_ctor, 0, 0, 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why rename it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. curl_file_create in interface.c use same name 'arginfo_curlfile_create' for arg info. So because I added new arg info for function curl_buffer_file_create I decided to rename arg info for constructor. |
||
ZEND_ARG_INFO(0, filename) | ||
ZEND_ARG_INFO(0, mimetype) | ||
ZEND_ARG_INFO(0, postname) | ||
|
@@ -154,8 +214,11 @@ ZEND_END_ARG_INFO() | |
|
||
|
||
static const zend_function_entry curlfile_funcs[] = { | ||
PHP_ME(CURLFile, __construct, arginfo_curlfile_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) | ||
PHP_ME(CURLFile, __construct, arginfo_curlfile_ctor, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) | ||
PHP_ME(CURLFile, getFilename, NULL, ZEND_ACC_PUBLIC) | ||
PHP_ME(CURLFile, setFilename, arginfo_curlfile_name, ZEND_ACC_PUBLIC) | ||
PHP_ME(CURLFile, getBuffer, NULL, ZEND_ACC_PUBLIC) | ||
PHP_ME(CURLFile, setBuffer, arginfo_curlfile_name, ZEND_ACC_PUBLIC) | ||
PHP_ME(CURLFile, getMimeType, NULL, ZEND_ACC_PUBLIC) | ||
PHP_ME(CURLFile, setMimeType, arginfo_curlfile_name, ZEND_ACC_PUBLIC) | ||
PHP_ME(CURLFile, getPostFilename, NULL, ZEND_ACC_PUBLIC) | ||
|
@@ -170,6 +233,7 @@ void curlfile_register_class(void) | |
INIT_CLASS_ENTRY( ce, "CURLFile", curlfile_funcs ); | ||
curl_CURLFile_class = zend_register_internal_class(&ce); | ||
zend_declare_property_string(curl_CURLFile_class, "name", sizeof("name")-1, "", ZEND_ACC_PUBLIC); | ||
zend_declare_property_string(curl_CURLFile_class, "buffer", sizeof("buffer")-1, "", ZEND_ACC_PUBLIC); | ||
zend_declare_property_string(curl_CURLFile_class, "mime", sizeof("mime")-1, "", ZEND_ACC_PUBLIC); | ||
zend_declare_property_string(curl_CURLFile_class, "postname", sizeof("postname")-1, "", ZEND_ACC_PUBLIC); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -427,6 +427,12 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_curlfile_create, 0, 0, 1) | |
ZEND_ARG_INFO(0, mimetype) | ||
ZEND_ARG_INFO(0, postname) | ||
ZEND_END_ARG_INFO() | ||
|
||
ZEND_BEGIN_ARG_INFO_EX(arginfo_curlbufferfile_create, 0, 0, 2) | ||
ZEND_ARG_INFO(0, buffer) | ||
ZEND_ARG_INFO(0, postname) | ||
ZEND_ARG_INFO(0, mimetype) | ||
ZEND_END_ARG_INFO() | ||
/* }}} */ | ||
|
||
/* {{{ curl_functions[] | ||
|
@@ -471,6 +477,7 @@ const zend_function_entry curl_functions[] = { | |
PHP_FE(curl_share_close, arginfo_curl_share_close) | ||
PHP_FE(curl_share_setopt, arginfo_curl_share_setopt) | ||
PHP_FE(curl_file_create, arginfo_curlfile_create) | ||
PHP_FE(curl_buffer_file_create, arginfo_curlbufferfile_create) | ||
PHP_FE_END | ||
}; | ||
/* }}} */ | ||
|
@@ -2543,34 +2550,64 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{ | |
zval *prop, rv; | ||
char *type = NULL, *filename = NULL; | ||
|
||
prop = zend_read_property(curl_CURLFile_class, current, "name", sizeof("name")-1, 0, &rv); | ||
if (Z_TYPE_P(prop) != IS_STRING) { | ||
php_error_docref(NULL, E_WARNING, "Invalid filename for key %s", ZSTR_VAL(string_key)); | ||
} else { | ||
postval = Z_STR_P(prop); | ||
prop = zend_read_property(curl_CURLFile_class, current, "mime", sizeof("mime")-1, 0, &rv); | ||
if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) { | ||
type = Z_STRVAL_P(prop); | ||
} | ||
|
||
if (php_check_open_basedir(ZSTR_VAL(postval))) { | ||
return 1; | ||
} | ||
prop = zend_read_property(curl_CURLFile_class, current, "postname", sizeof("postname")-1, 0, &rv); | ||
if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) { | ||
filename = Z_STRVAL_P(prop); | ||
} | ||
|
||
prop = zend_read_property(curl_CURLFile_class, current, "mime", sizeof("mime")-1, 0, &rv); | ||
if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) { | ||
type = Z_STRVAL_P(prop); | ||
} | ||
prop = zend_read_property(curl_CURLFile_class, current, "postname", sizeof("postname")-1, 0, &rv); | ||
if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) { | ||
filename = Z_STRVAL_P(prop); | ||
prop = zend_read_property(curl_CURLFile_class, current, "name", sizeof("name")-1, 0, &rv); | ||
if (zval_is_true(prop)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure I understand why zval_is_true check? If you wanted to check for name being empty/null, then it should be that check. Otherwise you'll get in trouble with "0" etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @smalyshev , can it be something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure it can be UNDEF, but I'd rather specify explicitly which values we do expect - i.e. if we expect name to be NULL when we use buffer. Also, whatever check you do here you should also do on __wakeup. In fact, I'd make it a macro or inline function to ensure they are always the same. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we will use NULL as base value for this condition, we must change this code: Previous variant of pull-request with separate CURLBufferFile seems now less terrible ))) |
||
/* upload from "name" */ | ||
if (Z_TYPE_P(prop) != IS_STRING) { | ||
php_error_docref(NULL, E_WARNING, "Invalid filename for key %s", ZSTR_VAL(string_key)); | ||
} else { | ||
postval = Z_STR_P(prop); | ||
|
||
if (php_check_open_basedir(ZSTR_VAL(postval))) { | ||
return 1; | ||
} | ||
|
||
form_error = curl_formadd(&first, &last, | ||
CURLFORM_COPYNAME, ZSTR_VAL(string_key), | ||
CURLFORM_NAMELENGTH, ZSTR_LEN(string_key), | ||
CURLFORM_FILENAME, filename ? filename : ZSTR_VAL(postval), | ||
CURLFORM_CONTENTTYPE, type ? type : "application/octet-stream", | ||
CURLFORM_FILE, ZSTR_VAL(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; | ||
} | ||
} | ||
form_error = curl_formadd(&first, &last, | ||
CURLFORM_COPYNAME, ZSTR_VAL(string_key), | ||
CURLFORM_NAMELENGTH, ZSTR_LEN(string_key), | ||
CURLFORM_FILENAME, filename ? filename : ZSTR_VAL(postval), | ||
CURLFORM_CONTENTTYPE, type ? type : "application/octet-stream", | ||
CURLFORM_FILE, ZSTR_VAL(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; | ||
} else { | ||
/* upload from "buffer" */ | ||
prop = zend_read_property(curl_CURLFile_class, current, "buffer", sizeof("buffer")-1, 0, &rv); | ||
if (Z_TYPE_P(prop) != IS_STRING) { | ||
php_error_docref(NULL, E_WARNING, "Invalid buffer for key %s", string_key->val); | ||
} else if(!filename) { | ||
php_error_docref(NULL, E_WARNING, "Invalid post file name for key %s", string_key->val); | ||
} else { | ||
postval = Z_STR_P(prop); | ||
|
||
form_error = curl_formadd(&first, &last, | ||
CURLFORM_COPYNAME, ZSTR_VAL(string_key), | ||
CURLFORM_NAMELENGTH, ZSTR_LEN(string_key), | ||
CURLFORM_BUFFER, filename, | ||
CURLFORM_BUFFERPTR, ZSTR_VAL(postval), | ||
CURLFORM_BUFFERLENGTH, ZSTR_LEN(postval), | ||
CURLFORM_CONTENTTYPE, type ? type : "application/octet-stream", | ||
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; | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why you need INTERNAL_FUNCTION_PARAMETERS? You don't seem to use them except for return_value. So maybe just pass return_value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, will change it.