- adjust cookie handling to work better together with libcurl
[m6w6/ext-http] / http_request_api.c
index 06b69f719e8039b72788da31a6f3043417258dca..d6a370481dbe2da6cb0c90cbb9e276f7f8f50d6d 100644 (file)
@@ -130,7 +130,7 @@ PHP_MINIT_FUNCTION(http_request)
        CRYPTO_set_id_callback(http_openssl_thread_id);
        CRYPTO_set_locking_callback(http_openssl_thread_lock);
 #endif
-#ifdef HTTP_NED_GNUTLS_TSL
+#ifdef HTTP_NEED_GNUTLS_TSL
        gcry_control(GCRYCTL_SET_THREAD_CBS, &http_gnutls_tsl);
 #endif
 
@@ -257,7 +257,7 @@ PHP_MSHUTDOWN_FUNCTION(http_request)
        }
 #define HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \
        if (!strcasecmp(key, keyname)) { \
-               zval *copy = http_request_option_cache(request, keyname, zval_copy(IS_STRING, *param)); \
+               zval *copy = http_request_option_cache_ex(request, keyname, strlen(keyname)+1, 0, zval_copy(IS_STRING, *param)); \
                if (obdc) { \
                        HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(copy), return FAILURE); \
                } \
@@ -272,9 +272,10 @@ PHP_MSHUTDOWN_FUNCTION(http_request)
        }
 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
        if (!strcasecmp(key, keyname)) { \
-               zval *copy = http_request_option_cache(request, keyname, zval_copy(IS_LONG, *param)); \
+               zval *copy = zval_copy(IS_LONG, *param); \
                HTTP_CURL_OPT(optname, Z_LVAL_P(copy)); \
                key = NULL; \
+               zval_free(&copy); \
                continue; \
        }
 /* }}} */
