- http_request_defaults() already takes care of resetting curl options so check strin...
[m6w6/ext-http] / http_request_api.c
index 8dc72dd6a01a6e7babc29037b6daaa01732fb504..8e7d0b0f3bf9a73443e4362d9cd23ef9a2bdd4ce 100644 (file)
@@ -6,7 +6,7 @@
     | modification, are permitted provided that the conditions mentioned |
     | in the accompanying LICENSE file are met.                          |
     +--------------------------------------------------------------------+
-    | Copyright (c) 2004-2005, Michael Wallner <mike@php.net>            |
+    | Copyright (c) 2004-2006, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
@@ -88,6 +88,10 @@ PHP_MINIT_FUNCTION(http_request)
        HTTP_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST);
        HTTP_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM);
        HTTP_LONG_CONSTANT("HTTP_AUTH_ANY", CURLAUTH_ANY);
+       
+       HTTP_LONG_CONSTANT("HTTP_VERSION_NONE", CURL_HTTP_VERSION_NONE);
+       HTTP_LONG_CONSTANT("HTTP_VERSION_1_0", CURL_HTTP_VERSION_1_0);
+       HTTP_LONG_CONSTANT("HTTP_VERSION_1_1", CURL_HTTP_VERSION_1_1);
 
        return SUCCESS;
 }
@@ -239,10 +243,10 @@ PHP_HTTP_API void _http_request_dtor(http_request *request)
        
        if (request->ch) {
                /* avoid nasty segfaults with already cleaned up callbacks */
-               curl_easy_setopt(request->ch, CURLOPT_NOPROGRESS, 1);
-               curl_easy_setopt(request->ch, CURLOPT_PROGRESSFUNCTION, NULL);
-               curl_easy_setopt(request->ch, CURLOPT_VERBOSE, 0);
-               curl_easy_setopt(request->ch, CURLOPT_DEBUGFUNCTION, NULL);
+               HTTP_CURL_OPT(NOPROGRESS, 1);
+               HTTP_CURL_OPT(PROGRESSFUNCTION, NULL);
+               HTTP_CURL_OPT(VERBOSE, 0);
+               HTTP_CURL_OPT(DEBUGFUNCTION, NULL);
                curl_easy_cleanup(request->ch);
                request->ch = NULL;
        }
@@ -326,6 +330,9 @@ PHP_HTTP_API void _http_request_defaults(http_request *request)
                HTTP_CURL_OPT(USERAGENT, "PECL::HTTP/" PHP_EXT_HTTP_VERSION " (PHP/" PHP_VERSION ")");
                HTTP_CURL_OPT(HTTPHEADER, NULL);
                HTTP_CURL_OPT(COOKIE, NULL);
+#if LIBCURL_VERSION_NUM >= 0x070e01
+               HTTP_CURL_OPT(COOKIELIST, NULL);
+#endif
                HTTP_CURL_OPT(COOKIEFILE, NULL);
                HTTP_CURL_OPT(COOKIEJAR, NULL);
                HTTP_CURL_OPT(RESUME_FROM, 0);
@@ -355,6 +362,7 @@ PHP_HTTP_API void _http_request_defaults(http_request *request)
                HTTP_CURL_OPT(IOCTLDATA, NULL);
                HTTP_CURL_OPT(READDATA, NULL);
                HTTP_CURL_OPT(INFILESIZE, 0);
+               HTTP_CURL_OPT(HTTP_VERSION, CURL_HTTP_VERSION_NONE);
        }
 }
 /* }}} */
