X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_request_object.c;h=047e2a34c13cda697627693b43636e017bd37bbc;hp=a9bd3bb12c3a436c92efb4275f4c8f93525dd276;hb=669d2e6a8bdc642b6b52693f4593f199ddd7e8d2;hpb=890a33b0f67eceab9885db7a2a9f46de0ea961f5 diff --git a/http_request_object.c b/http_request_object.c index a9bd3bb..047e2a3 100644 --- a/http_request_object.c +++ b/http_request_object.c @@ -12,10 +12,6 @@ /* $Id$ */ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - #define HTTP_WANT_CURL #include "php_http.h" @@ -24,6 +20,7 @@ #include "zend_interfaces.h" #include "php_http_api.h" +#include "php_http_cookie_api.h" #include "php_http_exception_object.h" #include "php_http_message_api.h" #include "php_http_message_object.h" @@ -32,189 +29,197 @@ #include "php_http_request_pool_api.h" #include "php_http_url_api.h" -#define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpRequest, method, ret_ref, req_args) -#define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpRequest, method, ret_ref) -#define HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility) -#define HTTP_REQUEST_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpRequest, method)) +#define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpRequest, method, 0, req_args) +#define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpRequest, method, 0) +#define HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility) +#define HTTP_REQUEST_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpRequest, method)) -HTTP_BEGIN_ARGS(__construct, 0, 0) +HTTP_BEGIN_ARGS(__construct, 0) HTTP_ARG_VAL(url, 0) HTTP_ARG_VAL(method, 0) HTTP_ARG_VAL(options, 0) HTTP_END_ARGS; -HTTP_EMPTY_ARGS(getOptions, 0); -HTTP_BEGIN_ARGS(setOptions, 0, 0) +HTTP_EMPTY_ARGS(getOptions); +HTTP_BEGIN_ARGS(setOptions, 0) HTTP_ARG_VAL(options, 0) HTTP_END_ARGS; -HTTP_EMPTY_ARGS(getSslOptions, 0); -HTTP_BEGIN_ARGS(setSslOptions, 0, 0) +HTTP_EMPTY_ARGS(getSslOptions); +HTTP_BEGIN_ARGS(setSslOptions, 0) HTTP_ARG_VAL(ssl_options, 0) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(addSslOptions, 0, 0) +HTTP_BEGIN_ARGS(addSslOptions, 0) HTTP_ARG_VAL(ssl_optins, 0) HTTP_END_ARGS; -HTTP_EMPTY_ARGS(getHeaders, 0); -HTTP_BEGIN_ARGS(setHeaders, 0, 0) +HTTP_EMPTY_ARGS(getHeaders); +HTTP_BEGIN_ARGS(setHeaders, 0) HTTP_ARG_VAL(headers, 0) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(addHeaders, 0, 1) +HTTP_BEGIN_ARGS(addHeaders, 1) HTTP_ARG_VAL(headers, 0) HTTP_END_ARGS; -HTTP_EMPTY_ARGS(getCookies, 0); -HTTP_BEGIN_ARGS(setCookies, 0, 0) +HTTP_EMPTY_ARGS(getCookies); +HTTP_BEGIN_ARGS(setCookies, 0) HTTP_ARG_VAL(cookies, 0) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(addCookies, 0, 1) +HTTP_BEGIN_ARGS(addCookies, 1) HTTP_ARG_VAL(cookies, 0) HTTP_END_ARGS; -HTTP_EMPTY_ARGS(getUrl, 0); -HTTP_BEGIN_ARGS(setUrl, 0, 1) +HTTP_EMPTY_ARGS(getUrl); +HTTP_BEGIN_ARGS(setUrl, 1) HTTP_ARG_VAL(url, 0) HTTP_END_ARGS; -HTTP_EMPTY_ARGS(getMethod, 0); -HTTP_BEGIN_ARGS(setMethod, 0, 1) +HTTP_EMPTY_ARGS(getMethod); +HTTP_BEGIN_ARGS(setMethod, 1) HTTP_ARG_VAL(request_method, 0) HTTP_END_ARGS; -HTTP_EMPTY_ARGS(getContentType, 0); -HTTP_BEGIN_ARGS(setContentType, 0, 1) +HTTP_EMPTY_ARGS(getContentType); +HTTP_BEGIN_ARGS(setContentType, 1) HTTP_ARG_VAL(content_type, 0) HTTP_END_ARGS; -HTTP_EMPTY_ARGS(getQueryData, 0); -HTTP_BEGIN_ARGS(setQueryData, 0, 0) +HTTP_EMPTY_ARGS(getQueryData); +HTTP_BEGIN_ARGS(setQueryData, 0) HTTP_ARG_VAL(query_data, 0) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(addQueryData, 0, 1) +HTTP_BEGIN_ARGS(addQueryData, 1) HTTP_ARG_VAL(query_data, 0) HTTP_END_ARGS; -HTTP_EMPTY_ARGS(getPostFields, 0); -HTTP_BEGIN_ARGS(setPostFields, 0, 0) +HTTP_EMPTY_ARGS(getPostFields); +HTTP_BEGIN_ARGS(setPostFields, 0) HTTP_ARG_VAL(post_fields, 0) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(addPostFields, 0, 1) +HTTP_BEGIN_ARGS(addPostFields, 1) HTTP_ARG_VAL(post_fields, 0) HTTP_END_ARGS; -HTTP_EMPTY_ARGS(getPostFiles, 0); -HTTP_BEGIN_ARGS(setPostFiles, 0, 0) +HTTP_EMPTY_ARGS(getPostFiles); +HTTP_BEGIN_ARGS(setPostFiles, 0) HTTP_ARG_VAL(post_files, 0) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(addPostFile, 0, 2) +HTTP_BEGIN_ARGS(addPostFile, 2) HTTP_ARG_VAL(formname, 0) HTTP_ARG_VAL(filename, 0) HTTP_ARG_VAL(content_type, 0) HTTP_END_ARGS; -HTTP_EMPTY_ARGS(getRawPostData, 0); -HTTP_BEGIN_ARGS(setRawPostData, 0, 0) +HTTP_EMPTY_ARGS(getRawPostData); +HTTP_BEGIN_ARGS(setRawPostData, 0) HTTP_ARG_VAL(raw_post_data, 0) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(addRawPostData, 0, 1) +HTTP_BEGIN_ARGS(addRawPostData, 1) HTTP_ARG_VAL(raw_post_data, 0) HTTP_END_ARGS; -HTTP_EMPTY_ARGS(getPutFile, 0); -HTTP_BEGIN_ARGS(setPutFile, 0, 0) +HTTP_EMPTY_ARGS(getPutFile); +HTTP_BEGIN_ARGS(setPutFile, 0) HTTP_ARG_VAL(filename, 0) HTTP_END_ARGS; -HTTP_EMPTY_ARGS(getResponseData, 0); -HTTP_BEGIN_ARGS(getResponseHeader, 0, 0) - HTTP_ARG_VAL(name, 0) +HTTP_EMPTY_ARGS(getPutData); +HTTP_BEGIN_ARGS(setPutData, 0) + HTTP_ARG_VAL(put_data, 0) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(getResponseCookie, 0, 0) +HTTP_BEGIN_ARGS(addPutData, 1) + HTTP_ARG_VAL(put_data, 0) +HTTP_END_ARGS; + +HTTP_EMPTY_ARGS(getResponseData); +HTTP_BEGIN_ARGS(getResponseHeader, 0) HTTP_ARG_VAL(name, 0) HTTP_END_ARGS; -HTTP_EMPTY_ARGS(getResponseBody, 0); -HTTP_EMPTY_ARGS(getResponseCode, 0); -HTTP_BEGIN_ARGS(getResponseInfo, 0, 0) +HTTP_BEGIN_ARGS(getResponseCookies, 0) + HTTP_ARG_VAL(flags, 0) + HTTP_ARG_VAL(allowed_extras, 0) +HTTP_END_ARGS; + +HTTP_EMPTY_ARGS(getResponseBody); +HTTP_EMPTY_ARGS(getResponseCode); +HTTP_EMPTY_ARGS(getResponseStatus); +HTTP_BEGIN_ARGS(getResponseInfo, 0) HTTP_ARG_VAL(name, 0) HTTP_END_ARGS; -HTTP_EMPTY_ARGS(getResponseMessage, 0); -HTTP_EMPTY_ARGS(getRawResponseMessage, 0); -HTTP_EMPTY_ARGS(getRequestMessage, 0); -HTTP_EMPTY_ARGS(getRawRequestMessage, 0); -HTTP_EMPTY_ARGS(getHistory, 0); -HTTP_EMPTY_ARGS(clearHistory, 0); -HTTP_EMPTY_ARGS(send, 0); +HTTP_EMPTY_ARGS(getResponseMessage); +HTTP_EMPTY_ARGS(getRawResponseMessage); +HTTP_EMPTY_ARGS(getRequestMessage); +HTTP_EMPTY_ARGS(getRawRequestMessage); +HTTP_EMPTY_ARGS(getHistory); +HTTP_EMPTY_ARGS(clearHistory); +HTTP_EMPTY_ARGS(send); -HTTP_BEGIN_ARGS(get, 0, 1) +HTTP_BEGIN_ARGS(get, 1) HTTP_ARG_VAL(url, 0) HTTP_ARG_VAL(options, 0) HTTP_ARG_VAL(info, 1) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(head, 0, 1) +HTTP_BEGIN_ARGS(head, 1) HTTP_ARG_VAL(url, 0) HTTP_ARG_VAL(options, 0) HTTP_ARG_VAL(info, 1) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(postData, 0, 2) +HTTP_BEGIN_ARGS(postData, 2) HTTP_ARG_VAL(url, 0) HTTP_ARG_VAL(data, 0) HTTP_ARG_VAL(options, 0) HTTP_ARG_VAL(info, 1) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(postFields, 0, 2) +HTTP_BEGIN_ARGS(postFields, 2) HTTP_ARG_VAL(url, 0) HTTP_ARG_VAL(data, 0) HTTP_ARG_VAL(options, 0) HTTP_ARG_VAL(info, 1) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(putFile, 0, 2) +HTTP_BEGIN_ARGS(putFile, 2) HTTP_ARG_VAL(url, 0) HTTP_ARG_VAL(file, 0) HTTP_ARG_VAL(options, 0) HTTP_ARG_VAL(info, 1) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(putStream, 0, 2) +HTTP_BEGIN_ARGS(putStream, 2) HTTP_ARG_VAL(url, 0) HTTP_ARG_VAL(stream, 0) HTTP_ARG_VAL(options, 0) HTTP_ARG_VAL(info, 1) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(methodRegister, 0, 1) +HTTP_BEGIN_ARGS(methodRegister, 1) HTTP_ARG_VAL(method_name, 0) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(methodUnregister, 0, 1) +HTTP_BEGIN_ARGS(methodUnregister, 1) HTTP_ARG_VAL(method, 0) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(methodName, 0, 1) +HTTP_BEGIN_ARGS(methodName, 1) HTTP_ARG_VAL(method_id, 0) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(methodExists, 0, 1) +HTTP_BEGIN_ARGS(methodExists, 1) HTTP_ARG_VAL(method, 0) HTTP_END_ARGS; -#define http_request_object_declare_default_properties() _http_request_object_declare_default_properties(TSRMLS_C) -static inline void _http_request_object_declare_default_properties(TSRMLS_D); - #define OBJ_PROP_CE http_request_object_ce zend_class_entry *http_request_object_ce; zend_function_entry http_request_object_fe[] = { @@ -262,12 +267,17 @@ zend_function_entry http_request_object_fe[] = { HTTP_REQUEST_ME(setPutFile, ZEND_ACC_PUBLIC) HTTP_REQUEST_ME(getPutFile, ZEND_ACC_PUBLIC) + HTTP_REQUEST_ME(setPutData, ZEND_ACC_PUBLIC) + HTTP_REQUEST_ME(getPutData, ZEND_ACC_PUBLIC) + HTTP_REQUEST_ME(addPutData, ZEND_ACC_PUBLIC) + HTTP_REQUEST_ME(send, ZEND_ACC_PUBLIC) HTTP_REQUEST_ME(getResponseData, ZEND_ACC_PUBLIC) HTTP_REQUEST_ME(getResponseHeader, ZEND_ACC_PUBLIC) - HTTP_REQUEST_ME(getResponseCookie, ZEND_ACC_PUBLIC) + HTTP_REQUEST_ME(getResponseCookies, ZEND_ACC_PUBLIC) HTTP_REQUEST_ME(getResponseCode, ZEND_ACC_PUBLIC) + HTTP_REQUEST_ME(getResponseStatus, ZEND_ACC_PUBLIC) HTTP_REQUEST_ME(getResponseBody, ZEND_ACC_PUBLIC) HTTP_REQUEST_ME(getResponseInfo, ZEND_ACC_PUBLIC) HTTP_REQUEST_ME(getResponseMessage, ZEND_ACC_PUBLIC) @@ -297,62 +307,6 @@ PHP_MINIT_FUNCTION(http_request_object) { HTTP_REGISTER_CLASS_EX(HttpRequest, http_request_object, NULL, 0); http_request_object_handlers.clone_obj = _http_request_object_clone_obj; - return SUCCESS; -} - -zend_object_value _http_request_object_new(zend_class_entry *ce TSRMLS_DC) -{ - return http_request_object_new_ex(ce, NULL, NULL); -} - -zend_object_value _http_request_object_new_ex(zend_class_entry *ce, CURL *ch, http_request_object **ptr TSRMLS_DC) -{ - zend_object_value ov; - http_request_object *o; - - o = ecalloc(1, sizeof(http_request_object)); - o->zo.ce = ce; - o->request = http_request_init_ex(NULL, ch, 0, NULL); - phpstr_init(&o->history); - - if (ptr) { - *ptr = o; - } - - ALLOC_HASHTABLE(OBJ_PROP(o)); - zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0); - zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); - - ov.handle = putObject(http_request_object, o); - ov.handlers = &http_request_object_handlers; - - return ov; -} - -zend_object_value _http_request_object_clone_obj(zval *this_ptr TSRMLS_DC) -{ - zend_object *old_zo; - zend_object_value new_ov; - http_request_object *new_obj; - getObject(http_request_object, old_obj); - - old_zo = zend_objects_get_address(this_ptr TSRMLS_CC); - new_ov = http_request_object_new_ex(old_zo->ce, NULL, &new_obj); - if (old_obj->request->ch) { - http_curl_init_ex(curl_easy_duphandle(old_obj->request->ch), new_obj->request); - } - - zend_objects_clone_members(&new_obj->zo, new_ov, old_zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC); - phpstr_append(&new_obj->history, old_obj->history.data, old_obj->history.used); - phpstr_append(&new_obj->request->conv.request, old_obj->request->conv.request.data, old_obj->request->conv.request.used); - phpstr_append(&new_obj->request->conv.response, old_obj->request->conv.response.data, old_obj->request->conv.response.used); - - return new_ov; -} - -static inline void _http_request_object_declare_default_properties(TSRMLS_D) -{ - zend_class_entry *ce = http_request_object_ce; DCL_PROP_N(PRIVATE, options); DCL_PROP_N(PRIVATE, postFields); @@ -361,19 +315,21 @@ static inline void _http_request_object_declare_default_properties(TSRMLS_D) DCL_PROP_N(PRIVATE, responseData); DCL_PROP_N(PRIVATE, responseMessage); DCL_PROP(PRIVATE, long, responseCode, 0); + DCL_PROP(PRIVATE, string, responseStatus, ""); DCL_PROP(PRIVATE, long, method, HTTP_GET); DCL_PROP(PRIVATE, string, url, ""); DCL_PROP(PRIVATE, string, contentType, ""); DCL_PROP(PRIVATE, string, rawPostData, ""); DCL_PROP(PRIVATE, string, queryData, ""); DCL_PROP(PRIVATE, string, putFile, ""); - + DCL_PROP(PRIVATE, string, putData, ""); + DCL_PROP_N(PRIVATE, history); DCL_PROP(PUBLIC, bool, recordHistory, 0); #ifndef WONKY /* * Request Method Constants - */ + */ /* HTTP/1.1 */ DCL_CONST(long, "METH_GET", HTTP_GET); DCL_CONST(long, "METH_HEAD", HTTP_HEAD); @@ -406,19 +362,80 @@ static inline void _http_request_object_declare_default_properties(TSRMLS_D) /* WebDAV Access Control - RFC 3744 */ DCL_CONST(long, "METH_ACL", HTTP_ACL); - /* cURL HTTP protocol versions */ + /* + * HTTP Protocol Version Constants + */ DCL_CONST(long, "VERSION_1_0", CURL_HTTP_VERSION_1_0); DCL_CONST(long, "VERSION_1_1", CURL_HTTP_VERSION_1_1); DCL_CONST(long, "VERSION_NONE", CURL_HTTP_VERSION_NONE); /* - * Auth Constants - */ + * Auth Constants + */ DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC); DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST); DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM); DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY); + + /* + * Proxy Type Constants + */ +# if HTTP_CURL_VERSION(7,15,2) + DCL_CONST(long, "PROXY_SOCKS4", CURLPROXY_SOCKS4); +# endif + DCL_CONST(long, "PROXY_SOCKS5", CURLPROXY_SOCKS5); + DCL_CONST(long, "PROXY_HTTP", CURLPROXY_HTTP); #endif /* WONKY */ + + return SUCCESS; +} + +zend_object_value _http_request_object_new(zend_class_entry *ce TSRMLS_DC) +{ + return http_request_object_new_ex(ce, NULL, NULL); +} + +zend_object_value _http_request_object_new_ex(zend_class_entry *ce, CURL *ch, http_request_object **ptr TSRMLS_DC) +{ + zend_object_value ov; + http_request_object *o; + + o = ecalloc(1, sizeof(http_request_object)); + o->zo.ce = ce; + o->request = http_request_init_ex(NULL, ch, 0, NULL); + + if (ptr) { + *ptr = o; + } + + ALLOC_HASHTABLE(OBJ_PROP(o)); + zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0); + zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); + + ov.handle = putObject(http_request_object, o); + ov.handlers = &http_request_object_handlers; + + return ov; +} + +zend_object_value _http_request_object_clone_obj(zval *this_ptr TSRMLS_DC) +{ + zend_object *old_zo; + zend_object_value new_ov; + http_request_object *new_obj; + getObject(http_request_object, old_obj); + + old_zo = zend_objects_get_address(this_ptr TSRMLS_CC); + new_ov = http_request_object_new_ex(old_zo->ce, NULL, &new_obj); + if (old_obj->request->ch) { + http_curl_init_ex(curl_easy_duphandle(old_obj->request->ch), new_obj->request); + } + + zend_objects_clone_members(&new_obj->zo, new_ov, old_zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC); + phpstr_append(&new_obj->request->conv.request, old_obj->request->conv.request.data, old_obj->request->conv.request.used); + phpstr_append(&new_obj->request->conv.response, old_obj->request->conv.response.data, old_obj->request->conv.response.used); + + return new_ov; } void _http_request_object_free(zend_object *object TSRMLS_DC) @@ -430,10 +447,55 @@ void _http_request_object_free(zend_object *object TSRMLS_DC) FREE_HASHTABLE(OBJ_PROP(o)); } http_request_free(&o->request); - phpstr_dtor(&o->history); efree(o); } +#define http_request_object_check_request_content_type(t) _http_request_object_check_request_content_type((t) TSRMLS_CC) +static inline void _http_request_object_check_request_content_type(zval *this_ptr TSRMLS_DC) +{ + zval *ctype = GET_PROP(contentType); + + if (Z_STRLEN_P(ctype)) { + zval **headers, *opts = GET_PROP(options); + + if ( (Z_TYPE_P(opts) == IS_ARRAY) && + (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void *) &headers)) && + (Z_TYPE_PP(headers) == IS_ARRAY)) { + zval **ct_header; + + /* only override if not already set */ + if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(headers), "Content-Type", sizeof("Content-Type"), (void *) &ct_header))) { + add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1); + } else + /* or not a string, zero length string or a string of spaces */ + if ((Z_TYPE_PP(ct_header) != IS_STRING) || !Z_STRLEN_PP(ct_header)) { + add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1); + } else { + int i, only_space = 1; + + /* check for spaces only */ + for (i = 0; i < Z_STRLEN_PP(ct_header); ++i) { + if (!isspace(Z_STRVAL_PP(ct_header)[i])) { + only_space = 0; + break; + } + } + if (only_space) { + add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1); + } + } + } else { + zval *headers; + + MAKE_STD_ZVAL(headers); + array_init(headers); + add_assoc_stringl(headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1); + zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, headers); + zval_ptr_dtor(&headers); + } + } +} + STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ptr TSRMLS_DC) { STATUS status = SUCCESS; @@ -451,13 +513,21 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ case HTTP_PUT: { - php_stream_statbuf ssb; - php_stream *stream = php_stream_open_wrapper(Z_STRVAL_P(GET_PROP(putFile)), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); + zval *put_data = GET_PROP(putData); - if (stream && !php_stream_stat(stream, &ssb)) { - obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 1); + http_request_object_check_request_content_type(getThis()); + if (Z_STRLEN_P(put_data)) { + obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_CSTRING, + estrndup(Z_STRVAL_P(put_data), Z_STRLEN_P(put_data)), Z_STRLEN_P(put_data), 1); } else { - status = FAILURE; + php_stream_statbuf ssb; + php_stream *stream = php_stream_open_wrapper_ex(Z_STRVAL_P(GET_PROP(putFile)), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT); + + if (stream && !php_stream_stat(stream, &ssb)) { + obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 1); + } else { + status = FAILURE; + } } } break; @@ -469,34 +539,9 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ zval *raw_data = GET_PROP(rawPostData); if (Z_STRLEN_P(raw_data)) { - zval *ctype = GET_PROP(contentType); - - if (Z_STRLEN_P(ctype)) { - zval **headers, *opts = GET_PROP(options); - - if ( (Z_TYPE_P(opts) == IS_ARRAY) && - (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) && - (Z_TYPE_PP(headers) == IS_ARRAY)) { - zval **ct_header; - - /* only override if not already set */ - if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(headers), "Content-Type", sizeof("Content-Type"), (void **) &ct_header)) && (Z_TYPE_PP(ct_header) == IS_STRING)) { - add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1); - } - } else { - zval *headers; - - MAKE_STD_ZVAL(headers); - array_init(headers); - add_assoc_stringl(headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1); - zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, headers); - zval_ptr_dtor(&headers); - } - } - + http_request_object_check_request_content_type(getThis()); obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_CSTRING, estrndup(Z_STRVAL_P(raw_data), Z_STRLEN_P(raw_data)), Z_STRLEN_P(raw_data), 1); - } else { zval *zfields = GET_PROP(postFields), *zfiles = GET_PROP(postFiles); HashTable *fields; @@ -535,7 +580,7 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ zval **entry, *pcb; if ( (Z_TYPE_P(options) != IS_ARRAY) - || (SUCCESS != zend_hash_find(Z_ARRVAL_P(options), "onprogress", sizeof("onprogress"), (void **) &entry) + || (SUCCESS != zend_hash_find(Z_ARRVAL_P(options), "onprogress", sizeof("onprogress"), (void *) &entry) || (!zval_is_true(*entry)))) { MAKE_STD_ZVAL(pcb); array_init(pcb); @@ -553,47 +598,42 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this_ptr TSRMLS_DC) { + STATUS ret; + zval *info; http_message *msg; + /* always fetch info */ + MAKE_STD_ZVAL(info); + array_init(info); + http_request_info(obj->request, Z_ARRVAL_P(info)); + SET_PROP(responseInfo, info); + zval_ptr_dtor(&info); + + /* parse response message */ phpstr_fix(&obj->request->conv.request); phpstr_fix(&obj->request->conv.response); - msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response)); - - if (!msg) { - return FAILURE; - } else { + if ((msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response)))) { char *body; size_t body_len; - zval *headers, *message, *resp, *info; + zval *headers, *message, *resp; if (zval_is_true(GET_PROP(recordHistory))) { - /* we need to act like a zipper, as we'll receive - * the requests and the responses in separate chains - * for redirects - */ - http_message *response = msg, *request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request)); - http_message *free_msg = request; - - do { - char *message; - size_t msglen; - - http_message_tostring(response, &message, &msglen); - phpstr_append(&obj->history, message, msglen); - efree(message); - - http_message_tostring(request, &message, &msglen); - phpstr_append(&obj->history, message, msglen); - efree(message); - - } while ((response = response->parent) && (request = request->parent)); - - http_message_free(&free_msg); - phpstr_fix(&obj->history); + zval *hist, *history = GET_PROP(history); + http_message *response = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response)); + http_message *request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request)); + + MAKE_STD_ZVAL(hist); + ZVAL_OBJVAL(hist, http_message_object_new_ex(http_message_object_ce, http_message_interconnect(response, request), NULL), 0); + if (Z_TYPE_P(history) == IS_OBJECT) { + http_message_object_prepend(hist, history); + } + SET_PROP(history, hist); + zval_ptr_dtor(&hist); } UPD_PROP(long, responseCode, msg->http.info.response.code); + UPD_PROP(string, responseStatus, msg->http.info.response.status); MAKE_STD_ZVAL(resp); array_init(resp); @@ -607,29 +647,72 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this zval_ptr_dtor(&resp); MAKE_STD_ZVAL(message); - ZVAL_OBJVAL(message, http_message_object_new_ex(http_message_object_ce, msg, NULL)); + ZVAL_OBJVAL(message, http_message_object_new_ex(http_message_object_ce, msg, NULL), 0); SET_PROP(responseMessage, message); zval_ptr_dtor(&message); - MAKE_STD_ZVAL(info); - array_init(info); - http_request_info(obj->request, Z_ARRVAL_P(info)); - SET_PROP(responseInfo, info); - zval_ptr_dtor(&info); + ret = SUCCESS; + } else { + /* update properties with empty values*/ + zval *resp = GET_PROP(responseData), *znull; + + MAKE_STD_ZVAL(znull); + ZVAL_NULL(znull); + SET_PROP(responseMessage, znull); + zval_ptr_dtor(&znull); - if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onfinish", sizeof("onfinish"))) { - zend_call_method_with_0_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "onfinish", NULL); + if (Z_TYPE_P(resp) == IS_ARRAY) { + zend_hash_clean(Z_ARRVAL_P(resp)); } - return SUCCESS; + UPD_PROP(long, responseCode, 0); + UPD_PROP(string, responseStatus, ""); + + /* append request message to history */ + if (zval_is_true(GET_PROP(recordHistory))) { + http_message *request; + + if ((request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request)))) { + zval *hist, *history = GET_PROP(history); + + MAKE_STD_ZVAL(hist); + ZVAL_OBJVAL(hist, http_message_object_new_ex(http_message_object_ce, request, NULL), 0); + if (Z_TYPE_P(history) == IS_OBJECT) { + http_message_object_prepend(hist, history); + } + SET_PROP(history, hist); + zval_ptr_dtor(&hist); + } + } + + ret = FAILURE; + } + + if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onfinish", sizeof("onfinish"))) { + zval *param; + + MAKE_STD_ZVAL(param); + ZVAL_BOOL(param, ret == SUCCESS); + zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "onfinish", NULL, param); + zval_ptr_dtor(¶m); + } + + return ret; +} + +static int apply_pretty_key(void *pDest, int num_args, va_list args, zend_hash_key *hash_key) +{ + if (hash_key->nKeyLength > 1) { + hash_key->h = zend_get_hash_value(pretty_key(hash_key->arKey, hash_key->nKeyLength - 1, 1, 0), hash_key->nKeyLength); } + return ZEND_HASH_APPLY_KEEP; } -#define http_request_object_set_options_subr(key, ow) \ - _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow)) -static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len, int overwrite) +#define http_request_object_set_options_subr(key, ow, pk) \ + _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow), (pk)) +static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len, int overwrite, int prettify_keys) { - zval *old_opts, *new_opts, *opts = NULL, **entry; + zval *old_opts, *new_opts, *opts = NULL, **entry = NULL; if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &opts)) { RETURN_FALSE; @@ -642,7 +725,10 @@ static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM array_copy(old_opts, new_opts); } - if (SUCCESS == zend_hash_find(Z_ARRVAL_P(new_opts), key, len, (void **) &entry)) { + if (prettify_keys && opts) { + zend_hash_apply_with_arguments(Z_ARRVAL_P(opts), apply_pretty_key, 0); + } + if (SUCCESS == zend_hash_find(Z_ARRVAL_P(new_opts), key, len, (void *) &entry)) { if (overwrite) { zend_hash_clean(Z_ARRVAL_PP(entry)); } @@ -676,7 +762,7 @@ static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS, array_init(return_value); if ( (Z_TYPE_P(opts) == IS_ARRAY) && - (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void **) &options))) { + (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void *) &options))) { convert_to_array(*options); array_copy(*options, return_value); } @@ -691,7 +777,7 @@ static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS, * Instantiate a new HttpRequest object. * * Accepts a string as optional parameter containing the target request url. - * Additianally accepts an optional int parameter specifying the request method + * Additionally accepts an optional int parameter specifying the request method * to use and an associative array as optional third parameter which will be * passed to HttpRequest::setOptions(). * @@ -724,9 +810,9 @@ PHP_METHOD(HttpRequest, __construct) * * Set the request options to use. See http_get() for a full list of available options. * - * Accepts an array as optional parameters, wich values will overwrite the - * currently set request options. If the parameter is empty or mitted, - * the optoions of the HttpRequest object will be reset. + * Accepts an array as optional parameters, which values will overwrite the + * currently set request options. If the parameter is empty or omitted, + * the options of the HttpRequest object will be reset. * * Returns TRUE on success, or FALSE on failure. */ @@ -814,7 +900,7 @@ PHP_METHOD(HttpRequest, getOptions) */ PHP_METHOD(HttpRequest, setSslOptions) { - http_request_object_set_options_subr("ssl", 1); + http_request_object_set_options_subr("ssl", 1, 0); } /* }}} */ @@ -828,7 +914,7 @@ PHP_METHOD(HttpRequest, setSslOptions) */ PHP_METHOD(HttpRequest, addSslOptions) { - http_request_object_set_options_subr("ssl", 0); + http_request_object_set_options_subr("ssl", 0, 0); } /* }}} */ @@ -848,14 +934,14 @@ PHP_METHOD(HttpRequest, getSslOptions) * * Add request header name/value pairs. * - * Expects an ssociative array as parameter containing additional header + * Expects an associative array as parameter containing additional header * name/value pairs. * * Returns TRUE on success, or FALSE on failure. */ PHP_METHOD(HttpRequest, addHeaders) { - http_request_object_set_options_subr("headers", 0); + http_request_object_set_options_subr("headers", 0, 1); } /* {{{ proto bool HttpRequest::setHeaders([array headers]) @@ -869,7 +955,7 @@ PHP_METHOD(HttpRequest, addHeaders) */ PHP_METHOD(HttpRequest, setHeaders) { - http_request_object_set_options_subr("headers", 1); + http_request_object_set_options_subr("headers", 1, 1); } /* }}} */ @@ -896,7 +982,7 @@ PHP_METHOD(HttpRequest, getHeaders) */ PHP_METHOD(HttpRequest, setCookies) { - http_request_object_set_options_subr("cookies", 1); + http_request_object_set_options_subr("cookies", 1, 0); } /* }}} */ @@ -911,7 +997,7 @@ PHP_METHOD(HttpRequest, setCookies) */ PHP_METHOD(HttpRequest, addCookies) { - http_request_object_set_options_subr("cookies", 0); + http_request_object_set_options_subr("cookies", 0, 0); } /* }}} */ @@ -1017,13 +1103,14 @@ PHP_METHOD(HttpRequest, setContentType) { char *ctype; int ct_len; - getObject(http_request_object, obj); if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) { RETURN_FALSE; } - HTTP_CHECK_CONTENT_TYPE(ctype, RETURN_FALSE); + if (ct_len) { + HTTP_CHECK_CONTENT_TYPE(ctype, RETURN_FALSE); + } UPD_STRL(contentType, ctype, ct_len); RETURN_TRUE; } @@ -1059,7 +1146,6 @@ PHP_METHOD(HttpRequest, getContentType) PHP_METHOD(HttpRequest, setQueryData) { zval *qdata = NULL; - getObject(http_request_object, obj); if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!", &qdata)) { RETURN_FALSE; @@ -1266,13 +1352,15 @@ PHP_METHOD(HttpRequest, addRawPostData) } if (data_len) { - zval *data = zval_copy(IS_STRING, GET_PROP(rawPostData)); + zval *data = GET_PROP(rawPostData); - Z_STRVAL_P(data) = erealloc(Z_STRVAL_P(data), (Z_STRLEN_P(data) += data_len) + 1); - Z_STRVAL_P(data)[Z_STRLEN_P(data)] = '\0'; - memcpy(Z_STRVAL_P(data) + Z_STRLEN_P(data) - data_len, raw_data, data_len); - SET_PROP(rawPostData, data); - zval_free(&data); + if (Z_STRLEN_P(data)) { + Z_STRVAL_P(data) = erealloc(Z_STRVAL_P(data), (Z_STRLEN_P(data) += data_len) + 1); + Z_STRVAL_P(data)[Z_STRLEN_P(data)] = '\0'; + memcpy(Z_STRVAL_P(data) + Z_STRLEN_P(data) - data_len, raw_data, data_len); + } else { + UPD_STRL(putData, raw_data, data_len); + } } RETURN_TRUE; @@ -1297,12 +1385,12 @@ PHP_METHOD(HttpRequest, getRawPostData) /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"]) * - * Add a file to the POST request, leaving prefiously set files unchanged. + * Add a file to the POST request, leaving previously set files unchanged. * Affects only POST and custom requests. Cannot be used with raw post data. * * Expects a string parameter containing the form element name, and a string * paremeter containing the path to the file which should be uploaded. - * Additionally accepts an optional string parameter which chould contain + * Additionally accepts an optional string parameter which should contain * the content type of the file. * * Returns TRUE on success, or FALSE if the content type seems not to contain a @@ -1432,12 +1520,92 @@ PHP_METHOD(HttpRequest, getPutFile) } /* }}} */ +/* {{{ proto bool HttpRequest::setPutData([string put_data]) + * + * Set PUT data to send, overwriting previously set PUT data. + * Affects only PUT requests. + * Only either PUT data or PUT file can be used for each request. + * PUT data has higher precedence and will be used even if a PUT + * file is set. + * + * Accepts a string as parameter containing the data to upload. + * + * Returns TRUE on success, or FALSE on failure. + */ +PHP_METHOD(HttpRequest, setPutData) +{ + char *put_data = NULL; + int data_len = 0; + + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &put_data, &data_len)) { + RETURN_FALSE; + } + + if (!put_data) { + put_data = ""; + } + + UPD_STRL(putData, put_data, data_len); + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto bool HttpRequest::addPutData(string put_data) + * + * Add PUT data, leaving previously set PUT data unchanged. + * Affects only PUT requests. + * + * Expects a string as parameter containing the data to concatenate. + * + * Returns TRUE on success, or FALSE on failure. + */ +PHP_METHOD(HttpRequest, addPutData) +{ + char *put_data; + int data_len; + + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &put_data, &data_len)) { + RETURN_FALSE; + } + + if (data_len) { + zval *data = GET_PROP(putData); + + if (Z_STRLEN_P(data)) { + Z_STRVAL_P(data) = erealloc(Z_STRVAL_P(data), (Z_STRLEN_P(data) += data_len) + 1); + Z_STRVAL_P(data)[Z_STRLEN_P(data)] = '\0'; + memcpy(Z_STRVAL_P(data) + Z_STRLEN_P(data) - data_len, put_data, data_len); + } else { + UPD_STRL(putData, put_data, data_len); + } + } + + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto string HttpRequest::getPutData() + * + * Get previously set PUT data. + * + * Returns a string containing the currently set raw post data. + */ +PHP_METHOD(HttpRequest, getPutData) +{ + NO_ARGS; + + IF_RETVAL_USED { + RETURN_PROP(putData); + } +} +/* }}} */ + /* {{{ proto array HttpRequest::getResponseData() * * Get all response data after the request has been sent. * * Returns an associative array with the key "headers" containing an associative - * array holding all response headers, as well as the ley "body" containing a + * array holding all response headers, as well as the key "body" containing a * string with the response body. * * If redirects were allowed and several responses were received, the data @@ -1461,7 +1629,7 @@ PHP_METHOD(HttpRequest, getResponseData) * If the parameter is empty or omitted all response headers will be returned. * * Returns either a string with the value of the header matching name if requested, - * FALSE on failure, or an associative array containing all reponse headers. + * FALSE on failure, or an associative array containing all response headers. * * If redirects were allowed and several responses were received, the data * references the last received response. @@ -1479,11 +1647,11 @@ PHP_METHOD(HttpRequest, getResponseHeader) data = GET_PROP(responseData); if ( (Z_TYPE_P(data) == IS_ARRAY) && - (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) && + (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void *) &headers)) && (Z_TYPE_PP(headers) == IS_ARRAY)) { if (!header_len || !header_name) { RETVAL_ZVAL(*headers, 1, 0); - } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void **) &header)) { + } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void *) &header)) { RETVAL_ZVAL(*header, 1, 0); } else { RETVAL_FALSE; @@ -1495,107 +1663,95 @@ PHP_METHOD(HttpRequest, getResponseHeader) } /* }}} */ -/* {{{ proto array HttpRequest::getResponseCookie([string name]) +/* {{{ proto array HttpRequest::getResponseCookies([int flags[, array allowed_extras]]) * * Get response cookie(s) after the request has been sent. * - * Accepts a string as optional parameter specifying the name of the cookie to read. - * If the parameter is empty or omitted, an associative array with all received - * cookies will be returned. - * - * Returns either an associative array with the cookie's name, value and any - * additional params of the cookie matching name if requested, FALSE on failure, - * or an array containing all received cookies as arrays. + * Returns an array of stdClass objects like http_parse_cookie would return. * * If redirects were allowed and several responses were received, the data * references the last received response. */ -PHP_METHOD(HttpRequest, getResponseCookie) +PHP_METHOD(HttpRequest, getResponseCookies) { IF_RETVAL_USED { - zval *data, **headers; - char *cookie_name = NULL; - int cookie_len = 0; + long flags = 0; + zval *allowed_extras_array = NULL, *data, **headers; - if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &cookie_name, &cookie_len)) { + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|la", &flags, &allowed_extras_array)) { RETURN_FALSE; } - array_init(return_value); - data = GET_PROP(responseData); if ( (Z_TYPE_P(data) == IS_ARRAY) && - (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) && + (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void *) &headers)) && (Z_TYPE_PP(headers) == IS_ARRAY)) { + int i = 0; ulong idx = 0; - char *key = NULL; - zval **header = NULL; - HashPosition pos1; - - convert_to_array(*headers); + char *key = NULL, **allowed_extras = NULL; + zval **header = NULL, **entry = NULL; + HashPosition pos, pos1, pos2; + + array_init(return_value); + + if (allowed_extras_array) { + allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *)); + FOREACH_VAL(pos, allowed_extras_array, entry) { + ZVAL_ADDREF(*entry); + convert_to_string_ex(entry); + allowed_extras[i++] = estrndup(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry)); + zval_ptr_dtor(entry); + } + } + FOREACH_HASH_KEYVAL(pos1, Z_ARRVAL_PP(headers), key, idx, header) { if (key && !strcasecmp(key, "Set-Cookie")) { - /* several cookies? */ + http_cookie_list list; + if (Z_TYPE_PP(header) == IS_ARRAY) { - zval **cookie; - HashPosition pos2; - - FOREACH_HASH_VAL(pos2, Z_ARRVAL_PP(header), cookie) { - zval *cookie_hash; - MAKE_STD_ZVAL(cookie_hash); - array_init(cookie_hash); - - if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(cookie), Z_ARRVAL_P(cookie_hash))) { - if (!cookie_len) { - add_next_index_zval(return_value, cookie_hash); - } else { - zval **name; - - if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) && - (!strcmp(Z_STRVAL_PP(name), cookie_name))) { - add_next_index_zval(return_value, cookie_hash); - return; /* <<< FOUND >>> */ - } else { - zval_dtor(cookie_hash); - efree(cookie_hash); - } - } - } else { - zval_dtor(cookie_hash); - efree(cookie_hash); + zval **single_header; + + FOREACH_VAL(pos2, *header, single_header) { + ZVAL_ADDREF(*single_header); + convert_to_string_ex(single_header); + if (http_parse_cookie_ex(&list, Z_STRVAL_PP(single_header), flags, allowed_extras)) { + zval *cookie; + + MAKE_STD_ZVAL(cookie); + object_init(cookie); + http_cookie_list_tostruct(&list, cookie); + add_next_index_zval(return_value, cookie); + http_cookie_list_dtor(&list); } + zval_ptr_dtor(single_header); } } else { - zval *cookie_hash; - - MAKE_STD_ZVAL(cookie_hash); - array_init(cookie_hash); + ZVAL_ADDREF(*header); convert_to_string_ex(header); - - if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(header), Z_ARRVAL_P(cookie_hash))) { - if (!cookie_len) { - add_next_index_zval(return_value, cookie_hash); - } else { - zval **name; - - if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) && - (!strcmp(Z_STRVAL_PP(name), cookie_name))) { - add_next_index_zval(return_value, cookie_hash); - } else { - zval_dtor(cookie_hash); - efree(cookie_hash); - } - } - } else { - zval_dtor(cookie_hash); - efree(cookie_hash); + if (http_parse_cookie_ex(&list, Z_STRVAL_PP(header), flags, allowed_extras)) { + zval *cookie; + + MAKE_STD_ZVAL(cookie); + object_init(cookie); + http_cookie_list_tostruct(&list, cookie); + add_next_index_zval(return_value, cookie); + http_cookie_list_dtor(&list); } + zval_ptr_dtor(header); } - break; } /* reset key */ key = NULL; } + + if (allowed_extras) { + for (i = 0; allowed_extras[i]; ++i) { + efree(allowed_extras[i]); + } + efree(allowed_extras); + } + } else { + RETURN_FALSE; } } } @@ -1619,7 +1775,7 @@ PHP_METHOD(HttpRequest, getResponseBody) zval *data = GET_PROP(responseData); if ( (Z_TYPE_P(data) == IS_ARRAY) && - (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body))) { + (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void *) &body))) { RETURN_ZVAL(*body, 1, 0); } else { RETURN_FALSE; @@ -1647,6 +1803,22 @@ PHP_METHOD(HttpRequest, getResponseCode) } /* }}} */ +/* {{{ proto string HttpRequest::getResponseStatus() + * + * Get the response status (i.e. the string after the response code) after the message has been sent. + * + * Returns a string containing the response status text. + */ +PHP_METHOD(HttpRequest, getResponseStatus) +{ + NO_ARGS; + + IF_RETVAL_USED { + RETURN_PROP(responseStatus); + } +} +/* }}} */ + /* {{{ proto mixed HttpRequest::getResponseInfo([string name]) * * Get response info after the request has been sent. @@ -1681,7 +1853,7 @@ PHP_METHOD(HttpRequest, getResponseInfo) } if (info_len && info_name) { - if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) { + if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void *) &infop)) { RETURN_ZVAL(*infop, 1, 0); } else { http_error_ex(HE_NOTICE, HTTP_E_INVALID_PARAM, "Could not find response info named %s", info_name); @@ -1702,24 +1874,22 @@ PHP_METHOD(HttpRequest, getResponseInfo) * * If redirects were allowed and several responses were received, the data * references the last received response. Use HttpMessage::getParentMessage() - * to access the data of previously received responses whithin this request + * to access the data of previously received responses within this request * cycle. * - * Throws HttpException. + * Throws HttpException, HttpRuntimeException. */ PHP_METHOD(HttpRequest, getResponseMessage) { - NO_ARGS; - - IF_RETVAL_USED { + NO_ARGS { zval *message; SET_EH_THROW_HTTP(); message = GET_PROP(responseMessage); if (Z_TYPE_P(message) == IS_OBJECT) { - RETVAL_OBJECT(message); + RETVAL_OBJECT(message, 1); } else { - RETVAL_NULL(); + http_error(HE_WARNING, HTTP_E_RUNTIME, "HttpRequest does not contain a response message"); } SET_EH_NORMAL(); } @@ -1734,7 +1904,7 @@ PHP_METHOD(HttpRequest, getResponseMessage) * * If redirects were allowed and several responses were received, the data * references the last received response. Use HttpMessage::getParentMessage() - * to access the data of previously sent requests whithin this request + * to access the data of previously sent requests within this request * cycle. * * Note that the internal request message is immutable, that means that the @@ -1754,7 +1924,7 @@ PHP_METHOD(HttpRequest, getRequestMessage) SET_EH_THROW_HTTP(); if ((msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request)))) { - ZVAL_OBJVAL(return_value, http_message_object_new_ex(http_message_object_ce, msg, NULL)); + RETVAL_OBJVAL(http_message_object_new_ex(http_message_object_ce, msg, NULL), 0); } SET_EH_NORMAL(); } @@ -1766,7 +1936,6 @@ PHP_METHOD(HttpRequest, getRequestMessage) * Get sent HTTP message. * * Returns an HttpMessage in a form of a string - * */ PHP_METHOD(HttpRequest, getRawRequestMessage) { @@ -1785,7 +1954,6 @@ PHP_METHOD(HttpRequest, getRawRequestMessage) * Get the entire HTTP response. * * Returns the complete web server response, including the headers in a form of a string. - * */ PHP_METHOD(HttpRequest, getRawResponseMessage) { @@ -1803,8 +1971,8 @@ PHP_METHOD(HttpRequest, getRawResponseMessage) * * Get all sent requests and received responses as an HttpMessage object. * - * If you don't want to record history at all, set the instance variable - * HttpRequest::$recoedHistory to FALSE. + * If you want to record history, set the instance variable + * HttpRequest::$recordHistory to TRUE. * * Returns an HttpMessage object representing the complete request/response * history. @@ -1812,23 +1980,21 @@ PHP_METHOD(HttpRequest, getRawResponseMessage) * The object references the last received response, use HttpMessage::getParentMessage() * to access the data of previously sent requests and received responses. * - * Note that the internal history is immutable, that means that any changes - * you make the the message list won't affect a history message list newly - * created by another call to HttpRequest::getHistory(). - * - * Throws HttpMalformedHeaderException, HttpEncodingException. + * Throws HttpRuntimeException. */ PHP_METHOD(HttpRequest, getHistory) { NO_ARGS; IF_RETVAL_USED { - http_message *msg; - getObject(http_request_object, obj); - + zval *hist; + SET_EH_THROW_HTTP(); - if ((msg = http_message_parse(PHPSTR_VAL(&obj->history), PHPSTR_LEN(&obj->history)))) { - ZVAL_OBJVAL(return_value, http_message_object_new_ex(http_message_object_ce, msg, NULL)); + hist = GET_PROP(history); + if (Z_TYPE_P(hist) == IS_OBJECT) { + RETVAL_OBJECT(hist, 1); + } else { + http_error(HE_WARNING, HTTP_E_RUNTIME, "The history is empty"); } SET_EH_NORMAL(); } @@ -1842,8 +2008,12 @@ PHP_METHOD(HttpRequest, getHistory) PHP_METHOD(HttpRequest, clearHistory) { NO_ARGS { - getObject(http_request_object, obj); - phpstr_dtor(&obj->history); + zval *hist; + + MAKE_STD_ZVAL(hist); + ZVAL_NULL(hist); + SET_PROP(history, hist); + zval_ptr_dtor(&hist); } } /* }}} */ @@ -1908,7 +2078,7 @@ PHP_METHOD(HttpRequest, send) } else if (SUCCESS == http_request_object_requesthandler(obj, getThis())) { http_request_exec(obj->request); if (SUCCESS == http_request_object_responsehandler(obj, getThis())) { - RETVAL_OBJECT(GET_PROP(responseMessage)); + RETVAL_OBJECT(GET_PROP(responseMessage), 1); } }