-#if HTTP_CURL_USE_ZEND_MM
- if (CURLE_OK != curl_global_init_mem(CURL_GLOBAL_ALL,
- http_curl_malloc,
- http_curl_free,
- http_curl_realloc,
- http_curl_strdup,
- http_curl_calloc)) {
- return FAILURE;
- }
-#else
- if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
- return FAILURE;
- }
-#endif
- return SUCCESS;
-}
-/* }}} */
-
-/* {{{ void *http_request_data_copy(int, void *) */
-void *_http_request_data_copy(int type, void *data TSRMLS_DC)
-{
- switch (type)
- {
- case COPY_STRING:
- {
- char *new_str = estrdup((const char*) data);
- zend_llist_add_element(&HTTP_G(request).copies.strings, &new_str);
- return new_str;
- }
-
- case COPY_SLIST:
- {
- zend_llist_add_element(&HTTP_G(request).copies.slists, &data);
- return data;
- }
-
- case COPY_CONTEXT:
- {
- zend_llist_add_element(&HTTP_G(request).copies.contexts, &data);
- return data;
- }
-
- default:
- {
- return data;
- }
- }
-}
-/* }}} */
-
-/* {{{ void http_request_data_free_string(char **) */
-void _http_request_data_free_string(void *string)
-{
- efree(*((char **)string));
-}
-/* }}} */
-
-/* {{{ void http_request_data_free_slist(struct curl_slist **) */
-void _http_request_data_free_slist(void *list)
-{
- curl_slist_free_all(*((struct curl_slist **) list));
-}
-/* }}} */
-
-/* {{{ _http_request_data_free_context(http_curl_callback_ctx **) */
-void _http_request_data_free_context(void *context)
-{
- efree(*((http_curl_callback_ctx **) context));
-}
-/* }}} */
-
-/* {{{ http_request_body *http_request_body_new() */
-PHP_HTTP_API http_request_body *_http_request_body_new(TSRMLS_D)
-{
- http_request_body *body = ecalloc(1, sizeof(http_request_body));
- return body;
-}
-/* }}} */
-
-/* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
-PHP_HTTP_API STATUS _http_request_body_fill(http_request_body *body, HashTable *fields, HashTable *files TSRMLS_DC)
-{
- if (files && (zend_hash_num_elements(files) > 0)) {
- char *key = NULL;
- ulong idx;
- zval **data;
- struct curl_httppost *http_post_data[2] = {NULL, NULL};
-
- /* normal data */
- FOREACH_HASH_KEYVAL(fields, key, idx, data) {
- CURLcode err;
- if (key) {
- convert_to_string_ex(data);
- err = curl_formadd(&http_post_data[0], &http_post_data[1],
- CURLFORM_COPYNAME, key,
- CURLFORM_COPYCONTENTS, Z_STRVAL_PP(data),
- CURLFORM_CONTENTSLENGTH, Z_STRLEN_PP(data),
- CURLFORM_END
- );
- if (CURLE_OK != err) {
- http_error_ex(E_WARNING, HTTP_E_CURL, "Could not encode post fields: %s", curl_easy_strerror(err));
- curl_formfree(http_post_data[0]);
- return FAILURE;
- }
-
- /* reset */
- key = NULL;
- }
- }
-
- /* file data */
- FOREACH_HASH_VAL(files, data) {
- CURLcode err;
- 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)) {
- err = curl_formadd(&http_post_data[0], &http_post_data[1],
- CURLFORM_COPYNAME, Z_STRVAL_PP(name),
- CURLFORM_FILE, Z_STRVAL_PP(file),
- CURLFORM_CONTENTTYPE, Z_STRVAL_PP(type),
- CURLFORM_END
- );
- if (CURLE_OK != err) {
- http_error_ex(E_WARNING, HTTP_E_CURL, "Could not encode post files: %s", curl_easy_strerror(err));
- curl_formfree(http_post_data[0]);
- return FAILURE;
- }
- } else {
- http_error(E_NOTICE, HTTP_E_PARAM, "Post file array entry misses either 'name', 'type' or 'file' entry");
- }
- }
-
- body->type = HTTP_REQUEST_BODY_CURLPOST;
- body->data = http_post_data[0];
- body->size = 0;
-