X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_curl.c;h=5fea9fea4f864a06baf78124bcc1ca5d8a442860;hp=2d109225cb8e1180eb8e664a9f3ccf1ba7cfdb38;hb=4957436be59f65fae9cdbaec1dc865acc680862f;hpb=a4fd0a809ae831f21cbaec14e24c3d3ee0569c12 diff --git a/php_http_curl.c b/php_http_curl.c index 2d10922..5fea9fe 100644 --- a/php_http_curl.c +++ b/php_http_curl.c @@ -28,6 +28,7 @@ typedef struct php_http_curl_request { HashTable cache; struct curl_slist *headers; + struct curl_slist *resolve; php_http_buffer_t cookies; long redirects; @@ -127,6 +128,7 @@ static void *php_http_curl_copy(void *opaque, void *handle TSRMLS_DC) void *ch; if ((ch = curl_easy_duphandle(handle))) { + curl_easy_reset(ch); get_storage(ch); return ch; } @@ -238,6 +240,8 @@ static int php_http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, curl->progress.state.info = "not disconnected"; } else if (php_memnstr(data, ZEND_STRL("closed"), data + length)) { curl->progress.state.info = "disconnected"; + } else if (php_memnstr(data, ZEND_STRL("Issue another request"), data + length)) { + curl->progress.state.info = "redirect"; } php_http_request_progress_notify(&curl->progress TSRMLS_CC); break; @@ -287,7 +291,7 @@ static int php_http_curl_dummy_callback(char *data, size_t n, size_t l, void *s) return n*l; } -static STATUS php_http_curl_request_prepare(php_http_request_t *h, php_http_request_method_t meth, const char *url, php_http_message_body_t *body) +static STATUS php_http_curl_request_prepare(php_http_request_t *h, const char *meth, const char *url, php_http_message_body_t *body) { php_http_curl_request_t *curl = h->ctx; php_http_curl_request_storage_t *storage = get_storage(curl->handle); @@ -301,30 +305,28 @@ static STATUS php_http_curl_request_prepare(php_http_request_t *h, php_http_requ curl_easy_setopt(curl->handle, CURLOPT_URL, storage->url); /* request method */ - switch (meth) { - case PHP_HTTP_GET: + switch (php_http_select_str(meth, 4, "GET", "HEAD", "POST", "PUT")) { + case 0: curl_easy_setopt(curl->handle, CURLOPT_HTTPGET, 1L); break; - case PHP_HTTP_HEAD: + case 1: curl_easy_setopt(curl->handle, CURLOPT_NOBODY, 1L); break; - case PHP_HTTP_POST: + case 2: curl_easy_setopt(curl->handle, CURLOPT_POST, 1L); break; - case PHP_HTTP_PUT: + case 3: curl_easy_setopt(curl->handle, CURLOPT_UPLOAD, 1L); break; default: { - const char *name = php_http_request_method_name(meth TSRMLS_CC); - - if (name) { - curl_easy_setopt(curl->handle, CURLOPT_CUSTOMREQUEST, name); + if (meth) { + curl_easy_setopt(curl->handle, CURLOPT_CUSTOMREQUEST, meth); } else { - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_METHOD, "Unsupported request method: %d (%s)", meth, url); + php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_METHOD, "Unsupported request method: '%s' (%s)", meth, url); return FAILURE; } break; @@ -339,17 +341,12 @@ static STATUS php_http_curl_request_prepare(php_http_request_t *h, php_http_requ * same semantics as those specified in section 9« reveal that not any single defined HTTP/1.1 method * does not allow a request body. */ - switch (meth) { - default: { - size_t body_size = php_http_message_body_size(body); - - curl_easy_setopt(curl->handle, CURLOPT_IOCTLDATA, body); - curl_easy_setopt(curl->handle, CURLOPT_READDATA, body); - curl_easy_setopt(curl->handle, CURLOPT_INFILESIZE, body_size); - curl_easy_setopt(curl->handle, CURLOPT_POSTFIELDSIZE, body_size); - break; - } - } + size_t body_size = php_http_message_body_size(body); + + curl_easy_setopt(curl->handle, CURLOPT_IOCTLDATA, body); + curl_easy_setopt(curl->handle, CURLOPT_READDATA, body); + curl_easy_setopt(curl->handle, CURLOPT_INFILESIZE, body_size); + curl_easy_setopt(curl->handle, CURLOPT_POSTFIELDSIZE, body_size); } return SUCCESS; @@ -628,6 +625,30 @@ static STATUS set_options(php_http_request_t *h, HashTable *options) if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("ipresolve"), IS_LONG)) && Z_LVAL_P(zoption)) { curl_easy_setopt(ch, CURLOPT_IPRESOLVE, Z_LVAL_P(zoption)); } +#if PHP_HTTP_CURL_VERSION(7,21,3) + if (curl->options.resolve) { + curl_slist_free_all(curl->options.resolve); + curl->options.resolve = NULL; + } + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("resolve"), IS_ARRAY))) { + php_http_array_hashkey_t key = php_http_array_hashkey_init(0); + HashPosition pos; + zval **data; + + FOREACH_KEYVAL(pos, zoption, key, data) { + zval *cpy = php_http_ztyp(IS_STRING, *data); + + curl->options.resolve = curl_slist_append(curl->options.resolve, Z_STRVAL_P(cpy)); + + zval_ptr_dtor(&cpy); + } + } +#endif +#if PHP_HTTP_CURL_VERSION(7,24,0) + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("dns_servers"), IS_STRING)) && Z_STRLEN_P(zoption)) { + curl_easy_setopt(ch, CURLOPT_DNS_SERVERS, Z_STRVAL_P(zoption)); + } +#endif /* limits */ if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("low_speed_limit"), IS_LONG))) { @@ -1064,7 +1085,6 @@ static STATUS get_info(CURL *ch, HashTable *info) add_assoc_zval_ex(&array, "ssl_engines", sizeof("ssl_engines"), subarray); curl_slist_free_all(s); } -#if PHP_HTTP_CURL_VERSION(7,14,1) if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_COOKIELIST, &s)) { MAKE_STD_ZVAL(subarray); array_init(subarray); @@ -1076,12 +1096,9 @@ static STATUS get_info(CURL *ch, HashTable *info) add_assoc_zval_ex(&array, "cookies", sizeof("cookies"), subarray); curl_slist_free_all(s); } -#endif -#if PHP_HTTP_CURL_VERSION(7,18,2) if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_URL, &c)) { add_assoc_string_ex(&array, "redirect_url", sizeof("redirect_url"), c ? c : "", 1); } -#endif #if PHP_HTTP_CURL_VERSION(7,19,0) if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_IP, &c)) { add_assoc_string_ex(&array, "primary_ip", sizeof("primary_ip"), c ? c : "", 1); @@ -1097,7 +1114,24 @@ static STATUS get_info(CURL *ch, HashTable *info) add_assoc_long_ex(&array, "condition_unmet", sizeof("condition_unmet"), l); } #endif +#if PHP_HTTP_CURL_VERSION(7,21,0) + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_PORT, &l)) { + add_assoc_long_ex(&array, "primary_port", sizeof("primary_port"), l); + } +#endif +#if PHP_HTTP_CURL_VERSION(7,21,0) + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_LOCAL_IP, &c)) { + add_assoc_string_ex(&array, "local_ip", sizeof("local_ip"), c ? c : "", 1); + } +#endif +#if PHP_HTTP_CURL_VERSION(7,21,0) + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_LOCAL_PORT, &l)) { + add_assoc_long_ex(&array, "local_port", sizeof("local_port"), l); + } +#endif + /* END::CURLINFO */ + #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL) { int i; @@ -1219,6 +1253,17 @@ static STATUS php_http_curl_request_datashare_setopt(php_http_request_datashare_ } break; +#if PHP_HTTP_CURL_VERSION(7,23,0) + case PHP_HTTP_REQUEST_DATASHARE_OPT_SSLSESSIONS: + if (CURLSHE_OK != (rc = curl_share_setopt(curl->handle, *((zend_bool *) arg) ? CURLSHOPT_SHARE : CURLSHOPT_UNSHARE, CURL_LOCK_DATA_SSL_SESSION))) { + TSRMLS_FETCH_FROM_CTX(h->ts); + + php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not %s sharing of SSL session data: %s", *((zend_bool *) arg) ? "enable" : "disable", curl_share_strerror(rc)); + return FAILURE; + } + break; +#endif + default: return FAILURE; } @@ -1289,7 +1334,7 @@ static void php_http_curl_request_pool_dtor(php_http_request_pool_t *h) h->ctx = NULL; } -static STATUS php_http_curl_request_pool_attach(php_http_request_pool_t *h, php_http_request_t *r, php_http_request_method_t m, const char *url, php_http_message_body_t *body) +static STATUS php_http_curl_request_pool_attach(php_http_request_pool_t *h, php_http_request_t *r, const char *m, const char *url, php_http_message_body_t *body) { php_http_curl_request_pool_t *curl = h->ctx; php_http_curl_request_t *recurl = r->ctx; @@ -1610,6 +1655,12 @@ static STATUS php_http_curl_request_reset(php_http_request_t *h) curl_easy_setopt(ch, CURLOPT_HTTPPROXYTUNNEL, 0L); curl_easy_setopt(ch, CURLOPT_DNS_CACHE_TIMEOUT, 60L); curl_easy_setopt(ch, CURLOPT_IPRESOLVE, 0); +#if PHP_HTTP_CURL_VERSION(7,21,3) + curl_easy_setopt(ch, CURLOPT_RESOLVE, NULL); +#endif +#if PHP_HTTP_CURL_VERSION(7,24,0) + curl_easy_setopt(ch, CURLOPT_DNS_SERVERS, NULL); +#endif curl_easy_setopt(ch, CURLOPT_LOW_SPEED_LIMIT, 0L); curl_easy_setopt(ch, CURLOPT_LOW_SPEED_TIME, 0L); /* LFS weirdance @@ -1704,7 +1755,7 @@ static STATUS php_http_curl_request_reset(php_http_request_t *h) return SUCCESS; } -static STATUS php_http_curl_request_exec(php_http_request_t *h, php_http_request_method_t meth, const char *url, php_http_message_body_t *body) +static STATUS php_http_curl_request_exec(php_http_request_t *h, const char *meth, const char *url, php_http_message_body_t *body) { uint tries = 0; CURLcode result;