@@ -373,8 +374,6 @@ PHP_HTTP_API http_request *_http_request_init_ex(http_request *request, CURL *ch
 /* {{{ void http_request_dtor(http_request *) */
 PHP_HTTP_API void _http_request_dtor(http_request *request)
 {
-       TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
-       
        http_curl_free(&request->ch);
        http_request_reset(request);
        
@@ -421,6 +420,32 @@ PHP_HTTP_API void _http_request_reset(http_request *request)
 }
 /* }}} */
 
+/* {{{ STATUS http_request_enable_cookies(http_request *) */
+PHP_HTTP_API STATUS _http_request_enable_cookies(http_request *request)
+{
+       TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
+       if (CURLE_OK == curl_easy_setopt(request->ch, CURLOPT_COOKIEFILE, "")) {
+               return SUCCESS;
+       }
+       http_error(HE_WARNING, HTTP_E_REQUEST, "Could not enable cookies for this session");
+       return FAILURE;
+}
+/* }}} */
+
+/* {{{ STATUS http_request_reset_cookies(http_request *) */
+PHP_HTTP_API STATUS _http_request_reset_cookies(http_request *request)
+{
+       TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
+#if HTTP_CURL_VERSION(7,14,1)
+       if (CURLE_OK == curl_easy_setopt(request->ch, CURLOPT_COOKIELIST, "ALL")) {
+               return SUCCESS;
+       }
+#endif
+       http_error(HE_WARNING, HTTP_E_REQUEST, "Could not reset cookies");
+       return FAILURE;
+}
+/* }}} */
+
 /* {{{ void http_request_defaults(http_request *) */
 PHP_HTTP_API void _http_request_defaults(http_request *request)
 {
@@ -451,8 +476,6 @@ PHP_HTTP_API void _http_request_defaults(http_request *request)
 #if HTTP_CURL_VERSION(7,14,1)
                HTTP_CURL_OPT(CURLOPT_COOKIELIST, NULL);
 #endif
-               HTTP_CURL_OPT(CURLOPT_COOKIEFILE, NULL);
-               HTTP_CURL_OPT(CURLOPT_COOKIEJAR, NULL);
                HTTP_CURL_OPT(CURLOPT_RANGE, NULL);
                HTTP_CURL_OPT(CURLOPT_RESUME_FROM, 0);
                HTTP_CURL_OPT(CURLOPT_MAXFILESIZE, 0);
@@ -493,8 +516,6 @@ PHP_HTTP_API void _http_request_defaults(http_request *request)
 
 PHP_HTTP_API void _http_request_set_progress_callback(http_request *request, zval *cb)
 {
-       TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
-       
        if (request->_progress_callback) {
                zval_ptr_dtor(&request->_progress_callback);
        }
@@ -760,27 +781,16 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                }
        }
 
-#if HTTP_CURL_VERSION(7,14,1)
-       /* reset cookies */
-       if ((zoption = http_request_option(request, options, "resetcookies", IS_BOOL)) && Z_LVAL_P(zoption)) {
-               HTTP_CURL_OPT(CURLOPT_COOKIELIST, "ALL");
-       }
-#endif
-       
-       /* session cookies */
-       if ((zoption = http_request_option(request, options, "cookiesession", IS_BOOL))) {
-               if (Z_LVAL_P(zoption)) {
-                       /* accept cookies for this session */
-                       HTTP_CURL_OPT(CURLOPT_COOKIEFILE, "");
-               } else {
-                       /* reset session cookies */
-                       HTTP_CURL_OPT(CURLOPT_COOKIESESSION, 1);
-               }
+       /* don't load session cookies from cookiestore */
+       if ((zoption = http_request_option(request, options, "cookiesession", IS_BOOL)) && Z_BVAL_P(zoption)) {
+               HTTP_CURL_OPT(CURLOPT_COOKIESESSION, 1);
        }
 
        /* cookiestore, read initial cookies from that file and store cookies back into that file */
-       if ((zoption = http_request_option(request, options, "cookiestore", IS_STRING)) && Z_STRLEN_P(zoption)) {
-               HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(zoption), return FAILURE);
+       if ((zoption = http_request_option(request, options, "cookiestore", IS_STRING))) {
+               if (Z_STRLEN_P(zoption)) {
+                       HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(zoption), return FAILURE);
+               }
                HTTP_CURL_OPT(CURLOPT_COOKIEFILE, Z_STRVAL_P(zoption));
                HTTP_CURL_OPT(CURLOPT_COOKIEJAR, Z_STRVAL_P(zoption));
        }
@@ -841,23 +851,22 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
        }
 
        /* request method */
