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 +----------------------------------------------------------------------+
24 #include "php_http_std_defs.h"
25 #include "php_http_api.h"
26 #include "php_http_request_api.h"
27 #include "php_http_url_api.h"
29 # include "php_http_request_object.h"
32 #include "phpstr/phpstr.h"
35 # include <winsock2.h>
38 #include <curl/curl.h>
40 ZEND_EXTERN_MODULE_GLOBALS(http
);
42 #ifndef HTTP_CURL_USE_ZEND_MM
43 # define HTTP_CURL_USE_ZEND_MM 0
46 #if LIBCURL_VERSION_NUM < 0x070c00
47 # define curl_easy_strerror(code) HTTP_G(request).error
50 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
51 #define HTTP_CURL_INFO_EX(I, X) \
52 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
54 case CURLINFO_STRING: \
57 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
58 add_assoc_string(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
63 case CURLINFO_DOUBLE: \
66 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
67 add_assoc_double(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), d); \
75 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
76 add_assoc_long(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), l); \
82 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
83 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
84 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
85 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
86 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
87 if (!strcasecmp(key, #keyname)) { \
88 convert_to_string_ex(param); \
89 HTTP_CURL_OPT(optname, http_request_data_copy(COPY_STRING, Z_STRVAL_PP(param))); \
93 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
94 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
95 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
96 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
97 if (!strcasecmp(key, #keyname)) { \
98 convert_to_long_ex(param); \
99 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
104 typedef struct _http_curl_conv
{
107 curl_infotype last_info
;
110 static const char *const http_request_methods
[HTTP_MAX_REQUEST_METHOD
+ 1];
111 #define http_curl_getopt(o, k, t) _http_curl_getopt_ex((o), (k), sizeof(k), (t) TSRMLS_CC)
112 #define http_curl_getopt_ex(o, k, l, t) _http_curl_getopt_ex((o), (k), (l), (t) TSRMLS_CC)
113 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
);
114 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
115 static int http_curl_progress_callback(void *, double, double, double, double);
116 static int http_curl_raw_callback(CURL
*, curl_infotype
, char *, size_t, void *);
117 static int http_curl_dummy_callback(char *data
, size_t n
, size_t l
, void *s
) { return n
*l
; }
119 #define HTTP_CURL_CALLBACK_DATA(from, type, var) \
120 http_curl_callback_ctx *__CTX = (http_curl_callback_ctx *) (from); \
121 TSRMLS_FETCH_FROM_CTX(__CTX->tsrm_ctx); \
122 type (var) = (type) (__CTX->data)
124 #define http_curl_callback_data(data) _http_curl_callback_data((data) TSRMLS_CC)
125 static http_curl_callback_ctx
*_http_curl_callback_data(void *data TSRMLS_DC
);
128 #if HTTP_CURL_USE_ZEND_MM
129 static void http_curl_free(void *p
) { efree(p
); }
130 static char *http_curl_strdup(const char *p
) { return estrdup(p
); }
131 static void *http_curl_malloc(size_t s
) { return emalloc(s
); }
132 static void *http_curl_realloc(void *p
, size_t s
) { return erealloc(p
, s
); }
133 static void *http_curl_calloc(size_t n
, size_t s
) { return ecalloc(n
, s
); }
136 /* {{{ STATUS http_request_global_init() */
137 STATUS
_http_request_global_init(void)
139 #if HTTP_CURL_USE_ZEND_MM
140 if (CURLE_OK
!= curl_global_init_mem(CURL_GLOBAL_ALL
,
149 if (CURLE_OK
!= curl_global_init(CURL_GLOBAL_ALL
)) {
157 /* {{{ void *http_request_data_copy(int, void *) */
158 void *_http_request_data_copy(int type
, void *data TSRMLS_DC
)
164 char *new_str
= estrdup(data
);
165 zend_llist_add_element(&HTTP_G(request
).copies
.strings
, &new_str
);
171 zend_llist_add_element(&HTTP_G(request
).copies
.slists
, &data
);
177 zend_llist_add_element(&HTTP_G(request
).copies
.contexts
, &data
);
189 /* {{{ void http_request_data_free_string(char **) */
190 void _http_request_data_free_string(void *string
)
192 efree(*((char **)string
));
196 /* {{{ void http_request_data_free_slist(struct curl_slist **) */
197 void _http_request_data_free_slist(void *list
)
199 curl_slist_free_all(*((struct curl_slist
**) list
));
203 /* {{{ _http_request_data_free_context(http_curl_callback_ctx **) */
204 void _http_request_data_free_context(void *context
)
206 efree(*((http_curl_callback_ctx
**) context
));
210 /* {{{ http_request_body *http_request_body_new() */
211 PHP_HTTP_API http_request_body
*_http_request_body_new(TSRMLS_D
)
213 http_request_body
*body
= ecalloc(1, sizeof(http_request_body
));
218 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
219 PHP_HTTP_API STATUS
_http_request_body_fill(http_request_body
*body
, HashTable
*fields
, HashTable
*files TSRMLS_DC
)
221 if (files
&& (zend_hash_num_elements(files
) > 0)) {
225 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
228 FOREACH_HASH_KEYVAL(fields
, key
, idx
, data
) {
231 convert_to_string_ex(data
);
232 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
233 CURLFORM_COPYNAME
, key
,
234 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
235 CURLFORM_CONTENTSLENGTH
, Z_STRLEN_PP(data
),
238 if (CURLE_OK
!= err
) {
239 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not encode post fields: %s", curl_easy_strerror(err
));
240 curl_formfree(http_post_data
[0]);
250 FOREACH_HASH_VAL(files
, data
) {
252 zval
**file
, **type
, **name
;
253 if ( SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) &&
254 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) &&
255 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
256 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
257 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
258 CURLFORM_FILE
, Z_STRVAL_PP(file
),
259 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
262 if (CURLE_OK
!= err
) {
263 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not encode post files: %s", curl_easy_strerror(err
));
264 curl_formfree(http_post_data
[0]);
268 http_error(E_NOTICE
, HTTP_E_PARAM
, "Post file array entry misses either 'name', 'type' or 'file' entry");
272 body
->type
= HTTP_REQUEST_BODY_CURLPOST
;
273 body
->data
= http_post_data
[0];
280 if (SUCCESS
!= http_urlencode_hash_ex(fields
, 1, NULL
, 0, &encoded
, &encoded_len
)) {
281 http_error(E_WARNING
, HTTP_E_ENCODE
, "Could not encode post data");
285 body
->type
= HTTP_REQUEST_BODY_CSTRING
;
286 body
->data
= encoded
;
287 body
->size
= encoded_len
;
294 /* {{{ void http_request_body_dtor(http_request_body *) */
295 PHP_HTTP_API
void _http_request_body_dtor(http_request_body
*body TSRMLS_DC
)
300 case HTTP_REQUEST_BODY_CSTRING
:
306 case HTTP_REQUEST_BODY_CURLPOST
:
307 curl_formfree(body
->data
);
310 case HTTP_REQUEST_BODY_UPLOADFILE
:
311 php_stream_close(body
->data
);
318 /* {{{ void http_request_body_free(http_request_body *) */
319 PHP_HTTP_API
void _http_request_body_free(http_request_body
*body TSRMLS_DC
)
322 http_request_body_dtor(body
);
328 /* {{{ STATUS http_request_init(CURL *, http_request_method, char *, http_request_body *, HashTable *) */
329 PHP_HTTP_API STATUS
_http_request_init(CURL
*ch
, http_request_method meth
, char *url
, http_request_body
*body
, HashTable
*options TSRMLS_DC
)
332 zend_bool range_req
= 0;
334 /* reset CURL handle */
335 #if LIBCURL_VERSION_NUM >= 0x070c01
341 HTTP_CURL_OPT(URL
, http_request_data_copy(COPY_STRING
, url
));
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 /* we'll get all data through the debug function */
349 HTTP_CURL_OPT(WRITEFUNCTION
, http_curl_dummy_callback
);
350 HTTP_CURL_OPT(HEADERFUNCTION
, NULL
);
352 HTTP_CURL_OPT(VERBOSE
, 1);
353 HTTP_CURL_OPT(DEBUGFUNCTION
, http_curl_raw_callback
);
355 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
356 HTTP_CURL_OPT(NOSIGNAL
, 1);
358 #if LIBCURL_VERSION_NUM < 0x070c00
359 HTTP_CURL_OPT(ERRORBUFFER
, HTTP_G(request
).error
);
362 /* progress callback */
363 if (zoption
= http_curl_getopt(options
, "onprogress", 0)) {
364 HTTP_CURL_OPT(NOPROGRESS
, 0);
365 HTTP_CURL_OPT(PROGRESSFUNCTION
, http_curl_progress_callback
);
366 HTTP_CURL_OPT(PROGRESSDATA
, http_curl_callback_data(zoption
));
368 HTTP_CURL_OPT(NOPROGRESS
, 1);
372 if (zoption
= http_curl_getopt(options
, "proxyhost", IS_STRING
)) {
373 HTTP_CURL_OPT(PROXY
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
375 if (zoption
= http_curl_getopt(options
, "proxyport", IS_LONG
)) {
376 HTTP_CURL_OPT(PROXYPORT
, Z_LVAL_P(zoption
));
379 if (zoption
= http_curl_getopt(options
, "proxyauth", IS_STRING
)) {
380 HTTP_CURL_OPT(PROXYUSERPWD
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
382 #if LIBCURL_VERSION_NUM >= 0x070a07
384 if (zoption
= http_curl_getopt(options
, "proxyauthtype", IS_LONG
)) {
385 HTTP_CURL_OPT(PROXYAUTH
, Z_LVAL_P(zoption
));
390 /* outgoing interface */
391 if (zoption
= http_curl_getopt(options
, "interface", IS_STRING
)) {
392 HTTP_CURL_OPT(INTERFACE
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
396 if (zoption
= http_curl_getopt(options
, "port", IS_LONG
)) {
397 HTTP_CURL_OPT(PORT
, Z_LVAL_P(zoption
));
401 if (zoption
= http_curl_getopt(options
, "httpauth", IS_STRING
)) {
402 HTTP_CURL_OPT(USERPWD
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
404 #if LIBCURL_VERSION_NUM >= 0x070a06
405 if (zoption
= http_curl_getopt(options
, "httpauthtype", IS_LONG
)) {
406 HTTP_CURL_OPT(HTTPAUTH
, Z_LVAL_P(zoption
));
410 /* compress, empty string enables deflate and gzip */
411 if ((zoption
= http_curl_getopt(options
, "compress", IS_BOOL
)) && Z_LVAL_P(zoption
)) {
412 HTTP_CURL_OPT(ENCODING
, "");
414 HTTP_CURL_OPT(ENCODING
, 0);
417 /* redirects, defaults to 0 */
418 if (zoption
= http_curl_getopt(options
, "redirect", IS_LONG
)) {
419 HTTP_CURL_OPT(FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
420 HTTP_CURL_OPT(MAXREDIRS
, Z_LVAL_P(zoption
));
421 if (zoption
= http_curl_getopt(options
, "unrestrictedauth", IS_BOOL
)) {
422 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
425 HTTP_CURL_OPT(FOLLOWLOCATION
, 0);
429 if (zoption
= http_curl_getopt(options
, "referer", IS_STRING
)) {
430 HTTP_CURL_OPT(REFERER
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
432 HTTP_CURL_OPT(REFERER
, NULL
);
435 /* useragent, default "PECL::HTTP/version (PHP/version)" */
436 if (zoption
= http_curl_getopt(options
, "useragent", IS_STRING
)) {
437 HTTP_CURL_OPT(USERAGENT
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
439 HTTP_CURL_OPT(USERAGENT
, "PECL::HTTP/" HTTP_PEXT_VERSION
" (PHP/" PHP_VERSION
")");
442 /* additional headers, array('name' => 'value') */
443 if (zoption
= http_curl_getopt(options
, "headers", IS_ARRAY
)) {
446 struct curl_slist
*headers
= NULL
;
448 FOREACH_KEY(zoption
, header_key
, header_idx
) {
451 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &header_val
)) {
452 char header
[1024] = {0};
453 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
454 headers
= curl_slist_append(headers
, http_request_data_copy(COPY_STRING
, header
));
463 HTTP_CURL_OPT(HTTPHEADER
, http_request_data_copy(COPY_SLIST
, headers
));
466 HTTP_CURL_OPT(HTTPHEADER
, NULL
);
469 /* cookies, array('name' => 'value') */
470 if (zoption
= http_curl_getopt(options
, "cookies", IS_ARRAY
)) {
471 char *cookie_key
= NULL
;
472 ulong cookie_idx
= 0;
473 phpstr
*qstr
= phpstr_new();
475 FOREACH_KEY(zoption
, cookie_key
, cookie_idx
) {
478 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &cookie_val
)) {
479 phpstr_appendf(qstr
, "%s=%s; ", cookie_key
, Z_STRVAL_PP(cookie_val
));
489 HTTP_CURL_OPT(COOKIE
, http_request_data_copy(COPY_STRING
, qstr
->data
));
493 HTTP_CURL_OPT(COOKIE
, NULL
);
496 /* session cookies */
497 if (zoption
= http_curl_getopt(options
, "cookiesession", IS_BOOL
)) {
498 if (Z_LVAL_P(zoption
)) {
499 /* accept cookies for this session */
500 HTTP_CURL_OPT(COOKIEFILE
, "");
502 /* reset session cookies */
503 HTTP_CURL_OPT(COOKIESESSION
, 1);
506 HTTP_CURL_OPT(COOKIEFILE
, NULL
);
509 /* cookiestore, read initial cookies from that file and store cookies back into that file */
510 if ((zoption
= http_curl_getopt(options
, "cookiestore", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
511 HTTP_CURL_OPT(COOKIEFILE
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
512 HTTP_CURL_OPT(COOKIEJAR
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
514 HTTP_CURL_OPT(COOKIEFILE
, NULL
);
515 HTTP_CURL_OPT(COOKIEJAR
, NULL
);
519 if (zoption
= http_curl_getopt(options
, "resume", IS_LONG
)) {
521 HTTP_CURL_OPT(RESUME_FROM
, Z_LVAL_P(zoption
));
523 HTTP_CURL_OPT(RESUME_FROM
, 0);
527 if (zoption
= http_curl_getopt(options
, "maxfilesize", IS_LONG
)) {
528 HTTP_CURL_OPT(MAXFILESIZE
, Z_LVAL_P(zoption
));
530 HTTP_CURL_OPT(MAXFILESIZE
, 0);
534 if (zoption
= http_curl_getopt(options
, "lastmodified", IS_LONG
)) {
535 HTTP_CURL_OPT(TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
536 HTTP_CURL_OPT(TIMEVALUE
, Z_LVAL_P(zoption
));
538 HTTP_CURL_OPT(TIMEVALUE
, 0);
541 /* timeout, defaults to 3 */
542 if (zoption
= http_curl_getopt(options
, "timeout", IS_LONG
)) {
543 HTTP_CURL_OPT(TIMEOUT
, Z_LVAL_P(zoption
));
545 HTTP_CURL_OPT(TIMEOUT
, 3);
548 /* connecttimeout, defaults to 3 */
549 if (zoption
= http_curl_getopt(options
, "connecttimeout", IS_LONG
)) {
550 HTTP_CURL_OPT(CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
552 HTTP_CURL_OPT(CONNECTTIMEOUT
, 3);
556 if (zoption
= http_curl_getopt(options
, "ssl", IS_ARRAY
)) {
561 FOREACH_KEYVAL(zoption
, key
, idx
, param
) {
563 HTTP_CURL_OPT_SSL_STRING(CERT
);
564 #if LIBCURL_VERSION_NUM >= 0x070903
565 HTTP_CURL_OPT_SSL_STRING(CERTTYPE
);
567 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD
);
569 HTTP_CURL_OPT_SSL_STRING(KEY
);
570 HTTP_CURL_OPT_SSL_STRING(KEYTYPE
);
571 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD
);
573 HTTP_CURL_OPT_SSL_STRING(ENGINE
);
574 HTTP_CURL_OPT_SSL_LONG(VERSION
);
576 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER
);
577 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST
);
578 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST
);
581 HTTP_CURL_OPT_STRING(CAINFO
);
582 #if LIBCURL_VERSION_NUM >= 0x070908
583 HTTP_CURL_OPT_STRING(CAPATH
);
585 HTTP_CURL_OPT_STRING(RANDOM_FILE
);
586 HTTP_CURL_OPT_STRING(EGDSOCKET
);
593 /* disable SSL verification by default */
594 HTTP_CURL_OPT(SSL_VERIFYPEER
, 0);
595 HTTP_CURL_OPT(SSL_VERIFYHOST
, 0);
602 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1);
606 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
610 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
614 curl_easy_setopt(ch
, CURLOPT_UPLOAD
, 1);
618 if (http_request_method_exists(0, meth
, NULL
)) {
619 curl_easy_setopt(ch
, CURLOPT_CUSTOMREQUEST
, http_request_method_name(meth
));
621 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Unsupported request method: %d", meth
);
627 /* attach request body */
628 if (body
&& (meth
!= HTTP_GET
) && (meth
!= HTTP_HEAD
)) {
631 case HTTP_REQUEST_BODY_CSTRING
:
632 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, body
->data
);
633 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, body
->size
);
636 case HTTP_REQUEST_BODY_CURLPOST
:
637 curl_easy_setopt(ch
, CURLOPT_HTTPPOST
, (struct curl_httppost
*) body
->data
);
640 case HTTP_REQUEST_BODY_UPLOADFILE
:
641 curl_easy_setopt(ch
, CURLOPT_READDATA
, http_curl_callback_data(body
));
642 curl_easy_setopt(ch
, CURLOPT_INFILESIZE
, body
->size
);
646 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Unknown request body type: %d", body
->type
);
656 /* {{{ STATUS http_request_exec(CURL *, HashTable *) */
657 PHP_HTTP_API STATUS
_http_request_exec(CURL
*ch
, HashTable
*info
, phpstr
*response
, phpstr
*request TSRMLS_DC
)
660 http_curl_conv conv
= {response
, request
, -1};
662 HTTP_CURL_OPT(DEBUGDATA
, http_curl_callback_data(&conv
));
664 /* perform request */
665 if (CURLE_OK
!= (result
= curl_easy_perform(ch
))) {
666 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not perform request: %s", curl_easy_strerror(result
));
671 http_request_info(ch
, info
);
678 /* {{{ void http_request_info(CURL *, HashTable *) */
679 PHP_HTTP_API
void _http_request_info(CURL
*ch
, HashTable
*info TSRMLS_DC
)
682 Z_ARRVAL(array
) = info
;
684 HTTP_CURL_INFO(EFFECTIVE_URL
);
685 #if LIBCURL_VERSION_NUM >= 0x070a07
686 HTTP_CURL_INFO(RESPONSE_CODE
);
688 HTTP_CURL_INFO_EX(HTTP_CODE
, RESPONSE_CODE
);
690 HTTP_CURL_INFO(HTTP_CONNECTCODE
);
691 #if LIBCURL_VERSION_NUM >= 0x070500
692 HTTP_CURL_INFO(FILETIME
);
694 HTTP_CURL_INFO(TOTAL_TIME
);
695 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
696 HTTP_CURL_INFO(CONNECT_TIME
);
697 HTTP_CURL_INFO(PRETRANSFER_TIME
);
698 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
699 #if LIBCURL_VERSION_NUM >= 0x070907
700 HTTP_CURL_INFO(REDIRECT_TIME
);
701 HTTP_CURL_INFO(REDIRECT_COUNT
);
703 HTTP_CURL_INFO(SIZE_UPLOAD
);
704 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
705 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
706 HTTP_CURL_INFO(SPEED_UPLOAD
);
707 HTTP_CURL_INFO(HEADER_SIZE
);
708 HTTP_CURL_INFO(REQUEST_SIZE
);
709 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
710 #if LIBCURL_VERSION_NUM >= 0x070c03
711 /*HTTP_CURL_INFO(SSL_ENGINES); todo: CURLINFO_SLIST */
713 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
714 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
715 HTTP_CURL_INFO(CONTENT_TYPE
);
716 #if LIBCURL_VERSION_NUM >= 0x070a03
717 /*HTTP_CURL_INFO(PRIVATE);*/
719 #if LIBCURL_VERSION_NUM >= 0x070a08
720 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
721 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
723 #if LIBCURL_VERSION_NUM >= 0x070c02
724 /*HTTP_CURL_INFO(OS_ERRNO);*/
726 #if LIBCURL_VERSION_NUM >= 0x070c03
727 HTTP_CURL_INFO(NUM_CONNECTS
);
732 /* {{{ STATUS http_request_ex(CURL *, http_request_method, char *, http_request_body, HashTable, HashTable, phpstr *) */
733 PHP_HTTP_API STATUS
_http_request_ex(CURL
*ch
, http_request_method meth
, char *url
, http_request_body
*body
, HashTable
*options
, HashTable
*info
, phpstr
*response TSRMLS_DC
)
736 zend_bool clean_curl
;
738 if ((clean_curl
= (!ch
))) {
739 if (!(ch
= curl_easy_init())) {
740 http_error(E_WARNING
, HTTP_E_CURL
, "Could not initialize curl.");
745 status
= ((SUCCESS
== http_request_init(ch
, meth
, url
, body
, options
)) &&
746 (SUCCESS
== http_request_exec(ch
, info
, response
, NULL
))) ? SUCCESS
: FAILURE
;
749 curl_easy_cleanup(ch
);
755 /* {{{ char *http_request_method_name(http_request_method) */
756 PHP_HTTP_API
const char *_http_request_method_name(http_request_method m TSRMLS_DC
)
760 if (HTTP_STD_REQUEST_METHOD(m
)) {
761 return http_request_methods
[m
];
764 if (SUCCESS
== zend_hash_index_find(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(m
), (void **) &meth
)) {
765 return Z_STRVAL_PP(meth
);
768 return http_request_methods
[0];
772 /* {{{ unsigned long http_request_method_exists(zend_bool, unsigned long, char *) */
773 PHP_HTTP_API
unsigned long _http_request_method_exists(zend_bool by_name
, unsigned long id
, const char *name TSRMLS_DC
)
778 for (i
= HTTP_NO_REQUEST_METHOD
+ 1; i
< HTTP_MAX_REQUEST_METHOD
; ++i
) {
779 if (!strcmp(name
, http_request_methods
[i
])) {
788 FOREACH_HASH_KEYVAL(&HTTP_G(request
).methods
.custom
, key
, idx
, data
) {
789 if (!strcmp(name
, Z_STRVAL_PP(data
))) {
790 return idx
+ HTTP_MAX_REQUEST_METHOD
;
796 return HTTP_STD_REQUEST_METHOD(id
) || zend_hash_index_exists(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(id
)) ? id
: 0;
801 /* {{{ unsigned long http_request_method_register(char *) */
802 PHP_HTTP_API
unsigned long _http_request_method_register(const char *method TSRMLS_DC
)
806 unsigned long meth_num
= HTTP_G(request
).methods
.custom
.nNextFreeElement
+ HTTP_MAX_REQUEST_METHOD
;
808 Z_ARRVAL(array
) = &HTTP_G(request
).methods
.custom
;
809 add_next_index_string(&array
, estrdup(method
), 0);
811 spprintf(&http_method
, 0, "HTTP_%s", method
);
812 zend_register_long_constant(http_method
, strlen(http_method
) + 1, meth_num
, CONST_CS
, http_module_number TSRMLS_CC
);
819 /* {{{ STATUS http_request_method_unregister(usngigned long) */
820 PHP_HTTP_API STATUS
_http_request_method_unregister(unsigned long method TSRMLS_DC
)
825 if (SUCCESS
!= zend_hash_index_find(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(method
), (void **) &zmethod
)) {
826 http_error_ex(E_NOTICE
, HTTP_E_PARAM
, "Request method with id %lu does not exist", method
);
830 spprintf(&http_method
, 0, "HTTP_%s", Z_STRVAL_PP(zmethod
));
832 if ( (SUCCESS
!= zend_hash_index_del(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(method
)))
833 || (SUCCESS
!= zend_hash_del(EG(zend_constants
), http_method
, strlen(http_method
) + 1))) {
834 http_error_ex(E_NOTICE
, 0, "Could not unregister request method: %s", http_method
);
845 /* {{{ char *http_request_methods[] */
846 static const char *const http_request_methods
[] = {
857 /* WebDAV - RFC 2518 */
865 /* WebDAV Versioning - RFC 3253 */
877 /* WebDAV Access Control - RFC 3744 */
883 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
884 static size_t http_curl_read_callback(void *data
, size_t len
, size_t n
, void *s
)
886 HTTP_CURL_CALLBACK_DATA(s
, http_request_body
*, body
);
888 if (body
->type
!= HTTP_REQUEST_BODY_UPLOADFILE
) {
891 return php_stream_read((php_stream
*) body
->data
, data
, len
* n
);
895 /* {{{ http_curl_callback_ctx http_curl_callback_data(void *) */
896 static http_curl_callback_ctx
*_http_curl_callback_data(void *data TSRMLS_DC
)
898 http_curl_callback_ctx
*ctx
= emalloc(sizeof(http_curl_callback_ctx
));
899 TSRMLS_SET_CTX(ctx
->tsrm_ctx
);
901 return http_request_data_copy(COPY_CONTEXT
, ctx
);
905 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
906 static int http_curl_progress_callback(void *data
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
908 zval
*params_pass
[4], params_local
[4], retval
;
909 HTTP_CURL_CALLBACK_DATA(data
, zval
*, func
);
911 params_pass
[0] = ¶ms_local
[0];
912 params_pass
[1] = ¶ms_local
[1];
913 params_pass
[2] = ¶ms_local
[2];
914 params_pass
[3] = ¶ms_local
[3];
916 INIT_PZVAL(params_pass
[0]);
917 INIT_PZVAL(params_pass
[1]);
918 INIT_PZVAL(params_pass
[2]);
919 INIT_PZVAL(params_pass
[3]);
920 ZVAL_DOUBLE(params_pass
[0], dltotal
);
921 ZVAL_DOUBLE(params_pass
[1], dlnow
);
922 ZVAL_DOUBLE(params_pass
[2], ultotal
);
923 ZVAL_DOUBLE(params_pass
[3], ulnow
);
925 return call_user_function(EG(function_table
), NULL
, func
, &retval
, 4, params_pass TSRMLS_CC
);
929 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
930 static int http_curl_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
932 HTTP_CURL_CALLBACK_DATA(ctx
, http_curl_conv
*, conv
);
936 case CURLINFO_DATA_IN
:
937 if (conv
->response
&& conv
->last_info
== CURLINFO_HEADER_IN
) {
938 phpstr_appends(conv
->response
, HTTP_CRLF
);
940 case CURLINFO_HEADER_IN
:
941 if (conv
->response
) {
942 phpstr_append(conv
->response
, data
, length
);
945 case CURLINFO_DATA_OUT
:
946 if (conv
->request
&& conv
->last_info
== CURLINFO_HEADER_OUT
) {
947 phpstr_appends(conv
->request
, HTTP_CRLF
);
949 case CURLINFO_HEADER_OUT
:
951 phpstr_append(conv
->request
, data
, length
);
957 conv
->last_info
= type
;
963 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
964 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
)
968 if (!options
|| (SUCCESS
!= zend_hash_find(options
, key
, keylen
, (void **) &zoption
))) {
972 if (Z_TYPE_PP(zoption
) != type
) {
975 case IS_BOOL
: convert_to_boolean_ex(zoption
); break;
976 case IS_LONG
: convert_to_long_ex(zoption
); break;
977 case IS_DOUBLE
: convert_to_double_ex(zoption
); break;
978 case IS_STRING
: convert_to_string_ex(zoption
); break;
979 case IS_ARRAY
: convert_to_array_ex(zoption
); break;
980 case IS_OBJECT
: convert_to_object_ex(zoption
); break;
995 * vim600: noet sw=4 ts=4 fdm=marker
996 * vim<600: noet sw=4 ts=4