@@ -401,13 +409,16 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
 
        /* proxy */
        if ((zoption = http_request_option(request, options, "proxyhost", IS_STRING))) {
-               HTTP_CURL_OPT(PROXY, Z_STRVAL_P(zoption));
+               if (Z_STRLEN_P(zoption)) {
+                       HTTP_CURL_OPT(PROXY, Z_STRVAL_P(zoption));
+               }
+
                /* port */
                if ((zoption = http_request_option(request, options, "proxyport", IS_LONG))) {
                        HTTP_CURL_OPT(PROXYPORT, Z_LVAL_P(zoption));
                }
                /* user:pass */
-               if ((zoption = http_request_option(request, options, "proxyauth", IS_STRING))) {
+               if ((zoption = http_request_option(request, options, "proxyauth", IS_STRING)) && Z_STRLEN_P(zoption)) {
                        HTTP_CURL_OPT(PROXYUSERPWD, Z_STRVAL_P(zoption));
                }
                /* auth method */
@@ -427,7 +438,7 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
        }
 
        /* auth */
-       if ((zoption = http_request_option(request, options, "httpauth", IS_STRING))) {
+       if ((zoption = http_request_option(request, options, "httpauth", IS_STRING)) && Z_STRLEN_P(zoption)) {
                HTTP_CURL_OPT(USERPWD, Z_STRVAL_P(zoption));
        }
        if ((zoption = http_request_option(request, options, "httpauthtype", IS_LONG))) {
@@ -444,12 +455,12 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
        }
 
        /* referer */
-       if ((zoption = http_request_option(request, options, "referer", IS_STRING))) {
+       if ((zoption = http_request_option(request, options, "referer", IS_STRING)) && Z_STRLEN_P(zoption)) {
                HTTP_CURL_OPT(REFERER, Z_STRVAL_P(zoption));
        }
 
        /* useragent, default "PECL::HTTP/version (PHP/version)" */
-       if ((zoption = http_request_option(request, options, "useragent", IS_STRING))) {
+       if ((zoption = http_request_option(request, options, "useragent", IS_STRING)) && Z_STRLEN_P(zoption)) {
                HTTP_CURL_OPT(USERAGENT, Z_STRVAL_P(zoption));
        }
 
@@ -482,20 +493,22 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                }
        }
        if ((zoption = http_request_option(request, options, "compress", IS_BOOL)) && Z_LVAL_P(zoption)) {
-               request->_cache.headers = curl_slist_append(request->_cache.headers, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5,*;q=0.1");
+               request->_cache.headers = curl_slist_append(request->_cache.headers, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
        }
        HTTP_CURL_OPT(HTTPHEADER, request->_cache.headers);
 
        /* cookies, array('name' => 'value') */
        if ((zoption = http_request_option(request, options, "cookies", IS_ARRAY))) {
                phpstr_dtor(&request->_cache.cookies);
-               if (SUCCESS == http_urlencode_hash_recursive(HASH_OF(zoption), &request->_cache.cookies, "; ", sizeof("; ")-1, NULL, 0)) {
-                       phpstr_fix(&request->_cache.cookies);
-                       HTTP_CURL_OPT(COOKIE, request->_cache.cookies.data);
+               if (zend_hash_num_elements(Z_ARRVAL_P(zoption))) {
+                       if (SUCCESS == http_urlencode_hash_recursive(HASH_OF(zoption), &request->_cache.cookies, "; ", sizeof("; ")-1, NULL, 0)) {
+                               phpstr_fix(&request->_cache.cookies);
+                               HTTP_CURL_OPT(COOKIE, request->_cache.cookies.data);
+                       }
                }
        }
 
-#if LIBCURL_VERSION_NUM >= 0x070f01
+#if LIBCURL_VERSION_NUM >= 0x070e01
        /* reset cookies */
        if ((zoption = http_request_option(request, options, "resetcookies", IS_BOOL)) && Z_LVAL_P(zoption)) {
                HTTP_CURL_OPT(COOKIELIST, "ALL");
@@ -531,6 +544,11 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                HTTP_CURL_OPT(MAXFILESIZE, Z_LVAL_P(zoption));
        }
 
+       /* http protocol */
+       if ((zoption = http_request_option(request, options, "protocol", IS_LONG))) {
+               HTTP_CURL_OPT(HTTP_VERSION, Z_LVAL_P(zoption));
+       }
+
        /* lastmodified */
        if ((zoption = http_request_option(request, options, "lastmodified", IS_LONG))) {
                if (Z_LVAL_P(zoption)) {
@@ -594,24 +612,24 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
        switch (request->meth)
        {
                case HTTP_GET:
-                       curl_easy_setopt(request->ch, CURLOPT_HTTPGET, 1);
+                       HTTP_CURL_OPT(HTTPGET, 1);
                break;
 
                case HTTP_HEAD:
-                       curl_easy_setopt(request->ch, CURLOPT_NOBODY, 1);
+                       HTTP_CURL_OPT(NOBODY, 1);
                break;
 
                case HTTP_POST:
-                       curl_easy_setopt(request->ch, CURLOPT_POST, 1);
+                       HTTP_CURL_OPT(POST, 1);
                break;
 
                case HTTP_PUT:
-                       curl_easy_setopt(request->ch, CURLOPT_UPLOAD, 1);
+                       HTTP_CURL_OPT(UPLOAD, 1);
                break;
 
                default:
                        if (http_request_method_exists(0, request->meth, NULL)) {
-                               curl_easy_setopt(request->ch, CURLOPT_CUSTOMREQUEST, http_request_method_name(request->meth));
+                               HTTP_CURL_OPT(CUSTOMREQUEST, http_request_method_name(request->meth));
                        } else {
                                http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unsupported request method: %d (%s)", request->meth, request->url);
                                return FAILURE;
@@ -620,22 +638,22 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
        }
 
        /* attach request body */
-       if (request->body && (request->meth != HTTP_GET) && (request->meth != HTTP_HEAD)) {
+       if (request->body && (request->meth != HTTP_GET) && (request->meth != HTTP_HEAD) && (request->meth != HTTP_OPTIONS)) {
                switch (request->body->type)
                {
                        case HTTP_REQUEST_BODY_CSTRING:
-                               curl_easy_setopt(request->ch, CURLOPT_POSTFIELDS, request->body->data);
-                               curl_easy_setopt(request->ch, CURLOPT_POSTFIELDSIZE, request->body->size);
+                               HTTP_CURL_OPT(POSTFIELDS, request->body->data);
+                               HTTP_CURL_OPT(POSTFIELDSIZE, request->body->size);
                        break;
 
                        case HTTP_REQUEST_BODY_CURLPOST:
-                               curl_easy_setopt(request->ch, CURLOPT_HTTPPOST, (struct curl_httppost *) request->body->data);
+                               HTTP_CURL_OPT(HTTPPOST, (struct curl_httppost *) request->body->data);
                        break;
 
                        case HTTP_REQUEST_BODY_UPLOADFILE:
-                               curl_easy_setopt(request->ch, CURLOPT_IOCTLDATA, request);
-                               curl_easy_setopt(request->ch, CURLOPT_READDATA, request);
-                               curl_easy_setopt(request->ch, CURLOPT_INFILESIZE, request->body->size);
+                               HTTP_CURL_OPT(IOCTLDATA, request);
+                               HTTP_CURL_OPT(READDATA, request);
+                               HTTP_CURL_OPT(INFILESIZE, request->body->size);
                        break;
 
                        default:
@@ -934,7 +952,7 @@ static int http_ssl_mutex_unlock(void **m)
 }
 
 static struct gcry_thread_cbs http_gnutls_tsl = {
-       GCRY_THREAD_OPTIONS_USER,
+       GCRY_THREAD_OPTION_USER,
        NULL,
        http_ssl_mutex_create,
        http_ssl_mutex_destroy,