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 #ifndef HTTP_DEBUG_REQPOOLS
44 # define HTTP_DEBUG_REQPOOLS 0
47 ZEND_EXTERN_MODULE_GLOBALS(http
)
49 #if LIBCURL_VERSION_NUM < 0x070c00
50 # define curl_easy_strerror(code) HTTP_G(request).curl.error
53 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
54 #define HTTP_CURL_INFO_EX(I, X) \
55 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
57 case CURLINFO_STRING: \
60 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
61 add_assoc_string(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
66 case CURLINFO_DOUBLE: \
69 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
70 add_assoc_double(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), d); \
78 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
79 add_assoc_long(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), l); \
85 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
86 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
87 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
88 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
89 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
90 if (!strcasecmp(key, #keyname)) { \
91 convert_to_string_ex(param); \
92 HTTP_CURL_OPT(optname, http_request_data_copy(COPY_STRING, Z_STRVAL_PP(param))); \
96 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
97 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
98 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
99 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
100 if (!strcasecmp(key, #keyname)) { \
101 convert_to_long_ex(param); \
102 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
108 static const char *const http_request_methods
[HTTP_MAX_REQUEST_METHOD
+ 1];
109 #define http_curl_getopt(o, k, t) _http_curl_getopt_ex((o), (k), sizeof(k), (t) TSRMLS_CC)
110 #define http_curl_getopt_ex(o, k, l, t) _http_curl_getopt_ex((o), (k), (l), (t) TSRMLS_CC)
111 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
);
112 static size_t http_curl_write_callback(char *, size_t, size_t, void *);
113 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
114 static int http_curl_progress_callback(void *, double, double, double, double);
115 static int http_curl_debug_callback(CURL
*, curl_infotype
, char *, size_t, void *);
120 } http_curl_callback_ctx
;
122 #define HTTP_CURL_CALLBACK_DATA(from, type, var) \
123 http_curl_callback_ctx *__CTX = (http_curl_callback_ctx *) (from); \
124 TSRMLS_FETCH_FROM_CTX(__CTX->tsrm_ctx); \
125 type (var) = (type) (__CTX->data)
127 #define http_curl_callback_data(data) _http_curl_callback_data((data) TSRMLS_CC)
128 static http_curl_callback_ctx
*_http_curl_callback_data(void *data TSRMLS_DC
);
130 static void http_request_pool_freebody(http_request_body
**body
);
131 static void http_request_pool_freehandle(zval
**request
, http_request_pool
*pool TSRMLS_DC
);
132 static void http_request_pool_responsehandler(zval
**req TSRMLS_DC
);
133 static inline STATUS
http_request_pool_select(http_request_pool
*pool
);
134 static inline void http_request_pool_perform(http_request_pool
*pool
);
136 #if HTTP_CURL_USE_ZEND_MM
137 static void http_curl_free(void *p
) { efree(p
); }
138 static char *http_curl_strdup(const char *p
) { return estrdup(p
); }
139 static void *http_curl_malloc(size_t s
) { return emalloc(s
); }
140 static void *http_curl_realloc(void *p
, size_t s
) { return erealloc(p
, s
); }
141 static void *http_curl_calloc(size_t n
, size_t s
) { return ecalloc(n
, s
); }
144 /* {{{ STATUS http_request_global_init() */
145 STATUS
_http_request_global_init(void)
147 #if HTTP_CURL_USE_ZEND_MM
148 if (CURLE_OK
!= curl_global_init_mem(CURL_GLOBAL_ALL
,
157 if (CURLE_OK
!= curl_global_init(CURL_GLOBAL_ALL
)) {
165 /* {{{ void *http_request_data_copy(int, void *) */
166 void *_http_request_data_copy(int type
, void *data TSRMLS_DC
)
172 char *new_str
= estrdup((const char*) data
);
173 zend_llist_add_element(&HTTP_G(request
).copies
.strings
, &new_str
);
179 zend_llist_add_element(&HTTP_G(request
).copies
.slists
, &data
);
191 /* {{{ void http_request_data_free_string(char **) */
192 void _http_request_data_free_string(void *string
)
194 efree(*((char **)string
));
198 /* {{{ void http_request_data_free_slist(struct curl_slist **) */
199 void _http_request_data_free_slist(void *list
)
201 curl_slist_free_all(*((struct curl_slist
**) list
));
205 /* {{{ http_request_body *http_request_body_new() */
206 PHP_HTTP_API http_request_body
*_http_request_body_new(TSRMLS_D
)
208 http_request_body
*body
= ecalloc(1, sizeof(http_request_body
));
213 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
214 PHP_HTTP_API STATUS
_http_request_body_fill(http_request_body
*body
, HashTable
*fields
, HashTable
*files TSRMLS_DC
)
216 if (files
&& (zend_hash_num_elements(files
) > 0)) {
220 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
223 FOREACH_HASH_KEYVAL(fields
, key
, idx
, data
) {
226 convert_to_string_ex(data
);
227 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
228 CURLFORM_COPYNAME
, key
,
229 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
230 CURLFORM_CONTENTSLENGTH
, Z_STRLEN_PP(data
),
233 if (CURLE_OK
!= err
) {
234 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not encode post fields: %s", curl_easy_strerror(err
));
235 curl_formfree(http_post_data
[0]);
245 FOREACH_HASH_VAL(files
, data
) {
247 zval
**file
, **type
, **name
;
248 if ( SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) &&
249 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) &&
250 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
251 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
252 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
253 CURLFORM_FILE
, Z_STRVAL_PP(file
),
254 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
257 if (CURLE_OK
!= err
) {
258 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not encode post files: %s", curl_easy_strerror(err
));
259 curl_formfree(http_post_data
[0]);
263 http_error(E_NOTICE
, HTTP_E_PARAM
, "Post file array entry misses either 'name', 'type' or 'file' entry");
267 body
->type
= HTTP_REQUEST_BODY_CURLPOST
;
268 body
->data
= http_post_data
[0];
275 if (SUCCESS
!= http_urlencode_hash_ex(fields
, 1, NULL
, 0, &encoded
, &encoded_len
)) {
276 http_error(E_WARNING
, HTTP_E_ENCODE
, "Could not encode post data");
280 body
->type
= HTTP_REQUEST_BODY_CSTRING
;
281 body
->data
= encoded
;
282 body
->size
= encoded_len
;
289 /* {{{ void http_request_body_dtor(http_request_body *) */
290 PHP_HTTP_API
void _http_request_body_dtor(http_request_body
*body TSRMLS_DC
)
295 case HTTP_REQUEST_BODY_CSTRING
:
301 case HTTP_REQUEST_BODY_CURLPOST
:
302 curl_formfree(body
->data
);
305 case HTTP_REQUEST_BODY_UPLOADFILE
:
306 php_stream_close(body
->data
);
313 /* {{{ void http_request_body_free(http_request_body *) */
314 PHP_HTTP_API
void _http_request_body_free(http_request_body
*body TSRMLS_DC
)
317 http_request_body_dtor(body
);
323 /* {{{ STATUS http_request_init(CURL *, http_request_method, char *, http_request_body *, HashTable *, phpstr *) */
324 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
)
327 zend_bool range_req
= 0;
329 /* reset CURL handle */
330 #if LIBCURL_VERSION_NUM >= 0x070c01
336 HTTP_CURL_OPT(URL
, http_request_data_copy(COPY_STRING
, (void *) url
));
340 HTTP_CURL_OPT(WRITEDATA
, http_curl_callback_data(response
));
341 HTTP_CURL_OPT(WRITEHEADER
, http_curl_callback_data(response
));
344 HTTP_CURL_OPT(HEADER
, 0);
345 HTTP_CURL_OPT(FILETIME
, 1);
346 HTTP_CURL_OPT(AUTOREFERER
, 1);
347 HTTP_CURL_OPT(READFUNCTION
, http_curl_read_callback
);
348 HTTP_CURL_OPT(WRITEFUNCTION
, http_curl_write_callback
);
349 HTTP_CURL_OPT(HEADERFUNCTION
, http_curl_write_callback
);
351 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
352 HTTP_CURL_OPT(NOSIGNAL
, 1);
354 #if LIBCURL_VERSION_NUM < 0x070c00
355 HTTP_CURL_OPT(ERRORBUFFER
, HTTP_G(request
).curl
.error
);
358 /* progress callback */
359 if (zoption
= http_curl_getopt(options
, "onprogress", 0)) {
360 HTTP_CURL_OPT(PROGRESSFUNCTION
, http_curl_progress_callback
);
361 HTTP_CURL_OPT(PROGRESSDATA
, http_curl_callback_data(zoption
));
363 HTTP_CURL_OPT(NOPROGRESS
, 1);
367 if (zoption
= http_curl_getopt(options
, "ondebug", 0)) {
368 HTTP_CURL_OPT(VERBOSE
, 1);
369 HTTP_CURL_OPT(DEBUGFUNCTION
, http_curl_debug_callback
);
370 HTTP_CURL_OPT(DEBUGDATA
, http_curl_callback_data(zoption
));
372 HTTP_CURL_OPT(VERBOSE
, 0);
376 if (zoption
= http_curl_getopt(options
, "proxyhost", IS_STRING
)) {
377 HTTP_CURL_OPT(PROXY
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
379 if (zoption
= http_curl_getopt(options
, "proxyport", IS_LONG
)) {
380 HTTP_CURL_OPT(PROXYPORT
, Z_LVAL_P(zoption
));
383 if (zoption
= http_curl_getopt(options
, "proxyauth", IS_STRING
)) {
384 HTTP_CURL_OPT(PROXYUSERPWD
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
386 #if LIBCURL_VERSION_NUM >= 0x070a07
388 if (zoption
= http_curl_getopt(options
, "proxyauthtype", IS_LONG
)) {
389 HTTP_CURL_OPT(PROXYAUTH
, Z_LVAL_P(zoption
));
394 /* outgoing interface */
395 if (zoption
= http_curl_getopt(options
, "interface", IS_STRING
)) {
396 HTTP_CURL_OPT(INTERFACE
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
400 if (zoption
= http_curl_getopt(options
, "port", IS_LONG
)) {
401 HTTP_CURL_OPT(PORT
, Z_LVAL_P(zoption
));
405 if (zoption
= http_curl_getopt(options
, "httpauth", IS_STRING
)) {
406 HTTP_CURL_OPT(USERPWD
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
408 #if LIBCURL_VERSION_NUM >= 0x070a06
409 if (zoption
= http_curl_getopt(options
, "httpauthtype", IS_LONG
)) {
410 HTTP_CURL_OPT(HTTPAUTH
, Z_LVAL_P(zoption
));
414 /* compress, empty string enables deflate and gzip */
415 if (zoption
= http_curl_getopt(options
, "compress", IS_BOOL
)) {
416 if (Z_LVAL_P(zoption
)) {
417 HTTP_CURL_OPT(ENCODING
, http_request_data_copy(COPY_STRING
, ""));
421 /* redirects, defaults to 0 */
422 if (zoption
= http_curl_getopt(options
, "redirect", IS_LONG
)) {
423 HTTP_CURL_OPT(FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
424 HTTP_CURL_OPT(MAXREDIRS
, Z_LVAL_P(zoption
));
425 if (zoption
= http_curl_getopt(options
, "unrestrictedauth", IS_BOOL
)) {
426 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
429 HTTP_CURL_OPT(FOLLOWLOCATION
, 0);
433 if (zoption
= http_curl_getopt(options
, "referer", IS_STRING
)) {
434 HTTP_CURL_OPT(REFERER
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
437 /* useragent, default "PECL::HTTP/version (PHP/version)" */
438 if (zoption
= http_curl_getopt(options
, "useragent", IS_STRING
)) {
439 HTTP_CURL_OPT(USERAGENT
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
441 HTTP_CURL_OPT(USERAGENT
, http_request_data_copy(COPY_STRING
, "PECL::HTTP/" HTTP_PEXT_VERSION
" (PHP/" PHP_VERSION
")"));
444 /* additional headers, array('name' => 'value') */
445 if (zoption
= http_curl_getopt(options
, "headers", IS_ARRAY
)) {
448 struct curl_slist
*headers
= NULL
;
450 FOREACH_KEY(zoption
, header_key
, header_idx
) {
453 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &header_val
)) {
454 char header
[1024] = {0};
455 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
456 headers
= curl_slist_append(headers
, http_request_data_copy(COPY_STRING
, header
));
465 HTTP_CURL_OPT(HTTPHEADER
, http_request_data_copy(COPY_SLIST
, headers
));
468 HTTP_CURL_OPT(HTTPHEADER
, NULL
);
471 /* cookies, array('name' => 'value') */
472 if (zoption
= http_curl_getopt(options
, "cookies", IS_ARRAY
)) {
473 char *cookie_key
= NULL
;
474 ulong cookie_idx
= 0;
475 phpstr
*qstr
= phpstr_new();
477 FOREACH_KEY(zoption
, cookie_key
, cookie_idx
) {
480 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &cookie_val
)) {
481 phpstr_appendf(qstr
, "%s=%s; ", cookie_key
, Z_STRVAL_PP(cookie_val
));
491 HTTP_CURL_OPT(COOKIE
, http_request_data_copy(COPY_STRING
, qstr
->data
));
497 if (zoption
= http_curl_getopt(options
, "cookiestore", IS_STRING
)) {
498 HTTP_CURL_OPT(COOKIEFILE
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
499 HTTP_CURL_OPT(COOKIEJAR
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
503 if (zoption
= http_curl_getopt(options
, "resume", IS_LONG
)) {
505 HTTP_CURL_OPT(RESUME_FROM
, Z_LVAL_P(zoption
));
509 if (zoption
= http_curl_getopt(options
, "maxfilesize", IS_LONG
)) {
510 HTTP_CURL_OPT(MAXFILESIZE
, Z_LVAL_P(zoption
));
514 if (zoption
= http_curl_getopt(options
, "lastmodified", IS_LONG
)) {
515 HTTP_CURL_OPT(TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
516 HTTP_CURL_OPT(TIMEVALUE
, Z_LVAL_P(zoption
));
520 if (zoption
= http_curl_getopt(options
, "timeout", IS_LONG
)) {
521 HTTP_CURL_OPT(TIMEOUT
, Z_LVAL_P(zoption
));
524 /* connecttimeout, defaults to 1 */
525 if (zoption
= http_curl_getopt(options
, "connecttimeout", IS_LONG
)) {
526 HTTP_CURL_OPT(CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
528 HTTP_CURL_OPT(CONNECTTIMEOUT
, 1);
532 if (zoption
= http_curl_getopt(options
, "ssl", IS_ARRAY
)) {
537 FOREACH_KEYVAL(zoption
, key
, idx
, param
) {
539 HTTP_CURL_OPT_SSL_STRING(CERT
);
540 #if LIBCURL_VERSION_NUM >= 0x070903
541 HTTP_CURL_OPT_SSL_STRING(CERTTYPE
);
543 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD
);
545 HTTP_CURL_OPT_SSL_STRING(KEY
);
546 HTTP_CURL_OPT_SSL_STRING(KEYTYPE
);
547 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD
);
549 HTTP_CURL_OPT_SSL_STRING(ENGINE
);
550 HTTP_CURL_OPT_SSL_LONG(VERSION
);
552 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER
);
553 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST
);
554 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST
);
557 HTTP_CURL_OPT_STRING(CAINFO
);
558 #if LIBCURL_VERSION_NUM >= 0x070908
559 HTTP_CURL_OPT_STRING(CAPATH
);
561 HTTP_CURL_OPT_STRING(RANDOM_FILE
);
562 HTTP_CURL_OPT_STRING(EGDSOCKET
);
569 /* disable SSL verification by default */
570 HTTP_CURL_OPT(SSL_VERIFYPEER
, 0);
571 HTTP_CURL_OPT(SSL_VERIFYHOST
, 0);
578 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1);
582 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
586 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
590 curl_easy_setopt(ch
, CURLOPT_UPLOAD
, 1);
594 if (http_request_method_exists(0, meth
, NULL
)) {
595 curl_easy_setopt(ch
, CURLOPT_CUSTOMREQUEST
, http_request_method_name(meth
));
597 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Unsupported request method: %d", meth
);
603 /* attach request body */
604 if (body
&& (meth
!= HTTP_GET
) && (meth
!= HTTP_HEAD
)) {
607 case HTTP_REQUEST_BODY_CSTRING
:
608 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, (char *) body
->data
);
609 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, body
->size
);
612 case HTTP_REQUEST_BODY_CURLPOST
:
613 curl_easy_setopt(ch
, CURLOPT_HTTPPOST
, (struct curl_httppost
*) body
->data
);
616 case HTTP_REQUEST_BODY_UPLOADFILE
:
617 case HTTP_REQUEST_BODY_UPLOADDATA
:
618 curl_easy_setopt(ch
, CURLOPT_READDATA
, body
);
619 curl_easy_setopt(ch
, CURLOPT_INFILESIZE
, body
->size
);
623 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Unknown request body type: %d", body
->type
);
633 /* {{{ STATUS http_request_exec(CURL *, HashTable *) */
634 PHP_HTTP_API STATUS
_http_request_exec(CURL
*ch
, HashTable
*info TSRMLS_DC
)
638 /* perform request */
639 if (CURLE_OK
!= (result
= curl_easy_perform(ch
))) {
640 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not perform request: %s", curl_easy_strerror(result
));
645 http_request_info(ch
, info
);
652 /* {{{ void http_request_info(CURL *, HashTable *) */
653 PHP_HTTP_API
void _http_request_info(CURL
*ch
, HashTable
*info TSRMLS_DC
)
656 Z_ARRVAL(array
) = info
;
658 HTTP_CURL_INFO(EFFECTIVE_URL
);
659 #if LIBCURL_VERSION_NUM >= 0x070a07
660 HTTP_CURL_INFO(RESPONSE_CODE
);
662 HTTP_CURL_INFO_EX(HTTP_CODE
, RESPONSE_CODE
);
664 HTTP_CURL_INFO(HTTP_CONNECTCODE
);
665 #if LIBCURL_VERSION_NUM >= 0x070500
666 HTTP_CURL_INFO(FILETIME
);
668 HTTP_CURL_INFO(TOTAL_TIME
);
669 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
670 HTTP_CURL_INFO(CONNECT_TIME
);
671 HTTP_CURL_INFO(PRETRANSFER_TIME
);
672 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
673 #if LIBCURL_VERSION_NUM >= 0x070907
674 HTTP_CURL_INFO(REDIRECT_TIME
);
675 HTTP_CURL_INFO(REDIRECT_COUNT
);
677 HTTP_CURL_INFO(SIZE_UPLOAD
);
678 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
679 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
680 HTTP_CURL_INFO(SPEED_UPLOAD
);
681 HTTP_CURL_INFO(HEADER_SIZE
);
682 HTTP_CURL_INFO(REQUEST_SIZE
);
683 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
684 #if LIBCURL_VERSION_NUM >= 0x070c03
685 /*HTTP_CURL_INFO(SSL_ENGINES); todo: CURLINFO_SLIST */
687 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
688 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
689 HTTP_CURL_INFO(CONTENT_TYPE
);
690 #if LIBCURL_VERSION_NUM >= 0x070a03
691 /*HTTP_CURL_INFO(PRIVATE);*/
693 #if LIBCURL_VERSION_NUM >= 0x070a08
694 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
695 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
697 #if LIBCURL_VERSION_NUM >= 0x070c02
698 /*HTTP_CURL_INFO(OS_ERRNO);*/
700 #if LIBCURL_VERSION_NUM >= 0x070c03
701 HTTP_CURL_INFO(NUM_CONNECTS
);
706 /* {{{ STATUS http_request_ex(CURL *, http_request_method, char *, http_request_body, HashTable, HashTable, phpstr *) */
707 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
)
709 if (SUCCESS
!= http_request_init(ch
, meth
, url
, body
, options
, response
)) {
712 return http_request_exec(ch
, info
);
716 /* {{{ char *http_request_method_name(http_request_method) */
717 PHP_HTTP_API
const char *_http_request_method_name(http_request_method m TSRMLS_DC
)
721 if (HTTP_STD_REQUEST_METHOD(m
)) {
722 return http_request_methods
[m
];
725 if (SUCCESS
== zend_hash_index_find(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(m
), (void **) &meth
)) {
726 return Z_STRVAL_PP(meth
);
729 return http_request_methods
[0];
733 /* {{{ unsigned long http_request_method_exists(zend_bool, unsigned long, char *) */
734 PHP_HTTP_API
unsigned long _http_request_method_exists(zend_bool by_name
, unsigned long id
, const char *name TSRMLS_DC
)
739 for (i
= HTTP_NO_REQUEST_METHOD
+ 1; i
< HTTP_MAX_REQUEST_METHOD
; ++i
) {
740 if (!strcmp(name
, http_request_methods
[i
])) {
749 FOREACH_HASH_KEYVAL(&HTTP_G(request
).methods
.custom
, key
, idx
, data
) {
750 if (!strcmp(name
, Z_STRVAL_PP(data
))) {
751 return idx
+ HTTP_MAX_REQUEST_METHOD
;
757 return HTTP_STD_REQUEST_METHOD(id
) || zend_hash_index_exists(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(id
)) ? id
: 0;
762 /* {{{ unsigned long http_request_method_register(char *) */
763 PHP_HTTP_API
unsigned long _http_request_method_register(const char *method TSRMLS_DC
)
767 unsigned long meth_num
= HTTP_G(request
).methods
.custom
.nNextFreeElement
+ HTTP_MAX_REQUEST_METHOD
;
769 Z_ARRVAL(array
) = &HTTP_G(request
).methods
.custom
;
770 add_next_index_string(&array
, estrdup(method
), 0);
772 spprintf(&http_method
, 0, "HTTP_%s", method
);
773 zend_register_long_constant(http_method
, strlen(http_method
) + 1, meth_num
, CONST_CS
, http_module_number TSRMLS_CC
);
780 /* {{{ STATUS http_request_method_unregister(usngigned long) */
781 PHP_HTTP_API STATUS
_http_request_method_unregister(unsigned long method TSRMLS_DC
)
786 if (SUCCESS
!= zend_hash_index_find(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(method
), (void **) &zmethod
)) {
787 http_error_ex(E_NOTICE
, HTTP_E_PARAM
, "Request method with id %lu does not exist", method
);
791 spprintf(&http_method
, 0, "HTTP_%s", Z_STRVAL_PP(zmethod
));
793 if ( (SUCCESS
!= zend_hash_index_del(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(method
)))
794 || (SUCCESS
!= zend_hash_del(EG(zend_constants
), http_method
, strlen(http_method
) + 1))) {
795 http_error_ex(E_NOTICE
, 0, "Could not unregister request method: %s", http_method
);
806 /* {{{ http_request_pool *http_request_pool_init(http_request_pool *) */
807 PHP_HTTP_API http_request_pool
*_http_request_pool_init(http_request_pool
*pool TSRMLS_DC
)
810 #if HTTP_DEBUG_REQPOOLS
811 fprintf(stderr
, "Initializing request pool\n");
813 if ((free_pool
= (!pool
))) {
814 pool
= emalloc(sizeof(http_request_pool
));
819 if (!(pool
->ch
= curl_multi_init())) {
820 http_error(E_WARNING
, HTTP_E_CURL
, "Could not initialize curl");
828 pool
->unfinished
= 0;
829 zend_llist_init(&pool
->handles
, sizeof(zval
*), (llist_dtor_func_t
) ZVAL_PTR_DTOR
, 0);
830 zend_llist_init(&pool
->bodies
, sizeof(http_request_body
*), (llist_dtor_func_t
) http_request_pool_freebody
, 0);
831 #if HTTP_DEBUG_REQPOOLS
832 fprintf(stderr
, "Initialized request pool %p\n", pool
);
838 /* {{{ STATUS http_request_pool_attach(http_request_pool *, zval *) */
839 PHP_HTTP_API STATUS
_http_request_pool_attach(http_request_pool
*pool
, zval
*request TSRMLS_DC
)
841 getObjectEx(http_request_object
, req
, request
);
842 #if HTTP_DEBUG_REQPOOLS
843 fprintf(stderr
, "Attaching request %p to pool %p\n", req
, pool
);
846 http_error(E_WARNING
, HTTP_E_CURL
, "HttpRequest object is already member of an HttpRequestPool");
849 http_request_body
*body
= http_request_body_new();
850 zval
*info
= GET_PROP_EX(req
, request
, responseInfo
);
852 if (SUCCESS
!= http_request_object_requesthandler(req
, request
, body
)) {
854 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not initialize HttpRequest object for attaching to the HttpRequestPool");
855 } else if (CURLM_OK
!= (code
= curl_multi_add_handle(pool
->ch
, req
->ch
))) {
856 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not attach HttpRequest object to the HttpRequestPool: %s", curl_multi_strerror(code
));
859 zval_add_ref(&request
);
860 zend_llist_add_element(&pool
->handles
, &request
);
861 zend_llist_add_element(&pool
->bodies
, &body
);
869 /* {{{ STATUS http_request_pool_detach(http_request_pool *, zval *) */
870 PHP_HTTP_API STATUS
_http_request_pool_detach(http_request_pool
*pool
, zval
*request TSRMLS_DC
)
872 getObjectEx(http_request_object
, req
, request
);
873 #if HTTP_DEBUG_REQPOOLS
874 fprintf(stderr
, "Detaching request %p (pool: %p) from pool %p\n", req
, req
->pool
, pool
);
876 if (req
->pool
!= pool
) {
877 http_error_ex(E_WARNING
, HTTP_E_CURL
, "HttpRequest object is not attached to this HttpRequestPool (%p != %p)", req
->pool
, pool
);
881 if (CURLM_OK
!= (code
= curl_multi_remove_handle(pool
->ch
, req
->ch
))) {
882 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not detach HttpRequest object from the HttpRequestPool: %s", curl_multi_strerror(code
));
885 zval_ptr_dtor(&request
);
893 /* {{{ void http_request_pool_detach_all(http_request_pool *) */
894 PHP_HTTP_API
void _http_request_pool_detach_all(http_request_pool
*pool TSRMLS_DC
)
896 #if HTTP_DEBUG_REQPOOLS
897 fprintf(stderr
, "Detaching all requests from pool %p\n", pool
);
899 zend_llist_apply_with_argument(&pool
->handles
, (llist_apply_with_arg_func_t
) http_request_pool_freehandle
, pool TSRMLS_CC
);
903 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
904 PHP_HTTP_API STATUS
_http_request_pool_send(http_request_pool
*pool TSRMLS_DC
)
906 #if HTTP_DEBUG_REQPOOLS
907 fprintf(stderr
, "Attempt to send requests of pool %p\n", pool
);
909 http_request_pool_perform(pool
);
910 while (pool
->unfinished
) {
911 #if HTTP_DEBUG_REQPOOLS
912 fprintf(stderr
, "%d unfinished requests of pool %p remaining\n", pool
->unfinished
, pool
);
914 if (SUCCESS
!= http_request_pool_select(pool
)) {
915 http_error(E_WARNING
, HTTP_E_CURL
, "Socket error");
918 http_request_pool_perform(pool
);
920 zend_llist_apply(&pool
->handles
, (llist_apply_func_t
) http_request_pool_responsehandler TSRMLS_CC
);
925 /* {{{ void http_request_pool_dtor(http_request_pool *) */
926 PHP_HTTP_API
void _http_request_pool_dtor(http_request_pool
*pool TSRMLS_DC
)
928 #if HTTP_DEBUG_REQPOOLS
929 fprintf(stderr
, "Destructing request pool %p\n", pool
);
931 pool
->unfinished
= 0;
932 zend_llist_clean(&pool
->handles
);
933 zend_llist_clean(&pool
->bodies
);
934 curl_multi_cleanup(pool
->ch
);
940 /* {{{ static void http_request_pool_freebody(http_request_body **) */
941 static void http_request_pool_freebody(http_request_body
**body
)
944 http_request_body_free(*body
);
948 /* {{{ static void http_request_pool_freehandle(zval **, http_request_pool *) */
949 static void http_request_pool_freehandle(zval
**request
, http_request_pool
*pool TSRMLS_DC
)
951 getObjectEx(http_request_object
, req
, *request
);
953 http_request_pool_detach(pool
, *request
);
955 #if HTTP_DEBUG_REQPOOLS
956 else fprintf(stderr
, "Request %p (pool: %p) is not (anymore) attached to pool %p\n", req
, req
->pool
, pool
);
961 /* {{{ static void http_request_pool_responsehandler(zval **) */
962 static void http_request_pool_responsehandler(zval
**req TSRMLS_DC
)
964 getObjectEx(http_request_object
, obj
, *req
);
965 #if HTTP_DEBUG_REQPOOLS
966 fprintf(stderr
, "Fetching data from request %p of pool %p\n", obj
, obj
->pool
);
968 http_request_object_responsehandler(obj
, *req
, NULL
);
972 /* {{{ static inline STATUS http_request_pool_select(http_request_pool *) */
973 static inline STATUS
http_request_pool_select(http_request_pool
*pool
)
977 struct timeval timeout
= {1, 0};
983 curl_multi_fdset(pool
->ch
, &R
, &W
, &E
, &MAX
);
984 return (-1 != select(MAX
+ 1, &R
, &W
, &E
, &timeout
)) ? SUCCESS
: FAILURE
;
988 /* {{{ static inline void http_request_pool_perform(http_request_pool *) */
989 static inline void http_request_pool_perform(http_request_pool
*pool
)
991 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(pool
->ch
, &pool
->unfinished
));
995 /* {{{ char *http_request_methods[] */
996 static const char *const http_request_methods
[] = {
1007 /* WebDAV - RFC 2518 */
1015 /* WebDAV Versioning - RFC 3253 */
1027 /* WebDAV Access Control - RFC 3744 */
1033 /* {{{ static size_t http_curl_write_callback(char *, size_t, size_t, void *) */
1034 static size_t http_curl_write_callback(char *buf
, size_t len
, size_t n
, void *s
)
1036 HTTP_CURL_CALLBACK_DATA(s
, phpstr
*, str
);
1037 return str
? phpstr_append(PHPSTR(str
), buf
, len
* n
) : len
* n
;
1041 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
1042 static size_t http_curl_read_callback(void *data
, size_t len
, size_t n
, void *s
)
1044 static char *offset
= NULL
, *original
= NULL
;
1045 HTTP_CURL_CALLBACK_DATA(s
, http_request_body
*, body
);
1049 case HTTP_REQUEST_BODY_UPLOADFILE
:
1052 return php_stream_read((php_stream
*) body
->data
, data
, len
* n
);
1056 case HTTP_REQUEST_BODY_UPLOADDATA
:
1059 if (original
!= s
) {
1060 original
= offset
= s
;
1062 if ((avail
= body
->size
- (offset
- original
)) < 1) {
1065 if (avail
< (len
* n
)) {
1066 memcpy(data
, offset
, avail
);
1070 memcpy(data
, offset
, len
* n
);
1084 /* {{{ http_curl_callback_ctx http_curl_callback_data(void *) */
1085 static http_curl_callback_ctx
*_http_curl_callback_data(void *data TSRMLS_DC
)
1087 http_curl_callback_ctx
*ctx
= emalloc(sizeof(http_curl_callback_ctx
));
1088 TSRMLS_SET_CTX(ctx
->tsrm_ctx
);
1094 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
1095 static int http_curl_progress_callback(void *data
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
1097 zval
*params_pass
[4], params_local
[4], retval
;
1098 HTTP_CURL_CALLBACK_DATA(data
, zval
*, func
);
1100 params_pass
[0] = ¶ms_local
[0];
1101 params_pass
[1] = ¶ms_local
[1];
1102 params_pass
[2] = ¶ms_local
[2];
1103 params_pass
[3] = ¶ms_local
[3];
1105 ZVAL_DOUBLE(params_pass
[0], dltotal
);
1106 ZVAL_DOUBLE(params_pass
[1], dlnow
);
1107 ZVAL_DOUBLE(params_pass
[2], ultotal
);
1108 ZVAL_DOUBLE(params_pass
[3], ulnow
);
1110 return call_user_function(EG(function_table
), NULL
, func
, &retval
, 4, params_pass TSRMLS_CC
);
1114 static int http_curl_debug_callback(CURL
*ch
, curl_infotype type
, char *string
, size_t length
, void *data
)
1116 zval
*params_pass
[2], params_local
[2], retval
;
1117 HTTP_CURL_CALLBACK_DATA(data
, zval
*, func
);
1119 params_pass
[0] = ¶ms_local
[0];
1120 params_pass
[1] = ¶ms_local
[1];
1122 ZVAL_LONG(params_pass
[0], type
);
1123 ZVAL_STRINGL(params_pass
[1], string
, length
, 1);
1125 call_user_function(EG(function_table
), NULL
, func
, &retval
, 2, params_pass TSRMLS_CC
);
1129 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
1130 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
)
1134 if (!options
|| (SUCCESS
!= zend_hash_find(options
, key
, keylen
, (void **) &zoption
))) {
1138 if (Z_TYPE_PP(zoption
) != type
) {
1141 case IS_BOOL
: convert_to_boolean_ex(zoption
); break;
1142 case IS_LONG
: convert_to_long_ex(zoption
); break;
1143 case IS_DOUBLE
: convert_to_double_ex(zoption
); break;
1144 case IS_STRING
: convert_to_string_ex(zoption
); break;
1145 case IS_ARRAY
: convert_to_array_ex(zoption
); break;
1146 case IS_OBJECT
: convert_to_object_ex(zoption
); break;
1161 * vim600: noet sw=4 ts=4 fdm=marker
1162 * vim<600: noet sw=4 ts=4