2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
23 # include <winsock2.h>
26 #include <curl/curl.h>
28 #include "phpstr/phpstr.h"
32 #include "php_http_std_defs.h"
33 #include "php_http_api.h"
34 #include "php_http_request_api.h"
35 #include "php_http_request_object.h"
36 #include "php_http_requestpool_object.h"
37 #include "php_http_url_api.h"
39 #ifndef HTTP_CURL_USE_ZEND_MM
40 # define HTTP_CURL_USE_ZEND_MM 0
43 ZEND_EXTERN_MODULE_GLOBALS(http
)
45 #if LIBCURL_VERSION_NUM < 0x070c00
46 # define curl_easy_strerror(code) HTTP_G(request).curl.error
49 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
50 #define HTTP_CURL_INFO_EX(I, X) \
51 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
53 case CURLINFO_STRING: \
56 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
57 add_assoc_string(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
62 case CURLINFO_DOUBLE: \
65 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
66 add_assoc_double(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), d); \
74 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
75 add_assoc_long(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), l); \
81 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
82 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
83 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
84 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
85 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
86 if (!strcasecmp(key, #keyname)) { \
87 convert_to_string_ex(param); \
88 HTTP_CURL_OPT(optname, http_request_data_copy(COPY_STRING, Z_STRVAL_PP(param))); \
92 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
93 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
94 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
95 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
96 if (!strcasecmp(key, #keyname)) { \
97 convert_to_long_ex(param); \
98 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
104 static const char *const http_request_methods
[HTTP_MAX_REQUEST_METHOD
+ 1];
105 #define http_curl_getopt(o, k, t) _http_curl_getopt_ex((o), (k), sizeof(k), (t) TSRMLS_CC)
106 #define http_curl_getopt_ex(o, k, l, t) _http_curl_getopt_ex((o), (k), (l), (t) TSRMLS_CC)
107 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
);
108 static size_t http_curl_write_callback(char *, size_t, size_t, void *);
109 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
110 static int http_curl_progress_callback(void *, double, double, double, double);
111 static int http_curl_debug_callback(CURL
*, curl_infotype
, char *, size_t, void *);
116 } http_curl_callback_ctx
;
118 #define HTTP_CURL_CALLBACK_DATA(from, type, var) \
119 http_curl_callback_ctx *__CTX = (http_curl_callback_ctx *) (from); \
120 TSRMLS_FETCH_FROM_CTX(__CTX->tsrm_ctx); \
121 type (var) = (type) (__CTX->data)
123 #define http_curl_callback_data(data) _http_curl_callback_data((data) TSRMLS_CC)
124 static http_curl_callback_ctx
*_http_curl_callback_data(void *data TSRMLS_DC
);
126 static void http_request_pool_freebody(http_request_body
**body
);
127 static void http_request_pool_responsehandler(zval
**req TSRMLS_DC
);
128 static inline STATUS
http_request_pool_select(http_request_pool
*pool
);
129 static inline void http_request_pool_perform(http_request_pool
*pool
);
131 #if HTTP_CURL_USE_ZEND_MM
132 static void http_curl_free(void *p
) { efree(p
); }
133 static char *http_curl_strdup(const char *p
) { return estrdup(p
); }
134 static void *http_curl_malloc(size_t s
) { return emalloc(s
); }
135 static void *http_curl_realloc(void *p
, size_t s
) { return erealloc(p
, s
); }
136 static void *http_curl_calloc(size_t n
, size_t s
) { return ecalloc(n
, s
); }
139 /* {{{ STATUS http_request_global_init() */
140 STATUS
_http_request_global_init(void)
142 #if HTTP_CURL_USE_ZEND_MM
143 if (CURLE_OK
!= curl_global_init_mem(CURL_GLOBAL_ALL
,
152 if (CURLE_OK
!= curl_global_init(CURL_GLOBAL_ALL
)) {
160 /* {{{ void *http_request_data_copy(int, void *) */
161 void *_http_request_data_copy(int type
, void *data TSRMLS_DC
)
167 char *new_str
= estrdup((const char*) data
);
168 zend_llist_add_element(&HTTP_G(request
).copies
.strings
, &new_str
);
174 zend_llist_add_element(&HTTP_G(request
).copies
.slists
, &data
);
186 /* {{{ void http_request_data_free_string(char **) */
187 void _http_request_data_free_string(void *string
)
189 efree(*((char **)string
));
193 /* {{{ void http_request_data_free_slist(struct curl_slist **) */
194 void _http_request_data_free_slist(void *list
)
196 curl_slist_free_all(*((struct curl_slist
**) list
));
200 /* {{{ http_request_body *http_request_body_new() */
201 PHP_HTTP_API http_request_body
*_http_request_body_new(TSRMLS_D
)
203 http_request_body
*body
= ecalloc(1, sizeof(http_request_body
));
208 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
209 PHP_HTTP_API STATUS
_http_request_body_fill(http_request_body
*body
, HashTable
*fields
, HashTable
*files TSRMLS_DC
)
211 if (files
&& (zend_hash_num_elements(files
) > 0)) {
215 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
218 FOREACH_HASH_KEYVAL(fields
, key
, idx
, data
) {
221 convert_to_string_ex(data
);
222 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
223 CURLFORM_COPYNAME
, key
,
224 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
225 CURLFORM_CONTENTSLENGTH
, Z_STRLEN_PP(data
),
228 if (CURLE_OK
!= err
) {
229 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not encode post fields: %s", curl_easy_strerror(err
));
230 curl_formfree(http_post_data
[0]);
240 FOREACH_HASH_VAL(files
, data
) {
242 zval
**file
, **type
, **name
;
243 if ( SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) &&
244 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) &&
245 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
246 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
247 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
248 CURLFORM_FILE
, Z_STRVAL_PP(file
),
249 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
252 if (CURLE_OK
!= err
) {
253 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not encode post files: %s", curl_easy_strerror(err
));
254 curl_formfree(http_post_data
[0]);
258 http_error(E_NOTICE
, HTTP_E_PARAM
, "Post file array entry misses either 'name', 'type' or 'file' entry");
262 body
->type
= HTTP_REQUEST_BODY_CURLPOST
;
263 body
->data
= http_post_data
[0];
270 if (SUCCESS
!= http_urlencode_hash_ex(fields
, 1, NULL
, 0, &encoded
, &encoded_len
)) {
271 http_error(E_WARNING
, HTTP_E_ENCODE
, "Could not encode post data");
275 body
->type
= HTTP_REQUEST_BODY_CSTRING
;
276 body
->data
= encoded
;
277 body
->size
= encoded_len
;
284 /* {{{ void http_request_body_dtor(http_request_body *) */
285 PHP_HTTP_API
void _http_request_body_dtor(http_request_body
*body TSRMLS_DC
)
290 case HTTP_REQUEST_BODY_CSTRING
:
296 case HTTP_REQUEST_BODY_CURLPOST
:
297 curl_formfree(body
->data
);
300 case HTTP_REQUEST_BODY_UPLOADFILE
:
301 php_stream_close(body
->data
);
308 /* {{{ void http_request_body_free(http_request_body *) */
309 PHP_HTTP_API
void _http_request_body_free(http_request_body
*body TSRMLS_DC
)
312 http_request_body_dtor(body
);
318 /* {{{ STATUS http_request_init(CURL *, http_request_method, char *, http_request_body *, HashTable *, phpstr *) */
319 PHP_HTTP_API STATUS
_http_request_init(CURL
*ch
, http_request_method meth
, const char *url
, http_request_body
*body
, HashTable
*options
, phpstr
*response TSRMLS_DC
)
322 zend_bool range_req
= 0;
324 /* reset CURL handle */
325 #if LIBCURL_VERSION_NUM >= 0x070c01
331 HTTP_CURL_OPT(URL
, http_request_data_copy(COPY_STRING
, (void *) url
));
335 HTTP_CURL_OPT(WRITEDATA
, http_curl_callback_data(response
));
336 HTTP_CURL_OPT(WRITEHEADER
, http_curl_callback_data(response
));
339 HTTP_CURL_OPT(HEADER
, 0);
340 HTTP_CURL_OPT(FILETIME
, 1);
341 HTTP_CURL_OPT(AUTOREFERER
, 1);
342 HTTP_CURL_OPT(READFUNCTION
, http_curl_read_callback
);
343 HTTP_CURL_OPT(WRITEFUNCTION
, http_curl_write_callback
);
344 HTTP_CURL_OPT(HEADERFUNCTION
, http_curl_write_callback
);
346 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
347 HTTP_CURL_OPT(NOSIGNAL
, 1);
349 #if LIBCURL_VERSION_NUM < 0x070c00
350 HTTP_CURL_OPT(ERRORBUFFER
, HTTP_G(request
).curl
.error
);
353 /* progress callback */
354 if (zoption
= http_curl_getopt(options
, "onprogress", 0)) {
355 HTTP_CURL_OPT(PROGRESSFUNCTION
, http_curl_progress_callback
);
356 HTTP_CURL_OPT(PROGRESSDATA
, http_curl_callback_data(zoption
));
358 HTTP_CURL_OPT(NOPROGRESS
, 1);
362 if (zoption
= http_curl_getopt(options
, "ondebug", 0)) {
363 HTTP_CURL_OPT(VERBOSE
, 1);
364 HTTP_CURL_OPT(DEBUGFUNCTION
, http_curl_debug_callback
);
365 HTTP_CURL_OPT(DEBUGDATA
, http_curl_callback_data(zoption
));
367 HTTP_CURL_OPT(VERBOSE
, 0);
371 if (zoption
= http_curl_getopt(options
, "proxyhost", IS_STRING
)) {
372 HTTP_CURL_OPT(PROXY
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
374 if (zoption
= http_curl_getopt(options
, "proxyport", IS_LONG
)) {
375 HTTP_CURL_OPT(PROXYPORT
, Z_LVAL_P(zoption
));
378 if (zoption
= http_curl_getopt(options
, "proxyauth", IS_STRING
)) {
379 HTTP_CURL_OPT(PROXYUSERPWD
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
381 #if LIBCURL_VERSION_NUM >= 0x070a07
383 if (zoption
= http_curl_getopt(options
, "proxyauthtype", IS_LONG
)) {
384 HTTP_CURL_OPT(PROXYAUTH
, Z_LVAL_P(zoption
));
389 /* outgoing interface */
390 if (zoption
= http_curl_getopt(options
, "interface", IS_STRING
)) {
391 HTTP_CURL_OPT(INTERFACE
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
395 if (zoption
= http_curl_getopt(options
, "port", IS_LONG
)) {
396 HTTP_CURL_OPT(PORT
, Z_LVAL_P(zoption
));
400 if (zoption
= http_curl_getopt(options
, "httpauth", IS_STRING
)) {
401 HTTP_CURL_OPT(USERPWD
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
403 #if LIBCURL_VERSION_NUM >= 0x070a06
404 if (zoption
= http_curl_getopt(options
, "httpauthtype", IS_LONG
)) {
405 HTTP_CURL_OPT(HTTPAUTH
, Z_LVAL_P(zoption
));
409 /* compress, empty string enables deflate and gzip */
410 if (zoption
= http_curl_getopt(options
, "compress", IS_BOOL
)) {
411 if (Z_LVAL_P(zoption
)) {
412 HTTP_CURL_OPT(ENCODING
, http_request_data_copy(COPY_STRING
, ""));
416 /* redirects, defaults to 0 */
417 if (zoption
= http_curl_getopt(options
, "redirect", IS_LONG
)) {
418 HTTP_CURL_OPT(FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
419 HTTP_CURL_OPT(MAXREDIRS
, Z_LVAL_P(zoption
));
420 if (zoption
= http_curl_getopt(options
, "unrestrictedauth", IS_BOOL
)) {
421 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
424 HTTP_CURL_OPT(FOLLOWLOCATION
, 0);
428 if (zoption
= http_curl_getopt(options
, "referer", IS_STRING
)) {
429 HTTP_CURL_OPT(REFERER
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
432 /* useragent, default "PECL::HTTP/version (PHP/version)" */
433 if (zoption
= http_curl_getopt(options
, "useragent", IS_STRING
)) {
434 HTTP_CURL_OPT(USERAGENT
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
436 HTTP_CURL_OPT(USERAGENT
, http_request_data_copy(COPY_STRING
, "PECL::HTTP/" HTTP_PEXT_VERSION
" (PHP/" PHP_VERSION
")"));
439 /* additional headers, array('name' => 'value') */
440 if (zoption
= http_curl_getopt(options
, "headers", IS_ARRAY
)) {
443 struct curl_slist
*headers
= NULL
;
445 FOREACH_KEY(zoption
, header_key
, header_idx
) {
448 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &header_val
)) {
449 char header
[1024] = {0};
450 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
451 headers
= curl_slist_append(headers
, http_request_data_copy(COPY_STRING
, header
));
460 HTTP_CURL_OPT(HTTPHEADER
, http_request_data_copy(COPY_SLIST
, headers
));
463 HTTP_CURL_OPT(HTTPHEADER
, NULL
);
466 /* cookies, array('name' => 'value') */
467 if (zoption
= http_curl_getopt(options
, "cookies", IS_ARRAY
)) {
468 char *cookie_key
= NULL
;
469 ulong cookie_idx
= 0;
470 phpstr
*qstr
= phpstr_new();
472 FOREACH_KEY(zoption
, cookie_key
, cookie_idx
) {
475 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &cookie_val
)) {
476 phpstr_appendf(qstr
, "%s=%s; ", cookie_key
, Z_STRVAL_PP(cookie_val
));
486 HTTP_CURL_OPT(COOKIE
, http_request_data_copy(COPY_STRING
, qstr
->data
));
492 if (zoption
= http_curl_getopt(options
, "cookiestore", IS_STRING
)) {
493 HTTP_CURL_OPT(COOKIEFILE
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
494 HTTP_CURL_OPT(COOKIEJAR
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
498 if (zoption
= http_curl_getopt(options
, "resume", IS_LONG
)) {
500 HTTP_CURL_OPT(RESUME_FROM
, Z_LVAL_P(zoption
));
504 if (zoption
= http_curl_getopt(options
, "maxfilesize", IS_LONG
)) {
505 HTTP_CURL_OPT(MAXFILESIZE
, Z_LVAL_P(zoption
));
509 if (zoption
= http_curl_getopt(options
, "lastmodified", IS_LONG
)) {
510 HTTP_CURL_OPT(TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
511 HTTP_CURL_OPT(TIMEVALUE
, Z_LVAL_P(zoption
));
515 if (zoption
= http_curl_getopt(options
, "timeout", IS_LONG
)) {
516 HTTP_CURL_OPT(TIMEOUT
, Z_LVAL_P(zoption
));
519 /* connecttimeout, defaults to 1 */
520 if (zoption
= http_curl_getopt(options
, "connecttimeout", IS_LONG
)) {
521 HTTP_CURL_OPT(CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
523 HTTP_CURL_OPT(CONNECTTIMEOUT
, 1);
527 if (zoption
= http_curl_getopt(options
, "ssl", IS_ARRAY
)) {
532 FOREACH_KEYVAL(zoption
, key
, idx
, param
) {
534 HTTP_CURL_OPT_SSL_STRING(CERT
);
535 #if LIBCURL_VERSION_NUM >= 0x070903
536 HTTP_CURL_OPT_SSL_STRING(CERTTYPE
);
538 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD
);
540 HTTP_CURL_OPT_SSL_STRING(KEY
);
541 HTTP_CURL_OPT_SSL_STRING(KEYTYPE
);
542 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD
);
544 HTTP_CURL_OPT_SSL_STRING(ENGINE
);
545 HTTP_CURL_OPT_SSL_LONG(VERSION
);
547 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER
);
548 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST
);
549 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST
);
552 HTTP_CURL_OPT_STRING(CAINFO
);
553 #if LIBCURL_VERSION_NUM >= 0x070908
554 HTTP_CURL_OPT_STRING(CAPATH
);
556 HTTP_CURL_OPT_STRING(RANDOM_FILE
);
557 HTTP_CURL_OPT_STRING(EGDSOCKET
);
564 /* disable SSL verification by default */
565 HTTP_CURL_OPT(SSL_VERIFYPEER
, 0);
566 HTTP_CURL_OPT(SSL_VERIFYHOST
, 0);
573 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1);
577 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
581 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
585 curl_easy_setopt(ch
, CURLOPT_UPLOAD
, 1);
589 if (http_request_method_exists(0, meth
, NULL
)) {
590 curl_easy_setopt(ch
, CURLOPT_CUSTOMREQUEST
, http_request_method_name(meth
));
592 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Unsupported request method: %d", meth
);
598 /* attach request body */
599 if (body
&& (meth
!= HTTP_GET
) && (meth
!= HTTP_HEAD
)) {
602 case HTTP_REQUEST_BODY_CSTRING
:
603 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, (char *) body
->data
);
604 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, body
->size
);
607 case HTTP_REQUEST_BODY_CURLPOST
:
608 curl_easy_setopt(ch
, CURLOPT_HTTPPOST
, (struct curl_httppost
*) body
->data
);
611 case HTTP_REQUEST_BODY_UPLOADFILE
:
612 case HTTP_REQUEST_BODY_UPLOADDATA
:
613 curl_easy_setopt(ch
, CURLOPT_READDATA
, body
);
614 curl_easy_setopt(ch
, CURLOPT_INFILESIZE
, body
->size
);
618 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Unknown request body type: %d", body
->type
);
628 /* {{{ STATUS http_request_exec(CURL *, HashTable *) */
629 PHP_HTTP_API STATUS
_http_request_exec(CURL
*ch
, HashTable
*info TSRMLS_DC
)
633 /* perform request */
634 if (CURLE_OK
!= (result
= curl_easy_perform(ch
))) {
635 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not perform request: %s", curl_easy_strerror(result
));
640 http_request_info(ch
, info
);
647 /* {{{ void http_request_info(CURL *, HashTable *) */
648 PHP_HTTP_API
void _http_request_info(CURL
*ch
, HashTable
*info TSRMLS_DC
)
651 Z_ARRVAL(array
) = info
;
653 HTTP_CURL_INFO(EFFECTIVE_URL
);
654 #if LIBCURL_VERSION_NUM >= 0x070a07
655 HTTP_CURL_INFO(RESPONSE_CODE
);
657 HTTP_CURL_INFO_EX(HTTP_CODE
, RESPONSE_CODE
);
659 HTTP_CURL_INFO(HTTP_CONNECTCODE
);
660 #if LIBCURL_VERSION_NUM >= 0x070500
661 HTTP_CURL_INFO(FILETIME
);
663 HTTP_CURL_INFO(TOTAL_TIME
);
664 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
665 HTTP_CURL_INFO(CONNECT_TIME
);
666 HTTP_CURL_INFO(PRETRANSFER_TIME
);
667 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
668 #if LIBCURL_VERSION_NUM >= 0x070907
669 HTTP_CURL_INFO(REDIRECT_TIME
);
670 HTTP_CURL_INFO(REDIRECT_COUNT
);
672 HTTP_CURL_INFO(SIZE_UPLOAD
);
673 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
674 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
675 HTTP_CURL_INFO(SPEED_UPLOAD
);
676 HTTP_CURL_INFO(HEADER_SIZE
);
677 HTTP_CURL_INFO(REQUEST_SIZE
);
678 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
679 #if LIBCURL_VERSION_NUM >= 0x070c03
680 /*HTTP_CURL_INFO(SSL_ENGINES); todo: CURLINFO_SLIST */
682 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
683 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
684 HTTP_CURL_INFO(CONTENT_TYPE
);
685 #if LIBCURL_VERSION_NUM >= 0x070a03
686 /*HTTP_CURL_INFO(PRIVATE);*/
688 #if LIBCURL_VERSION_NUM >= 0x070a08
689 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
690 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
692 #if LIBCURL_VERSION_NUM >= 0x070c02
693 /*HTTP_CURL_INFO(OS_ERRNO);*/
695 #if LIBCURL_VERSION_NUM >= 0x070c03
696 HTTP_CURL_INFO(NUM_CONNECTS
);
701 /* {{{ STATUS http_request_ex(CURL *, http_request_method, char *, http_request_body, HashTable, HashTable, phpstr *) */
702 PHP_HTTP_API STATUS
_http_request_ex(CURL
*ch
, http_request_method meth
, const char *url
, http_request_body
*body
, HashTable
*options
, HashTable
*info
, phpstr
*response TSRMLS_DC
)
704 if (SUCCESS
!= http_request_init(ch
, meth
, url
, body
, options
, response
)) {
707 return http_request_exec(ch
, info
);
711 /* {{{ char *http_request_method_name(http_request_method) */
712 PHP_HTTP_API
const char *_http_request_method_name(http_request_method m TSRMLS_DC
)
716 if (HTTP_STD_REQUEST_METHOD(m
)) {
717 return http_request_methods
[m
];
720 if (SUCCESS
== zend_hash_index_find(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(m
), (void **) &meth
)) {
721 return Z_STRVAL_PP(meth
);
724 return http_request_methods
[0];
728 /* {{{ unsigned long http_request_method_exists(zend_bool, unsigned long, char *) */
729 PHP_HTTP_API
unsigned long _http_request_method_exists(zend_bool by_name
, unsigned long id
, const char *name TSRMLS_DC
)
734 for (i
= HTTP_NO_REQUEST_METHOD
+ 1; i
< HTTP_MAX_REQUEST_METHOD
; ++i
) {
735 if (!strcmp(name
, http_request_methods
[i
])) {
744 FOREACH_HASH_KEYVAL(&HTTP_G(request
).methods
.custom
, key
, idx
, data
) {
745 if (!strcmp(name
, Z_STRVAL_PP(data
))) {
746 return idx
+ HTTP_MAX_REQUEST_METHOD
;
752 return HTTP_STD_REQUEST_METHOD(id
) || zend_hash_index_exists(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(id
)) ? id
: 0;
757 /* {{{ unsigned long http_request_method_register(char *) */
758 PHP_HTTP_API
unsigned long _http_request_method_register(const char *method TSRMLS_DC
)
762 unsigned long meth_num
= HTTP_G(request
).methods
.custom
.nNextFreeElement
+ HTTP_MAX_REQUEST_METHOD
;
764 Z_ARRVAL(array
) = &HTTP_G(request
).methods
.custom
;
765 add_next_index_string(&array
, estrdup(method
), 0);
767 spprintf(&http_method
, 0, "HTTP_%s", method
);
768 zend_register_long_constant(http_method
, strlen(http_method
) + 1, meth_num
, CONST_CS
, http_module_number TSRMLS_CC
);
775 /* {{{ STATUS http_request_method_unregister(usngigned long) */
776 PHP_HTTP_API STATUS
_http_request_method_unregister(unsigned long method TSRMLS_DC
)
781 if (SUCCESS
!= zend_hash_index_find(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(method
), (void **) &zmethod
)) {
782 http_error_ex(E_NOTICE
, HTTP_E_PARAM
, "Request method with id %lu does not exist", method
);
786 spprintf(&http_method
, 0, "HTTP_%s", Z_STRVAL_PP(zmethod
));
788 if ( (SUCCESS
!= zend_hash_index_del(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(method
)))
789 || (SUCCESS
!= zend_hash_del(EG(zend_constants
), http_method
, strlen(http_method
) + 1))) {
790 http_error_ex(E_NOTICE
, 0, "Could not unregister request method: %s", http_method
);
801 /* {{{ http_request_pool *http_request_pool_init(http_request_pool *) */
802 PHP_HTTP_API http_request_pool
*_http_request_pool_init(http_request_pool
*pool TSRMLS_DC
)
806 if ((free_pool
= (!pool
))) {
807 pool
= emalloc(sizeof(http_request_pool
));
812 if (!(pool
->ch
= curl_multi_init())) {
813 http_error(E_WARNING
, HTTP_E_CURL
, "Could not initialize curl");
821 pool
->unfinished
= 0;
822 zend_llist_init(&pool
->handles
, sizeof(zval
*), (llist_dtor_func_t
) ZVAL_PTR_DTOR
, 0);
823 zend_llist_init(&pool
->bodies
, sizeof(http_request_body
*), (llist_dtor_func_t
) http_request_pool_freebody
, 0);
829 /* {{{ STATUS http_request_pool_attach(http_request_pool *, zval *) */
830 PHP_HTTP_API STATUS
_http_request_pool_attach(http_request_pool
*pool
, zval
*request TSRMLS_DC
)
832 getObjectEx(http_request_object
, req
, request
);
835 http_error(E_WARNING
, HTTP_E_CURL
, "HttpRequest object is already member of an HttpRequestPool");
838 http_request_body
*body
= http_request_body_new();
839 zval
*info
= GET_PROP_EX(req
, request
, responseInfo
);
841 if (SUCCESS
!= http_request_object_requesthandler(req
, request
, body
)) {
843 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not initialize HttpRequest object for attaching to the HttpRequestPool");
844 } else if (CURLM_OK
!= (code
= curl_multi_add_handle(pool
->ch
, req
->ch
))) {
845 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not attach HttpRequest object to the HttpRequestPool: %s", curl_multi_strerror(code
));
848 zval_add_ref(&request
);
849 zend_llist_add_element(&pool
->handles
, &request
);
850 zend_llist_add_element(&pool
->bodies
, &body
);
858 /* {{{ STATUS http_request_pool_detach(http_request_pool *, zval *) */
859 PHP_HTTP_API STATUS
_http_request_pool_detach(http_request_pool
*pool
, zval
*request TSRMLS_DC
)
861 getObjectEx(http_request_object
, req
, request
);
863 if (!req
->attached
) {
864 http_error(E_WARNING
, HTTP_E_CURL
, "HttpRequest object is not attached to an HttpRequestPool");
868 if (CURLM_OK
!= (code
= curl_multi_remove_handle(pool
->ch
, req
->ch
))) {
869 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not detach HttpRequest object from the HttpRequestPool: %s", curl_multi_strerror(code
));
872 zval_ptr_dtor(&request
);
880 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
881 PHP_HTTP_API STATUS
_http_request_pool_send(http_request_pool
*pool TSRMLS_DC
)
883 http_request_pool_perform(pool
);
884 while (pool
->unfinished
) {
885 if (SUCCESS
!= http_request_pool_select(pool
)) {
886 http_error(E_WARNING
, HTTP_E_CURL
, "Socket error");
889 http_request_pool_perform(pool
);
891 zend_llist_apply(&pool
->handles
, (llist_apply_func_t
) http_request_pool_responsehandler TSRMLS_CC
);
898 /* {{{ static void http_request_pool_free_body(http_request_body *) */
899 static void http_request_pool_freebody(http_request_body
**body
)
902 http_request_body_free(*body
);
906 /* {{{ static void http_request_pool_responsehandler(zval **) */
907 static void http_request_pool_responsehandler(zval
**req TSRMLS_DC
)
909 getObjectEx(http_request_object
, obj
, *req
);
910 http_request_object_responsehandler(obj
, *req
, NULL
);
914 /* {{{ static inline STATUS http_request_pool_select(http_request_pool *) */
915 static inline STATUS
http_request_pool_select(http_request_pool
*pool
)
919 struct timeval timeout
= {1, 0};
925 curl_multi_fdset(pool
->ch
, &R
, &W
, &E
, &MAX
);
926 return (-1 != select(MAX
+ 1, &R
, &W
, &E
, &timeout
)) ? SUCCESS
: FAILURE
;
930 /* {{{ static inline void http_request_pool_perform(http_request_pool *) */
931 static inline void http_request_pool_perform(http_request_pool
*pool
)
933 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(pool
->ch
, &pool
->unfinished
));
937 /* {{{ char *http_request_methods[] */
938 static const char *const http_request_methods
[] = {
949 /* WebDAV - RFC 2518 */
957 /* WebDAV Versioning - RFC 3253 */
969 /* WebDAV Access Control - RFC 3744 */
975 /* {{{ static size_t http_curl_write_callback(char *, size_t, size_t, void *) */
976 static size_t http_curl_write_callback(char *buf
, size_t len
, size_t n
, void *s
)
978 HTTP_CURL_CALLBACK_DATA(s
, phpstr
*, str
);
979 return str
? phpstr_append(PHPSTR(str
), buf
, len
* n
) : len
* n
;
983 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
984 static size_t http_curl_read_callback(void *data
, size_t len
, size_t n
, void *s
)
986 static char *offset
= NULL
, *original
= NULL
;
987 HTTP_CURL_CALLBACK_DATA(s
, http_request_body
*, body
);
991 case HTTP_REQUEST_BODY_UPLOADFILE
:
994 return php_stream_read((php_stream
*) body
->data
, data
, len
* n
);
998 case HTTP_REQUEST_BODY_UPLOADDATA
:
1001 if (original
!= s
) {
1002 original
= offset
= s
;
1004 if ((avail
= body
->size
- (offset
- original
)) < 1) {
1007 if (avail
< (len
* n
)) {
1008 memcpy(data
, offset
, avail
);
1012 memcpy(data
, offset
, len
* n
);
1026 /* {{{ http_curl_callback_ctx http_curl_callback_data(void *) */
1027 static http_curl_callback_ctx
*_http_curl_callback_data(void *data TSRMLS_DC
)
1029 http_curl_callback_ctx
*ctx
= emalloc(sizeof(http_curl_callback_ctx
));
1030 TSRMLS_SET_CTX(ctx
->tsrm_ctx
);
1036 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
1037 static int http_curl_progress_callback(void *data
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
1039 zval
*params_pass
[4], params_local
[4], retval
;
1040 HTTP_CURL_CALLBACK_DATA(data
, zval
*, func
);
1042 params_pass
[0] = ¶ms_local
[0];
1043 params_pass
[1] = ¶ms_local
[1];
1044 params_pass
[2] = ¶ms_local
[2];
1045 params_pass
[3] = ¶ms_local
[3];
1047 ZVAL_DOUBLE(params_pass
[0], dltotal
);
1048 ZVAL_DOUBLE(params_pass
[1], dlnow
);
1049 ZVAL_DOUBLE(params_pass
[2], ultotal
);
1050 ZVAL_DOUBLE(params_pass
[3], ulnow
);
1052 return call_user_function(EG(function_table
), NULL
, func
, &retval
, 4, params_pass TSRMLS_CC
);
1056 static int http_curl_debug_callback(CURL
*ch
, curl_infotype type
, char *string
, size_t length
, void *data
)
1058 zval
*params_pass
[2], params_local
[2], retval
;
1059 HTTP_CURL_CALLBACK_DATA(data
, zval
*, func
);
1061 params_pass
[0] = ¶ms_local
[0];
1062 params_pass
[1] = ¶ms_local
[1];
1064 ZVAL_LONG(params_pass
[0], type
);
1065 ZVAL_STRINGL(params_pass
[1], string
, length
, 1);
1067 call_user_function(EG(function_table
), NULL
, func
, &retval
, 2, params_pass TSRMLS_CC
);
1071 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
1072 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
)
1076 if (!options
|| (SUCCESS
!= zend_hash_find(options
, key
, keylen
, (void **) &zoption
))) {
1080 if (Z_TYPE_PP(zoption
) != type
) {
1083 case IS_BOOL
: convert_to_boolean_ex(zoption
); break;
1084 case IS_LONG
: convert_to_long_ex(zoption
); break;
1085 case IS_DOUBLE
: convert_to_double_ex(zoption
); break;
1086 case IS_STRING
: convert_to_string_ex(zoption
); break;
1087 case IS_ARRAY
: convert_to_array_ex(zoption
); break;
1088 case IS_OBJECT
: convert_to_object_ex(zoption
); break;
1103 * vim600: noet sw=4 ts=4 fdm=marker
1104 * vim<600: noet sw=4 ts=4