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_url_api.h"
37 #ifndef HTTP_CURL_USE_ZEND_MM
38 # define HTTP_CURL_USE_ZEND_MM 0
41 ZEND_EXTERN_MODULE_GLOBALS(http
)
43 #if LIBCURL_VERSION_NUM < 0x070c00
44 # define curl_easy_strerror(code) HTTP_G(request).error
47 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
48 #define HTTP_CURL_INFO_EX(I, X) \
49 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
51 case CURLINFO_STRING: \
54 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
55 add_assoc_string(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
60 case CURLINFO_DOUBLE: \
63 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
64 add_assoc_double(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), d); \
72 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
73 add_assoc_long(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), l); \
79 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
80 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
81 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
82 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
83 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
84 if (!strcasecmp(key, #keyname)) { \
85 convert_to_string_ex(param); \
86 HTTP_CURL_OPT(optname, http_request_data_copy(COPY_STRING, Z_STRVAL_PP(param))); \
90 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
91 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
92 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
93 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
94 if (!strcasecmp(key, #keyname)) { \
95 convert_to_long_ex(param); \
96 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
102 static const char *const http_request_methods
[HTTP_MAX_REQUEST_METHOD
+ 1];
103 #define http_curl_getopt(o, k, t) _http_curl_getopt_ex((o), (k), sizeof(k), (t) TSRMLS_CC)
104 #define http_curl_getopt_ex(o, k, l, t) _http_curl_getopt_ex((o), (k), (l), (t) TSRMLS_CC)
105 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
);
106 static size_t http_curl_write_callback(char *, size_t, size_t, void *);
107 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
108 static int http_curl_progress_callback(void *, double, double, double, double);
109 static int http_curl_debug_callback(CURL
*, curl_infotype
, char *, size_t, void *);
111 #define HTTP_CURL_CALLBACK_DATA(from, type, var) \
112 http_curl_callback_ctx *__CTX = (http_curl_callback_ctx *) (from); \
113 TSRMLS_FETCH_FROM_CTX(__CTX->tsrm_ctx); \
114 type (var) = (type) (__CTX->data)
116 #define http_curl_callback_data(data) _http_curl_callback_data((data) TSRMLS_CC)
117 static http_curl_callback_ctx
*_http_curl_callback_data(void *data TSRMLS_DC
);
120 #if HTTP_CURL_USE_ZEND_MM
121 static void http_curl_free(void *p
) { efree(p
); }
122 static char *http_curl_strdup(const char *p
) { return estrdup(p
); }
123 static void *http_curl_malloc(size_t s
) { return emalloc(s
); }
124 static void *http_curl_realloc(void *p
, size_t s
) { return erealloc(p
, s
); }
125 static void *http_curl_calloc(size_t n
, size_t s
) { return ecalloc(n
, s
); }
128 /* {{{ STATUS http_request_global_init() */
129 STATUS
_http_request_global_init(void)
131 #if HTTP_CURL_USE_ZEND_MM
132 if (CURLE_OK
!= curl_global_init_mem(CURL_GLOBAL_ALL
,
141 if (CURLE_OK
!= curl_global_init(CURL_GLOBAL_ALL
)) {
149 /* {{{ void *http_request_data_copy(int, void *) */
150 void *_http_request_data_copy(int type
, void *data TSRMLS_DC
)
156 char *new_str
= estrdup((const char*) data
);
157 zend_llist_add_element(&HTTP_G(request
).copies
.strings
, &new_str
);
163 zend_llist_add_element(&HTTP_G(request
).copies
.slists
, &data
);
169 zend_llist_add_element(&HTTP_G(request
).copies
.contexts
, &data
);
181 /* {{{ void http_request_data_free_string(char **) */
182 void _http_request_data_free_string(void *string
)
184 efree(*((char **)string
));
188 /* {{{ void http_request_data_free_slist(struct curl_slist **) */
189 void _http_request_data_free_slist(void *list
)
191 curl_slist_free_all(*((struct curl_slist
**) list
));
195 /* {{{ _http_request_data_free_context(http_curl_callback_ctx **) */
196 void _http_request_data_free_context(void *context
)
198 efree(*((http_curl_callback_ctx
**) context
));
202 /* {{{ http_request_body *http_request_body_new() */
203 PHP_HTTP_API http_request_body
*_http_request_body_new(TSRMLS_D
)
205 http_request_body
*body
= ecalloc(1, sizeof(http_request_body
));
210 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
211 PHP_HTTP_API STATUS
_http_request_body_fill(http_request_body
*body
, HashTable
*fields
, HashTable
*files TSRMLS_DC
)
213 if (files
&& (zend_hash_num_elements(files
) > 0)) {
217 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
220 FOREACH_HASH_KEYVAL(fields
, key
, idx
, data
) {
223 convert_to_string_ex(data
);
224 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
225 CURLFORM_COPYNAME
, key
,
226 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
227 CURLFORM_CONTENTSLENGTH
, Z_STRLEN_PP(data
),
230 if (CURLE_OK
!= err
) {
231 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not encode post fields: %s", curl_easy_strerror(err
));
232 curl_formfree(http_post_data
[0]);
242 FOREACH_HASH_VAL(files
, data
) {
244 zval
**file
, **type
, **name
;
245 if ( SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) &&
246 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) &&
247 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
248 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
249 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
250 CURLFORM_FILE
, Z_STRVAL_PP(file
),
251 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
254 if (CURLE_OK
!= err
) {
255 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not encode post files: %s", curl_easy_strerror(err
));
256 curl_formfree(http_post_data
[0]);
260 http_error(E_NOTICE
, HTTP_E_PARAM
, "Post file array entry misses either 'name', 'type' or 'file' entry");
264 body
->type
= HTTP_REQUEST_BODY_CURLPOST
;
265 body
->data
= http_post_data
[0];
272 if (SUCCESS
!= http_urlencode_hash_ex(fields
, 1, NULL
, 0, &encoded
, &encoded_len
)) {
273 http_error(E_WARNING
, HTTP_E_ENCODE
, "Could not encode post data");
277 body
->type
= HTTP_REQUEST_BODY_CSTRING
;
278 body
->data
= encoded
;
279 body
->size
= encoded_len
;
286 /* {{{ void http_request_body_dtor(http_request_body *) */
287 PHP_HTTP_API
void _http_request_body_dtor(http_request_body
*body TSRMLS_DC
)
292 case HTTP_REQUEST_BODY_CSTRING
:
298 case HTTP_REQUEST_BODY_CURLPOST
:
299 curl_formfree(body
->data
);
302 case HTTP_REQUEST_BODY_UPLOADFILE
:
303 php_stream_close(body
->data
);
310 /* {{{ void http_request_body_free(http_request_body *) */
311 PHP_HTTP_API
void _http_request_body_free(http_request_body
*body TSRMLS_DC
)
314 http_request_body_dtor(body
);
320 /* {{{ STATUS http_request_init(CURL *, http_request_method, char *, http_request_body *, HashTable *, phpstr *) */
321 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
)
324 zend_bool range_req
= 0;
326 /* reset CURL handle */
327 #if LIBCURL_VERSION_NUM >= 0x070c01
333 HTTP_CURL_OPT(URL
, http_request_data_copy(COPY_STRING
, (void *) url
));
337 HTTP_CURL_OPT(WRITEDATA
, http_curl_callback_data(response
));
338 HTTP_CURL_OPT(WRITEHEADER
, http_curl_callback_data(response
));
341 HTTP_CURL_OPT(HEADER
, 0);
342 HTTP_CURL_OPT(FILETIME
, 1);
343 HTTP_CURL_OPT(AUTOREFERER
, 1);
344 HTTP_CURL_OPT(READFUNCTION
, http_curl_read_callback
);
345 HTTP_CURL_OPT(WRITEFUNCTION
, http_curl_write_callback
);
346 HTTP_CURL_OPT(HEADERFUNCTION
, http_curl_write_callback
);
348 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
349 HTTP_CURL_OPT(NOSIGNAL
, 1);
351 #if LIBCURL_VERSION_NUM < 0x070c00
352 HTTP_CURL_OPT(ERRORBUFFER
, HTTP_G(request
).error
);
355 /* progress callback */
356 if (zoption
= http_curl_getopt(options
, "onprogress", 0)) {
357 HTTP_CURL_OPT(PROGRESSFUNCTION
, http_curl_progress_callback
);
358 HTTP_CURL_OPT(PROGRESSDATA
, http_curl_callback_data(zoption
));
360 HTTP_CURL_OPT(NOPROGRESS
, 1);
364 if (zoption
= http_curl_getopt(options
, "ondebug", 0)) {
365 HTTP_CURL_OPT(VERBOSE
, 1);
366 HTTP_CURL_OPT(DEBUGFUNCTION
, http_curl_debug_callback
);
367 HTTP_CURL_OPT(DEBUGDATA
, http_curl_callback_data(zoption
));
369 HTTP_CURL_OPT(VERBOSE
, 0);
373 if (zoption
= http_curl_getopt(options
, "proxyhost", IS_STRING
)) {
374 HTTP_CURL_OPT(PROXY
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
376 if (zoption
= http_curl_getopt(options
, "proxyport", IS_LONG
)) {
377 HTTP_CURL_OPT(PROXYPORT
, Z_LVAL_P(zoption
));
380 if (zoption
= http_curl_getopt(options
, "proxyauth", IS_STRING
)) {
381 HTTP_CURL_OPT(PROXYUSERPWD
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
383 #if LIBCURL_VERSION_NUM >= 0x070a07
385 if (zoption
= http_curl_getopt(options
, "proxyauthtype", IS_LONG
)) {
386 HTTP_CURL_OPT(PROXYAUTH
, Z_LVAL_P(zoption
));
391 /* outgoing interface */
392 if (zoption
= http_curl_getopt(options
, "interface", IS_STRING
)) {
393 HTTP_CURL_OPT(INTERFACE
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
397 if (zoption
= http_curl_getopt(options
, "port", IS_LONG
)) {
398 HTTP_CURL_OPT(PORT
, Z_LVAL_P(zoption
));
402 if (zoption
= http_curl_getopt(options
, "httpauth", IS_STRING
)) {
403 HTTP_CURL_OPT(USERPWD
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
405 #if LIBCURL_VERSION_NUM >= 0x070a06
406 if (zoption
= http_curl_getopt(options
, "httpauthtype", IS_LONG
)) {
407 HTTP_CURL_OPT(HTTPAUTH
, Z_LVAL_P(zoption
));
411 /* compress, empty string enables deflate and gzip */
412 if (zoption
= http_curl_getopt(options
, "compress", IS_BOOL
)) {
413 if (Z_LVAL_P(zoption
)) {
414 HTTP_CURL_OPT(ENCODING
, http_request_data_copy(COPY_STRING
, ""));
418 /* redirects, defaults to 0 */
419 if (zoption
= http_curl_getopt(options
, "redirect", IS_LONG
)) {
420 HTTP_CURL_OPT(FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
421 HTTP_CURL_OPT(MAXREDIRS
, Z_LVAL_P(zoption
));
422 if (zoption
= http_curl_getopt(options
, "unrestrictedauth", IS_BOOL
)) {
423 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
426 HTTP_CURL_OPT(FOLLOWLOCATION
, 0);
430 if (zoption
= http_curl_getopt(options
, "referer", IS_STRING
)) {
431 HTTP_CURL_OPT(REFERER
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
434 /* useragent, default "PECL::HTTP/version (PHP/version)" */
435 if (zoption
= http_curl_getopt(options
, "useragent", IS_STRING
)) {
436 HTTP_CURL_OPT(USERAGENT
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
438 HTTP_CURL_OPT(USERAGENT
, http_request_data_copy(COPY_STRING
, "PECL::HTTP/" HTTP_PEXT_VERSION
" (PHP/" PHP_VERSION
")"));
441 /* additional headers, array('name' => 'value') */
442 if (zoption
= http_curl_getopt(options
, "headers", IS_ARRAY
)) {
445 struct curl_slist
*headers
= NULL
;
447 FOREACH_KEY(zoption
, header_key
, header_idx
) {
450 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &header_val
)) {
451 char header
[1024] = {0};
452 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
453 headers
= curl_slist_append(headers
, http_request_data_copy(COPY_STRING
, header
));
462 HTTP_CURL_OPT(HTTPHEADER
, http_request_data_copy(COPY_SLIST
, headers
));
465 HTTP_CURL_OPT(HTTPHEADER
, NULL
);
468 /* cookies, array('name' => 'value') */
469 if (zoption
= http_curl_getopt(options
, "cookies", IS_ARRAY
)) {
470 char *cookie_key
= NULL
;
471 ulong cookie_idx
= 0;
472 phpstr
*qstr
= phpstr_new();
474 FOREACH_KEY(zoption
, cookie_key
, cookie_idx
) {
477 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &cookie_val
)) {
478 phpstr_appendf(qstr
, "%s=%s; ", cookie_key
, Z_STRVAL_PP(cookie_val
));
488 HTTP_CURL_OPT(COOKIE
, http_request_data_copy(COPY_STRING
, qstr
->data
));
494 if (zoption
= http_curl_getopt(options
, "cookiestore", IS_STRING
)) {
495 HTTP_CURL_OPT(COOKIEFILE
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
496 HTTP_CURL_OPT(COOKIEJAR
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
500 if (zoption
= http_curl_getopt(options
, "resume", IS_LONG
)) {
502 HTTP_CURL_OPT(RESUME_FROM
, Z_LVAL_P(zoption
));
506 if (zoption
= http_curl_getopt(options
, "maxfilesize", IS_LONG
)) {
507 HTTP_CURL_OPT(MAXFILESIZE
, Z_LVAL_P(zoption
));
511 if (zoption
= http_curl_getopt(options
, "lastmodified", IS_LONG
)) {
512 HTTP_CURL_OPT(TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
513 HTTP_CURL_OPT(TIMEVALUE
, Z_LVAL_P(zoption
));
517 if (zoption
= http_curl_getopt(options
, "timeout", IS_LONG
)) {
518 HTTP_CURL_OPT(TIMEOUT
, Z_LVAL_P(zoption
));
521 /* connecttimeout, defaults to 1 */
522 if (zoption
= http_curl_getopt(options
, "connecttimeout", IS_LONG
)) {
523 HTTP_CURL_OPT(CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
525 HTTP_CURL_OPT(CONNECTTIMEOUT
, 1);
529 if (zoption
= http_curl_getopt(options
, "ssl", IS_ARRAY
)) {
534 FOREACH_KEYVAL(zoption
, key
, idx
, param
) {
536 HTTP_CURL_OPT_SSL_STRING(CERT
);
537 #if LIBCURL_VERSION_NUM >= 0x070903
538 HTTP_CURL_OPT_SSL_STRING(CERTTYPE
);
540 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD
);
542 HTTP_CURL_OPT_SSL_STRING(KEY
);
543 HTTP_CURL_OPT_SSL_STRING(KEYTYPE
);
544 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD
);
546 HTTP_CURL_OPT_SSL_STRING(ENGINE
);
547 HTTP_CURL_OPT_SSL_LONG(VERSION
);
549 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER
);
550 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST
);
551 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST
);
554 HTTP_CURL_OPT_STRING(CAINFO
);
555 #if LIBCURL_VERSION_NUM >= 0x070908
556 HTTP_CURL_OPT_STRING(CAPATH
);
558 HTTP_CURL_OPT_STRING(RANDOM_FILE
);
559 HTTP_CURL_OPT_STRING(EGDSOCKET
);
566 /* disable SSL verification by default */
567 HTTP_CURL_OPT(SSL_VERIFYPEER
, 0);
568 HTTP_CURL_OPT(SSL_VERIFYHOST
, 0);
575 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1);
579 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
583 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
587 curl_easy_setopt(ch
, CURLOPT_UPLOAD
, 1);
591 if (http_request_method_exists(0, meth
, NULL
)) {
592 curl_easy_setopt(ch
, CURLOPT_CUSTOMREQUEST
, http_request_method_name(meth
));
594 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Unsupported request method: %d", meth
);
600 /* attach request body */
601 if (body
&& (meth
!= HTTP_GET
) && (meth
!= HTTP_HEAD
)) {
604 case HTTP_REQUEST_BODY_CSTRING
:
605 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, body
->data
);
606 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, body
->size
);
609 case HTTP_REQUEST_BODY_CURLPOST
:
610 curl_easy_setopt(ch
, CURLOPT_HTTPPOST
, (struct curl_httppost
*) body
->data
);
613 case HTTP_REQUEST_BODY_UPLOADFILE
:
614 curl_easy_setopt(ch
, CURLOPT_READDATA
, http_curl_callback_data(body
));
615 curl_easy_setopt(ch
, CURLOPT_INFILESIZE
, body
->size
);
619 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Unknown request body type: %d", body
->type
);
629 /* {{{ STATUS http_request_exec(CURL *, HashTable *) */
630 PHP_HTTP_API STATUS
_http_request_exec(CURL
*ch
, HashTable
*info TSRMLS_DC
)
634 /* perform request */
635 if (CURLE_OK
!= (result
= curl_easy_perform(ch
))) {
636 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not perform request: %s", curl_easy_strerror(result
));
641 http_request_info(ch
, info
);
648 /* {{{ void http_request_info(CURL *, HashTable *) */
649 PHP_HTTP_API
void _http_request_info(CURL
*ch
, HashTable
*info TSRMLS_DC
)
652 Z_ARRVAL(array
) = info
;
654 HTTP_CURL_INFO(EFFECTIVE_URL
);
655 #if LIBCURL_VERSION_NUM >= 0x070a07
656 HTTP_CURL_INFO(RESPONSE_CODE
);
658 HTTP_CURL_INFO_EX(HTTP_CODE
, RESPONSE_CODE
);
660 HTTP_CURL_INFO(HTTP_CONNECTCODE
);
661 #if LIBCURL_VERSION_NUM >= 0x070500
662 HTTP_CURL_INFO(FILETIME
);
664 HTTP_CURL_INFO(TOTAL_TIME
);
665 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
666 HTTP_CURL_INFO(CONNECT_TIME
);
667 HTTP_CURL_INFO(PRETRANSFER_TIME
);
668 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
669 #if LIBCURL_VERSION_NUM >= 0x070907
670 HTTP_CURL_INFO(REDIRECT_TIME
);
671 HTTP_CURL_INFO(REDIRECT_COUNT
);
673 HTTP_CURL_INFO(SIZE_UPLOAD
);
674 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
675 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
676 HTTP_CURL_INFO(SPEED_UPLOAD
);
677 HTTP_CURL_INFO(HEADER_SIZE
);
678 HTTP_CURL_INFO(REQUEST_SIZE
);
679 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
680 #if LIBCURL_VERSION_NUM >= 0x070c03
681 /*HTTP_CURL_INFO(SSL_ENGINES); todo: CURLINFO_SLIST */
683 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
684 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
685 HTTP_CURL_INFO(CONTENT_TYPE
);
686 #if LIBCURL_VERSION_NUM >= 0x070a03
687 /*HTTP_CURL_INFO(PRIVATE);*/
689 #if LIBCURL_VERSION_NUM >= 0x070a08
690 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
691 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
693 #if LIBCURL_VERSION_NUM >= 0x070c02
694 /*HTTP_CURL_INFO(OS_ERRNO);*/
696 #if LIBCURL_VERSION_NUM >= 0x070c03
697 HTTP_CURL_INFO(NUM_CONNECTS
);
702 /* {{{ STATUS http_request_ex(CURL *, http_request_method, char *, http_request_body, HashTable, HashTable, phpstr *) */
703 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
)
706 zend_bool clean_curl
;
708 if ((clean_curl
= (!ch
))) {
709 if (!(ch
= curl_easy_init())) {
710 http_error(E_WARNING
, HTTP_E_CURL
, "Could not initialize curl.");
715 status
= ((SUCCESS
== http_request_init(ch
, meth
, url
, body
, options
, response
)) &&
716 (SUCCESS
== http_request_exec(ch
, info
))) ? SUCCESS
: FAILURE
;
719 curl_easy_cleanup(ch
);
725 /* {{{ char *http_request_method_name(http_request_method) */
726 PHP_HTTP_API
const char *_http_request_method_name(http_request_method m TSRMLS_DC
)
730 if (HTTP_STD_REQUEST_METHOD(m
)) {
731 return http_request_methods
[m
];
734 if (SUCCESS
== zend_hash_index_find(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(m
), (void **) &meth
)) {
735 return Z_STRVAL_PP(meth
);
738 return http_request_methods
[0];
742 /* {{{ unsigned long http_request_method_exists(zend_bool, unsigned long, char *) */
743 PHP_HTTP_API
unsigned long _http_request_method_exists(zend_bool by_name
, unsigned long id
, const char *name TSRMLS_DC
)
748 for (i
= HTTP_NO_REQUEST_METHOD
+ 1; i
< HTTP_MAX_REQUEST_METHOD
; ++i
) {
749 if (!strcmp(name
, http_request_methods
[i
])) {
758 FOREACH_HASH_KEYVAL(&HTTP_G(request
).methods
.custom
, key
, idx
, data
) {
759 if (!strcmp(name
, Z_STRVAL_PP(data
))) {
760 return idx
+ HTTP_MAX_REQUEST_METHOD
;
766 return HTTP_STD_REQUEST_METHOD(id
) || zend_hash_index_exists(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(id
)) ? id
: 0;
771 /* {{{ unsigned long http_request_method_register(char *) */
772 PHP_HTTP_API
unsigned long _http_request_method_register(const char *method TSRMLS_DC
)
776 unsigned long meth_num
= HTTP_G(request
).methods
.custom
.nNextFreeElement
+ HTTP_MAX_REQUEST_METHOD
;
778 Z_ARRVAL(array
) = &HTTP_G(request
).methods
.custom
;
779 add_next_index_string(&array
, estrdup(method
), 0);
781 spprintf(&http_method
, 0, "HTTP_%s", method
);
782 zend_register_long_constant(http_method
, strlen(http_method
) + 1, meth_num
, CONST_CS
, http_module_number TSRMLS_CC
);
789 /* {{{ STATUS http_request_method_unregister(usngigned long) */
790 PHP_HTTP_API STATUS
_http_request_method_unregister(unsigned long method TSRMLS_DC
)
795 if (SUCCESS
!= zend_hash_index_find(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(method
), (void **) &zmethod
)) {
796 http_error_ex(E_NOTICE
, HTTP_E_PARAM
, "Request method with id %lu does not exist", method
);
800 spprintf(&http_method
, 0, "HTTP_%s", Z_STRVAL_PP(zmethod
));
802 if ( (SUCCESS
!= zend_hash_index_del(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(method
)))
803 || (SUCCESS
!= zend_hash_del(EG(zend_constants
), http_method
, strlen(http_method
) + 1))) {
804 http_error_ex(E_NOTICE
, 0, "Could not unregister request method: %s", http_method
);
815 /* {{{ char *http_request_methods[] */
816 static const char *const http_request_methods
[] = {
827 /* WebDAV - RFC 2518 */
835 /* WebDAV Versioning - RFC 3253 */
847 /* WebDAV Access Control - RFC 3744 */
853 /* {{{ static size_t http_curl_write_callback(char *, size_t, size_t, void *) */
854 static size_t http_curl_write_callback(char *buf
, size_t len
, size_t n
, void *s
)
856 HTTP_CURL_CALLBACK_DATA(s
, phpstr
*, str
);
857 return str
? phpstr_append(PHPSTR(str
), buf
, len
* n
) : len
* n
;
861 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
862 static size_t http_curl_read_callback(void *data
, size_t len
, size_t n
, void *s
)
864 static char *offset
= NULL
, *original
= NULL
;
865 HTTP_CURL_CALLBACK_DATA(s
, http_request_body
*, body
);
867 if (body
->type
!= HTTP_REQUEST_BODY_UPLOADFILE
) {
870 return php_stream_read((php_stream
*) body
->data
, data
, len
* n
);
874 /* {{{ http_curl_callback_ctx http_curl_callback_data(void *) */
875 static http_curl_callback_ctx
*_http_curl_callback_data(void *data TSRMLS_DC
)
877 http_curl_callback_ctx
*ctx
= emalloc(sizeof(http_curl_callback_ctx
));
878 TSRMLS_SET_CTX(ctx
->tsrm_ctx
);
880 return http_request_data_copy(COPY_CONTEXT
, ctx
);
884 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
885 static int http_curl_progress_callback(void *data
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
887 zval
*params_pass
[4], params_local
[4], retval
;
888 HTTP_CURL_CALLBACK_DATA(data
, zval
*, func
);
890 params_pass
[0] = ¶ms_local
[0];
891 params_pass
[1] = ¶ms_local
[1];
892 params_pass
[2] = ¶ms_local
[2];
893 params_pass
[3] = ¶ms_local
[3];
895 ZVAL_DOUBLE(params_pass
[0], dltotal
);
896 ZVAL_DOUBLE(params_pass
[1], dlnow
);
897 ZVAL_DOUBLE(params_pass
[2], ultotal
);
898 ZVAL_DOUBLE(params_pass
[3], ulnow
);
900 return call_user_function(EG(function_table
), NULL
, func
, &retval
, 4, params_pass TSRMLS_CC
);
904 static int http_curl_debug_callback(CURL
*ch
, curl_infotype type
, char *string
, size_t length
, void *data
)
906 zval
*params_pass
[2], params_local
[2], retval
;
907 HTTP_CURL_CALLBACK_DATA(data
, zval
*, func
);
909 params_pass
[0] = ¶ms_local
[0];
910 params_pass
[1] = ¶ms_local
[1];
912 ZVAL_LONG(params_pass
[0], type
);
913 ZVAL_STRINGL(params_pass
[1], string
, length
, 1);
915 call_user_function(EG(function_table
), NULL
, func
, &retval
, 2, params_pass TSRMLS_CC
);
919 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
920 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
)
924 if (!options
|| (SUCCESS
!= zend_hash_find(options
, key
, keylen
, (void **) &zoption
))) {
928 if (Z_TYPE_PP(zoption
) != type
) {
931 case IS_BOOL
: convert_to_boolean_ex(zoption
); break;
932 case IS_LONG
: convert_to_long_ex(zoption
); break;
933 case IS_DOUBLE
: convert_to_double_ex(zoption
); break;
934 case IS_STRING
: convert_to_string_ex(zoption
); break;
935 case IS_ARRAY
: convert_to_array_ex(zoption
); break;
936 case IS_OBJECT
: convert_to_object_ex(zoption
); break;
951 * vim600: noet sw=4 ts=4 fdm=marker
952 * vim<600: noet sw=4 ts=4