From 2655b1c8838edb32ef8ac67376f123e8d613a9af Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Sat, 24 Jun 2006 16:58:50 +0000 Subject: [PATCH] - curl_formget() patch was accepted into libcurl-7.15.5 - move code into http_request_body_encode() function --- config9.m4 | 5 ++ http_functions.c | 83 +++------------------------- http_request_body_api.c | 106 ++++++++++++++++++++++++++++++++++++ php_http_request_body_api.h | 3 + 4 files changed, 122 insertions(+), 75 deletions(-) diff --git a/config9.m4 b/config9.m4 index e1c4ab8..09e31c5 100644 --- a/config9.m4 +++ b/config9.m4 @@ -226,6 +226,11 @@ dnl ---- [AC_DEFINE([HAVE_CURL_GETFORMDATA], [1], [ ])], [ ], [$CURL_LIBS -L$CURL_DIR/$PHP_LIBDIR] ) + dnl New API function which obsoletes use of Curl_getFormData (>=7.15.5) + PHP_CHECK_LIBRARY(curl, curl_formget, + [AC_DEFINE([HAVE_CURL_FORMGET], [1], [ ])], [ ], + [$CURL_LIBS -L$CURL_DIR/$PHP_LIBDIR] + ) fi dnl ---- diff --git a/http_functions.c b/http_functions.c index bf1e193..33c1e4a 100644 --- a/http_functions.c +++ b/http_functions.c @@ -1670,28 +1670,6 @@ PHP_FUNCTION(http_request) } /* }}} */ -#ifdef HAVE_CURL_GETFORMDATA -static char *file_get_contents(char *file, size_t *len TSRMLS_DC) -{ - php_stream *s = NULL; - char *buf = NULL; - - if ((s = php_stream_open_wrapper_ex(file, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT))) { - *len = php_stream_copy_to_mem(s, &buf, (size_t) -1, 0); - php_stream_close(s); - } else { - *len = 0; - } - return buf; -} -struct FormData { - struct FormData *next; - int type; - char *line; - size_t length; -}; -CURLcode Curl_getFormData(struct FormData **, struct curl_httppost *post, curl_off_t *size); - /* {{{ proto string http_request_body_encode(array fields, array files) * * Generate x-www-form-urlencoded resp. form-data encoded request body. @@ -1703,13 +1681,8 @@ PHP_FUNCTION(http_request_body_encode) zval *fields = NULL, *files = NULL; HashTable *fields_ht, *files_ht; http_request_body body; - phpstr rbuf; - struct FormData *data, *ptr; - curl_off_t size; - char *fdata = NULL; - size_t fsize = 0; - CURLcode rc; - int fgc_error = 0; + char *buf; + size_t len; if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!a!", &fields, &files)) { RETURN_FALSE; @@ -1717,54 +1690,14 @@ PHP_FUNCTION(http_request_body_encode) fields_ht = (fields && Z_TYPE_P(fields) == IS_ARRAY) ? Z_ARRVAL_P(fields) : NULL; files_ht = (files && Z_TYPE_P(files) == IS_ARRAY) ? Z_ARRVAL_P(files) : NULL; - if (!http_request_body_fill(&body, fields_ht, files_ht)) { - RETURN_FALSE; - } - - switch (body.type) { - case HTTP_REQUEST_BODY_CURLPOST: - if (CURLE_OK != (rc = Curl_getFormData(&data, body.data, &size))) { - http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Could not encode request body: %s", curl_easy_strerror(rc)); - RETVAL_FALSE; - } else { - phpstr_init_ex(&rbuf, (size_t) size, PHPSTR_INIT_PREALLOC); - for (ptr = data; ptr; ptr = ptr->next) { - if (!fgc_error) { - if (ptr->type) { - if ((fdata = file_get_contents(ptr->line, &fsize TSRMLS_CC))) { - phpstr_append(&rbuf, fdata, fsize); - efree(fdata); - } else { - fgc_error = 1; - } - } else { - phpstr_append(&rbuf, ptr->line, ptr->length); - } - } - curl_free(ptr->line); - } - curl_free(data); - if (fgc_error) { - phpstr_dtor(&rbuf); - RETVAL_FALSE; - } else { - RETVAL_PHPSTR_VAL(&rbuf); - } - } - http_request_body_dtor(&body); - break; - - case HTTP_REQUEST_BODY_CSTRING: - RETVAL_STRINGL(body.data, body.size, 0); - break; - - default: - http_request_body_dtor(&body); - RETVAL_FALSE; - break; + if (http_request_body_fill(&body, fields_ht, files_ht) && (SUCCESS == http_request_body_encode(&body, &buf, &len))) { + RETVAL_STRINGL(buf, len, 0); + } else { + http_error(HE_WARNING, HTTP_E_RUNTIME, "Could not encode request body"); + RETVAL_FALSE; } + http_request_body_dtor(&body); } -#endif /* HAVE_CURL_GETFORMDATA */ #endif /* HTTP_HAVE_CURL */ /* }}} HAVE_CURL */ diff --git a/http_request_body_api.c b/http_request_body_api.c index 816e9c2..a577023 100644 --- a/http_request_body_api.c +++ b/http_request_body_api.c @@ -21,6 +21,63 @@ #include "php_http_url_api.h" #include "php_http_request_body_api.h" +#if defined(HAVE_CURL_GETFORMDATA) && !defined(HAVE_CURL_FORMGET) +struct FormData { + struct FormData *next; + int type; + char *line; + size_t length; +}; + +CURLcode Curl_getFormData(struct FormData **, struct curl_httppost *post, curl_off_t *size); + +static char *file_get_contents(char *file, size_t *len TSRMLS_DC) +{ + php_stream *s = NULL; + char *buf = NULL; + + if ((s = php_stream_open_wrapper_ex(file, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT))) { + *len = php_stream_copy_to_mem(s, &buf, (size_t) -1, 0); + php_stream_close(s); + } else { + *len = 0; + } + return buf; +} + +static int curl_formget(struct FormData *post, phpstr *str TSRMLS_DC) +{ + int fgc_error = 0; + char *fdata; + size_t fsize; + struct FormData *next, *pptr = post; + + while (pptr) { + next = pptr->next; + + if (!fgc_error) { + if (pptr->type) { + if ((fdata = file_get_contents(pptr->line, &fsize TSRMLS_CC))) { + phpstr_append(str, fdata, fsize); + efree(fdata); + } else { + fgc_error = 1; + } + } else { + phpstr_append(str, pptr->line, pptr->length); + } + } + + curl_free(pptr->line); + curl_free(pptr); + pptr = next; + } + + return fgc_error; +} +#endif + + /* {{{ http_request_body *http_request_body_new() */ PHP_HTTP_API http_request_body *_http_request_body_init_ex(http_request_body *body, int type, void *data, size_t size, zend_bool free ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC) { @@ -143,6 +200,55 @@ 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: + { +#if defined(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; + } +#elif defined(HAVE_CURL_GETFORMDATA) + struct FormData *data; + curl_off_t size; + + if (!Curl_getFormData(&data, body->data, &size)) { + phpstr str; + + phpstr_init_ex(&str, (size_t) size, 0); + if (curl_formget(data, &str TSRMLS_CC)) { + phpstr_dtor(&str); + } else { + phpstr_fix(&str); + *buf = PHPSTR_VAL(&str); + *len = PHPSTR_LEN(&len); + 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) diff --git a/php_http_request_body_api.h b/php_http_request_body_api.h index 739168f..ad698a5 100644 --- a/php_http_request_body_api.h +++ b/php_http_request_body_api.h @@ -39,6 +39,9 @@ PHP_HTTP_API http_request_body *_http_request_body_init_ex(http_request_body *bo #define http_request_body_fill(b, fields, files) _http_request_body_fill((b), (fields), (files) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC TSRMLS_CC) 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); +#define http_request_body_encode(b, s, l) _http_request_body_encode((b), (s), (l) TSRMLS_CC) +PHP_HTTP_API STATUS _http_request_body_encode(http_request_body *body, char **buf, size_t *len TSRMLS_DC); + #define http_request_body_dtor(b) _http_request_body_dtor((b) TSRMLS_CC) PHP_HTTP_API void _http_request_body_dtor(http_request_body *body TSRMLS_DC); -- 2.30.2