X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_request_api.c;h=b6e40344c27001aeba102f149c63ea4b164af5a2;hp=adc3a5644a548a584a2930a77b1e8088d8d6d6fe;hb=refs%2Fheads%2Fv1.7.x;hpb=b1ace11a9604ffcc496d32827aa66a2ba99db5ff diff --git a/http_request_api.c b/http_request_api.c index adc3a56..b6e4034 100644 --- a/http_request_api.c +++ b/http_request_api.c @@ -6,7 +6,7 @@ | modification, are permitted provided that the conditions mentioned | | in the accompanying LICENSE file are met. | +--------------------------------------------------------------------+ - | Copyright (c) 2004-2007, Michael Wallner | + | Copyright (c) 2004-2010, Michael Wallner | +--------------------------------------------------------------------+ */ @@ -192,7 +192,13 @@ PHP_MINIT_FUNCTION(http_request) HTTP_LONG_CONSTANT("HTTP_PROXY_SOCKS5", CURLPROXY_SOCKS5); HTTP_LONG_CONSTANT("HTTP_PROXY_HTTP", CURLPROXY_HTTP); #if HTTP_CURL_VERSION(7,19,4) - TTP_LONG_CONSTANT("HTTP_PROXY_HTTP_1_0", CURLPROXY_HTTP_1_0); + HTTP_LONG_CONSTANT("HTTP_PROXY_HTTP_1_0", CURLPROXY_HTTP_1_0); +#endif + +#if HTTP_CURL_VERSION(7,19,1) + HTTP_LONG_CONSTANT("HTTP_POSTREDIR_301", CURL_REDIR_POST_301); + HTTP_LONG_CONSTANT("HTTP_POSTREDIR_302", CURL_REDIR_POST_302); + HTTP_LONG_CONSTANT("HTTP_POSTREDIR_ALL", CURL_REDIR_POST_ALL); #endif return SUCCESS; } @@ -471,7 +477,11 @@ PHP_HTTP_API void _http_request_defaults(http_request *request) HTTP_CURL_OPT(CURLOPT_PROXY, NULL); HTTP_CURL_OPT(CURLOPT_PROXYPORT, 0L); HTTP_CURL_OPT(CURLOPT_PROXYTYPE, 0L); - HTTP_CURL_OPT(CURLOPT_PROXYUSERPWD, NULL); + /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */ +#if HTTP_CURL_VERSION(7,19,1) + HTTP_CURL_OPT(CURLOPT_PROXYUSERNAME, NULL); + HTTP_CURL_OPT(CURLOPT_PROXYPASSWORD, NULL); +#endif HTTP_CURL_OPT(CURLOPT_PROXYAUTH, 0L); HTTP_CURL_OPT(CURLOPT_HTTPPROXYTUNNEL, 0L); HTTP_CURL_OPT(CURLOPT_DNS_CACHE_TIMEOUT, 60L); @@ -490,11 +500,18 @@ PHP_HTTP_API void _http_request_defaults(http_request *request) HTTP_CURL_OPT(CURLOPT_FORBID_REUSE, 0L); HTTP_CURL_OPT(CURLOPT_INTERFACE, NULL); HTTP_CURL_OPT(CURLOPT_PORT, 0L); +#if HTTP_CURL_VERSION(7,19,0) + HTTP_CURL_OPT(CURLOPT_ADDRESS_SCOPE, 0L); +#endif #if HTTP_CURL_VERSION(7,15,2) HTTP_CURL_OPT(CURLOPT_LOCALPORT, 0L); HTTP_CURL_OPT(CURLOPT_LOCALPORTRANGE, 0L); #endif - HTTP_CURL_OPT(CURLOPT_USERPWD, NULL); + /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */ +#if HTTP_CURL_VERSION(7,19,1) + HTTP_CURL_OPT(CURLOPT_USERNAME, NULL); + HTTP_CURL_OPT(CURLOPT_PASSWORD, NULL); +#endif HTTP_CURL_OPT(CURLOPT_HTTPAUTH, 0L); HTTP_CURL_OPT(CURLOPT_ENCODING, NULL); #if HTTP_CURL_VERSION(7,16,2) @@ -513,6 +530,10 @@ PHP_HTTP_API void _http_request_defaults(http_request *request) HTTP_CURL_OPT(CURLOPT_USERAGENT, "PECL::HTTP/" PHP_HTTP_VERSION " (PHP/" PHP_VERSION ")"); HTTP_CURL_OPT(CURLOPT_HTTPHEADER, NULL); HTTP_CURL_OPT(CURLOPT_COOKIE, NULL); + HTTP_CURL_OPT(CURLOPT_COOKIESESSION, 0L); + /* these options would enable curl's cookie engine by default which we don't want + HTTP_CURL_OPT(CURLOPT_COOKIEFILE, NULL); + HTTP_CURL_OPT(CURLOPT_COOKIEJAR, NULL); */ #if HTTP_CURL_VERSION(7,14,1) HTTP_CURL_OPT(CURLOPT_COOKIELIST, NULL); #endif @@ -594,7 +615,10 @@ 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, http_curl_init(request), return FAILURE); - + + if (!request->url) { + return FAILURE; + } if (!(storage = http_request_storage_get(request->ch))) { return FAILURE; } @@ -636,7 +660,7 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti } } #if HTTP_CURL_VERSION(7,19,4) - if ((zoption = http_request_option, request, options, "noproxy", IS_STRING))) { + if ((zoption = http_request_option(request, options, "noproxy", IS_STRING))) { HTTP_CURL_OPT(CURLOPT_NOPROXY, Z_STRVAL_P(zoption)); } #endif @@ -825,31 +849,54 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti HashKey header_key = initHashKey(0); zval **header_val; HashPosition pos; - + phpstr header; + + phpstr_init(&header); FOREACH_KEYVAL(pos, zoption, header_key, header_val) { if (header_key.type == HASH_KEY_IS_STRING) { - char header[1024]; zval *header_cpy = http_zsep(IS_STRING, *header_val); if (!strcasecmp(header_key.str, "range")) { range_req = 1; } - snprintf(header, sizeof(header), "%s: %s", header_key.str, Z_STRVAL_P(header_cpy)); - request->_cache.headers = curl_slist_append(request->_cache.headers, header); + + phpstr_appendf(&header, "%s: %s", header_key.str, Z_STRVAL_P(header_cpy)); + phpstr_fix(&header); + request->_cache.headers = curl_slist_append(request->_cache.headers, PHPSTR_VAL(&header)); + phpstr_reset(&header); + zval_ptr_dtor(&header_cpy); } } + phpstr_dtor(&header); } /* etag */ if ((zoption = http_request_option(request, options, "etag", IS_STRING)) && Z_STRLEN_P(zoption)) { - char match_header[1024], *quoted_etag = NULL; - - if ((Z_STRVAL_P(zoption)[0] != '"') || (Z_STRVAL_P(zoption)[Z_STRLEN_P(zoption)-1] != '"')) { - spprintf("ed_etag, 0, "\"%s\"", Z_STRVAL_P(zoption)); + zend_bool is_quoted; + phpstr header; + + phpstr_init(&header); + phpstr_appendf(&header, "%s: ", range_req?"If-Match":"If-None-Match"); + if ((Z_STRVAL_P(zoption)[0] == '"') && (Z_STRVAL_P(zoption)[Z_STRLEN_P(zoption)-1] == '"')) { + /* properly quoted etag */ + phpstr_append(&header, Z_STRVAL_P(zoption), Z_STRLEN_P(zoption)); + } else if ((Z_STRVAL_P(zoption)[0] == 'W') && (Z_STRVAL_P(zoption)[1] == '/')) { + /* weak etag */ + if ((Z_STRLEN_P(zoption) > 3) && (Z_STRVAL_P(zoption)[2] == '"') && (Z_STRVAL_P(zoption)[Z_STRLEN_P(zoption)-1] == '"')) { + /* quoted */ + phpstr_append(&header, Z_STRVAL_P(zoption), Z_STRLEN_P(zoption)); + } else { + /* unquoted */ + phpstr_appendf(&header, "W/\"%s\"", Z_STRVAL_P(zoption) + 2); + } + } else { + /* assume unquoted etag */ + phpstr_appendf(&header, "\"%s\"", Z_STRVAL_P(zoption)); } - snprintf(match_header, sizeof(match_header), "%s: %s", range_req?"If-Match":"If-None-Match", quoted_etag?quoted_etag:Z_STRVAL_P(zoption)); - request->_cache.headers = curl_slist_append(request->_cache.headers, match_header); - STR_FREE(quoted_etag); + phpstr_fix(&header); + + request->_cache.headers = curl_slist_append(request->_cache.headers, PHPSTR_VAL(&header)); + phpstr_dtor(&header); } /* compression */ if ((zoption = http_request_option(request, options, "compress", IS_BOOL)) && Z_LVAL_P(zoption)) { @@ -1064,6 +1111,11 @@ PHP_HTTP_API void _http_request_exec(http_request *request) retry: if (CURLE_OK != (result = curl_easy_perform(request->ch))) { http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(result), http_request_storage_get(request->ch)->errorbuffer, request->url); +#ifdef ZEND_ENGINE_2 + if ((HTTP_G->only_exceptions || GLOBAL_ERROR_HANDLING == EH_THROW) && EG(exception)) { + add_property_long(EG(exception), "curlCode", result); + } +#endif if (request->_retry.count > tries++) { switch (result) {