- some housekeeping
[m6w6/ext-http] / http_request_api.c
index 662c2ab7bc48311cff20747aa411f167fc3a2558..444e43fe5a6929992d3c04317a55d9ac9485cf1c 100644 (file)
 
 /* $Id$ */
 
-#ifdef HAVE_CONFIG_H
-#      include "config.h"
-#endif
-
+#define HTTP_WANT_SAPI
 #define HTTP_WANT_CURL
 #include "php_http.h"
 
@@ -88,6 +85,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;
 }
@@ -164,7 +165,8 @@ PHP_MSHUTDOWN_FUNCTION(http_request)
                } \
        }
 
-#define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(request->ch, CURLOPT_##OPTION, (p))
+#define HTTP_CURL_OPT(OPTION, p) HTTP_CURL_OPT_EX(request->ch, OPTION, (p))
+#define HTTP_CURL_OPT_EX(ch, OPTION, p) curl_easy_setopt((ch), CURLOPT_##OPTION, (p))
 #define HTTP_CURL_OPT_STRING(keyname, obdc) HTTP_CURL_OPT_STRING_EX(keyname, keyname, obdc)
 #define HTTP_CURL_OPT_SSL_STRING(keyname, obdc) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname, obdc)
 #define HTTP_CURL_OPT_SSL_STRING_(keyname,obdc ) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname, obdc)
@@ -205,6 +207,55 @@ static int http_curl_dummy_callback(char *data, size_t n, size_t l, void *s) { r
 static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *);
 /* }}} */
 
