- #ifdef CURLOPT_COOKIELIST out for libcurl < v7.14.1
[m6w6/ext-http] / http_request_object.c
index bc7a0f854e1107fd361fd67fd7f501ca8bd14aa3..60935420a651332df6c027089e3512c694b26459 100644 (file)
@@ -72,6 +72,10 @@ HTTP_BEGIN_ARGS(addCookies, 1)
        HTTP_ARG_VAL(cookies, 0)
 HTTP_END_ARGS;
 
+#if HTTP_CURL_VERSION(7,14,1)
+HTTP_EMPTY_ARGS(resetCookies);
+#endif
+
 HTTP_EMPTY_ARGS(getUrl);
 HTTP_BEGIN_ARGS(setUrl, 1)
        HTTP_ARG_VAL(url, 0)
@@ -190,6 +194,13 @@ HTTP_BEGIN_ARGS(postFields, 2)
        HTTP_ARG_VAL(info, 1)
 HTTP_END_ARGS;
 
+HTTP_BEGIN_ARGS(putData, 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, 2)
        HTTP_ARG_VAL(url, 0)
        HTTP_ARG_VAL(file, 0)
@@ -220,6 +231,11 @@ HTTP_BEGIN_ARGS(methodExists, 1)
        HTTP_ARG_VAL(method, 0)
 HTTP_END_ARGS;
 
+HTTP_BEGIN_ARGS(encodeBody, 2)
+       HTTP_ARG_VAL(fields, 0)
+       HTTP_ARG_VAL(files, 0)
+HTTP_END_ARGS;
+
 #define OBJ_PROP_CE http_request_object_ce
 zend_class_entry *http_request_object_ce;
 zend_function_entry http_request_object_fe[] = {
@@ -238,6 +254,9 @@ zend_function_entry http_request_object_fe[] = {
        HTTP_REQUEST_ME(addCookies, ZEND_ACC_PUBLIC)
        HTTP_REQUEST_ME(getCookies, ZEND_ACC_PUBLIC)
        HTTP_REQUEST_ME(setCookies, ZEND_ACC_PUBLIC)
+#if HTTP_CURL_VERSION(7,14,1)
+       HTTP_REQUEST_ME(resetCookies, ZEND_ACC_PUBLIC)
+#endif
 
        HTTP_REQUEST_ME(setMethod, ZEND_ACC_PUBLIC)
        HTTP_REQUEST_ME(getMethod, ZEND_ACC_PUBLIC)
@@ -291,6 +310,7 @@ zend_function_entry http_request_object_fe[] = {
        HTTP_REQUEST_ALIAS(head, http_head)
        HTTP_REQUEST_ALIAS(postData, http_post_data)
        HTTP_REQUEST_ALIAS(postFields, http_post_fields)
+       HTTP_REQUEST_ALIAS(putData, http_put_data)
        HTTP_REQUEST_ALIAS(putFile, http_put_file)
        HTTP_REQUEST_ALIAS(putStream, http_put_stream)
 
@@ -298,6 +318,8 @@ zend_function_entry http_request_object_fe[] = {
        HTTP_REQUEST_ALIAS(methodUnregister, http_request_method_unregister)
        HTTP_REQUEST_ALIAS(methodName, http_request_method_name)
        HTTP_REQUEST_ALIAS(methodExists, http_request_method_exists)
+       
+       HTTP_REQUEST_ALIAS(encodeBody, http_request_body_encode)
 
        EMPTY_FUNCTION_ENTRY
 };
@@ -450,6 +472,15 @@ void _http_request_object_free(zend_object *object TSRMLS_DC)
        efree(o);
 }
 
+#if HTTP_CURL_VERSION(7,14,1)
+#define http_request_object_resetcookies(o) _http_request_object_resetcookies((o) TSRMLS_CC)
+static inline STATUS _http_request_object_resetcookies(zval *this_ptr TSRMLS_DC)
+{
+       getObject(http_request_object, obj);
+       return curl_easy_setopt(obj->request->ch, CURLOPT_COOKIELIST, "ALL");
+}
+#endif
+
 #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)
 {
@@ -509,7 +540,7 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
        {
                case HTTP_GET:
                case HTTP_HEAD:
-               break;
+                       break;
 
                case HTTP_PUT:
                {
@@ -529,8 +560,8 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
                                        status = FAILURE;
                                }
                        }
+                       break;
                }
-               break;
 
                case HTTP_POST:
                default:
@@ -556,8 +587,8 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
                                        }
                                }
                        }
+                       break;
                }
-               break;
        }
 
        if (status == SUCCESS) {
@@ -633,7 +664,7 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this
                }
 
                UPD_PROP(long, responseCode, msg->http.info.response.code);
-               UPD_PROP(string, responseStatus, msg->http.info.response.status);
+               UPD_PROP(string, responseStatus, msg->http.info.response.status ? msg->http.info.response.status : "");
 
                MAKE_STD_ZVAL(resp);
                array_init(resp);
@@ -688,12 +719,14 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this
                ret = FAILURE;
        }
        
-       if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onfinish", sizeof("onfinish"))) {
+       if (!EG(exception) && 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);
+               with_error_handling(EH_NORMAL, NULL) {
+                       zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "onfinish", NULL, param);
+               } end_error_handling();
                zval_ptr_dtor(&param);
        }
        
