#define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
#define HASH_ORNULL(z) ((z) ? Z_ARRVAL_P(z) : NULL)
#define NO_ARGS if (ZEND_NUM_ARGS()) WRONG_PARAM_COUNT
-#define HTTP_URL_ARGSEP_OVERRIDE zend_alter_ini_entry("arg_separator.output", sizeof("arg_separator.output") - 1, "&", 1, ZEND_INI_ALL, ZEND_INI_STAGE_RUNTIME)
-#define HTTP_URL_ARGSEP_RESTORE zend_restore_ini_entry("arg_separator.output", sizeof("arg_separator.output") - 1, ZEND_INI_STAGE_RUNTIME)
#define array_copy(src, dst) zend_hash_copy(Z_ARRVAL_P(dst), Z_ARRVAL_P(src), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *))
#define array_merge(src, dst) zend_hash_merge(Z_ARRVAL_P(dst), Z_ARRVAL_P(src), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *), 1)
zend_object_value ov;
httpi_response_object *o;
- o = ecalloc(sizeof(httpi_response_object), 1);
+ o = ecalloc(1, sizeof(httpi_response_object));
o->zo.ce = ce;
ALLOC_HASHTABLE(OBJ_PROP(o));
typedef struct {
zend_object zo;
CURL *ch;
+
+ struct curl_httppost *post_data[2];
+
} httpi_request_object;
#define httpi_request_declare_default_properties(ce) _httpi_request_declare_default_properties(ce TSRMLS_CC)
DCL_PROP_N(PROTECTED, options);
DCL_PROP_N(PROTECTED, responseInfo);
DCL_PROP_N(PROTECTED, responseData);
+ DCL_PROP_N(PROTECTED, postData);
+ DCL_PROP_N(PROTECTED, postFiles);
DCL_PROP(PROTECTED, long, method, HTTP_GET);
zend_object_value ov;
httpi_request_object *o;
- o = ecalloc(sizeof(httpi_request_object), 1);
+ o = ecalloc(1, sizeof(httpi_request_object));
o->zo.ce = ce;
o->ch = curl_easy_init();
+ o->post_data[0] = NULL;
+ o->post_data[1] = NULL;
ALLOC_HASHTABLE(OBJ_PROP(o));
zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
PHP_ME(HTTPi_Request, getQueryData, NULL, ZEND_ACC_PUBLIC)
PHP_ME(HTTPi_Request, addQueryData, NULL, ZEND_ACC_PUBLIC)
PHP_ME(HTTPi_Request, unsetQueryData, NULL, ZEND_ACC_PUBLIC)
-/*
+
PHP_ME(HTTPi_Request, setPostData, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(HTTPi_Request, getPostData, NULL, ZEND_ACC_PUBLIC)
PHP_ME(HTTPi_Request, addPostData, NULL, ZEND_ACC_PUBLIC)
PHP_ME(HTTPi_Request, unsetPostData, NULL, ZEND_ACC_PUBLIC)
PHP_ME(HTTPi_Request, addPostFile, NULL, ZEND_ACC_PUBLIC)
-*/
+ PHP_ME(HTTPi_Request, getPostFiles, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(HTTPi_Request, unsetPostFiles, NULL, ZEND_ACC_PUBLIC)
+
PHP_ME(HTTPi_Request, send, NULL, ZEND_ACC_PUBLIC)
PHP_ME(HTTPi_Request, getResponseData, NULL, ZEND_ACC_PUBLIC)
INIT_PARR(obj, options);
INIT_PARR(obj, responseInfo);
INIT_PARR(obj, responseData);
+ INIT_PARR(obj, postData);
+ INIT_PARR(obj, postFiles);
if (URL) {
UPD_PROP(obj, string, url, URL);
FREE_PARR(obj, options);
FREE_PARR(obj, responseInfo);
FREE_PARR(obj, responseData);
+ FREE_PARR(obj, postData);
+ FREE_PARR(obj, postFiles);
}
/* }}} */
}
/* }}} */
+/* {{{ proto bool HTTPi_Request::addPostData(array post_data)
+ *
+ */
+PHP_METHOD(HTTPi_Request, addPostData)
+{
+ zval *post, *post_data;
+ getObject(httpi_request_object, obj);
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
+ RETURN_FALSE;
+ }
+
+ post = GET_PROP(obj, postData);
+ array_merge(post_data, post);
+
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto bool HTTPi_Request::setPostData(array post_data)
+ *
+ */
+PHP_METHOD(HTTPi_Request, setPostData)
+{
+ zval *post, *post_data;
+ getObject(httpi_request_object, obj);
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
+ RETURN_FALSE;
+ }
+
+ post = GET_PROP(obj, postData);
+ zend_hash_clean(Z_ARRVAL_P(post));
+ array_copy(post_data, post);
+
+ RETURN_TRUE;
+}
+/* }}}*/
+
+/* {{{ proto array HTTPi_Request::getPostData()
+ *
+ */
+PHP_METHOD(HTTPi_Request, getPostData)
+{
+ zval *post_data;
+ getObject(httpi_request_object, obj);
+
+ NO_ARGS;
+
+ post_data = GET_PROP(obj, postData);
+ array_init(return_value);
+ array_copy(post_data, return_value);
+}
+/* }}} */
+
+/* {{{ proto void HTTPi_Request::unsetPostData()
+ *
+ */
+PHP_METHOD(HTTPi_Request, unsetPostData)
+{
+ zval *post_data;
+ getObject(httpi_request_object, obj);
+
+ NO_ARGS;
+
+ post_data = GET_PROP(obj, postData);
+ zend_hash_clean(Z_ARRVAL_P(post_data));
+}
+/* }}} */
+
+/* {{{ proto bool HTTPi_Request::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
+ *
+ */
+PHP_METHOD(HTTPi_Request, addPostFile)
+{
+ zval *files, *entry;
+ char *name, *file, *type = NULL;
+ int name_len, file_len, type_len = 0;
+ getObject(httpi_request_object, obj);
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &name, &name_len, &file, &file_len, &type, &type_len)) {
+ RETURN_FALSE;
+ }
+
+ if (type_len) {
+ if (!strchr(type, '/')) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type);
+ RETURN_FALSE;
+ }
+ } else {
+ type = "application/x-octetstream";
+ type_len = sizeof("application/x-octetstream") - 1;
+ }
+
+ MAKE_STD_ZVAL(entry);
+ array_init(entry);
+
+ add_assoc_stringl(entry, "name", name, name_len, 1);
+ add_assoc_stringl(entry, "type", type, type_len, 1);
+ add_assoc_stringl(entry, "file", file, file_len, 1);
+
+ files = GET_PROP(obj, postFiles);
+ add_next_index_zval(files, entry);
+
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto array HTTPi_Request::getPostFiles()
+ *
+ */
+PHP_METHOD(HTTPi_Request, getPostFiles)
+{
+ zval *files;
+ getObject(httpi_request_object, obj);
+
+ NO_ARGS;
+
+ files = GET_PROP(obj, postFiles);
+
+ array_init(return_value);
+ array_copy(files, return_value);
+}
+/* }}} */
+
+/* {{{ proto void HTTPi_Request::unsetPostFiles()
+ *
+ */
+PHP_METHOD(HTTPi_Request, unsetPostFiles)
+{
+ zval *files;
+ getObject(httpi_request_object, obj);
+
+ NO_ARGS;
+
+ files = GET_PROP(obj, postFiles);
+ zend_hash_clean(Z_ARRVAL_P(files));
+}
+/* }}} */
+
/* {{{ proto array HTTPi_Request::getResponseData()
*
*/
{
STATUS status = FAILURE;
zval *meth, *URL, *qdata, *opts, *info, *resp;
- char *response_data, *request_uri, *uri;
+ char *response_data, *request_uri;
size_t response_len;
getObject(httpi_request_object, obj);
info = GET_PROP(obj, responseInfo);
resp = GET_PROP(obj, responseData);
- uri = http_absolute_uri(Z_STRVAL_P(URL), NULL);
- request_uri = ecalloc(HTTP_URI_MAXLEN + 1, 1);
- strcpy(request_uri, uri);
- efree(uri);
+ // HTTP_URI_MAXLEN+1 big char *
+ request_uri = http_absolute_uri(Z_STRVAL_P(URL), NULL);
if (Z_STRLEN_P(qdata) && (strlen(request_uri) < HTTP_URI_MAXLEN)) {
if (!strchr(request_uri, '?')) {
break;
case HTTP_POST:
+ {
+ zval *post_files, *post_data, **data;
+
+ post_files = GET_PROP(obj, postFiles);
+ post_data = GET_PROP(obj, postData);
+
+ if (!zend_hash_num_elements(Z_ARRVAL_P(post_files))) {
+
+ /* urlencoded post */
+ status = http_post_array_ex(obj->ch, request_uri, Z_ARRVAL_P(post_data), Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &response_data, &response_len);
+
+ } else {
+
+ /*
+ * multipart post
+ */
+
+ /* normal data */
+ for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(post_data));
+ zend_hash_get_current_data(Z_ARRVAL_P(post_data), (void **) &data) == SUCCESS;
+ zend_hash_move_forward(Z_ARRVAL_P(post_data))) {
+
+ char *key;
+ long idx;
+
+ if (HASH_KEY_IS_STRING == zend_hash_get_current_key(Z_ARRVAL_P(post_data), &key, &idx, 0)) {
+ convert_to_string_ex(data);
+ curl_formadd(&obj->post_data[0], &obj->post_data[1],
+ CURLFORM_COPYNAME, key,
+ CURLFORM_COPYCONTENTS, Z_STRVAL_PP(data),
+ CURLFORM_CONTENTSLENGTH, Z_STRLEN_PP(data),
+ CURLFORM_END
+ );
+ }
+ }
+
+ /* file data */
+ for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(post_files));
+ zend_hash_get_current_data(Z_ARRVAL_P(post_files), (void **) &data) == SUCCESS;
+ zend_hash_move_forward(Z_ARRVAL_P(post_files))) {
+
+ zval **file, **type, **name;
+
+ if (
+ SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "name", sizeof("name"), (void **) &name) &&
+ SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &type) &&
+ SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "file", sizeof("file"), (void **) &file)
+ ) {
+
+ curl_formadd(&obj->post_data[0], &obj->post_data[1],
+ CURLFORM_COPYNAME, Z_STRVAL_PP(name),
+ CURLFORM_FILE, Z_STRVAL_PP(file),
+ CURLFORM_CONTENTTYPE, Z_STRVAL_PP(type),
+ CURLFORM_END
+ );
+ }
+ }
+
+ status = http_post_curldata_ex(obj->ch, request_uri, obj->post_data[0], Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &response_data, &response_len);
+ curl_formfree(obj->post_data[0]);
+ }
+ }
break;
default:
if (qstr.c) {
curl_easy_setopt(ch, CURLOPT_COOKIE, qstr.c);
- efree(qstr.c);
+ /* FIXXXME: mem-leak */
}
}
PHP_HTTP_API char *_http_date(time_t t TSRMLS_DC)
{
struct tm *gmtime, tmbuf;
- char *date = ecalloc(31, 1);
+ char *date = ecalloc(1, 31);
gmtime = php_gmtime_r(&t, &tmbuf);
snprintf(date, 30,
char ssb_buf[128] = {0};
unsigned char digest[16];
PHP_MD5_CTX ctx;
- char *new_etag = ecalloc(33, 1);
+ char *new_etag = ecalloc(1, 33);
PHP_MD5Init(&ctx);
int count, i;
if (count = OG(ob_nesting_level)) {
- stack = ecalloc(sizeof(php_ob_buffer *), count);
+ stack = ecalloc(count, sizeof(php_ob_buffer *));
if (count > 1) {
zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP,
}
HTTP_G(etag) = estrdup(etag);
- etag_header = ecalloc(sizeof("ETag: \"\"") + etag_len, 1);
+ etag_header = ecalloc(1, sizeof("ETag: \"\"") + etag_len);
sprintf(etag_header, "ETag: \"%s\"", etag);
if (SUCCESS != (status = http_send_header(etag_header))) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't send '%s' header", etag_header);
const size_t cc_len TSRMLS_DC)
{
STATUS status;
- char *cc_header = ecalloc(sizeof("Cache-Control: ") + cc_len, 1);
+ char *cc_header = ecalloc(1, sizeof("Cache-Control: ") + cc_len);
sprintf(cc_header, "Cache-Control: %s", cache_control);
if (SUCCESS != (status = http_send_header(cc_header))) {
}
HTTP_G(ctype) = estrndup(content_type, ct_len);
- ct_header = ecalloc(sizeof("Content-Type: ") + ct_len, 1);
+ ct_header = ecalloc(1, sizeof("Content-Type: ") + ct_len);
sprintf(ct_header, "Content-Type: %s", content_type);
if (SUCCESS != (status = http_send_header(ct_header))) {
char *cd_header;
if (send_inline) {
- cd_header = ecalloc(sizeof("Content-Disposition: inline; filename=\"\"") + f_len, 1);
+ cd_header = ecalloc(1, sizeof("Content-Disposition: inline; filename=\"\"") + f_len);
sprintf(cd_header, "Content-Disposition: inline; filename=\"%s\"", filename);
} else {
- cd_header = ecalloc(sizeof("Content-Disposition: attachment; filename=\"\"") + f_len, 1);
+ cd_header = ecalloc(1, sizeof("Content-Disposition: attachment; filename=\"\"") + f_len);
sprintf(cd_header, "Content-Disposition: attachment; filename=\"%s\"", filename);
}
PHP_HTTP_API char *_http_absolute_uri(const char *url,
const char *proto TSRMLS_DC)
{
- char URI[HTTP_URI_MAXLEN + 1], *PTR, *proto_ptr, *host, *path;
+ char *proto_ptr, *host, *path, *PTR, *URI = ecalloc(1, HTTP_URI_MAXLEN + 1);
zval *zhost;
if (!url || !strlen(url)) {
/* Mess around with already absolute URIs */
else if (proto_ptr = strstr(url, "://")) {
if (!proto || !strncmp(url, proto, strlen(proto))) {
- return estrdup(url);
+ strncpy(URI, url, HTTP_URI_MAXLEN);
+ return URI;
} else {
snprintf(URI, HTTP_URI_MAXLEN, "%s%s", proto, proto_ptr + 3);
- return estrdup(URI);
+ return URI;
}
}
}
/* strip everything after a new line */
- PTR = URI;
- while (*PTR != 0) {
- if (*PTR == '\n' || *PTR == '\r') {
- *PTR = 0;
- break;
- }
- PTR++;
+ if ((PTR = strchr(URI, '\r')) || (PTR = strchr(URI, '\n'))) {
+ PTR = 0;
}
-
- return estrdup(URI);
+
+ return URI;
}
/* }}} */
char *d_ptr;
*decoded_len = 0;
- *decoded = (char *) ecalloc(encoded_len, 1);
+ *decoded = ecalloc(1, encoded_len);
d_ptr = *decoded;
e_ptr = encoded;
}
/* }}} */
-/* {{{ STATUS http_post_array(char *, HashTable *, HashTable *, HashTable *, char **, size_t *) */
-PHP_HTTP_API STATUS _http_post_array(const char *URL, HashTable *postarray,
+/* {{{ STATUS http_post_array_ex(CURL *, char *, HashTable *, HashTable *, HashTable *, char **, size_t *) */
+PHP_HTTP_API STATUS _http_post_array_ex(CURL *ch, const char *URL, HashTable *postarray,
HashTable *options, HashTable *info, char **data, size_t *data_len TSRMLS_DC)
{
smart_str qstr = {0};
STATUS status;
+ HTTP_URL_ARGSEP_OVERRIDE;
if (php_url_encode_hash_ex(postarray, &qstr, NULL,0,NULL,0,NULL,0,NULL TSRMLS_CC) != SUCCESS) {
if (qstr.c) {
efree(qstr.c);
}
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not encode post data");
+ HTTP_URL_ARGSEP_RESTORE;
return FAILURE;
}
smart_str_0(&qstr);
+ HTTP_URL_ARGSEP_RESTORE;
- status = http_post_data(URL, qstr.c, qstr.len, options, info, data, data_len);
+ if (ch) {
+ status = http_post_data_ex(ch, URL, qstr.c, qstr.len, options, info, data, data_len);
+ } else {
+ status = http_post_data(URL, qstr.c, qstr.len, options, info, data, data_len);
+ }
+
if (qstr.c) {
efree(qstr.c);
}
}
/* }}} */
+/* {{{ STATUS http_post_curldata_ex(CURL *, char *, curl_httppost *, HashTable *, HashTable *, char **, size_t *) */
+PHP_HTTP_API STATUS _http_post_curldata_ex(CURL *ch, const char *URL,
+ struct curl_httppost *curldata, HashTable *options, HashTable *info,
+ char **data, size_t *data_len TSRMLS_DC)
+{
+ http_curl_initbuf(CURLBUF_EVRY);
+ http_curl_setopts(ch, URL, options);
+ curl_easy_setopt(ch, CURLOPT_POST, 1);
+ curl_easy_setopt(ch, CURLOPT_HTTPPOST, curldata);
+
+ if (CURLE_OK != curl_easy_perform(ch)) {
+ http_curl_freebuf(CURLBUF_EVRY);
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
+ return FAILURE;
+ }
+ if (info) {
+ http_curl_getinfo(ch, info);
+ }
+ http_curl_movebuf(CURLBUF_EVRY, data, data_len);
+ return SUCCESS;}
+/* }}} */
+
#endif
/* }}} HAVE_CURL */
/* server vars shorthand */
#define HTTP_SERVER_VARS Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER])
+/* override arg_separator.output to "&" for data used in outgoing requests */
+#include "zend_ini.h"
+#define HTTP_URL_ARGSEP_OVERRIDE zend_alter_ini_entry("arg_separator.output", sizeof("arg_separator.output") - 1, "&", 1, ZEND_INI_ALL, ZEND_INI_STAGE_RUNTIME)
+#define HTTP_URL_ARGSEP_RESTORE zend_restore_ini_entry("arg_separator.output", sizeof("arg_separator.output") - 1, ZEND_INI_STAGE_RUNTIME)
+
/* {{{ HAVE_CURL */
#ifdef HTTP_HAVE_CURL
#include <curl/curl.h>
#define http_post_data_ex(c, u, pd, pl, o, i, d, l) _http_post_data_ex((c), (u), (pd), (pl), (o), (i), (d), (l) TSRMLS_CC)
PHP_HTTP_API STATUS _http_post_data_ex(CURL *ch, const char *URL, char *postdata, size_t postdata_len, HashTable *options, HashTable *info, char **data, size_t *data_len TSRMLS_DC);
-#define http_post_array(u, p, o, i, d, l) _http_post_array((u), (p), (o), (i), (d), (l) TSRMLS_CC)
-PHP_HTTP_API STATUS _http_post_array(const char *URL, HashTable *postarray, HashTable *options, HashTable *info, char **data, size_t *data_len TSRMLS_DC);
+#define http_post_array(u, p, o, i, d, l) _http_post_array_ex(NULL, (u), (p), (o), (i), (d), (l) TSRMLS_CC)
+#define http_post_array_ex(c, u, p, o, i, d, l) _http_post_array_ex((c), (u), (p), (o), (i), (d), (l) TSRMLS_CC)
+PHP_HTTP_API STATUS _http_post_array_ex(CURL *ch, const char *URL, HashTable *postarray, HashTable *options, HashTable *info, char **data, size_t *data_len TSRMLS_DC);
+
+#define http_post_curldata_ex(c, u, h, o, i, d, l) _http_post_curldata_ex((c), (u), (h), (o), (i), (d), (l) TSRMLS_CC)
+PHP_HTTP_API STATUS _http_post_curldata_ex(CURL *ch, const char *URL, struct curl_httppost *curldata, HashTable *options, HashTable *info, char **data, size_t *data_len TSRMLS_DC);
#endif
/* }}} HAVE_CURL */