2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
13 #include <php/Zend/zend.h>
14 #include <php/Zend/zend_API.h>
16 #include "php_http_api.h"
18 /* resource_factory ops */
20 static void *php_http_curl_ctor(void *opaque TSRMLS_DC
)
24 if ((ch
= curl_easy_init())) {
31 static void *php_http_curl_copy(void *opaque
, void *handle TSRMLS_DC
)
35 if ((ch
= curl_easy_duphandle(handle
))) {
43 static void php_http_curl_dtor(void *opaque
, void *handle TSRMLS_DC
)
45 php_http_curl_client_storage_t
*st
= get_storage(handle
);
47 curl_easy_cleanup(handle
);
53 if (st
->cookiestore
) {
54 pefree(st
->cookiestore
, 1);
62 static size_t php_http_curl_client_read_callback(void *data
, size_t len
, size_t n
, void *ctx
)
64 php_http_message_body_t
*body
= ctx
;
67 TSRMLS_FETCH_FROM_CTX(body
->ts
);
68 return php_stream_read(php_http_message_body_stream(body
), data
, len
* n
);
73 static int php_http_curl_client_progress_callback(void *ctx
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
75 php_http_client_t
*h
= ctx
;
76 php_http_curl_client_t
*curl
= h
->ctx
;
77 TSRMLS_FETCH_FROM_CTX(h
->ts
);
79 curl
->progress
.state
.dl
.total
= dltotal
;
80 curl
->progress
.state
.dl
.now
= dlnow
;
81 curl
->progress
.state
.ul
.total
= ultotal
;
82 curl
->progress
.state
.ul
.now
= ulnow
;
84 php_http_client_progress_notify(&curl
->progress TSRMLS_CC
);
89 static curlioerr
php_http_curl_client_ioctl_callback(CURL
*ch
, curliocmd cmd
, void *ctx
)
91 php_http_message_body_t
*body
= ctx
;
93 if (cmd
!= CURLIOCMD_RESTARTREAD
) {
94 return CURLIOE_UNKNOWNCMD
;
98 TSRMLS_FETCH_FROM_CTX(body
->ts
);
100 if (SUCCESS
== php_stream_rewind(php_http_message_body_stream(body
))) {
105 return CURLIOE_FAILRESTART
;
108 static int php_http_curl_client_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
110 php_http_client_t
*h
= ctx
;
111 php_http_curl_client_t
*curl
= h
->ctx
;
113 TSRMLS_FETCH_FROM_CTX(h
->ts
);
118 if (php_memnstr(data
, ZEND_STRL("About to connect"), data
+ length
)) {
119 curl
->progress
.state
.info
= "resolve";
120 } else if (php_memnstr(data
, ZEND_STRL("Trying"), data
+ length
)) {
121 curl
->progress
.state
.info
= "connect";
122 } else if (php_memnstr(data
, ZEND_STRL("Connected"), data
+ length
)) {
123 curl
->progress
.state
.info
= "connected";
124 } else if (php_memnstr(data
, ZEND_STRL("left intact"), data
+ length
)) {
125 curl
->progress
.state
.info
= "not disconnected";
126 } else if (php_memnstr(data
, ZEND_STRL("closed"), data
+ length
)) {
127 curl
->progress
.state
.info
= "disconnected";
128 } else if (php_memnstr(data
, ZEND_STRL("Issue another request"), data
+ length
)) {
129 curl
->progress
.state
.info
= "redirect";
131 php_http_client_progress_notify(&curl
->progress TSRMLS_CC
);
133 case CURLINFO_HEADER_OUT
:
134 case CURLINFO_DATA_OUT
:
135 case CURLINFO_SSL_DATA_OUT
:
136 curl
->progress
.state
.info
= "send";
138 case CURLINFO_HEADER_IN
:
139 case CURLINFO_DATA_IN
:
140 case CURLINFO_SSL_DATA_IN
:
141 curl
->progress
.state
.info
= "receive";
148 case CURLINFO_HEADER_IN
:
149 case CURLINFO_DATA_IN
:
150 php_http_buffer_append(h
->response
.buffer
, data
, length
);
152 if (curl
->options
.redirects
) {
153 flags
|= PHP_HTTP_MESSAGE_PARSER_EMPTY_REDIRECTS
;
156 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE
== php_http_message_parser_parse(h
->response
.parser
, h
->response
.buffer
, flags
, &h
->response
.message
)) {
161 case CURLINFO_HEADER_OUT
:
162 case CURLINFO_DATA_OUT
:
163 php_http_buffer_append(h
->request
.buffer
, data
, length
);
165 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE
== php_http_message_parser_parse(h
->request
.parser
, h
->request
.buffer
, flags
, &h
->request
.message
)) {
175 _dpf(type
, data
, length
);
181 static int php_http_curl_client_dummy_callback(char *data
, size_t n
, size_t l
, void *s
)
186 static STATUS
get_info(CURL
*ch
, HashTable
*info
)
191 struct curl_slist
*s
, *p
;
192 zval
*subarray
, array
;
193 INIT_PZVAL_ARRAY(&array
, info
);
195 /* BEGIN::CURLINFO */
196 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_EFFECTIVE_URL
, &c
)) {
197 add_assoc_string_ex(&array
, "effective_url", sizeof("effective_url"), c
? c
: "", 1);
199 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_RESPONSE_CODE
, &l
)) {
200 add_assoc_long_ex(&array
, "response_code", sizeof("response_code"), l
);
202 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_TOTAL_TIME
, &d
)) {
203 add_assoc_double_ex(&array
, "total_time", sizeof("total_time"), d
);
205 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_NAMELOOKUP_TIME
, &d
)) {
206 add_assoc_double_ex(&array
, "namelookup_time", sizeof("namelookup_time"), d
);
208 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONNECT_TIME
, &d
)) {
209 add_assoc_double_ex(&array
, "connect_time", sizeof("connect_time"), d
);
211 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRETRANSFER_TIME
, &d
)) {
212 add_assoc_double_ex(&array
, "pretransfer_time", sizeof("pretransfer_time"), d
);
214 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SIZE_UPLOAD
, &d
)) {
215 add_assoc_double_ex(&array
, "size_upload", sizeof("size_upload"), d
);
217 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SIZE_DOWNLOAD
, &d
)) {
218 add_assoc_double_ex(&array
, "size_download", sizeof("size_download"), d
);
220 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SPEED_DOWNLOAD
, &d
)) {
221 add_assoc_double_ex(&array
, "speed_download", sizeof("speed_download"), d
);
223 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SPEED_UPLOAD
, &d
)) {
224 add_assoc_double_ex(&array
, "speed_upload", sizeof("speed_upload"), d
);
226 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HEADER_SIZE
, &l
)) {
227 add_assoc_long_ex(&array
, "header_size", sizeof("header_size"), l
);
229 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REQUEST_SIZE
, &l
)) {
230 add_assoc_long_ex(&array
, "request_size", sizeof("request_size"), l
);
232 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SSL_VERIFYRESULT
, &l
)) {
233 add_assoc_long_ex(&array
, "ssl_verifyresult", sizeof("ssl_verifyresult"), l
);
235 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_FILETIME
, &l
)) {
236 add_assoc_long_ex(&array
, "filetime", sizeof("filetime"), l
);
238 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
)) {
239 add_assoc_double_ex(&array
, "content_length_download", sizeof("content_length_download"), d
);
241 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_LENGTH_UPLOAD
, &d
)) {
242 add_assoc_double_ex(&array
, "content_length_upload", sizeof("content_length_upload"), d
);
244 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_STARTTRANSFER_TIME
, &d
)) {
245 add_assoc_double_ex(&array
, "starttransfer_time", sizeof("starttransfer_time"), d
);
247 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_TYPE
, &c
)) {
248 add_assoc_string_ex(&array
, "content_type", sizeof("content_type"), c
? c
: "", 1);
250 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_TIME
, &d
)) {
251 add_assoc_double_ex(&array
, "redirect_time", sizeof("redirect_time"), d
);
253 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_COUNT
, &l
)) {
254 add_assoc_long_ex(&array
, "redirect_count", sizeof("redirect_count"), l
);
256 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HTTP_CONNECTCODE
, &l
)) {
257 add_assoc_long_ex(&array
, "connect_code", sizeof("connect_code"), l
);
259 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HTTPAUTH_AVAIL
, &l
)) {
260 add_assoc_long_ex(&array
, "httpauth_avail", sizeof("httpauth_avail"), l
);
262 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PROXYAUTH_AVAIL
, &l
)) {
263 add_assoc_long_ex(&array
, "proxyauth_avail", sizeof("proxyauth_avail"), l
);
265 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_OS_ERRNO
, &l
)) {
266 add_assoc_long_ex(&array
, "os_errno", sizeof("os_errno"), l
);
268 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_NUM_CONNECTS
, &l
)) {
269 add_assoc_long_ex(&array
, "num_connects", sizeof("num_connects"), l
);
271 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SSL_ENGINES
, &s
)) {
272 MAKE_STD_ZVAL(subarray
);
273 array_init(subarray
);
274 for (p
= s
; p
; p
= p
->next
) {
276 add_next_index_string(subarray
, p
->data
, 1);
279 add_assoc_zval_ex(&array
, "ssl_engines", sizeof("ssl_engines"), subarray
);
280 curl_slist_free_all(s
);
282 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_COOKIELIST
, &s
)) {
283 MAKE_STD_ZVAL(subarray
);
284 array_init(subarray
);
285 for (p
= s
; p
; p
= p
->next
) {
287 add_next_index_string(subarray
, p
->data
, 1);
290 add_assoc_zval_ex(&array
, "cookies", sizeof("cookies"), subarray
);
291 curl_slist_free_all(s
);
293 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_URL
, &c
)) {
294 add_assoc_string_ex(&array
, "redirect_url", sizeof("redirect_url"), c
? c
: "", 1);
296 #if PHP_HTTP_CURL_VERSION(7,19,0)
297 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRIMARY_IP
, &c
)) {
298 add_assoc_string_ex(&array
, "primary_ip", sizeof("primary_ip"), c
? c
: "", 1);
301 #if PHP_HTTP_CURL_VERSION(7,19,0)
302 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_APPCONNECT_TIME
, &d
)) {
303 add_assoc_double_ex(&array
, "appconnect_time", sizeof("appconnect_time"), d
);
306 #if PHP_HTTP_CURL_VERSION(7,19,4)
307 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONDITION_UNMET
, &l
)) {
308 add_assoc_long_ex(&array
, "condition_unmet", sizeof("condition_unmet"), l
);
311 #if PHP_HTTP_CURL_VERSION(7,21,0)
312 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRIMARY_PORT
, &l
)) {
313 add_assoc_long_ex(&array
, "primary_port", sizeof("primary_port"), l
);
316 #if PHP_HTTP_CURL_VERSION(7,21,0)
317 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_LOCAL_IP
, &c
)) {
318 add_assoc_string_ex(&array
, "local_ip", sizeof("local_ip"), c
? c
: "", 1);
321 #if PHP_HTTP_CURL_VERSION(7,21,0)
322 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_LOCAL_PORT
, &l
)) {
323 add_assoc_long_ex(&array
, "local_port", sizeof("local_port"), l
);
329 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
333 struct curl_certinfo
*ci
;
334 char *colon
, *keyname
;
336 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CERTINFO
, &ci
)) {
337 MAKE_STD_ZVAL(ci_array
);
338 array_init(ci_array
);
340 for (i
= 0; i
< ci
->num_of_certs
; ++i
) {
343 MAKE_STD_ZVAL(subarray
);
344 array_init(subarray
);
345 for (p
= s
; p
; p
= p
->next
) {
347 if ((colon
= strchr(p
->data
, ':'))) {
348 keyname
= estrndup(p
->data
, colon
- p
->data
);
349 add_assoc_string_ex(subarray
, keyname
, colon
- p
->data
+ 1, colon
+ 1, 1);
352 add_next_index_string(subarray
, p
->data
, 1);
356 add_next_index_zval(ci_array
, subarray
);
358 add_assoc_zval_ex(&array
, "certinfo", sizeof("certinfo"), ci_array
);
362 add_assoc_string_ex(&array
, "error", sizeof("error"), get_storage(ch
)->errorbuffer
, 1);
367 /* curl client options */
369 static php_http_options_t php_http_curl_client_options
;
371 #define PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN 0x0001
372 #define PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR 0x0002
373 #define PHP_HTTP_CURL_CLIENT_OPTION_TRANSFORM_MS 0x0004
375 static STATUS
php_http_curl_client_option_set_ssl_verifyhost(php_http_option_t
*opt
, zval
*val
, void *userdata
)
377 php_http_client_t
*h
= userdata
;
378 php_http_curl_client_t
*curl
= h
->ctx
;
379 CURL
*ch
= curl
->handle
;
381 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_SSL_VERIFYHOST
, Z_BVAL_P(val
) ? 2 : 0)) {
387 static STATUS
php_http_curl_client_option_set_cookiestore(php_http_option_t
*opt
, zval
*val
, void *userdata
)
389 php_http_client_t
*h
= userdata
;
390 php_http_curl_client_t
*curl
= h
->ctx
;
391 CURL
*ch
= curl
->handle
;
394 php_http_curl_client_storage_t
*storage
= get_storage(curl
->handle
);
396 if (storage
->cookiestore
) {
397 pefree(storage
->cookiestore
, 1);
399 storage
->cookiestore
= pestrndup(Z_STRVAL_P(val
), Z_STRLEN_P(val
), 1);
400 if ( CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIEFILE
, storage
->cookiestore
)
401 || CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIEJAR
, storage
->cookiestore
)
409 static STATUS
php_http_curl_client_option_set_cookies(php_http_option_t
*opt
, zval
*val
, void *userdata
)
411 php_http_client_t
*h
= userdata
;
412 php_http_curl_client_t
*curl
= h
->ctx
;
413 CURL
*ch
= curl
->handle
;
414 TSRMLS_FETCH_FROM_CTX(h
->ts
);
416 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
417 if (curl
->options
.encode_cookies
) {
418 if (SUCCESS
== php_http_url_encode_hash_ex(HASH_OF(val
), &curl
->options
.cookies
, ZEND_STRL(";"), ZEND_STRL("="), NULL
, 0 TSRMLS_CC
)) {
419 php_http_buffer_fix(&curl
->options
.cookies
);
420 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIE
, curl
->options
.cookies
.data
)) {
428 php_http_array_hashkey_t cookie_key
= php_http_array_hashkey_init(0);
431 FOREACH_KEYVAL(pos
, val
, cookie_key
, cookie_val
) {
432 zval
*zv
= php_http_ztyp(IS_STRING
, *cookie_val
);
434 php_http_array_hashkey_stringify(&cookie_key
);
435 php_http_buffer_appendf(&curl
->options
.cookies
, "%s=%s; ", cookie_key
.str
, Z_STRVAL_P(zv
));
436 php_http_array_hashkey_stringfree(&cookie_key
);
441 php_http_buffer_fix(&curl
->options
.cookies
);
442 if (PHP_HTTP_BUFFER_LEN(&curl
->options
.cookies
)) {
443 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIE
, PHP_HTTP_BUFFER_VAL(&curl
->options
.cookies
))) {
452 static STATUS
php_http_curl_client_option_set_encodecookies(php_http_option_t
*opt
, zval
*val
, void *userdata
)
454 php_http_client_t
*h
= userdata
;
455 php_http_curl_client_t
*curl
= h
->ctx
;
457 curl
->options
.encode_cookies
= Z_BVAL_P(val
);
461 static STATUS
php_http_curl_client_option_set_lastmodified(php_http_option_t
*opt
, zval
*val
, void *userdata
)
463 php_http_client_t
*h
= userdata
;
464 php_http_curl_client_t
*curl
= h
->ctx
;
465 CURL
*ch
= curl
->handle
;
466 TSRMLS_FETCH_FROM_CTX(h
->ts
);
469 if (Z_LVAL_P(val
) > 0) {
470 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, Z_LVAL_P(val
))) {
474 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, (long) PHP_HTTP_G
->env
.request
.time
+ Z_LVAL_P(val
))) {
478 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, (long) (curl
->options
.range_request
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
))) {
482 if ( CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, 0)
483 || CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, 0)
491 static STATUS
php_http_curl_client_option_set_compress(php_http_option_t
*opt
, zval
*val
, void *userdata
)
493 php_http_client_t
*h
= userdata
;
494 php_http_curl_client_t
*curl
= h
->ctx
;
497 curl
->options
.headers
= curl_slist_append(curl
->options
.headers
, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
502 static STATUS
php_http_curl_client_option_set_etag(php_http_option_t
*opt
, zval
*val
, void *userdata
)
504 php_http_client_t
*h
= userdata
;
505 php_http_curl_client_t
*curl
= h
->ctx
;
506 php_http_buffer_t header
;
507 zend_bool is_quoted
= !((Z_STRVAL_P(val
)[0] != '"') || (Z_STRVAL_P(val
)[Z_STRLEN_P(val
)-1] != '"'));
509 php_http_buffer_init(&header
);
510 php_http_buffer_appendf(&header
, is_quoted
?"%s: %s":"%s: \"%s\"", curl
->options
.range_request
?"If-Match":"If-None-Match", Z_STRVAL_P(val
));
511 php_http_buffer_fix(&header
);
512 curl
->options
.headers
= curl_slist_append(curl
->options
.headers
, PHP_HTTP_BUFFER_VAL(&header
));
513 php_http_buffer_dtor(&header
);
517 static STATUS
php_http_curl_client_option_set_range(php_http_option_t
*opt
, zval
*val
, void *userdata
)
519 php_http_client_t
*h
= userdata
;
520 php_http_curl_client_t
*curl
= h
->ctx
;
521 CURL
*ch
= curl
->handle
;
522 TSRMLS_FETCH_FROM_CTX(h
->ts
);
524 php_http_buffer_reset(&curl
->options
.ranges
);
526 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
528 zval
**rr
, **rb
, **re
;
530 FOREACH_VAL(pos
, val
, rr
) {
531 if (Z_TYPE_PP(rr
) == IS_ARRAY
) {
532 if (2 == php_http_array_list(*rr TSRMLS_CC
, 2, &rb
, &re
)) {
533 if ( ((Z_TYPE_PP(rb
) == IS_LONG
) || ((Z_TYPE_PP(rb
) == IS_STRING
) && is_numeric_string(Z_STRVAL_PP(rb
), Z_STRLEN_PP(rb
), NULL
, NULL
, 1))) &&
534 ((Z_TYPE_PP(re
) == IS_LONG
) || ((Z_TYPE_PP(re
) == IS_STRING
) && is_numeric_string(Z_STRVAL_PP(re
), Z_STRLEN_PP(re
), NULL
, NULL
, 1)))) {
535 zval
*rbl
= php_http_ztyp(IS_LONG
, *rb
);
536 zval
*rel
= php_http_ztyp(IS_LONG
, *re
);
538 if ((Z_LVAL_P(rbl
) >= 0) && (Z_LVAL_P(rel
) >= 0)) {
539 php_http_buffer_appendf(&curl
->options
.ranges
, "%ld-%ld,", Z_LVAL_P(rbl
), Z_LVAL_P(rel
));
549 if (PHP_HTTP_BUFFER_LEN(&curl
->options
.ranges
)) {
550 curl
->options
.range_request
= 1;
551 /* ditch last comma */
552 PHP_HTTP_BUFFER_VAL(&curl
->options
.ranges
)[PHP_HTTP_BUFFER_LEN(&curl
->options
.ranges
)-- -1] = '\0';
556 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_RANGE
, PHP_HTTP_BUFFER_VAL(&curl
->options
.ranges
))) {
562 static STATUS
php_http_curl_client_option_set_resume(php_http_option_t
*opt
, zval
*val
, void *userdata
)
564 php_http_client_t
*h
= userdata
;
565 php_http_curl_client_t
*curl
= h
->ctx
;
566 CURL
*ch
= curl
->handle
;
568 if (Z_LVAL_P(val
) > 0) {
569 curl
->options
.range_request
= 1;
571 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_RESUME_FROM
, Z_LVAL_P(val
))) {
577 static STATUS
php_http_curl_client_option_set_retrydelay(php_http_option_t
*opt
, zval
*val
, void *userdata
)
579 php_http_client_t
*h
= userdata
;
580 php_http_curl_client_t
*curl
= h
->ctx
;
582 curl
->options
.retry
.delay
= Z_DVAL_P(val
);
586 static STATUS
php_http_curl_client_option_set_retrycount(php_http_option_t
*opt
, zval
*val
, void *userdata
)
588 php_http_client_t
*h
= userdata
;
589 php_http_curl_client_t
*curl
= h
->ctx
;
591 curl
->options
.retry
.count
= Z_LVAL_P(val
);
595 static STATUS
php_http_curl_client_option_set_redirect(php_http_option_t
*opt
, zval
*val
, void *userdata
)
597 php_http_client_t
*h
= userdata
;
598 php_http_curl_client_t
*curl
= h
->ctx
;
599 CURL
*ch
= curl
->handle
;
601 if ( CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, Z_LVAL_P(val
) ? 1L : 0L)
602 || CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_MAXREDIRS
, curl
->options
.redirects
= Z_LVAL_P(val
))
609 static STATUS
php_http_curl_client_option_set_portrange(php_http_option_t
*opt
, zval
*val
, void *userdata
)
611 php_http_client_t
*h
= userdata
;
612 php_http_curl_client_t
*curl
= h
->ctx
;
613 CURL
*ch
= curl
->handle
;
614 long localport
= 0, localportrange
= 0;
615 TSRMLS_FETCH_FROM_CTX(h
->ts
);
617 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
618 zval
**z_port_start
, *zps_copy
= NULL
, **z_port_end
, *zpe_copy
= NULL
;
620 switch (php_http_array_list(val TSRMLS_CC
, 2, &z_port_start
, &z_port_end
)) {
622 zps_copy
= php_http_ztyp(IS_LONG
, *z_port_start
);
623 zpe_copy
= php_http_ztyp(IS_LONG
, *z_port_end
);
624 localportrange
= labs(Z_LVAL_P(zps_copy
)-Z_LVAL_P(zpe_copy
))+1L;
628 zps_copy
= php_http_ztyp(IS_LONG
, *z_port_start
);
630 localport
= (zpe_copy
&& Z_LVAL_P(zpe_copy
) > 0) ? MIN(Z_LVAL_P(zps_copy
), Z_LVAL_P(zpe_copy
)) : Z_LVAL_P(zps_copy
);
631 zval_ptr_dtor(&zps_copy
);
633 zval_ptr_dtor(&zpe_copy
);
640 if ( CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_LOCALPORT
, localport
)
641 || CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_LOCALPORTRANGE
, localportrange
)
648 static STATUS
php_http_curl_client_option_set_resolve(php_http_option_t
*opt
, zval
*val
, void *userdata
)
650 php_http_client_t
*h
= userdata
;
651 php_http_curl_client_t
*curl
= h
->ctx
;
652 CURL
*ch
= curl
->handle
;
653 TSRMLS_FETCH_FROM_CTX(h
->ts
);
655 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
656 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
660 FOREACH_KEYVAL(pos
, val
, key
, data
) {
661 zval
*cpy
= php_http_ztyp(IS_STRING
, *data
);
662 curl
->options
.resolve
= curl_slist_append(curl
->options
.resolve
, Z_STRVAL_P(cpy
));
666 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_RESOLVE
, curl
->options
.resolve
)) {
670 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_RESOLVE
, NULL
)) {
677 static void php_http_curl_client_options_init(php_http_options_t
*registry TSRMLS_DC
)
679 php_http_option_t
*opt
;
682 if ((opt
= php_http_option_register(registry
, ZEND_STRL("proxyhost"), CURLOPT_PROXY
, IS_STRING
))) {
683 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN
;
685 php_http_option_register(registry
, ZEND_STRL("proxytype"), CURLOPT_PROXYTYPE
, IS_LONG
);
686 php_http_option_register(registry
, ZEND_STRL("proxyport"), CURLOPT_PROXYPORT
, IS_LONG
);
687 if ((opt
= php_http_option_register(registry
, ZEND_STRL("proxyauth"), CURLOPT_PROXYUSERPWD
, IS_STRING
))) {
688 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN
;
690 php_http_option_register(registry
, ZEND_STRL("proxyauthtype"), CURLOPT_PROXYAUTH
, IS_LONG
);
691 php_http_option_register(registry
, ZEND_STRL("proxytunnel"), CURLOPT_HTTPPROXYTUNNEL
, IS_BOOL
);
692 #if PHP_HTTP_CURL_VERSION(7,19,4)
693 php_http_option_register(registry
, ZEND_STRL("noproxy"), CURLOPT_NOPROXY
, IS_STRING
);
697 if ((opt
= php_http_option_register(registry
, ZEND_STRL("dns_cache_timeout"), CURLOPT_DNS_CACHE_TIMEOUT
, IS_LONG
))) {
698 Z_LVAL(opt
->defval
) = 60;
700 php_http_option_register(registry
, ZEND_STRL("ipresolve"), CURLOPT_IPRESOLVE
, IS_LONG
);
701 #if PHP_HTTP_CURL_VERSION(7,21,3)
702 if ((opt
= php_http_option_register(registry
, ZEND_STRL("resolve"), CURLOPT_RESOLVE
, IS_ARRAY
))) {
703 opt
->setter
= php_http_curl_client_option_set_resolve
;
706 #if PHP_HTTP_CURL_VERSION(7,24,0)
707 if ((opt
= php_http_option_register(registry
, ZEND_STRL("dns_servers"), CURLOPT_DNS_SERVERS
, IS_STRING
))) {
708 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN
;
713 php_http_option_register(registry
, ZEND_STRL("low_speed_limit"), CURLOPT_LOW_SPEED_LIMIT
, IS_LONG
);
714 php_http_option_register(registry
, ZEND_STRL("low_speed_time"), CURLOPT_LOW_SPEED_TIME
, IS_LONG
);
717 php_http_option_register(registry, ZEND_STRL("max_send_speed"), CURLOPT_MAX_SEND_SPEED_LARGE, IS_LONG);
718 php_http_option_register(registry, ZEND_STRL("max_recv_speed"), CURLOPT_MAX_RECV_SPEED_LARGE, IS_LONG);
721 /* connection handling */
723 if ((opt = php_http_option_register(registry, ZEND_STRL("maxconnects"), CURLOPT_MAXCONNECTS, IS_LONG))) {
724 Z_LVAL(opt->defval) = 5;
727 php_http_option_register(registry
, ZEND_STRL("fresh_connect"), CURLOPT_FRESH_CONNECT
, IS_BOOL
);
728 php_http_option_register(registry
, ZEND_STRL("forbid_reuse"), CURLOPT_FORBID_REUSE
, IS_BOOL
);
730 /* outgoing interface */
731 php_http_option_register(registry
, ZEND_STRL("interface"), CURLOPT_INTERFACE
, IS_STRING
);
732 if ((opt
= php_http_option_register(registry
, ZEND_STRL("portrange"), CURLOPT_LOCALPORT
, IS_ARRAY
))) {
733 opt
->setter
= php_http_curl_client_option_set_portrange
;
736 /* another endpoint port */
737 php_http_option_register(registry
, ZEND_STRL("port"), CURLOPT_PORT
, IS_LONG
);
739 /* RFC4007 zone_id */
740 #if PHP_HTTP_CURL_VERSION(7,19,0)
741 php_http_option_register(registry
, ZEND_STRL("address_scope"), CURLOPT_ADDRESS_SCOPE
, IS_LONG
);
745 if ((opt
= php_http_option_register(registry
, ZEND_STRL("httpauth"), CURLOPT_USERPWD
, IS_STRING
))) {
746 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN
;
748 php_http_option_register(registry
, ZEND_STRL("httpauthtype"), CURLOPT_HTTPAUTH
, IS_LONG
);
751 if ((opt
= php_http_option_register(registry
, ZEND_STRL("redirect"), CURLOPT_FOLLOWLOCATION
, IS_LONG
))) {
752 opt
->setter
= php_http_curl_client_option_set_redirect
;
754 php_http_option_register(registry
, ZEND_STRL("unrestrictedauth"), CURLOPT_UNRESTRICTED_AUTH
, IS_BOOL
);
755 #if PHP_HTTP_CURL_VERSION(7,19,1)
756 php_http_option_register(registry
, ZEND_STRL("postredir"), CURLOPT_POSTREDIR
, IS_BOOL
);
758 php_http_option_register(registry
, ZEND_STRL("postredir"), CURLOPT_POST301
, IS_BOOL
);
762 if ((opt
= php_http_option_register(registry
, ZEND_STRL("retrycount"), 0, IS_LONG
))) {
763 opt
->setter
= php_http_curl_client_option_set_retrycount
;
765 if ((opt
= php_http_option_register(registry
, ZEND_STRL("retrydelay"), 0, IS_DOUBLE
))) {
766 opt
->setter
= php_http_curl_client_option_set_retrydelay
;
770 if ((opt
= php_http_option_register(registry
, ZEND_STRL("referer"), CURLOPT_REFERER
, IS_STRING
))) {
771 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN
;
773 if ((opt
= php_http_option_register(registry
, ZEND_STRL("autoreferer"), CURLOPT_AUTOREFERER
, IS_BOOL
))) {
774 ZVAL_BOOL(&opt
->defval
, 1);
778 if ((opt
= php_http_option_register(registry
, ZEND_STRL("useragent"), CURLOPT_USERAGENT
, IS_STRING
))) {
779 /* don't check strlen, to allow sending no useragent at all */
780 ZVAL_STRING(&opt
->defval
, "PECL::HTTP/" PHP_HTTP_EXT_VERSION
" (PHP/" PHP_VERSION
")", 0);
784 if ((opt
= php_http_option_register(registry
, ZEND_STRL("resume"), CURLOPT_RESUME_FROM
, IS_LONG
))) {
785 opt
->setter
= php_http_curl_client_option_set_resume
;
788 if ((opt
= php_http_option_register(registry
, ZEND_STRL("range"), CURLOPT_RANGE
, IS_ARRAY
))) {
789 opt
->setter
= php_http_curl_client_option_set_range
;
793 if ((opt
= php_http_option_register(registry
, ZEND_STRL("etag"), 0, IS_STRING
))) {
794 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN
;
795 opt
->setter
= php_http_curl_client_option_set_etag
;
799 if ((opt
= php_http_option_register(registry
, ZEND_STRL("compress"), 0, IS_BOOL
))) {
800 opt
->setter
= php_http_curl_client_option_set_compress
;
804 if ((opt
= php_http_option_register(registry
, ZEND_STRL("lastmodified"), 0, IS_LONG
))) {
805 opt
->setter
= php_http_curl_client_option_set_lastmodified
;
809 if ((opt
= php_http_option_register(registry
, ZEND_STRL("encodecookies"), 0, IS_BOOL
))) {
810 opt
->setter
= php_http_curl_client_option_set_encodecookies
;
811 ZVAL_BOOL(&opt
->defval
, 1);
813 if ((opt
= php_http_option_register(registry
, ZEND_STRL("cookies"), 0, IS_ARRAY
))) {
814 opt
->setter
= php_http_curl_client_option_set_cookies
;
817 /* cookiesession, don't load session cookies from cookiestore */
818 php_http_option_register(registry
, ZEND_STRL("cookiesession"), CURLOPT_COOKIESESSION
, IS_BOOL
);
819 /* cookiestore, read initial cookies from that file and store cookies back into that file */
820 if ((opt
= php_http_option_register(registry
, ZEND_STRL("cookiestore"), 0, IS_STRING
))) {
821 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN
;
822 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR
;
823 opt
->setter
= php_http_curl_client_option_set_cookiestore
;
827 php_http_option_register(registry
, ZEND_STRL("maxfilesize"), CURLOPT_MAXFILESIZE
, IS_LONG
);
829 /* http protocol version */
830 php_http_option_register(registry
, ZEND_STRL("protocol"), CURLOPT_HTTP_VERSION
, IS_LONG
);
833 if ((opt
= php_http_option_register(registry
, ZEND_STRL("timeout"), CURLOPT_TIMEOUT_MS
, IS_DOUBLE
))) {
834 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_TRANSFORM_MS
;
836 if ((opt
= php_http_option_register(registry
, ZEND_STRL("connecttimeout"), CURLOPT_CONNECTTIMEOUT_MS
, IS_DOUBLE
))) {
837 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_TRANSFORM_MS
;
838 Z_DVAL(opt
->defval
) = 3;
842 #if PHP_HTTP_CURL_VERSION(7,25,0)
843 php_http_option_register(registry
, ZEND_STRL("tcp_keepalive"), CURLOPT_TCP_KEEPALIVE
, IS_BOOL
);
844 if ((opt
= php_http_option_register(registry
, ZEND_STRL("tcp_keepidle"), CURLOPT_TCP_KEEPIDLE
, IS_LONG
))) {
845 Z_LVAL(opt
->defval
) = 60;
847 if ((opt
= php_http_option_register(registry
, ZEND_STRL("tcp_keepintvl"), CURLOPT_TCP_KEEPINTVL
, IS_LONG
))) {
848 Z_LVAL(opt
->defval
) = 60;
853 if ((opt
= php_http_option_register(registry
, ZEND_STRL("ssl"), 0, IS_ARRAY
))) {
854 registry
= &opt
->suboptions
;
856 if ((opt
= php_http_option_register(registry
, ZEND_STRL("cert"), CURLOPT_SSLCERT
, IS_STRING
))) {
857 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN
;
858 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR
;
860 php_http_option_register(registry
, ZEND_STRL("certtype"), CURLOPT_SSLCERTTYPE
, IS_STRING
);
861 php_http_option_register(registry
, ZEND_STRL("certpasswd"), CURLOPT_SSLCERTPASSWD
, IS_STRING
);
863 if ((opt
= php_http_option_register(registry
, ZEND_STRL("key"), CURLOPT_SSLKEY
, IS_STRING
))) {
864 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN
;
865 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR
;
867 php_http_option_register(registry
, ZEND_STRL("keytype"), CURLOPT_SSLKEYTYPE
, IS_STRING
);
868 php_http_option_register(registry
, ZEND_STRL("keypasswd"), CURLOPT_SSLKEYPASSWD
, IS_STRING
);
869 php_http_option_register(registry
, ZEND_STRL("engine"), CURLOPT_SSLENGINE
, IS_STRING
);
870 php_http_option_register(registry
, ZEND_STRL("version"), CURLOPT_SSLVERSION
, IS_LONG
);
871 if ((opt
= php_http_option_register(registry
, ZEND_STRL("verifypeer"), CURLOPT_SSL_VERIFYPEER
, IS_BOOL
))) {
872 ZVAL_BOOL(&opt
->defval
, 1);
874 if ((opt
= php_http_option_register(registry
, ZEND_STRL("verifyhost"), CURLOPT_SSL_VERIFYHOST
, IS_BOOL
))) {
875 ZVAL_BOOL(&opt
->defval
, 1);
876 opt
->setter
= php_http_curl_client_option_set_ssl_verifyhost
;
878 php_http_option_register(registry
, ZEND_STRL("cipher_list"), CURLOPT_SSL_CIPHER_LIST
, IS_STRING
);
879 if ((opt
= php_http_option_register(registry
, ZEND_STRL("cainfo"), CURLOPT_CAINFO
, IS_STRING
))) {
880 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN
;
881 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR
;
882 #ifdef PHP_HTTP_CURL_CAINFO
883 ZVAL_STRING(&opt
->defval
, PHP_HTTP_CURL_CAINFO
, 0);
886 if ((opt
= php_http_option_register(registry
, ZEND_STRL("capath"), CURLOPT_CAPATH
, IS_STRING
))) {
887 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN
;
888 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR
;
890 if ((opt
= php_http_option_register(registry
, ZEND_STRL("random_file"), CURLOPT_RANDOM_FILE
, IS_STRING
))) {
891 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN
;
892 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR
;
894 if ((opt
= php_http_option_register(registry
, ZEND_STRL("egdsocket"), CURLOPT_EGDSOCKET
, IS_STRING
))) {
895 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN
;
896 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR
;
898 #if PHP_HTTP_CURL_VERSION(7,19,0)
899 if ((opt
= php_http_option_register(registry
, ZEND_STRL("issuercert"), CURLOPT_ISSUERCERT
, IS_STRING
))) {
900 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN
;
901 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR
;
903 # ifdef PHP_HTTP_HAVE_OPENSSL
904 if ((opt
= php_http_option_register(registry
, ZEND_STRL("crlfile"), CURLOPT_CRLFILE
, IS_STRING
))) {
905 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN
;
906 opt
->flags
|= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR
;
910 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
911 php_http_option_register(registry
, ZEND_STRL("certinfo"), CURLOPT_CERTINFO
, IS_BOOL
);
916 static zval
*php_http_curl_client_get_option(php_http_option_t
*opt
, HashTable
*options
, void *userdata
)
918 php_http_client_t
*h
= userdata
;
919 php_http_curl_client_t
*curl
= h
->ctx
;
922 if ((option
= php_http_option_get(opt
, options
, NULL
))) {
923 option
= php_http_ztyp(opt
->type
, option
);
924 zend_hash_quick_update(&curl
->options
.cache
, opt
->name
.s
, opt
->name
.l
, opt
->name
.h
, &option
, sizeof(zval
*), NULL
);
929 static STATUS
php_http_curl_client_set_option(php_http_option_t
*opt
, zval
*val
, void *userdata
)
931 php_http_client_t
*h
= userdata
;
932 php_http_curl_client_t
*curl
= h
->ctx
;
933 CURL
*ch
= curl
->handle
;
936 TSRMLS_FETCH_FROM_CTX(h
->ts
);
945 rv
= opt
->setter(opt
, val
, h
);
946 } else if (CURLE_OK
!= curl_easy_setopt(ch
, opt
->option
, (long) Z_BVAL_P(val
))) {
953 rv
= opt
->setter(opt
, val
, h
);
954 } else if (CURLE_OK
!= curl_easy_setopt(ch
, opt
->option
, Z_LVAL_P(val
))) {
960 if (!(opt
->flags
& PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN
) || Z_STRLEN_P(val
)) {
961 if (!(opt
->flags
& PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR
) || !Z_STRVAL_P(val
) || SUCCESS
== php_check_open_basedir(Z_STRVAL_P(val
) TSRMLS_CC
)) {
963 rv
= opt
->setter(opt
, val
, h
);
964 } else if (CURLE_OK
!= curl_easy_setopt(ch
, opt
->option
, Z_STRVAL_P(val
))) {
972 if (opt
->flags
& PHP_HTTP_CURL_CLIENT_OPTION_TRANSFORM_MS
) {
978 rv
= opt
->setter(opt
, val
, h
);
979 } else if (CURLE_OK
!= curl_easy_setopt(ch
, opt
->option
, (long) Z_DVAL_P(val
))) {
986 rv
= opt
->setter(opt
, val
, h
);
987 } else if (Z_TYPE_P(val
) != IS_NULL
) {
988 rv
= php_http_options_apply(&opt
->suboptions
, Z_ARRVAL_P(val
), h
);
994 rv
= opt
->setter(opt
, val
, h
);
1000 if (rv
!= SUCCESS
) {
1001 php_http_error(HE_NOTICE
, PHP_HTTP_E_CLIENT
, "Could not set option %s", opt
->name
.s
);
1008 static STATUS
php_http_curl_client_reset(php_http_client_t
*h
);
1010 static php_http_client_t
*php_http_curl_client_init(php_http_client_t
*h
, void *handle
)
1012 php_http_curl_client_t
*ctx
;
1013 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1015 if (!handle
&& !(handle
= php_http_resource_factory_handle_ctor(h
->rf TSRMLS_CC
))) {
1016 php_http_error(HE_WARNING
, PHP_HTTP_E_CLIENT
, "could not initialize curl handle");
1020 ctx
= ecalloc(1, sizeof(*ctx
));
1021 ctx
->handle
= handle
;
1022 php_http_buffer_init(&ctx
->options
.cookies
);
1023 php_http_buffer_init(&ctx
->options
.ranges
);
1024 zend_hash_init(&ctx
->options
.cache
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
1028 curl_easy_setopt(handle
, CURLOPT_NOSIGNAL
, 1L);
1030 curl_easy_setopt(handle
, CURLOPT_HEADER
, 0L);
1031 curl_easy_setopt(handle
, CURLOPT_FILETIME
, 1L);
1032 curl_easy_setopt(handle
, CURLOPT_AUTOREFERER
, 1L);
1033 curl_easy_setopt(handle
, CURLOPT_VERBOSE
, 1L);
1034 curl_easy_setopt(handle
, CURLOPT_NOPROGRESS
, 0L);
1035 curl_easy_setopt(handle
, CURLOPT_HEADERFUNCTION
, NULL
);
1036 curl_easy_setopt(handle
, CURLOPT_WRITEFUNCTION
, php_http_curl_client_dummy_callback
);
1037 curl_easy_setopt(handle
, CURLOPT_DEBUGFUNCTION
, php_http_curl_client_raw_callback
);
1038 curl_easy_setopt(handle
, CURLOPT_READFUNCTION
, php_http_curl_client_read_callback
);
1039 curl_easy_setopt(handle
, CURLOPT_IOCTLFUNCTION
, php_http_curl_client_ioctl_callback
);
1040 curl_easy_setopt(handle
, CURLOPT_PROGRESSFUNCTION
, php_http_curl_client_progress_callback
);
1041 curl_easy_setopt(handle
, CURLOPT_DEBUGDATA
, h
);
1042 curl_easy_setopt(handle
, CURLOPT_PROGRESSDATA
, h
);
1044 php_http_curl_client_reset(h
);
1049 static php_http_client_t
*php_http_curl_client_copy(php_http_client_t
*from
, php_http_client_t
*to
)
1051 php_http_curl_client_t
*ctx
= from
->ctx
;
1053 TSRMLS_FETCH_FROM_CTX(from
->ts
);
1055 if (!(copy
= php_http_resource_factory_handle_copy(from
->rf
, ctx
->handle TSRMLS_CC
))) {
1060 return php_http_curl_client_init(to
, copy
);
1062 return php_http_client_init(NULL
, from
->ops
, from
->rf
, copy TSRMLS_CC
);
1066 static void php_http_curl_client_dtor(php_http_client_t
*h
)
1068 php_http_curl_client_t
*ctx
= h
->ctx
;
1069 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1071 curl_easy_setopt(ctx
->handle
, CURLOPT_NOPROGRESS
, 1L);
1072 curl_easy_setopt(ctx
->handle
, CURLOPT_PROGRESSFUNCTION
, NULL
);
1073 curl_easy_setopt(ctx
->handle
, CURLOPT_VERBOSE
, 0L);
1074 curl_easy_setopt(ctx
->handle
, CURLOPT_DEBUGFUNCTION
, NULL
);
1076 php_http_resource_factory_handle_dtor(h
->rf
, ctx
->handle TSRMLS_CC
);
1078 php_http_buffer_dtor(&ctx
->options
.ranges
);
1079 php_http_buffer_dtor(&ctx
->options
.cookies
);
1080 zend_hash_destroy(&ctx
->options
.cache
);
1082 if (ctx
->options
.headers
) {
1083 curl_slist_free_all(ctx
->options
.headers
);
1084 ctx
->options
.headers
= NULL
;
1086 php_http_client_progress_dtor(&ctx
->progress TSRMLS_CC
);
1092 static STATUS
php_http_curl_client_reset(php_http_client_t
*h
)
1094 php_http_curl_client_t
*curl
= h
->ctx
;
1095 CURL
*ch
= curl
->handle
;
1096 php_http_curl_client_storage_t
*st
;
1098 if ((st
= get_storage(ch
))) {
1103 if (st
->cookiestore
) {
1104 pefree(st
->cookiestore
, 1);
1105 st
->cookiestore
= NULL
;
1107 st
->errorbuffer
[0] = '\0';
1110 curl_easy_setopt(ch
, CURLOPT_URL
, NULL
);
1111 /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
1112 #if PHP_HTTP_CURL_VERSION(7,19,1)
1113 curl_easy_setopt(ch
, CURLOPT_PROXYUSERNAME
, NULL
);
1114 curl_easy_setopt(ch
, CURLOPT_PROXYPASSWORD
, NULL
);
1115 curl_easy_setopt(ch
, CURLOPT_USERNAME
, NULL
);
1116 curl_easy_setopt(ch
, CURLOPT_PASSWORD
, NULL
);
1119 #if PHP_HTTP_CURL_VERSION(7,21,3)
1120 if (curl
->options
.resolve
) {
1121 curl_slist_free_all(curl
->options
.resolve
);
1122 curl
->options
.resolve
= NULL
;
1125 curl
->options
.retry
.count
= 0;
1126 curl
->options
.retry
.delay
= 0;
1127 curl
->options
.redirects
= 0;
1128 curl
->options
.encode_cookies
= 1;
1130 if (curl
->options
.headers
) {
1131 curl_slist_free_all(curl
->options
.headers
);
1132 curl
->options
.headers
= NULL
;
1135 php_http_buffer_reset(&curl
->options
.cookies
);
1136 php_http_buffer_reset(&curl
->options
.ranges
);
1141 PHP_HTTP_API STATUS
php_http_curl_client_prepare(php_http_client_t
*h
, php_http_message_t
*msg
)
1144 php_http_curl_client_t
*curl
= h
->ctx
;
1145 php_http_curl_client_storage_t
*storage
= get_storage(curl
->handle
);
1146 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1149 if (!PHP_HTTP_INFO(msg
).request
.url
) {
1150 php_http_error(HE_WARNING
, PHP_HTTP_E_CLIENT
, "Cannot request empty URL");
1153 storage
->errorbuffer
[0] = '\0';
1155 pefree(storage
->url
, 1);
1157 storage
->url
= pestrdup(PHP_HTTP_INFO(msg
).request
.url
, 1);
1158 curl_easy_setopt(curl
->handle
, CURLOPT_URL
, storage
->url
);
1160 /* request method */
1161 switch (php_http_select_str(PHP_HTTP_INFO(msg
).request
.method
, 4, "GET", "HEAD", "POST", "PUT")) {
1163 curl_easy_setopt(curl
->handle
, CURLOPT_HTTPGET
, 1L);
1167 curl_easy_setopt(curl
->handle
, CURLOPT_NOBODY
, 1L);
1171 curl_easy_setopt(curl
->handle
, CURLOPT_POST
, 1L);
1175 curl_easy_setopt(curl
->handle
, CURLOPT_UPLOAD
, 1L);
1179 if (PHP_HTTP_INFO(msg
).request
.method
) {
1180 curl_easy_setopt(curl
->handle
, CURLOPT_CUSTOMREQUEST
, PHP_HTTP_INFO(msg
).request
.method
);
1182 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_METHOD
, "Cannot use empty request method");
1189 /* request headers */
1190 php_http_message_update_headers(msg
);
1191 if (zend_hash_num_elements(&msg
->hdrs
)) {
1192 php_http_array_hashkey_t header_key
= php_http_array_hashkey_init(0);
1195 php_http_buffer_t header
;
1197 php_http_buffer_init(&header
);
1198 FOREACH_HASH_KEYVAL(pos
, &msg
->hdrs
, header_key
, header_val
) {
1199 if (header_key
.type
== HASH_KEY_IS_STRING
) {
1200 zval
*header_cpy
= php_http_ztyp(IS_STRING
, *header_val
);
1202 php_http_buffer_appendf(&header
, "%s: %s", header_key
.str
, Z_STRVAL_P(header_cpy
));
1203 php_http_buffer_fix(&header
);
1204 curl
->options
.headers
= curl_slist_append(curl
->options
.headers
, PHP_HTTP_BUFFER_VAL(&header
));
1205 php_http_buffer_reset(&header
);
1207 zval_ptr_dtor(&header_cpy
);
1210 php_http_buffer_dtor(&header
);
1211 curl_easy_setopt(curl
->handle
, CURLOPT_HTTPHEADER
, curl
->options
.headers
);
1214 /* attach request body */
1215 if ((body_size
= php_http_message_body_size(&msg
->body
))) {
1216 /* RFC2616, section 4.3 (para. 4) states that »a message-body MUST NOT be included in a request if the
1217 * specification of the request method (section 5.1.1) does not allow sending an entity-body in request.«
1218 * Following the clause in section 5.1.1 (para. 2) that request methods »MUST be implemented with the
1219 * same semantics as those specified in section 9« reveal that not any single defined HTTP/1.1 method
1220 * does not allow a request body.
1222 php_stream_rewind(php_http_message_body_stream(&msg
->body
));
1223 curl_easy_setopt(curl
->handle
, CURLOPT_IOCTLDATA
, &msg
->body
);
1224 curl_easy_setopt(curl
->handle
, CURLOPT_READDATA
, &msg
->body
);
1225 curl_easy_setopt(curl
->handle
, CURLOPT_INFILESIZE
, body_size
);
1226 curl_easy_setopt(curl
->handle
, CURLOPT_POSTFIELDSIZE
, body_size
);
1232 static STATUS
php_http_curl_client_exec(php_http_client_t
*h
, php_http_message_t
*msg
)
1236 php_http_curl_client_t
*curl
= h
->ctx
;
1237 php_http_curl_client_storage_t
*storage
= get_storage(curl
->handle
);
1238 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1240 if (SUCCESS
!= php_http_curl_client_prepare(h
, msg
)) {
1245 if (CURLE_OK
!= (result
= curl_easy_perform(curl
->handle
))) {
1246 php_http_error(HE_WARNING
, PHP_HTTP_E_CLIENT
, "%s; %s (%s)", curl_easy_strerror(result
), storage
->errorbuffer
, storage
->url
);
1248 if (EG(exception
)) {
1249 add_property_long(EG(exception
), "curlCode", result
);
1252 if (curl
->options
.retry
.count
> tries
++) {
1254 case CURLE_COULDNT_RESOLVE_PROXY
:
1255 case CURLE_COULDNT_RESOLVE_HOST
:
1256 case CURLE_COULDNT_CONNECT
:
1257 case CURLE_WRITE_ERROR
:
1258 case CURLE_READ_ERROR
:
1259 case CURLE_OPERATION_TIMEDOUT
:
1260 case CURLE_SSL_CONNECT_ERROR
:
1261 case CURLE_GOT_NOTHING
:
1262 case CURLE_SSL_ENGINE_SETFAILED
:
1263 case CURLE_SEND_ERROR
:
1264 case CURLE_RECV_ERROR
:
1265 case CURLE_SSL_ENGINE_INITFAILED
:
1266 case CURLE_LOGIN_DENIED
:
1267 if (curl
->options
.retry
.delay
>= PHP_HTTP_DIFFSEC
) {
1268 php_http_sleep(curl
->options
.retry
.delay
);
1282 static STATUS
php_http_curl_client_setopt(php_http_client_t
*h
, php_http_client_setopt_opt_t opt
, void *arg
)
1284 php_http_curl_client_t
*curl
= h
->ctx
;
1285 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1288 case PHP_HTTP_CLIENT_OPT_SETTINGS
:
1289 return php_http_options_apply(&php_http_curl_client_options
, arg
, h
);
1292 case PHP_HTTP_CLIENT_OPT_PROGRESS_CALLBACK
:
1293 if (curl
->progress
.in_cb
) {
1294 php_http_error(HE_WARNING
, PHP_HTTP_E_CLIENT
, "Cannot change progress callback while executing it");
1297 if (curl
->progress
.callback
) {
1298 php_http_client_progress_dtor(&curl
->progress TSRMLS_CC
);
1300 curl
->progress
.callback
= arg
;
1303 case PHP_HTTP_CLIENT_OPT_COOKIES_ENABLE
:
1304 /* are cookies already enabled anyway? */
1305 if (!get_storage(curl
->handle
)->cookiestore
) {
1306 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_COOKIEFILE
, "")) {
1312 case PHP_HTTP_CLIENT_OPT_COOKIES_RESET
:
1313 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_COOKIELIST
, "ALL")) {
1318 case PHP_HTTP_CLIENT_OPT_COOKIES_RESET_SESSION
:
1319 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_COOKIELIST
, "SESS")) {
1324 case PHP_HTTP_CLIENT_OPT_COOKIES_FLUSH
:
1325 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_COOKIELIST
, "FLUSH")) {
1337 static STATUS
php_http_curl_client_getopt(php_http_client_t
*h
, php_http_client_getopt_opt_t opt
, void *arg
)
1339 php_http_curl_client_t
*curl
= h
->ctx
;
1342 case PHP_HTTP_CLIENT_OPT_PROGRESS_INFO
:
1343 *((php_http_client_progress_t
**) arg
) = &curl
->progress
;
1346 case PHP_HTTP_CLIENT_OPT_TRANSFER_INFO
:
1347 get_info(curl
->handle
, arg
);
1357 static php_http_resource_factory_ops_t php_http_curl_client_resource_factory_ops
= {
1363 static php_http_client_ops_t php_http_curl_client_ops
= {
1364 &php_http_curl_client_resource_factory_ops
,
1365 php_http_curl_client_init
,
1366 php_http_curl_client_copy
,
1367 php_http_curl_client_dtor
,
1368 php_http_curl_client_reset
,
1369 php_http_curl_client_exec
,
1370 php_http_curl_client_setopt
,
1371 php_http_curl_client_getopt
,
1372 (php_http_new_t
) php_http_curl_client_object_new_ex
,
1373 php_http_curl_client_get_class_entry
1376 PHP_HTTP_API php_http_client_ops_t
*php_http_curl_client_get_ops(void)
1378 return &php_http_curl_client_ops
;
1382 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpClientCURL, method, 0, req_args)
1383 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpClientCURL, method, 0)
1384 #define PHP_HTTP_CURL_CLIENT_ME(method, visibility) PHP_ME(HttpClientCURL, method, PHP_HTTP_ARGS(HttpClientCURL, method), visibility)
1385 #define PHP_HTTP_CURL_CLIENT_CLIENT_MALIAS(me, vis) ZEND_FENTRY(me, ZEND_MN(HttpClient_##me), PHP_HTTP_ARGS(HttpClientCURL, me), vis)
1387 PHP_HTTP_BEGIN_ARGS(send
, 0)
1388 PHP_HTTP_ARG_VAL(request
, 0)
1391 static zend_class_entry
*php_http_curl_client_class_entry
;
1393 zend_class_entry
*php_http_curl_client_get_class_entry(void)
1395 return php_http_curl_client_class_entry
;
1398 static zend_function_entry php_http_curl_client_method_entry
[] = {
1399 PHP_HTTP_CURL_CLIENT_CLIENT_MALIAS(send
, ZEND_ACC_PUBLIC
)
1400 EMPTY_FUNCTION_ENTRY
1403 zend_object_value
php_http_curl_client_object_new(zend_class_entry
*ce TSRMLS_DC
)
1405 return php_http_curl_client_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
1408 zend_object_value
php_http_curl_client_object_new_ex(zend_class_entry
*ce
, php_http_client_t
*r
, php_http_client_object_t
**ptr TSRMLS_DC
)
1410 zend_object_value ov
;
1411 php_http_client_object_t
*o
;
1413 o
= ecalloc(1, sizeof(php_http_client_object_t
));
1414 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
1415 object_properties_init((zend_object
*) o
, ce
);
1417 if (!(o
->client
= r
)) {
1418 o
->client
= php_http_client_init(NULL
, &php_http_curl_client_ops
, NULL
, NULL TSRMLS_CC
);
1425 ov
.handle
= zend_objects_store_put(o
, NULL
, php_http_client_object_free
, NULL TSRMLS_CC
);
1426 ov
.handlers
= php_http_client_get_object_handlers();
1432 PHP_MINIT_FUNCTION(http_curl_client
)
1434 php_http_options_t
*options
;
1436 if (SUCCESS
!= php_http_persistent_handle_provide(ZEND_STRL("http_client.curl"), &php_http_curl_client_resource_factory_ops
, NULL
, NULL
)) {
1440 if ((options
= php_http_options_init(&php_http_curl_client_options
, 1))) {
1441 options
->getter
= php_http_curl_client_get_option
;
1442 options
->setter
= php_http_curl_client_set_option
;
1444 php_http_curl_client_options_init(options TSRMLS_CC
);
1447 PHP_HTTP_REGISTER_CLASS(http
\\Curl
, Client
, http_curl_client
, php_http_client_get_class_entry(), 0);
1448 php_http_curl_client_class_entry
->create_object
= php_http_curl_client_object_new
;
1451 * HTTP Protocol Version Constants
1453 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("HTTP_VERSION_1_0"), CURL_HTTP_VERSION_1_0 TSRMLS_CC
);
1454 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("HTTP_VERSION_1_1"), CURL_HTTP_VERSION_1_1 TSRMLS_CC
);
1455 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("HTTP_VERSION_NONE"), CURL_HTTP_VERSION_NONE TSRMLS_CC
); /* to be removed */
1456 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("HTTP_VERSION_ANY"), CURL_HTTP_VERSION_NONE TSRMLS_CC
);
1459 * SSL Version Constants
1461 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("SSL_VERSION_TLSv1"), CURL_SSLVERSION_TLSv1 TSRMLS_CC
);
1462 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("SSL_VERSION_SSLv2"), CURL_SSLVERSION_SSLv2 TSRMLS_CC
);
1463 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("SSL_VERSION_SSLv3"), CURL_SSLVERSION_SSLv3 TSRMLS_CC
);
1464 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("SSL_VERSION_ANY"), CURL_SSLVERSION_DEFAULT TSRMLS_CC
);
1467 * DNS IPvX resolving
1469 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("IPRESOLVE_V4"), CURL_IPRESOLVE_V4 TSRMLS_CC
);
1470 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("IPRESOLVE_V6"), CURL_IPRESOLVE_V6 TSRMLS_CC
);
1471 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("IPRESOLVE_ANY"), CURL_IPRESOLVE_WHATEVER TSRMLS_CC
);
1476 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("AUTH_BASIC"), CURLAUTH_BASIC TSRMLS_CC
);
1477 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("AUTH_DIGEST"), CURLAUTH_DIGEST TSRMLS_CC
);
1478 #if PHP_HTTP_CURL_VERSION(7,19,3)
1479 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("AUTH_DIGEST_IE"), CURLAUTH_DIGEST_IE TSRMLS_CC
);
1481 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("AUTH_NTLM"), CURLAUTH_NTLM TSRMLS_CC
);
1482 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("AUTH_GSSNEG"), CURLAUTH_GSSNEGOTIATE TSRMLS_CC
);
1483 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("AUTH_ANY"), CURLAUTH_ANY TSRMLS_CC
);
1486 * Proxy Type Constants
1488 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("PROXY_SOCKS4"), CURLPROXY_SOCKS4 TSRMLS_CC
);
1489 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("PROXY_SOCKS4A"), CURLPROXY_SOCKS5 TSRMLS_CC
);
1490 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("PROXY_SOCKS5_HOSTNAME"), CURLPROXY_SOCKS5 TSRMLS_CC
);
1491 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("PROXY_SOCKS5"), CURLPROXY_SOCKS5 TSRMLS_CC
);
1492 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("PROXY_HTTP"), CURLPROXY_HTTP TSRMLS_CC
);
1493 # if PHP_HTTP_CURL_VERSION(7,19,4)
1494 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("PROXY_HTTP_1_0"), CURLPROXY_HTTP_1_0 TSRMLS_CC
);
1498 * Post Redirection Constants
1500 #if PHP_HTTP_CURL_VERSION(7,19,1)
1501 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("POSTREDIR_301"), CURL_REDIR_POST_301 TSRMLS_CC
);
1502 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("POSTREDIR_302"), CURL_REDIR_POST_302 TSRMLS_CC
);
1503 zend_declare_class_constant_long(php_http_curl_client_class_entry
, ZEND_STRL("POSTREDIR_ALL"), CURL_REDIR_POST_ALL TSRMLS_CC
);
1509 PHP_MSHUTDOWN_FUNCTION(http_curl_client
)
1511 php_http_options_dtor(&php_http_curl_client_options
);
1520 * vim600: noet sw=4 ts=4 fdm=marker
1521 * vim<600: noet sw=4 ts=4