X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_request_body_api.c;h=f2d5f5684569e8294b8e5aac3e3bdd2830d8671c;hp=401e0ac453500c15d9b5a34d616224c2632263c9;hb=0a8e6133c3535730804b759cf406407fccac2ee7;hpb=43923eca4429ed3ce5fb85407345d7030a1879f1 diff --git a/http_request_body_api.c b/http_request_body_api.c index 401e0ac..f2d5f56 100644 --- a/http_request_body_api.c +++ b/http_request_body_api.c @@ -6,16 +6,12 @@ | modification, are permitted provided that the conditions mentioned | | in the accompanying LICENSE file are met. | +--------------------------------------------------------------------+ - | Copyright (c) 2004-2005, Michael Wallner | + | Copyright (c) 2004-2007, Michael Wallner | +--------------------------------------------------------------------+ */ /* $Id$ */ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - #define HTTP_WANT_CURL #include "php_http.h" @@ -34,6 +30,7 @@ PHP_HTTP_API http_request_body *_http_request_body_init_ex(http_request_body *bo body->type = type; body->free = free; + body->priv = 0; body->data = data; body->size = size; @@ -45,22 +42,21 @@ PHP_HTTP_API http_request_body *_http_request_body_init_ex(http_request_body *bo PHP_HTTP_API http_request_body *_http_request_body_fill(http_request_body *body, HashTable *fields, HashTable *files ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC) { if (files && (zend_hash_num_elements(files) > 0)) { - char *key = NULL; - ulong idx; + HashKey key = initHashKey(0); zval **data; HashPosition pos; struct curl_httppost *http_post_data[2] = {NULL, NULL}; /* normal data */ if (fields) { - FOREACH_HASH_KEYVAL(pos, fields, key, idx, data) { - if (key) { + FOREACH_HASH_KEYVAL(pos, fields, key, data) { + if (key.type == HASH_KEY_IS_STRING) { CURLcode err; zval *orig = *data; convert_to_string_ex(data); err = curl_formadd(&http_post_data[0], &http_post_data[1], - CURLFORM_COPYNAME, key, + CURLFORM_COPYNAME, key.str, CURLFORM_COPYCONTENTS, Z_STRVAL_PP(data), CURLFORM_CONTENTSLENGTH, (long) Z_STRLEN_PP(data), CURLFORM_END @@ -75,9 +71,6 @@ PHP_HTTP_API http_request_body *_http_request_body_fill(http_request_body *body, curl_formfree(http_post_data[0]); return NULL; } - - /* reset */ - key = NULL; } } } @@ -88,9 +81,9 @@ PHP_HTTP_API http_request_body *_http_request_body_fill(http_request_body *body, if (Z_TYPE_PP(data) != IS_ARRAY) { http_error(HE_NOTICE, HTTP_E_INVALID_PARAM, "Unrecognized type of post file array entry"); - } else 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)) { + } else 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)) { http_error(HE_NOTICE, HTTP_E_INVALID_PARAM, "Post file array entry misses either 'name', 'type' or 'file' entry"); } else { CURLcode err; @@ -146,27 +139,58 @@ PHP_HTTP_API http_request_body *_http_request_body_fill(http_request_body *body, } } +/* STATUS http_request_body_encode(http_request_body *, char**, size_t *) */ +PHP_HTTP_API STATUS _http_request_body_encode(http_request_body *body, char **buf, size_t *len TSRMLS_DC) +{ + switch (body->type) { + case HTTP_REQUEST_BODY_CURLPOST: + { +#ifdef HAVE_CURL_FORMGET + phpstr str; + + phpstr_init_ex(&str, 0x8000, 0); + if (curl_formget(body->data, &str, (curl_formget_callback) phpstr_append)) { + phpstr_dtor(&str); + } else { + phpstr_fix(&str); + *buf = PHPSTR_VAL(&str); + *len = PHPSTR_LEN(&str); + return SUCCESS; + } +#endif + break; + } + + case HTTP_REQUEST_BODY_CSTRING: + *buf = estrndup(body->data, *len = body->size); + return SUCCESS; + + default: + break; + } + return FAILURE; +} +/* }}} */ /* {{{ void http_request_body_dtor(http_request_body *) */ PHP_HTTP_API void _http_request_body_dtor(http_request_body *body TSRMLS_DC) { if (body) { if (body->free) { - switch (body->type) - { + switch (body->type) { case HTTP_REQUEST_BODY_CSTRING: if (body->data) { efree(body->data); } - break; + break; case HTTP_REQUEST_BODY_CURLPOST: curl_formfree(body->data); - break; + break; case HTTP_REQUEST_BODY_UPLOADFILE: php_stream_close(body->data); - break; + break; } } memset(body, 0, sizeof(http_request_body));