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_http_api.h"
15 #define PHP_HTTP_CURL_OPT_STRING(OPTION, ldiff, obdc) \
18 PHP_HTTP_CURL_OPT_STRING_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION, obdc); \
20 #define PHP_HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \
21 if (!strcasecmp(key.str, keyname)) { \
22 zval *copy = cache_option(&curl->options.cache, keyname, strlen(keyname)+1, 0, php_http_ztyp(IS_STRING, *param)); \
24 if (SUCCESS != php_check_open_basedir(Z_STRVAL_P(copy) TSRMLS_CC)) { \
28 curl_easy_setopt(ch, optname, Z_STRVAL_P(copy)); \
29 zval_ptr_dtor(©); \
32 #define PHP_HTTP_CURL_OPT_LONG(OPTION, ldiff) \
35 PHP_HTTP_CURL_OPT_LONG_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION); \
37 #define PHP_HTTP_CURL_OPT_LONG_EX(keyname, optname) \
38 if (!strcasecmp(key.str, keyname)) { \
39 zval *copy = php_http_ztyp(IS_LONG, *param); \
40 curl_easy_setopt(ch, optname, Z_LVAL_P(copy)); \
41 zval_ptr_dtor(©); \
46 /* resource_factory ops */
48 static void *php_http_curl_ctor(void *opaque TSRMLS_DC
)
52 if ((ch
= curl_easy_init())) {
59 static void *php_http_curl_copy(void *opaque
, void *handle TSRMLS_DC
)
63 if ((ch
= curl_easy_duphandle(handle
))) {
71 static void php_http_curl_dtor(void *opaque
, void *handle TSRMLS_DC
)
73 php_http_client_curl_storage_t
*st
= get_storage(handle
);
75 curl_easy_cleanup(handle
);
81 if (st
->cookiestore
) {
82 pefree(st
->cookiestore
, 1);
90 static size_t php_http_client_curl_read_callback(void *data
, size_t len
, size_t n
, void *ctx
)
92 php_http_message_body_t
*body
= ctx
;
95 TSRMLS_FETCH_FROM_CTX(body
->ts
);
96 return php_stream_read(php_http_message_body_stream(body
), data
, len
* n
);
101 static int php_http_client_curl_progress_callback(void *ctx
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
103 php_http_client_t
*h
= ctx
;
104 php_http_client_curl_t
*curl
= h
->ctx
;
105 TSRMLS_FETCH_FROM_CTX(h
->ts
);
107 curl
->progress
.state
.dl
.total
= dltotal
;
108 curl
->progress
.state
.dl
.now
= dlnow
;
109 curl
->progress
.state
.ul
.total
= ultotal
;
110 curl
->progress
.state
.ul
.now
= ulnow
;
112 php_http_client_progress_notify(&curl
->progress TSRMLS_CC
);
117 static curlioerr
php_http_client_curl_ioctl_callback(CURL
*ch
, curliocmd cmd
, void *ctx
)
119 php_http_message_body_t
*body
= ctx
;
121 if (cmd
!= CURLIOCMD_RESTARTREAD
) {
122 return CURLIOE_UNKNOWNCMD
;
126 TSRMLS_FETCH_FROM_CTX(body
->ts
);
128 if (SUCCESS
== php_stream_rewind(php_http_message_body_stream(body
))) {
133 return CURLIOE_FAILRESTART
;
136 static int php_http_client_curl_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
138 php_http_client_t
*h
= ctx
;
139 php_http_client_curl_t
*curl
= h
->ctx
;
141 TSRMLS_FETCH_FROM_CTX(h
->ts
);
146 if (php_memnstr(data
, ZEND_STRL("About to connect"), data
+ length
)) {
147 curl
->progress
.state
.info
= "resolve";
148 } else if (php_memnstr(data
, ZEND_STRL("Trying"), data
+ length
)) {
149 curl
->progress
.state
.info
= "connect";
150 } else if (php_memnstr(data
, ZEND_STRL("Connected"), data
+ length
)) {
151 curl
->progress
.state
.info
= "connected";
152 } else if (php_memnstr(data
, ZEND_STRL("left intact"), data
+ length
)) {
153 curl
->progress
.state
.info
= "not disconnected";
154 } else if (php_memnstr(data
, ZEND_STRL("closed"), data
+ length
)) {
155 curl
->progress
.state
.info
= "disconnected";
156 } else if (php_memnstr(data
, ZEND_STRL("Issue another request"), data
+ length
)) {
157 curl
->progress
.state
.info
= "redirect";
159 php_http_client_progress_notify(&curl
->progress TSRMLS_CC
);
161 case CURLINFO_HEADER_OUT
:
162 case CURLINFO_DATA_OUT
:
163 case CURLINFO_SSL_DATA_OUT
:
164 curl
->progress
.state
.info
= "send";
166 case CURLINFO_HEADER_IN
:
167 case CURLINFO_DATA_IN
:
168 case CURLINFO_SSL_DATA_IN
:
169 curl
->progress
.state
.info
= "receive";
176 case CURLINFO_HEADER_IN
:
177 case CURLINFO_DATA_IN
:
178 case CURLINFO_HEADER_OUT
:
179 case CURLINFO_DATA_OUT
:
180 php_http_buffer_append(h
->buffer
, data
, length
);
182 if (curl
->options
.redirects
) {
183 flags
|= PHP_HTTP_MESSAGE_PARSER_EMPTY_REDIRECTS
;
186 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE
== php_http_message_parser_parse(h
->parser
, h
->buffer
, flags
, &h
->message
)) {
196 _dpf(type
, data
, length
);
202 static int php_http_client_curl_dummy_callback(char *data
, size_t n
, size_t l
, void *s
)
207 static inline zval
*cache_option(HashTable
*cache
, char *key
, size_t keylen
, ulong h
, zval
*opt
)
212 zend_hash_quick_update(cache
, key
, keylen
, h
, &opt
, sizeof(zval
*), NULL
);
214 zend_hash_update(cache
, key
, keylen
, &opt
, sizeof(zval
*), NULL
);
220 static inline zval
*get_option(HashTable
*cache
, HashTable
*options
, char *key
, size_t keylen
, int type
)
224 ulong h
= zend_hash_func(key
, keylen
);
226 if (SUCCESS
== zend_hash_quick_find(options
, key
, keylen
, h
, (void *) &zoption
)) {
227 zval
*option
= php_http_ztyp(type
, *zoption
);
230 zval
*cached
= cache_option(cache
, key
, keylen
, h
, option
);
232 zval_ptr_dtor(&option
);
242 static STATUS
set_options(php_http_client_t
*h
, HashTable
*options
)
246 php_http_client_curl_t
*curl
= h
->ctx
;
247 CURL
*ch
= curl
->handle
;
248 TSRMLS_FETCH_FROM_CTX(h
->ts
);
251 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("proxyhost"), IS_STRING
))) {
252 curl_easy_setopt(ch
, CURLOPT_PROXY
, Z_STRVAL_P(zoption
));
254 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("proxytype"), IS_LONG
))) {
255 curl_easy_setopt(ch
, CURLOPT_PROXYTYPE
, Z_LVAL_P(zoption
));
258 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("proxyport"), IS_LONG
))) {
259 curl_easy_setopt(ch
, CURLOPT_PROXYPORT
, Z_LVAL_P(zoption
));
262 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("proxyauth"), IS_STRING
)) && Z_STRLEN_P(zoption
)) {
263 curl_easy_setopt(ch
, CURLOPT_PROXYUSERPWD
, Z_STRVAL_P(zoption
));
266 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("proxyauthtype"), IS_LONG
))) {
267 curl_easy_setopt(ch
, CURLOPT_PROXYAUTH
, Z_LVAL_P(zoption
));
270 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("proxytunnel"), IS_BOOL
)) && Z_BVAL_P(zoption
)) {
271 curl_easy_setopt(ch
, CURLOPT_HTTPPROXYTUNNEL
, 1L);
274 #if PHP_HTTP_CURL_VERSION(7,19,4)
275 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("noproxy"), IS_STRING
))) {
276 curl_easy_setopt(ch
, CURLOPT_NOPROXY
, Z_STRVAL_P(zoption
));
281 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("dns_cache_timeout"), IS_LONG
))) {
282 curl_easy_setopt(ch
, CURLOPT_DNS_CACHE_TIMEOUT
, Z_LVAL_P(zoption
));
284 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("ipresolve"), IS_LONG
)) && Z_LVAL_P(zoption
)) {
285 curl_easy_setopt(ch
, CURLOPT_IPRESOLVE
, Z_LVAL_P(zoption
));
287 #if PHP_HTTP_CURL_VERSION(7,21,3)
288 if (curl
->options
.resolve
) {
289 curl_slist_free_all(curl
->options
.resolve
);
290 curl
->options
.resolve
= NULL
;
292 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("resolve"), IS_ARRAY
))) {
293 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
297 FOREACH_KEYVAL(pos
, zoption
, key
, data
) {
298 zval
*cpy
= php_http_ztyp(IS_STRING
, *data
);
300 curl
->options
.resolve
= curl_slist_append(curl
->options
.resolve
, Z_STRVAL_P(cpy
));
306 #if PHP_HTTP_CURL_VERSION(7,24,0)
307 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("dns_servers"), IS_STRING
)) && Z_STRLEN_P(zoption
)) {
308 curl_easy_setopt(ch
, CURLOPT_DNS_SERVERS
, Z_STRVAL_P(zoption
));
313 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("low_speed_limit"), IS_LONG
))) {
314 curl_easy_setopt(ch
, CURLOPT_LOW_SPEED_LIMIT
, Z_LVAL_P(zoption
));
316 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("low_speed_time"), IS_LONG
))) {
317 curl_easy_setopt(ch
, CURLOPT_LOW_SPEED_TIME
, Z_LVAL_P(zoption
));
320 if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_send_speed"), IS_LONG))) {
321 curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
323 if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_recv_speed"), IS_LONG))) {
324 curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
328 if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("maxconnects"), IS_LONG))) {
329 curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, Z_LVAL_P(zoption));
331 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("fresh_connect"), IS_BOOL
)) && Z_BVAL_P(zoption
)) {
332 curl_easy_setopt(ch
, CURLOPT_FRESH_CONNECT
, 1L);
334 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("forbid_reuse"), IS_BOOL
)) && Z_BVAL_P(zoption
)) {
335 curl_easy_setopt(ch
, CURLOPT_FORBID_REUSE
, 1L);
338 /* outgoing interface */
339 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("interface"), IS_STRING
))) {
340 curl_easy_setopt(ch
, CURLOPT_INTERFACE
, Z_STRVAL_P(zoption
));
342 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("portrange"), IS_ARRAY
))) {
345 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption
));
346 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void *) &prs
)) {
347 zend_hash_move_forward(Z_ARRVAL_P(zoption
));
348 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void *) &pre
)) {
349 zval
*prs_cpy
= php_http_ztyp(IS_LONG
, *prs
);
350 zval
*pre_cpy
= php_http_ztyp(IS_LONG
, *pre
);
352 if (Z_LVAL_P(prs_cpy
) && Z_LVAL_P(pre_cpy
)) {
353 curl_easy_setopt(ch
, CURLOPT_LOCALPORT
, MIN(Z_LVAL_P(prs_cpy
), Z_LVAL_P(pre_cpy
)));
354 curl_easy_setopt(ch
, CURLOPT_LOCALPORTRANGE
, labs(Z_LVAL_P(prs_cpy
)-Z_LVAL_P(pre_cpy
))+1L);
356 zval_ptr_dtor(&prs_cpy
);
357 zval_ptr_dtor(&pre_cpy
);
364 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("port"), IS_LONG
))) {
365 curl_easy_setopt(ch
, CURLOPT_PORT
, Z_LVAL_P(zoption
));
368 /* RFC4007 zone_id */
369 #if PHP_HTTP_CURL_VERSION(7,19,0)
370 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("address_scope"), IS_LONG
))) {
371 curl_easy_setopt(ch
, CURLOPT_ADDRESS_SCOPE
, Z_LVAL_P(zoption
));
376 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("httpauth"), IS_STRING
)) && Z_STRLEN_P(zoption
)) {
377 curl_easy_setopt(ch
, CURLOPT_USERPWD
, Z_STRVAL_P(zoption
));
379 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("httpauthtype"), IS_LONG
))) {
380 curl_easy_setopt(ch
, CURLOPT_HTTPAUTH
, Z_LVAL_P(zoption
));
383 /* redirects, defaults to 0 */
384 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("redirect"), IS_LONG
))) {
385 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1L : 0L);
386 curl_easy_setopt(ch
, CURLOPT_MAXREDIRS
, curl
->options
.redirects
= Z_LVAL_P(zoption
));
387 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("unrestrictedauth"), IS_BOOL
))) {
388 curl_easy_setopt(ch
, CURLOPT_UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
390 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("postredir"), IS_BOOL
))) {
391 #if PHP_HTTP_CURL_VERSION(7,19,1)
392 curl_easy_setopt(ch
, CURLOPT_POSTREDIR
, Z_BVAL_P(zoption
) ? 1L : 0L);
394 curl_easy_setopt(ch
, CURLOPT_POST301
, Z_BVAL_P(zoption
) ? 1L : 0L);
398 curl
->options
.redirects
= 0;
401 /* retries, defaults to 0 */
402 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("retrycount"), IS_LONG
))) {
403 curl
->options
.retry
.count
= Z_LVAL_P(zoption
);
404 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("retrydelay"), IS_DOUBLE
))) {
405 curl
->options
.retry
.delay
= Z_DVAL_P(zoption
);
407 curl
->options
.retry
.delay
= 0;
410 curl
->options
.retry
.count
= 0;
414 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("referer"), IS_STRING
)) && Z_STRLEN_P(zoption
)) {
415 curl_easy_setopt(ch
, CURLOPT_REFERER
, Z_STRVAL_P(zoption
));
418 /* useragent, default "PECL::HTTP/version (PHP/version)" */
419 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("useragent"), IS_STRING
))) {
420 /* allow to send no user agent, not even default one */
421 if (Z_STRLEN_P(zoption
)) {
422 curl_easy_setopt(ch
, CURLOPT_USERAGENT
, Z_STRVAL_P(zoption
));
424 curl_easy_setopt(ch
, CURLOPT_USERAGENT
, NULL
);
429 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("resume"), IS_LONG
)) && (Z_LVAL_P(zoption
) > 0)) {
431 curl_easy_setopt(ch
, CURLOPT_RESUME_FROM
, Z_LVAL_P(zoption
));
433 /* or range of kind array(array(0,499), array(100,1499)) */
434 else if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("range"), IS_ARRAY
)) && zend_hash_num_elements(Z_ARRVAL_P(zoption
))) {
435 HashPosition pos1
, pos2
;
436 zval
**rr
, **rb
, **re
;
437 php_http_buffer_t rs
;
439 php_http_buffer_init(&rs
);
440 FOREACH_VAL(pos1
, zoption
, rr
) {
441 if (Z_TYPE_PP(rr
) == IS_ARRAY
) {
442 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(rr
), &pos2
);
443 if (SUCCESS
== zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr
), (void *) &rb
, &pos2
)) {
444 zend_hash_move_forward_ex(Z_ARRVAL_PP(rr
), &pos2
);
445 if (SUCCESS
== zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr
), (void *) &re
, &pos2
)) {
446 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))) &&
447 ((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)))) {
448 zval
*rbl
= php_http_ztyp(IS_LONG
, *rb
);
449 zval
*rel
= php_http_ztyp(IS_LONG
, *re
);
451 if ((Z_LVAL_P(rbl
) >= 0) && (Z_LVAL_P(rel
) >= 0)) {
452 php_http_buffer_appendf(&rs
, "%ld-%ld,", Z_LVAL_P(rbl
), Z_LVAL_P(rel
));
462 if (PHP_HTTP_BUFFER_LEN(&rs
)) {
466 /* ditch last comma */
467 PHP_HTTP_BUFFER_VAL(&rs
)[PHP_HTTP_BUFFER_LEN(&rs
)-- -1] = '\0';
469 MAKE_STD_ZVAL(cached_range
);
470 ZVAL_STRINGL(cached_range
, PHP_HTTP_BUFFER_VAL(&rs
), PHP_HTTP_BUFFER_LEN(&rs
), 0);
471 curl_easy_setopt(ch
, CURLOPT_RANGE
, Z_STRVAL_P(cache_option(&curl
->options
.cache
, ZEND_STRS("range"), 0, cached_range
)));
472 zval_ptr_dtor(&cached_range
);
476 /* initialize headers */
477 if (curl
->options
.headers
) {
478 curl_slist_free_all(curl
->options
.headers
);
479 curl
->options
.headers
= NULL
;
482 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("etag"), IS_STRING
)) && Z_STRLEN_P(zoption
)) {
483 zend_bool is_quoted
= !((Z_STRVAL_P(zoption
)[0] != '"') || (Z_STRVAL_P(zoption
)[Z_STRLEN_P(zoption
)-1] != '"'));
484 php_http_buffer_t header
;
486 php_http_buffer_init(&header
);
487 php_http_buffer_appendf(&header
, is_quoted
?"%s: %s":"%s: \"%s\"", range_req
?"If-Match":"If-None-Match", Z_STRVAL_P(zoption
));
488 php_http_buffer_fix(&header
);
489 curl
->options
.headers
= curl_slist_append(curl
->options
.headers
, PHP_HTTP_BUFFER_VAL(&header
));
490 php_http_buffer_dtor(&header
);
493 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("compress"), IS_BOOL
)) && Z_LVAL_P(zoption
)) {
494 curl
->options
.headers
= curl_slist_append(curl
->options
.headers
, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
496 curl_easy_setopt(ch
, CURLOPT_HTTPHEADER
, curl
->options
.headers
);
499 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("lastmodified"), IS_LONG
))) {
500 if (Z_LVAL_P(zoption
)) {
501 if (Z_LVAL_P(zoption
) > 0) {
502 curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, Z_LVAL_P(zoption
));
504 curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, (long) PHP_HTTP_G
->env
.request
.time
+ Z_LVAL_P(zoption
));
506 curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, (long) (range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
));
508 curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, CURL_TIMECOND_NONE
);
512 /* cookies, array('name' => 'value') */
513 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("cookies"), IS_ARRAY
))) {
514 php_http_buffer_dtor(&curl
->options
.cookies
);
515 if (zend_hash_num_elements(Z_ARRVAL_P(zoption
))) {
516 zval
*urlenc_cookies
= NULL
;
517 /* check whether cookies should not be urlencoded; default is to urlencode them */
518 if ((!(urlenc_cookies
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("encodecookies"), IS_BOOL
))) || Z_BVAL_P(urlenc_cookies
)) {
519 if (SUCCESS
== php_http_url_encode_hash_ex(HASH_OF(zoption
), &curl
->options
.cookies
, ZEND_STRS(";"), ZEND_STRS("="), NULL
, 0 TSRMLS_CC
)) {
520 php_http_buffer_fix(&curl
->options
.cookies
);
521 curl_easy_setopt(ch
, CURLOPT_COOKIE
, curl
->options
.cookies
.data
);
525 php_http_array_hashkey_t cookie_key
= php_http_array_hashkey_init(0);
528 FOREACH_KEYVAL(pos
, zoption
, cookie_key
, cookie_val
) {
529 if (cookie_key
.type
== HASH_KEY_IS_STRING
) {
530 zval
*val
= php_http_ztyp(IS_STRING
, *cookie_val
);
531 php_http_buffer_appendf(&curl
->options
.cookies
, "%s=%s; ", cookie_key
.str
, Z_STRVAL_P(val
));
536 php_http_buffer_fix(&curl
->options
.cookies
);
537 if (PHP_HTTP_BUFFER_LEN(&curl
->options
.cookies
)) {
538 curl_easy_setopt(ch
, CURLOPT_COOKIE
, PHP_HTTP_BUFFER_VAL(&curl
->options
.cookies
));
544 /* don't load session cookies from cookiestore */
545 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("cookiesession"), IS_BOOL
)) && Z_BVAL_P(zoption
)) {
546 curl_easy_setopt(ch
, CURLOPT_COOKIESESSION
, 1L);
549 /* cookiestore, read initial cookies from that file and store cookies back into that file */
550 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("cookiestore"), IS_STRING
))) {
551 php_http_client_curl_storage_t
*storage
= get_storage(curl
->handle
);
553 if (Z_STRLEN_P(zoption
)) {
554 if (SUCCESS
!= php_check_open_basedir(Z_STRVAL_P(zoption
) TSRMLS_CC
)) {
558 if (storage
->cookiestore
) {
559 pefree(storage
->cookiestore
, 1);
561 storage
->cookiestore
= pestrndup(Z_STRVAL_P(zoption
), Z_STRLEN_P(zoption
), 1);
562 curl_easy_setopt(ch
, CURLOPT_COOKIEFILE
, storage
->cookiestore
);
563 curl_easy_setopt(ch
, CURLOPT_COOKIEJAR
, storage
->cookiestore
);
567 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("maxfilesize"), IS_LONG
))) {
568 curl_easy_setopt(ch
, CURLOPT_MAXFILESIZE
, Z_LVAL_P(zoption
));
572 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("protocol"), IS_LONG
))) {
573 curl_easy_setopt(ch
, CURLOPT_HTTP_VERSION
, Z_LVAL_P(zoption
));
576 /* timeout, defaults to 0 */
577 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("timeout"), IS_DOUBLE
))) {
578 curl_easy_setopt(ch
, CURLOPT_TIMEOUT_MS
, (long)(Z_DVAL_P(zoption
)*1000));
580 /* connecttimeout, defaults to 0 */
581 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("connecttimeout"), IS_DOUBLE
))) {
582 curl_easy_setopt(ch
, CURLOPT_CONNECTTIMEOUT_MS
, (long)(Z_DVAL_P(zoption
)*1000));
586 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("ssl"), IS_ARRAY
))) {
587 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
591 FOREACH_KEYVAL(pos
, zoption
, key
, param
) {
592 if (key
.type
== HASH_KEY_IS_STRING
) {
593 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERT
, 0, 1);
594 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTTYPE
, 0, 0);
595 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTPASSWD
, 0, 0);
597 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEY
, 0, 0);
598 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYTYPE
, 0, 0);
599 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYPASSWD
, 0, 0);
601 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLENGINE
, 0, 0);
602 PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSLVERSION
, 0);
604 PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYPEER
, 1);
605 PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYHOST
, 1);
606 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSL_CIPHER_LIST
, 1, 0);
608 PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAINFO
, -3, 1);
609 PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAPATH
, -3, 1);
610 PHP_HTTP_CURL_OPT_STRING(CURLOPT_RANDOM_FILE
, -3, 1);
611 PHP_HTTP_CURL_OPT_STRING(CURLOPT_EGDSOCKET
, -3, 1);
612 #if PHP_HTTP_CURL_VERSION(7,19,0)
613 PHP_HTTP_CURL_OPT_STRING(CURLOPT_ISSUERCERT
, -3, 1);
614 #if defined(PHP_HTTP_HAVE_OPENSSL)
615 PHP_HTTP_CURL_OPT_STRING(CURLOPT_CRLFILE
, -3, 1);
618 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
619 PHP_HTTP_CURL_OPT_LONG(CURLOPT_CERTINFO
, -3);
627 static STATUS
get_info(CURL
*ch
, HashTable
*info
)
632 struct curl_slist
*s
, *p
;
633 zval
*subarray
, array
;
634 INIT_PZVAL_ARRAY(&array
, info
);
636 /* BEGIN::CURLINFO */
637 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_EFFECTIVE_URL
, &c
)) {
638 add_assoc_string_ex(&array
, "effective_url", sizeof("effective_url"), c
? c
: "", 1);
640 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_RESPONSE_CODE
, &l
)) {
641 add_assoc_long_ex(&array
, "response_code", sizeof("response_code"), l
);
643 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_TOTAL_TIME
, &d
)) {
644 add_assoc_double_ex(&array
, "total_time", sizeof("total_time"), d
);
646 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_NAMELOOKUP_TIME
, &d
)) {
647 add_assoc_double_ex(&array
, "namelookup_time", sizeof("namelookup_time"), d
);
649 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONNECT_TIME
, &d
)) {
650 add_assoc_double_ex(&array
, "connect_time", sizeof("connect_time"), d
);
652 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRETRANSFER_TIME
, &d
)) {
653 add_assoc_double_ex(&array
, "pretransfer_time", sizeof("pretransfer_time"), d
);
655 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SIZE_UPLOAD
, &d
)) {
656 add_assoc_double_ex(&array
, "size_upload", sizeof("size_upload"), d
);
658 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SIZE_DOWNLOAD
, &d
)) {
659 add_assoc_double_ex(&array
, "size_download", sizeof("size_download"), d
);
661 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SPEED_DOWNLOAD
, &d
)) {
662 add_assoc_double_ex(&array
, "speed_download", sizeof("speed_download"), d
);
664 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SPEED_UPLOAD
, &d
)) {
665 add_assoc_double_ex(&array
, "speed_upload", sizeof("speed_upload"), d
);
667 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HEADER_SIZE
, &l
)) {
668 add_assoc_long_ex(&array
, "header_size", sizeof("header_size"), l
);
670 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REQUEST_SIZE
, &l
)) {
671 add_assoc_long_ex(&array
, "request_size", sizeof("request_size"), l
);
673 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SSL_VERIFYRESULT
, &l
)) {
674 add_assoc_long_ex(&array
, "ssl_verifyresult", sizeof("ssl_verifyresult"), l
);
676 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_FILETIME
, &l
)) {
677 add_assoc_long_ex(&array
, "filetime", sizeof("filetime"), l
);
679 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
)) {
680 add_assoc_double_ex(&array
, "content_length_download", sizeof("content_length_download"), d
);
682 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_LENGTH_UPLOAD
, &d
)) {
683 add_assoc_double_ex(&array
, "content_length_upload", sizeof("content_length_upload"), d
);
685 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_STARTTRANSFER_TIME
, &d
)) {
686 add_assoc_double_ex(&array
, "starttransfer_time", sizeof("starttransfer_time"), d
);
688 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_TYPE
, &c
)) {
689 add_assoc_string_ex(&array
, "content_type", sizeof("content_type"), c
? c
: "", 1);
691 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_TIME
, &d
)) {
692 add_assoc_double_ex(&array
, "redirect_time", sizeof("redirect_time"), d
);
694 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_COUNT
, &l
)) {
695 add_assoc_long_ex(&array
, "redirect_count", sizeof("redirect_count"), l
);
697 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HTTP_CONNECTCODE
, &l
)) {
698 add_assoc_long_ex(&array
, "connect_code", sizeof("connect_code"), l
);
700 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HTTPAUTH_AVAIL
, &l
)) {
701 add_assoc_long_ex(&array
, "httpauth_avail", sizeof("httpauth_avail"), l
);
703 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PROXYAUTH_AVAIL
, &l
)) {
704 add_assoc_long_ex(&array
, "proxyauth_avail", sizeof("proxyauth_avail"), l
);
706 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_OS_ERRNO
, &l
)) {
707 add_assoc_long_ex(&array
, "os_errno", sizeof("os_errno"), l
);
709 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_NUM_CONNECTS
, &l
)) {
710 add_assoc_long_ex(&array
, "num_connects", sizeof("num_connects"), l
);
712 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SSL_ENGINES
, &s
)) {
713 MAKE_STD_ZVAL(subarray
);
714 array_init(subarray
);
715 for (p
= s
; p
; p
= p
->next
) {
717 add_next_index_string(subarray
, p
->data
, 1);
720 add_assoc_zval_ex(&array
, "ssl_engines", sizeof("ssl_engines"), subarray
);
721 curl_slist_free_all(s
);
723 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_COOKIELIST
, &s
)) {
724 MAKE_STD_ZVAL(subarray
);
725 array_init(subarray
);
726 for (p
= s
; p
; p
= p
->next
) {
728 add_next_index_string(subarray
, p
->data
, 1);
731 add_assoc_zval_ex(&array
, "cookies", sizeof("cookies"), subarray
);
732 curl_slist_free_all(s
);
734 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_URL
, &c
)) {
735 add_assoc_string_ex(&array
, "redirect_url", sizeof("redirect_url"), c
? c
: "", 1);
737 #if PHP_HTTP_CURL_VERSION(7,19,0)
738 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRIMARY_IP
, &c
)) {
739 add_assoc_string_ex(&array
, "primary_ip", sizeof("primary_ip"), c
? c
: "", 1);
742 #if PHP_HTTP_CURL_VERSION(7,19,0)
743 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_APPCONNECT_TIME
, &d
)) {
744 add_assoc_double_ex(&array
, "appconnect_time", sizeof("appconnect_time"), d
);
747 #if PHP_HTTP_CURL_VERSION(7,19,4)
748 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONDITION_UNMET
, &l
)) {
749 add_assoc_long_ex(&array
, "condition_unmet", sizeof("condition_unmet"), l
);
752 #if PHP_HTTP_CURL_VERSION(7,21,0)
753 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRIMARY_PORT
, &l
)) {
754 add_assoc_long_ex(&array
, "primary_port", sizeof("primary_port"), l
);
757 #if PHP_HTTP_CURL_VERSION(7,21,0)
758 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_LOCAL_IP
, &c
)) {
759 add_assoc_string_ex(&array
, "local_ip", sizeof("local_ip"), c
? c
: "", 1);
762 #if PHP_HTTP_CURL_VERSION(7,21,0)
763 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_LOCAL_PORT
, &l
)) {
764 add_assoc_long_ex(&array
, "local_port", sizeof("local_port"), l
);
770 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
774 struct curl_certinfo
*ci
;
775 char *colon
, *keyname
;
777 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CERTINFO
, &ci
)) {
778 MAKE_STD_ZVAL(ci_array
);
779 array_init(ci_array
);
781 for (i
= 0; i
< ci
->num_of_certs
; ++i
) {
784 MAKE_STD_ZVAL(subarray
);
785 array_init(subarray
);
786 for (p
= s
; p
; p
= p
->next
) {
788 if ((colon
= strchr(p
->data
, ':'))) {
789 keyname
= estrndup(p
->data
, colon
- p
->data
);
790 add_assoc_string_ex(subarray
, keyname
, colon
- p
->data
+ 1, colon
+ 1, 1);
793 add_next_index_string(subarray
, p
->data
, 1);
797 add_next_index_zval(ci_array
, subarray
);
799 add_assoc_zval_ex(&array
, "certinfo", sizeof("certinfo"), ci_array
);
803 add_assoc_string_ex(&array
, "error", sizeof("error"), get_storage(ch
)->errorbuffer
, 1);
809 /* request handler ops */
811 static STATUS
php_http_client_curl_reset(php_http_client_t
*h
);
813 static php_http_client_t
*php_http_client_curl_init(php_http_client_t
*h
, void *handle
)
815 php_http_client_curl_t
*ctx
;
816 TSRMLS_FETCH_FROM_CTX(h
->ts
);
818 if (!handle
&& !(handle
= php_http_resource_factory_handle_ctor(h
->rf TSRMLS_CC
))) {
819 php_http_error(HE_WARNING
, PHP_HTTP_E_CLIENT
, "could not initialize curl handle");
823 ctx
= ecalloc(1, sizeof(*ctx
));
824 ctx
->handle
= handle
;
825 php_http_buffer_init(&ctx
->options
.cookies
);
826 zend_hash_init(&ctx
->options
.cache
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
830 curl_easy_setopt(handle
, CURLOPT_NOSIGNAL
, 1L);
832 curl_easy_setopt(handle
, CURLOPT_HEADER
, 0L);
833 curl_easy_setopt(handle
, CURLOPT_FILETIME
, 1L);
834 curl_easy_setopt(handle
, CURLOPT_AUTOREFERER
, 1L);
835 curl_easy_setopt(handle
, CURLOPT_VERBOSE
, 1L);
836 curl_easy_setopt(handle
, CURLOPT_NOPROGRESS
, 0L);
837 curl_easy_setopt(handle
, CURLOPT_HEADERFUNCTION
, NULL
);
838 curl_easy_setopt(handle
, CURLOPT_WRITEFUNCTION
, php_http_client_curl_dummy_callback
);
839 curl_easy_setopt(handle
, CURLOPT_DEBUGFUNCTION
, php_http_client_curl_raw_callback
);
840 curl_easy_setopt(handle
, CURLOPT_READFUNCTION
, php_http_client_curl_read_callback
);
841 curl_easy_setopt(handle
, CURLOPT_IOCTLFUNCTION
, php_http_client_curl_ioctl_callback
);
842 curl_easy_setopt(handle
, CURLOPT_PROGRESSFUNCTION
, php_http_client_curl_progress_callback
);
843 curl_easy_setopt(handle
, CURLOPT_DEBUGDATA
, h
);
844 curl_easy_setopt(handle
, CURLOPT_PROGRESSDATA
, h
);
846 php_http_client_curl_reset(h
);
851 static php_http_client_t
*php_http_client_curl_copy(php_http_client_t
*from
, php_http_client_t
*to
)
853 php_http_client_curl_t
*ctx
= from
->ctx
;
855 TSRMLS_FETCH_FROM_CTX(from
->ts
);
857 if (!(copy
= php_http_resource_factory_handle_copy(from
->rf
, ctx
->handle TSRMLS_CC
))) {
862 return php_http_client_curl_init(to
, copy
);
864 return php_http_client_init(NULL
, from
->ops
, from
->rf
, copy TSRMLS_CC
);
868 static void php_http_client_curl_dtor(php_http_client_t
*h
)
870 php_http_client_curl_t
*ctx
= h
->ctx
;
871 TSRMLS_FETCH_FROM_CTX(h
->ts
);
873 curl_easy_setopt(ctx
->handle
, CURLOPT_NOPROGRESS
, 1L);
874 curl_easy_setopt(ctx
->handle
, CURLOPT_PROGRESSFUNCTION
, NULL
);
875 curl_easy_setopt(ctx
->handle
, CURLOPT_VERBOSE
, 0L);
876 curl_easy_setopt(ctx
->handle
, CURLOPT_DEBUGFUNCTION
, NULL
);
878 php_http_resource_factory_handle_dtor(h
->rf
, ctx
->handle TSRMLS_CC
);
880 php_http_buffer_dtor(&ctx
->options
.cookies
);
881 zend_hash_destroy(&ctx
->options
.cache
);
883 if (ctx
->options
.headers
) {
884 curl_slist_free_all(ctx
->options
.headers
);
885 ctx
->options
.headers
= NULL
;
887 php_http_client_progress_dtor(&ctx
->progress TSRMLS_CC
);
892 static STATUS
php_http_client_curl_reset(php_http_client_t
*h
)
894 CURL
*ch
= ((php_http_client_curl_t
*) h
->ctx
)->handle
;
895 php_http_client_curl_storage_t
*st
;
897 if ((st
= get_storage(ch
))) {
902 if (st
->cookiestore
) {
903 pefree(st
->cookiestore
, 1);
904 st
->cookiestore
= NULL
;
906 st
->errorbuffer
[0] = '\0';
909 curl_easy_setopt(ch
, CURLOPT_URL
, NULL
);
910 #if PHP_HTTP_CURL_VERSION(7,19,4)
911 curl_easy_setopt(ch
, CURLOPT_NOPROXY
, NULL
);
913 curl_easy_setopt(ch
, CURLOPT_PROXY
, NULL
);
914 curl_easy_setopt(ch
, CURLOPT_PROXYPORT
, 0L);
915 curl_easy_setopt(ch
, CURLOPT_PROXYTYPE
, 0L);
916 /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
917 #if PHP_HTTP_CURL_VERSION(7,19,1)
918 curl_easy_setopt(ch
, CURLOPT_PROXYUSERNAME
, NULL
);
919 curl_easy_setopt(ch
, CURLOPT_PROXYPASSWORD
, NULL
);
921 curl_easy_setopt(ch
, CURLOPT_PROXYAUTH
, 0L);
922 curl_easy_setopt(ch
, CURLOPT_HTTPPROXYTUNNEL
, 0L);
923 curl_easy_setopt(ch
, CURLOPT_DNS_CACHE_TIMEOUT
, 60L);
924 curl_easy_setopt(ch
, CURLOPT_IPRESOLVE
, 0);
925 #if PHP_HTTP_CURL_VERSION(7,21,3)
926 curl_easy_setopt(ch
, CURLOPT_RESOLVE
, NULL
);
928 #if PHP_HTTP_CURL_VERSION(7,24,0)
929 curl_easy_setopt(ch
, CURLOPT_DNS_SERVERS
, NULL
);
931 curl_easy_setopt(ch
, CURLOPT_LOW_SPEED_LIMIT
, 0L);
932 curl_easy_setopt(ch
, CURLOPT_LOW_SPEED_TIME
, 0L);
934 curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) 0);
935 curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) 0);
938 curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, 5L); */
939 curl_easy_setopt(ch
, CURLOPT_FRESH_CONNECT
, 0L);
940 curl_easy_setopt(ch
, CURLOPT_FORBID_REUSE
, 0L);
941 curl_easy_setopt(ch
, CURLOPT_INTERFACE
, NULL
);
942 curl_easy_setopt(ch
, CURLOPT_PORT
, 0L);
943 #if PHP_HTTP_CURL_VERSION(7,19,0)
944 curl_easy_setopt(ch
, CURLOPT_ADDRESS_SCOPE
, 0L);
946 curl_easy_setopt(ch
, CURLOPT_LOCALPORT
, 0L);
947 curl_easy_setopt(ch
, CURLOPT_LOCALPORTRANGE
, 0L);
948 /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
949 #if PHP_HTTP_CURL_VERSION(7,19,1)
950 curl_easy_setopt(ch
, CURLOPT_USERNAME
, NULL
);
951 curl_easy_setopt(ch
, CURLOPT_PASSWORD
, NULL
);
953 curl_easy_setopt(ch
, CURLOPT_HTTPAUTH
, 0L);
954 curl_easy_setopt(ch
, CURLOPT_ENCODING
, NULL
);
955 /* we do this ourself anyway */
956 curl_easy_setopt(ch
, CURLOPT_HTTP_CONTENT_DECODING
, 0L);
957 curl_easy_setopt(ch
, CURLOPT_HTTP_TRANSFER_DECODING
, 0L);
958 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, 0L);
959 #if PHP_HTTP_CURL_VERSION(7,19,1)
960 curl_easy_setopt(ch
, CURLOPT_POSTREDIR
, 0L);
962 curl_easy_setopt(ch
, CURLOPT_POST301
, 0L);
964 curl_easy_setopt(ch
, CURLOPT_UNRESTRICTED_AUTH
, 0L);
965 curl_easy_setopt(ch
, CURLOPT_REFERER
, NULL
);
966 curl_easy_setopt(ch
, CURLOPT_USERAGENT
, "PECL::HTTP/" PHP_HTTP_EXT_VERSION
" (PHP/" PHP_VERSION
")");
967 curl_easy_setopt(ch
, CURLOPT_HTTPHEADER
, NULL
);
968 curl_easy_setopt(ch
, CURLOPT_COOKIE
, NULL
);
969 curl_easy_setopt(ch
, CURLOPT_COOKIESESSION
, 0L);
970 /* these options would enable curl's cookie engine by default which we don't want
971 curl_easy_setopt(ch, CURLOPT_COOKIEFILE, NULL);
972 curl_easy_setopt(ch, CURLOPT_COOKIEJAR, NULL); */
973 curl_easy_setopt(ch
, CURLOPT_COOKIELIST
, NULL
);
974 curl_easy_setopt(ch
, CURLOPT_RANGE
, NULL
);
975 curl_easy_setopt(ch
, CURLOPT_RESUME_FROM
, 0L);
976 curl_easy_setopt(ch
, CURLOPT_MAXFILESIZE
, 0L);
977 curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, 0L);
978 curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, 0L);
979 curl_easy_setopt(ch
, CURLOPT_TIMEOUT
, 0L);
980 curl_easy_setopt(ch
, CURLOPT_CONNECTTIMEOUT
, 3);
981 curl_easy_setopt(ch
, CURLOPT_SSLCERT
, NULL
);
982 curl_easy_setopt(ch
, CURLOPT_SSLCERTTYPE
, NULL
);
983 curl_easy_setopt(ch
, CURLOPT_SSLCERTPASSWD
, NULL
);
984 curl_easy_setopt(ch
, CURLOPT_SSLKEY
, NULL
);
985 curl_easy_setopt(ch
, CURLOPT_SSLKEYTYPE
, NULL
);
986 curl_easy_setopt(ch
, CURLOPT_SSLKEYPASSWD
, NULL
);
987 curl_easy_setopt(ch
, CURLOPT_SSLENGINE
, NULL
);
988 curl_easy_setopt(ch
, CURLOPT_SSLVERSION
, 0L);
989 curl_easy_setopt(ch
, CURLOPT_SSL_VERIFYPEER
, 0L);
990 curl_easy_setopt(ch
, CURLOPT_SSL_VERIFYHOST
, 0L);
991 curl_easy_setopt(ch
, CURLOPT_SSL_CIPHER_LIST
, NULL
);
992 #if PHP_HTTP_CURL_VERSION(7,19,0)
993 curl_easy_setopt(ch
, CURLOPT_ISSUERCERT
, NULL
);
994 #if defined(PHP_HTTP_HAVE_OPENSSL)
995 curl_easy_setopt(ch
, CURLOPT_CRLFILE
, NULL
);
998 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
999 curl_easy_setopt(ch
, CURLOPT_CERTINFO
, NULL
);
1001 #ifdef PHP_HTTP_CURL_CAINFO
1002 curl_easy_setopt(ch
, CURLOPT_CAINFO
, PHP_HTTP_CURL_CAINFO
);
1004 curl_easy_setopt(ch
, CURLOPT_CAINFO
, NULL
);
1006 curl_easy_setopt(ch
, CURLOPT_CAPATH
, NULL
);
1007 curl_easy_setopt(ch
, CURLOPT_RANDOM_FILE
, NULL
);
1008 curl_easy_setopt(ch
, CURLOPT_EGDSOCKET
, NULL
);
1009 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, NULL
);
1010 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, 0L);
1011 curl_easy_setopt(ch
, CURLOPT_HTTPPOST
, NULL
);
1012 curl_easy_setopt(ch
, CURLOPT_IOCTLDATA
, NULL
);
1013 curl_easy_setopt(ch
, CURLOPT_READDATA
, NULL
);
1014 curl_easy_setopt(ch
, CURLOPT_INFILESIZE
, 0L);
1015 curl_easy_setopt(ch
, CURLOPT_HTTP_VERSION
, CURL_HTTP_VERSION_NONE
);
1016 curl_easy_setopt(ch
, CURLOPT_CUSTOMREQUEST
, NULL
);
1017 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 0L);
1018 curl_easy_setopt(ch
, CURLOPT_POST
, 0L);
1019 curl_easy_setopt(ch
, CURLOPT_UPLOAD
, 0L);
1020 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1L);
1025 STATUS
php_http_client_curl_prepare(php_http_client_t
*h
, php_http_message_t
*msg
)
1027 php_http_client_curl_t
*curl
= h
->ctx
;
1028 php_http_client_curl_storage_t
*storage
= get_storage(curl
->handle
);
1029 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1032 if (!PHP_HTTP_INFO(msg
).request
.url
) {
1033 php_http_error(HE_WARNING
, PHP_HTTP_E_CLIENT
, "Cannot request empty URL");
1036 storage
->errorbuffer
[0] = '\0';
1038 pefree(storage
->url
, 1);
1040 storage
->url
= pestrdup(PHP_HTTP_INFO(msg
).request
.url
, 1);
1041 curl_easy_setopt(curl
->handle
, CURLOPT_URL
, storage
->url
);
1043 /* request method */
1044 switch (php_http_select_str(PHP_HTTP_INFO(msg
).request
.method
, 4, "GET", "HEAD", "POST", "PUT")) {
1046 curl_easy_setopt(curl
->handle
, CURLOPT_HTTPGET
, 1L);
1050 curl_easy_setopt(curl
->handle
, CURLOPT_NOBODY
, 1L);
1054 curl_easy_setopt(curl
->handle
, CURLOPT_POST
, 1L);
1058 curl_easy_setopt(curl
->handle
, CURLOPT_UPLOAD
, 1L);
1062 if (PHP_HTTP_INFO(msg
).request
.method
) {
1063 curl_easy_setopt(curl
->handle
, CURLOPT_CUSTOMREQUEST
, PHP_HTTP_INFO(msg
).request
.method
);
1065 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_METHOD
, "Cannot use empty request method");
1072 /* request headers */
1073 if (zend_hash_num_elements(&msg
->hdrs
)) {
1074 php_http_array_hashkey_t header_key
= php_http_array_hashkey_init(0);
1077 php_http_buffer_t header
;
1079 php_http_buffer_init(&header
);
1080 FOREACH_HASH_KEYVAL(pos
, &msg
->hdrs
, header_key
, header_val
) {
1081 if (header_key
.type
== HASH_KEY_IS_STRING
) {
1082 zval
*header_cpy
= php_http_ztyp(IS_STRING
, *header_val
);
1084 php_http_buffer_appendf(&header
, "%s: %s", header_key
.str
, Z_STRVAL_P(header_cpy
));
1085 php_http_buffer_fix(&header
);
1086 curl
->options
.headers
= curl_slist_append(curl
->options
.headers
, PHP_HTTP_BUFFER_VAL(&header
));
1087 php_http_buffer_reset(&header
);
1089 zval_ptr_dtor(&header_cpy
);
1092 php_http_buffer_dtor(&header
);
1093 curl_easy_setopt(curl
->handle
, CURLOPT_HTTPHEADER
, curl
->options
.headers
);
1096 /* attach request body */
1098 /* RFC2616, section 4.3 (para. 4) states that »a message-body MUST NOT be included in a request if the
1099 * specification of the request method (section 5.1.1) does not allow sending an entity-body in request.«
1100 * Following the clause in section 5.1.1 (para. 2) that request methods »MUST be implemented with the
1101 * same semantics as those specified in section 9« reveal that not any single defined HTTP/1.1 method
1102 * does not allow a request body.
1104 size_t body_size
= php_http_message_body_size(&msg
->body
);
1106 curl_easy_setopt(curl
->handle
, CURLOPT_IOCTLDATA
, &msg
->body
);
1107 curl_easy_setopt(curl
->handle
, CURLOPT_READDATA
, &msg
->body
);
1108 curl_easy_setopt(curl
->handle
, CURLOPT_INFILESIZE
, body_size
);
1109 curl_easy_setopt(curl
->handle
, CURLOPT_POSTFIELDSIZE
, body_size
);
1115 static STATUS
php_http_client_curl_exec(php_http_client_t
*h
, php_http_message_t
*msg
)
1119 php_http_client_curl_t
*curl
= h
->ctx
;
1120 php_http_client_curl_storage_t
*storage
= get_storage(curl
->handle
);
1121 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1123 if (SUCCESS
!= php_http_client_curl_prepare(h
, msg
)) {
1128 if (CURLE_OK
!= (result
= curl_easy_perform(curl
->handle
))) {
1129 php_http_error(HE_WARNING
, PHP_HTTP_E_CLIENT
, "%s; %s (%s)", curl_easy_strerror(result
), storage
->errorbuffer
, storage
->url
);
1131 if (EG(exception
)) {
1132 add_property_long(EG(exception
), "curlCode", result
);
1135 if (curl
->options
.retry
.count
> tries
++) {
1137 case CURLE_COULDNT_RESOLVE_PROXY
:
1138 case CURLE_COULDNT_RESOLVE_HOST
:
1139 case CURLE_COULDNT_CONNECT
:
1140 case CURLE_WRITE_ERROR
:
1141 case CURLE_READ_ERROR
:
1142 case CURLE_OPERATION_TIMEDOUT
:
1143 case CURLE_SSL_CONNECT_ERROR
:
1144 case CURLE_GOT_NOTHING
:
1145 case CURLE_SSL_ENGINE_SETFAILED
:
1146 case CURLE_SEND_ERROR
:
1147 case CURLE_RECV_ERROR
:
1148 case CURLE_SSL_ENGINE_INITFAILED
:
1149 case CURLE_LOGIN_DENIED
:
1150 if (curl
->options
.retry
.delay
>= PHP_HTTP_DIFFSEC
) {
1151 php_http_sleep(curl
->options
.retry
.delay
);
1165 static STATUS
php_http_client_curl_setopt(php_http_client_t
*h
, php_http_client_setopt_opt_t opt
, void *arg
)
1167 php_http_client_curl_t
*curl
= h
->ctx
;
1168 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1171 case PHP_HTTP_CLIENT_OPT_SETTINGS
:
1172 return set_options(h
, arg
);
1175 case PHP_HTTP_CLIENT_OPT_PROGRESS_CALLBACK
:
1176 if (curl
->progress
.in_cb
) {
1177 php_http_error(HE_WARNING
, PHP_HTTP_E_CLIENT
, "Cannot change progress callback while executing it");
1180 if (curl
->progress
.callback
) {
1181 php_http_client_progress_dtor(&curl
->progress TSRMLS_CC
);
1183 curl
->progress
.callback
= arg
;
1186 case PHP_HTTP_CLIENT_OPT_COOKIES_ENABLE
:
1187 /* are cookies already enabled anyway? */
1188 if (!get_storage(curl
->handle
)->cookiestore
) {
1189 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_COOKIEFILE
, "")) {
1195 case PHP_HTTP_CLIENT_OPT_COOKIES_RESET
:
1196 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_COOKIELIST
, "ALL")) {
1201 case PHP_HTTP_CLIENT_OPT_COOKIES_RESET_SESSION
:
1202 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_COOKIELIST
, "SESS")) {
1207 case PHP_HTTP_CLIENT_OPT_COOKIES_FLUSH
:
1208 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_COOKIELIST
, "FLUSH")) {
1220 static STATUS
php_http_client_curl_getopt(php_http_client_t
*h
, php_http_client_getopt_opt_t opt
, void *arg
)
1222 php_http_client_curl_t
*curl
= h
->ctx
;
1225 case PHP_HTTP_CLIENT_OPT_PROGRESS_INFO
:
1226 *((php_http_client_progress_t
**) arg
) = &curl
->progress
;
1229 case PHP_HTTP_CLIENT_OPT_TRANSFER_INFO
:
1230 get_info(curl
->handle
, arg
);
1240 static php_http_resource_factory_ops_t php_http_client_curl_resource_factory_ops
= {
1246 static php_http_client_ops_t php_http_client_curl_ops
= {
1247 &php_http_client_curl_resource_factory_ops
,
1248 php_http_client_curl_init
,
1249 php_http_client_curl_copy
,
1250 php_http_client_curl_dtor
,
1251 php_http_client_curl_reset
,
1252 php_http_client_curl_exec
,
1253 php_http_client_curl_setopt
,
1254 php_http_client_curl_getopt
1257 PHP_HTTP_API php_http_client_ops_t
*php_http_client_curl_get_ops(void)
1259 return &php_http_client_curl_ops
;
1263 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpClientCURL, method, 0, req_args)
1264 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpClientCURL, method, 0)
1265 #define PHP_HTTP_CURL_ME(method, visibility) PHP_ME(HttpClientCURL, method, PHP_HTTP_ARGS(HttpClientCURL, method), visibility)
1266 #define PHP_HTTP_CURL_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpClientCURL, method))
1267 #define PHP_HTTP_CURL_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpClientCURL_##al), PHP_HTTP_ARGS(HttpClientCURL, al), vis)
1270 zend_class_entry
*php_http_client_curl_class_entry
;
1271 zend_function_entry php_http_client_curl_method_entry
[] = {
1272 EMPTY_FUNCTION_ENTRY
1275 zend_object_value
php_http_client_curl_object_new(zend_class_entry
*ce TSRMLS_DC
)
1277 return php_http_client_curl_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
1280 zend_object_value
php_http_client_curl_object_new_ex(zend_class_entry
*ce
, php_http_client_t
*r
, php_http_client_object_t
**ptr TSRMLS_DC
)
1282 zend_object_value ov
;
1283 php_http_client_object_t
*o
;
1285 o
= ecalloc(1, sizeof(php_http_client_object_t
));
1286 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
1287 object_properties_init((zend_object
*) o
, ce
);
1289 if (!(o
->client
= r
)) {
1290 o
->client
= php_http_client_init(NULL
, &php_http_client_curl_ops
, NULL
, NULL TSRMLS_CC
);
1297 ov
.handle
= zend_objects_store_put(o
, NULL
, php_http_client_object_free
, NULL TSRMLS_CC
);
1298 ov
.handlers
= php_http_client_get_object_handlers();
1304 PHP_MINIT_FUNCTION(http_client_curl
)
1306 if (SUCCESS
!= php_http_persistent_handle_provide(ZEND_STRL("http_client.curl"), &php_http_client_curl_resource_factory_ops
, NULL
, NULL
)) {
1310 PHP_HTTP_REGISTER_CLASS(http
\\Client
, CURL
, http_client_curl
, php_http_client_class_entry
, 0);
1311 php_http_client_curl_class_entry
->create_object
= php_http_client_curl_object_new
;
1314 * HTTP Protocol Version Constants
1316 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("HTTP_VERSION_1_0"), CURL_HTTP_VERSION_1_0 TSRMLS_CC
);
1317 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("HTTP_VERSION_1_1"), CURL_HTTP_VERSION_1_1 TSRMLS_CC
);
1318 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("HTTP_VERSION_NONE"), CURL_HTTP_VERSION_NONE TSRMLS_CC
); /* to be removed */
1319 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("HTTP_VERSION_ANY"), CURL_HTTP_VERSION_NONE TSRMLS_CC
);
1322 * SSL Version Constants
1324 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("SSL_VERSION_TLSv1"), CURL_SSLVERSION_TLSv1 TSRMLS_CC
);
1325 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("SSL_VERSION_SSLv2"), CURL_SSLVERSION_SSLv2 TSRMLS_CC
);
1326 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("SSL_VERSION_SSLv3"), CURL_SSLVERSION_SSLv3 TSRMLS_CC
);
1327 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("SSL_VERSION_ANY"), CURL_SSLVERSION_DEFAULT TSRMLS_CC
);
1330 * DNS IPvX resolving
1332 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("IPRESOLVE_V4"), CURL_IPRESOLVE_V4 TSRMLS_CC
);
1333 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("IPRESOLVE_V6"), CURL_IPRESOLVE_V6 TSRMLS_CC
);
1334 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("IPRESOLVE_ANY"), CURL_IPRESOLVE_WHATEVER TSRMLS_CC
);
1339 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("AUTH_BASIC"), CURLAUTH_BASIC TSRMLS_CC
);
1340 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("AUTH_DIGEST"), CURLAUTH_DIGEST TSRMLS_CC
);
1341 #if PHP_HTTP_CURL_VERSION(7,19,3)
1342 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("AUTH_DIGEST_IE"), CURLAUTH_DIGEST_IE TSRMLS_CC
);
1344 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("AUTH_NTLM"), CURLAUTH_NTLM TSRMLS_CC
);
1345 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("AUTH_GSSNEG"), CURLAUTH_GSSNEGOTIATE TSRMLS_CC
);
1346 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("AUTH_ANY"), CURLAUTH_ANY TSRMLS_CC
);
1349 * Proxy Type Constants
1351 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("PROXY_SOCKS4"), CURLPROXY_SOCKS4 TSRMLS_CC
);
1352 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("PROXY_SOCKS4A"), CURLPROXY_SOCKS5 TSRMLS_CC
);
1353 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("PROXY_SOCKS5_HOSTNAME"), CURLPROXY_SOCKS5 TSRMLS_CC
);
1354 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("PROXY_SOCKS5"), CURLPROXY_SOCKS5 TSRMLS_CC
);
1355 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("PROXY_HTTP"), CURLPROXY_HTTP TSRMLS_CC
);
1356 # if PHP_HTTP_CURL_VERSION(7,19,4)
1357 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("PROXY_HTTP_1_0"), CURLPROXY_HTTP_1_0 TSRMLS_CC
);
1361 * Post Redirection Constants
1363 #if PHP_HTTP_CURL_VERSION(7,19,1)
1364 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("POSTREDIR_301"), CURL_REDIR_POST_301 TSRMLS_CC
);
1365 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("POSTREDIR_302"), CURL_REDIR_POST_302 TSRMLS_CC
);
1366 zend_declare_class_constant_long(php_http_client_curl_class_entry
, ZEND_STRL("POSTREDIR_ALL"), CURL_REDIR_POST_ALL TSRMLS_CC
);
1377 * vim600: noet sw=4 ts=4 fdm=marker
1378 * vim<600: noet sw=4 ts=4