+/* {{{ CURL *http_curl_init(http_request *) */
+PHP_HTTP_API CURL * _http_curl_init_ex(CURL *ch, http_request *request)
+{
+       if (ch || (ch = curl_easy_init())) {
+#if defined(ZTS)
+               HTTP_CURL_OPT_EX(ch, NOSIGNAL, 1);
+#endif
+               HTTP_CURL_OPT_EX(ch, HEADER, 0);
+               HTTP_CURL_OPT_EX(ch, FILETIME, 1);
+               HTTP_CURL_OPT_EX(ch, AUTOREFERER, 1);
+               HTTP_CURL_OPT_EX(ch, VERBOSE, 1);
+               HTTP_CURL_OPT_EX(ch, HEADERFUNCTION, NULL);
+               HTTP_CURL_OPT_EX(ch, DEBUGFUNCTION, http_curl_raw_callback);
+               HTTP_CURL_OPT_EX(ch, READFUNCTION, http_curl_read_callback);
+               HTTP_CURL_OPT_EX(ch, IOCTLFUNCTION, http_curl_ioctl_callback);
+               HTTP_CURL_OPT_EX(ch, WRITEFUNCTION, http_curl_dummy_callback);
+               
+               /* set context */
+               if (request) {
+                       HTTP_CURL_OPT_EX(ch, PRIVATE, request);
+                       HTTP_CURL_OPT_EX(ch, DEBUGDATA, request);
+                       HTTP_CURL_OPT_EX(ch, ERRORBUFFER, request->_error);
+                       
+                       /* attach curl handle */
+                       request->ch = ch;
+                       /* set defaults (also in http_request_reset()) */
+                       http_request_defaults(request);
+               }
+       }
+       
+       return ch;
+}
+/* }}} */
+
+/* {{{ void http_curl_free(CURL **) */
+PHP_HTTP_API void _http_curl_free(CURL **ch)
+{
+       if (*ch) {
+               /* avoid nasty segfaults with already cleaned up callbacks */
+               HTTP_CURL_OPT_EX(*ch, NOPROGRESS, 1);
+               HTTP_CURL_OPT_EX(*ch, PROGRESSFUNCTION, NULL);
+               HTTP_CURL_OPT_EX(*ch, VERBOSE, 0);
+               HTTP_CURL_OPT_EX(*ch, DEBUGFUNCTION, NULL);
+               curl_easy_cleanup(*ch);
+               *ch = NULL;
+       }
+}
+/* }}} */
+
 /* {{{ http_request *http_request_init(http_request *) */
 PHP_HTTP_API http_request *_http_request_init_ex(http_request *request, CURL *ch, http_request_method meth, const char *url ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
 {
@@ -237,16 +288,7 @@ PHP_HTTP_API void _http_request_dtor(http_request *request)
 {
        TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
        
-       if (request->ch) {
-               /* avoid nasty segfaults with already cleaned up callbacks */
-               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;
-       }
-       
+       http_curl_free(&request->ch);
        http_request_reset(request);
        
        phpstr_dtor(&request->_cache.cookies);
@@ -284,6 +326,10 @@ PHP_HTTP_API void _http_request_reset(http_request *request)
        phpstr_dtor(&request->conv.request);
        phpstr_dtor(&request->conv.response);
        http_request_body_dtor(request->body);
+       
+       if (request->ch) {
+               http_request_defaults(request);
+       }
 }
 /* }}} */
 
@@ -291,23 +337,6 @@ PHP_HTTP_API void _http_request_reset(http_request *request)
 PHP_HTTP_API void _http_request_defaults(http_request *request)
 {
        if (request->ch) {
-#ifdef HAVE_CURL_EASY_RESET
-               curl_easy_reset(request->ch);
-#endif
-#if defined(ZTS)
-               HTTP_CURL_OPT(NOSIGNAL, 1);
-#endif
-               HTTP_CURL_OPT(PRIVATE, request);
-               HTTP_CURL_OPT(ERRORBUFFER, request->_error);
-               HTTP_CURL_OPT(HEADER, 0);
-               HTTP_CURL_OPT(FILETIME, 1);
-               HTTP_CURL_OPT(AUTOREFERER, 1);
-               HTTP_CURL_OPT(VERBOSE, 1);
-               HTTP_CURL_OPT(HEADERFUNCTION, NULL);
-               HTTP_CURL_OPT(DEBUGFUNCTION, http_curl_raw_callback);
-               HTTP_CURL_OPT(READFUNCTION, http_curl_read_callback);
-               HTTP_CURL_OPT(IOCTLFUNCTION, http_curl_ioctl_callback);
-               HTTP_CURL_OPT(WRITEFUNCTION, http_curl_dummy_callback);
                HTTP_CURL_OPT(PROGRESSFUNCTION, NULL);
                HTTP_CURL_OPT(URL, NULL);
                HTTP_CURL_OPT(NOPROGRESS, 1);
@@ -358,6 +387,12 @@ 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);
+               HTTP_CURL_OPT(CUSTOMREQUEST, NULL);
+               HTTP_CURL_OPT(NOBODY, 0);
+               HTTP_CURL_OPT(POST, 0);
+               HTTP_CURL_OPT(UPLOAD, 0);
+               HTTP_CURL_OPT(HTTPGET, 1);
        }
 }
 /* }}} */
@@ -389,12 +424,9 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
 
        TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
        
-       HTTP_CHECK_CURL_INIT(request->ch, curl_easy_init(), return FAILURE);
-       
-       http_request_defaults(request);
+       HTTP_CHECK_CURL_INIT(request->ch, http_curl_init(request), return FAILURE);
        
        /* set options */
-       HTTP_CURL_OPT(DEBUGDATA, request);
        HTTP_CURL_OPT(URL, request->url);
 
        /* progress callback */
@@ -404,13 +436,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 */
@@ -430,7 +465,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))) {
@@ -447,13 +482,18 @@ 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))) {
-               HTTP_CURL_OPT(USERAGENT, Z_STRVAL_P(zoption));
+               /* allow to send no user agent, not even default one */
+               if (Z_STRLEN_P(zoption)) {
+                       HTTP_CURL_OPT(USERAGENT, Z_STRVAL_P(zoption));
+               } else {
+                       HTTP_CURL_OPT(USERAGENT, NULL);
+               }
        }
 
        /* additional headers, array('name' => 'value') */
@@ -485,16 +525,18 @@ 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);
+                       }
                }
        }
 
@@ -534,13 +576,18 @@ 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)) {
                        if (Z_LVAL_P(zoption) > 0) {
                                HTTP_CURL_OPT(TIMEVALUE, Z_LVAL_P(zoption));
                        } else {
-                               HTTP_CURL_OPT(TIMEVALUE, time(NULL) + Z_LVAL_P(zoption));
+                               HTTP_CURL_OPT(TIMEVALUE, HTTP_GET_REQUEST_TIME() + Z_LVAL_P(zoption));
                        }
                        HTTP_CURL_OPT(TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE);
                } else {
@@ -623,9 +670,13 @@ 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_EMPTY:
+                               /* nothing */
+                       break;
+                       
                        case HTTP_REQUEST_BODY_CSTRING:
                                HTTP_CURL_OPT(POSTFIELDS, request->body->data);
                                HTTP_CURL_OPT(POSTFIELDSIZE, request->body->size);
@@ -937,7 +988,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,