@@ -755,7 +788,7 @@ static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS,
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                zval *opts, **options;
 
                opts = GET_PROP(options);
@@ -851,6 +884,10 @@ PHP_METHOD(HttpRequest, setOptions)
                                zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "seturl", NULL, *opt);
                        } else if (!strcmp(key, "method")) {
                                zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setmethod", NULL, *opt);
+#if HTTP_CURL_VERSION(7,14,1)
+                       } else if (!strcmp(key, "resetcookies")) {
+                               http_request_object_resetcookies(getThis());
+#endif
                        } else {
                                ZVAL_ADDREF(*opt);
                                add_assoc_zval(add_opts, key, *opt);
@@ -883,7 +920,7 @@ PHP_METHOD(HttpRequest, getOptions)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(options);
        }
 }
@@ -1013,6 +1050,19 @@ PHP_METHOD(HttpRequest, getCookies)
 }
 /* }}} */
 
+#if HTTP_CURL_VERSION(7,14,1)
+/* {{{ proto bool HttpRequest::resetCookies()
+ *
+ * Reset all cookies. Note that customly set cookies are not affected.
+ */
+PHP_METHOD(HttpRequest, resetCookies)
+{
+       NO_ARGS;
+       RETURN_SUCCESS(http_request_object_resetcookies(getThis()));
+}
+/* }}} */
+#endif
+
 /* {{{ proto bool HttpRequest::setUrl(string url)
  *
  * Set the request URL.
@@ -1045,7 +1095,7 @@ PHP_METHOD(HttpRequest, getUrl)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(url);
        }
 }
@@ -1083,7 +1133,7 @@ PHP_METHOD(HttpRequest, getMethod)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(method);
        }
 }
@@ -1126,7 +1176,7 @@ PHP_METHOD(HttpRequest, getContentType)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(contentType);
        }
 }
@@ -1185,7 +1235,7 @@ PHP_METHOD(HttpRequest, getQueryData)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(queryData);
        }
 }
@@ -1297,7 +1347,7 @@ PHP_METHOD(HttpRequest, getPostFields)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(postFields);
        }
 }
@@ -1377,7 +1427,7 @@ PHP_METHOD(HttpRequest, getRawPostData)
 {
        NO_ARGS;
        
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(rawPostData);
        }
 }
@@ -1475,7 +1525,7 @@ PHP_METHOD(HttpRequest, getPostFiles)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(postFiles);
        }
 }
@@ -1514,7 +1564,7 @@ PHP_METHOD(HttpRequest, getPutFile)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(putFile);
        }
 }
@@ -1594,7 +1644,7 @@ PHP_METHOD(HttpRequest, getPutData)
 {
        NO_ARGS;
        
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(putData);
        }
 }
@@ -1615,7 +1665,7 @@ PHP_METHOD(HttpRequest, getResponseData)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(responseData);
        }
 }
@@ -1636,7 +1686,7 @@ PHP_METHOD(HttpRequest, getResponseData)
  */
 PHP_METHOD(HttpRequest, getResponseHeader)
 {
-       IF_RETVAL_USED {
+       if (return_value_used) {
                zval *data, **headers, **header;
                char *header_name = NULL;
                int header_len = 0;
@@ -1674,7 +1724,7 @@ PHP_METHOD(HttpRequest, getResponseHeader)
  */
 PHP_METHOD(HttpRequest, getResponseCookies)
 {
-       IF_RETVAL_USED {
+       if (return_value_used) {
                long flags = 0;
                zval *allowed_extras_array = NULL, *data, **headers;
 
@@ -1770,7 +1820,7 @@ PHP_METHOD(HttpRequest, getResponseBody)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                zval **body;
                zval *data = GET_PROP(responseData);
                
@@ -1797,7 +1847,7 @@ PHP_METHOD(HttpRequest, getResponseCode)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(responseCode);
        }
 }
@@ -1813,7 +1863,7 @@ PHP_METHOD(HttpRequest, getResponseStatus)
 {
        NO_ARGS;
        
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(responseStatus);
        }
 }
@@ -1837,7 +1887,7 @@ PHP_METHOD(HttpRequest, getResponseStatus)
  */
 PHP_METHOD(HttpRequest, getResponseInfo)
 {
-       IF_RETVAL_USED {
+       if (return_value_used) {
                zval *info, **infop;
                char *info_name = NULL;
                int info_len = 0;
@@ -1918,7 +1968,7 @@ PHP_METHOD(HttpRequest, getRequestMessage)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                http_message *msg;
                getObject(http_request_object, obj);
 
@@ -1941,7 +1991,7 @@ PHP_METHOD(HttpRequest, getRawRequestMessage)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                getObject(http_request_object, obj);
 
                RETURN_PHPSTR_DUP(&obj->request->conv.request);
@@ -1959,7 +2009,7 @@ PHP_METHOD(HttpRequest, getRawResponseMessage)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                getObject(http_request_object, obj);
 
                RETURN_PHPSTR_DUP(&obj->request->conv.response);
@@ -1986,7 +2036,7 @@ PHP_METHOD(HttpRequest, getHistory)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                zval *hist;
                
                SET_EH_THROW_HTTP();