-       switch (request->meth)
-       {
+       switch (request->meth) {
                case HTTP_GET:
                        HTTP_CURL_OPT(CURLOPT_HTTPGET, 1);
-               break;
+                       break;
 
                case HTTP_HEAD:
                        HTTP_CURL_OPT(CURLOPT_NOBODY, 1);
-               break;
+                       break;
 
                case HTTP_POST:
                        HTTP_CURL_OPT(CURLOPT_POST, 1);
-               break;
+                       break;
 
                case HTTP_PUT:
                        HTTP_CURL_OPT(CURLOPT_UPLOAD, 1);
-               break;
+                       break;
 
                default:
                        if (http_request_method_exists(0, request->meth, NULL)) {
@@ -866,20 +875,19 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                                http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unsupported request method: %d (%s)", request->meth, request->url);
                                return FAILURE;
                        }
-               break;
+                       break;
        }
 
        /* attach request body */
        if (request->body && (request->meth != HTTP_GET) && (request->meth != HTTP_HEAD) && (request->meth != HTTP_OPTIONS)) {
-               switch (request->body->type)
-               {
+               switch (request->body->type) {
                        case HTTP_REQUEST_BODY_EMPTY:
                                /* nothing */
-                       break;
+                               break;
                        
                        case HTTP_REQUEST_BODY_CURLPOST:
                                HTTP_CURL_OPT(CURLOPT_HTTPPOST, (struct curl_httppost *) request->body->data);
-                       break;
+                               break;
 
                        case HTTP_REQUEST_BODY_CSTRING:
                                if (request->meth != HTTP_PUT) {
@@ -892,13 +900,12 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                                HTTP_CURL_OPT(CURLOPT_IOCTLDATA, request);
                                HTTP_CURL_OPT(CURLOPT_READDATA, request);
                                HTTP_CURL_OPT(CURLOPT_INFILESIZE, request->body->size);
-                       break;
+                               break;
 
                        default:
                                /* shouldn't ever happen */
                                http_error_ex(HE_ERROR, 0, "Unknown request body type: %d (%s)", request->body->type, request->url);
                                return FAILURE;
-                       break;
                }
        }
 
@@ -964,8 +971,7 @@ static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *ct
        TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
        
        if (request->body) {
-               switch (request->body->type)
-               {
+               switch (request->body->type) {
                        case HTTP_REQUEST_BODY_CSTRING:
                        {
                                size_t out = MIN(len * n, request->body->size - request->body->priv);
@@ -975,12 +981,11 @@ static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *ct
                                        request->body->priv += out;
                                        return out;
                                }
+                               break;
                        }
-                       break;
                        
                        case HTTP_REQUEST_BODY_UPLOADFILE:
                                return php_stream_read((php_stream *) request->body->data, data, len * n);
-                       break;
                }
        }
        return 0;
@@ -1004,7 +1009,9 @@ static int http_curl_progress_callback(void *ctx, double dltotal, double dlnow,
        add_assoc_double(param, "ultotal", ultotal);
        add_assoc_double(param, "ulnow", ulnow);
        
-       call_user_function(EG(function_table), NULL, request->_progress_callback, &retval, 1, &param TSRMLS_CC);
+       with_error_handling(EH_NORMAL, NULL) {
+               call_user_function(EG(function_table), NULL, request->_progress_callback, &retval, 1, &param TSRMLS_CC);
+       } end_error_handling();
        
        zval_ptr_dtor(&param);
        zval_dtor(&retval);
@@ -1024,18 +1031,17 @@ static curlioerr http_curl_ioctl_callback(CURL *ch, curliocmd cmd, void *ctx)
        }
        
        if (request->body) {
-               switch (request->body->type)
-               {
+               switch (request->body->type) {
                        case HTTP_REQUEST_BODY_CSTRING:
                                request->body->priv = 0;
                                return CURLIOE_OK;
-                       break;
+                               break;
                                
                        case HTTP_REQUEST_BODY_UPLOADFILE:
                                if (SUCCESS == php_stream_rewind((php_stream *) request->body->data)) {
                                        return CURLIOE_OK;
                                }
-                       break;
+                               break;
                }
        }
        
@@ -1048,22 +1054,21 @@ static int http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size
 {
        http_request *request = (http_request *) ctx;
 
-       switch (type)
-       {
+       switch (type) {
                case CURLINFO_DATA_IN:
                        if (request->conv.last_type == CURLINFO_HEADER_IN) {
                                phpstr_appends(&request->conv.response, HTTP_CRLF);
                        }
                case CURLINFO_HEADER_IN:
                        phpstr_append(&request->conv.response, data, length);
-               break;
+                       break;
                case CURLINFO_DATA_OUT:
                        if (request->conv.last_type == CURLINFO_HEADER_OUT) {
                                phpstr_appends(&request->conv.request, HTTP_CRLF);
                        }
                case CURLINFO_HEADER_OUT:
                        phpstr_append(&request->conv.request, data, length);
-               break;
+                       break;
                default:
 #if 0
                        fprintf(stderr, "## ", type);
@@ -1083,7 +1088,7 @@ static int http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size
                                fprintf(stderr, "\n");
                        }
 #endif
-               break;
+                       break;
        }
 
        if (type) {