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_curl_api.h"
35 #include "php_http_url_api.h"
37 ZEND_EXTERN_MODULE_GLOBALS(http
)
39 #if LIBCURL_VERSION_NUM >= 0x070c01
40 # define http_curl_reset(ch) curl_easy_reset(ch)
42 # define http_curl_reset(ch)
45 #if LIBCURL_VERSION_NUM < 0x070c00
46 # define http_curl_error(dummy) HTTP_G(curlerr)
47 # define curl_easy_strerror(code) "unkown error"
49 # define http_curl_error(code) curl_easy_strerror(code)
52 #define http_curl_startup(ch, clean_curl, URL, options, response) \
54 if (!(ch = curl_easy_init())) { \
55 http_error(E_WARNING, HTTP_E_CURL, "Could not initialize curl"); \
60 http_curl_reset(ch); \
62 http_curl_setopts(ch, URL, options, response);
64 #define http_curl_perform(ch, clean_curl, response) \
67 if (CURLE_OK != (result = curl_easy_perform(ch))) { \
68 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not perform request: %s", curl_easy_strerror(result)); \
69 http_curl_cleanup(ch, clean_curl, response); \
74 #define http_curl_cleanup(ch, clean_curl, response) \
75 zend_llist_clean(&HTTP_G(to_free)); \
77 curl_easy_cleanup(ch); \
80 phpstr_fix(PHPSTR(response))
82 #define http_curl_copystr(s) _http_curl_copystr((s) TSRMLS_CC)
83 static inline char *_http_curl_copystr(const char *str TSRMLS_DC
);
85 #define http_curl_setopts(c, u, o, r) _http_curl_setopts((c), (u), (o), (r) TSRMLS_CC)
86 static void _http_curl_setopts(CURL
*ch
, const char *url
, HashTable
*options
, phpstr
*response TSRMLS_DC
);
88 #define http_curl_getopt(o, k) _http_curl_getopt((o), (k) TSRMLS_CC, 0)
89 #define http_curl_getopt1(o, k, t1) _http_curl_getopt((o), (k) TSRMLS_CC, 1, (t1))
90 #define http_curl_getopt2(o, k, t1, t2) _http_curl_getopt((o), (k) TSRMLS_CC, 2, (t1), (t2))
91 static inline zval
*_http_curl_getopt(HashTable
*options
, char *key TSRMLS_DC
, int checks
, ...);
93 static size_t http_curl_callback(char *, size_t, size_t, void *);
95 #define http_curl_getinfo(c, h) _http_curl_getinfo((c), (h) TSRMLS_CC)
96 static inline void _http_curl_getinfo(CURL
*ch
, HashTable
*info TSRMLS_DC
);
98 /* {{{ static inline char *http_curl_copystr(char *) */
99 static inline char *_http_curl_copystr(const char *str TSRMLS_DC
)
101 char *new_str
= estrdup(str
);
102 zend_llist_add_element(&HTTP_G(to_free
), &new_str
);
107 /* {{{ static size_t http_curl_callback(char *, size_t, size_t, void *) */
108 static size_t http_curl_callback(char *buf
, size_t len
, size_t n
, void *s
)
110 phpstr_append(PHPSTR(s
), buf
, len
*= n
);
115 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, int, ...) */
116 static inline zval
*_http_curl_getopt(HashTable
*options
, char *key TSRMLS_DC
, int checks
, ...)
122 if (SUCCESS
!= zend_hash_find(options
, key
, strlen(key
) + 1, (void **) &zoption
)) {
129 va_start(types
, checks
);
130 for (i
= 0; i
< checks
; ++i
) {
131 if ((va_arg(types
, int)) == (Z_TYPE_PP(zoption
))) {
141 /* {{{ static void http_curl_setopts(CURL *, char *, HashTable *, phpstr *) */
142 static void _http_curl_setopts(CURL
*ch
, const char *url
, HashTable
*options
, phpstr
*response TSRMLS_DC
)
145 zend_bool range_req
= 0;
147 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
149 /* standard options */
150 HTTP_CURL_OPT(URL
, url
);
151 HTTP_CURL_OPT(HEADER
, 0);
152 HTTP_CURL_OPT(FILETIME
, 1);
153 HTTP_CURL_OPT(NOPROGRESS
, 1);
154 HTTP_CURL_OPT(AUTOREFERER
, 1);
155 HTTP_CURL_OPT(WRITEFUNCTION
, http_curl_callback
);
156 HTTP_CURL_OPT(HEADERFUNCTION
, http_curl_callback
);
157 HTTP_CURL_OPT(WRITEDATA
, response
);
158 HTTP_CURL_OPT(WRITEHEADER
, response
);
159 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
160 HTTP_CURL_OPT(NOSIGNAL
, 1);
162 #if LIBCURL_VERSION_NUM < 0x070c00
163 HTTP_CURL_OPT(ERRORBUFFER
, HTTP_G(curlerr
));
166 if ((!options
) || (1 > zend_hash_num_elements(options
))) {
171 if (zoption
= http_curl_getopt1(options
, "proxyhost", IS_STRING
)) {
172 HTTP_CURL_OPT(PROXY
, http_curl_copystr(Z_STRVAL_P(zoption
)));
174 if (zoption
= http_curl_getopt1(options
, "proxyport", IS_LONG
)) {
175 HTTP_CURL_OPT(PROXYPORT
, Z_LVAL_P(zoption
));
178 if (zoption
= http_curl_getopt1(options
, "proxyauth", IS_STRING
)) {
179 HTTP_CURL_OPT(PROXYUSERPWD
, http_curl_copystr(Z_STRVAL_P(zoption
)));
181 #if LIBCURL_VERSION_NUM >= 0x070a07
183 if (zoption
= http_curl_getopt1(options
, "proxyauthtype", IS_LONG
)) {
184 HTTP_CURL_OPT(PROXYAUTH
, Z_LVAL_P(zoption
));
189 /* outgoing interface */
190 if (zoption
= http_curl_getopt1(options
, "interface", IS_STRING
)) {
191 HTTP_CURL_OPT(INTERFACE
, http_curl_copystr(Z_STRVAL_P(zoption
)));
195 if (zoption
= http_curl_getopt1(options
, "port", IS_LONG
)) {
196 HTTP_CURL_OPT(PORT
, Z_LVAL_P(zoption
));
200 if (zoption
= http_curl_getopt1(options
, "httpauth", IS_STRING
)) {
201 HTTP_CURL_OPT(USERPWD
, http_curl_copystr(Z_STRVAL_P(zoption
)));
203 #if LIBCURL_VERSION_NUM >= 0x070a06
204 if (zoption
= http_curl_getopt1(options
, "httpauthtype", IS_LONG
)) {
205 HTTP_CURL_OPT(HTTPAUTH
, Z_LVAL_P(zoption
));
209 /* compress, empty string enables deflate and gzip */
210 if (zoption
= http_curl_getopt2(options
, "compress", IS_LONG
, IS_BOOL
)) {
211 if (Z_LVAL_P(zoption
)) {
212 HTTP_CURL_OPT(ENCODING
, "");
216 /* redirects, defaults to 0 */
217 if (zoption
= http_curl_getopt1(options
, "redirect", IS_LONG
)) {
218 HTTP_CURL_OPT(FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
219 HTTP_CURL_OPT(MAXREDIRS
, Z_LVAL_P(zoption
));
220 if (zoption
= http_curl_getopt2(options
, "unrestrictedauth", IS_LONG
, IS_BOOL
)) {
221 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
224 HTTP_CURL_OPT(FOLLOWLOCATION
, 0);
228 if (zoption
= http_curl_getopt1(options
, "referer", IS_STRING
)) {
229 HTTP_CURL_OPT(REFERER
, http_curl_copystr(Z_STRVAL_P(zoption
)));
232 /* useragent, default "PECL::HTTP/version (PHP/version)" */
233 if (zoption
= http_curl_getopt1(options
, "useragent", IS_STRING
)) {
234 HTTP_CURL_OPT(USERAGENT
, http_curl_copystr(Z_STRVAL_P(zoption
)));
236 HTTP_CURL_OPT(USERAGENT
,
237 "PECL::HTTP/" HTTP_PEXT_VERSION
" (PHP/" PHP_VERSION
")");
240 /* additional headers, array('name' => 'value') */
241 if (zoption
= http_curl_getopt1(options
, "headers", IS_ARRAY
)) {
244 struct curl_slist
*headers
= NULL
;
246 FOREACH_KEY(zoption
, header_key
, header_idx
) {
249 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &header_val
)) {
250 char header
[1024] = {0};
251 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
252 headers
= curl_slist_append(headers
, http_curl_copystr(header
));
261 HTTP_CURL_OPT(HTTPHEADER
, headers
);
265 /* cookies, array('name' => 'value') */
266 if (zoption
= http_curl_getopt1(options
, "cookies", IS_ARRAY
)) {
267 char *cookie_key
= NULL
;
269 phpstr
*qstr
= phpstr_new();
271 FOREACH_KEY(zoption
, cookie_key
, cookie_idx
) {
274 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &cookie_val
)) {
275 phpstr_appendf(qstr
, "%s=%s; ", cookie_key
, Z_STRVAL_PP(cookie_val
));
285 HTTP_CURL_OPT(COOKIE
, http_curl_copystr(qstr
->data
));
291 if (zoption
= http_curl_getopt1(options
, "cookiestore", IS_STRING
)) {
292 HTTP_CURL_OPT(COOKIEFILE
, http_curl_copystr(Z_STRVAL_P(zoption
)));
293 HTTP_CURL_OPT(COOKIEJAR
, http_curl_copystr(Z_STRVAL_P(zoption
)));
297 if (zoption
= http_curl_getopt1(options
, "resume", IS_LONG
)) {
299 HTTP_CURL_OPT(RESUME_FROM
, Z_LVAL_P(zoption
));
303 if (zoption
= http_curl_getopt1(options
, "maxfilesize", IS_LONG
)) {
304 HTTP_CURL_OPT(MAXFILESIZE
, Z_LVAL_P(zoption
));
308 if (zoption
= http_curl_getopt1(options
, "lastmodified", IS_LONG
)) {
309 HTTP_CURL_OPT(TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
310 HTTP_CURL_OPT(TIMEVALUE
, Z_LVAL_P(zoption
));
314 if (zoption
= http_curl_getopt1(options
, "timeout", IS_LONG
)) {
315 HTTP_CURL_OPT(TIMEOUT
, Z_LVAL_P(zoption
));
319 if (zoption
= http_curl_getopt1(options
, "connecttimeout", IS_LONG
)) {
320 HTTP_CURL_OPT(CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
324 if (zoption
= http_curl_getopt1(options
, "ssl", IS_ARRAY
)) {
329 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
330 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
331 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
332 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
333 if (!strcasecmp(key, #keyname)) { \
334 convert_to_string_ex(param); \
335 HTTP_CURL_OPT(optname, Z_STRVAL_PP(param)); \
339 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
340 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
341 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
342 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
343 if (!strcasecmp(key, #keyname)) { \
344 convert_to_long_ex(param); \
345 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
350 FOREACH_KEYVAL(zoption
, key
, idx
, param
) {
352 HTTP_CURL_OPT_SSL_STRING(CERT
);
353 #if LIBCURL_VERSION_NUM >= 0x070903
354 HTTP_CURL_OPT_SSL_STRING(CERTTYPE
);
356 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD
);
358 HTTP_CURL_OPT_SSL_STRING(KEY
);
359 HTTP_CURL_OPT_SSL_STRING(KEYTYPE
);
360 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD
);
362 HTTP_CURL_OPT_SSL_STRING(ENGINE
);
363 HTTP_CURL_OPT_SSL_LONG(VERSION
);
365 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER
);
366 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST
);
367 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST
);
370 HTTP_CURL_OPT_STRING(CAINFO
);
371 #if LIBCURL_VERSION_NUM >= 0x070908
372 HTTP_CURL_OPT_STRING(CAPATH
);
374 HTTP_CURL_OPT_STRING(RANDOM_FILE
);
375 HTTP_CURL_OPT_STRING(EGDSOCKET
);
385 /* {{{ static inline http_curl_getinfo(CURL, HashTable *) */
386 static inline void _http_curl_getinfo(CURL
*ch
, HashTable
*info TSRMLS_DC
)
389 Z_ARRVAL(array
) = info
;
391 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
392 #define HTTP_CURL_INFO_EX(I, X) \
393 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
395 case CURLINFO_STRING: \
398 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
399 add_assoc_string(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
404 case CURLINFO_DOUBLE: \
407 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
408 add_assoc_double(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), d); \
413 case CURLINFO_LONG: \
416 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
417 add_assoc_long(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), l); \
423 HTTP_CURL_INFO(EFFECTIVE_URL
);
425 #if LIBCURL_VERSION_NUM >= 0x070a07
426 HTTP_CURL_INFO(RESPONSE_CODE
);
428 HTTP_CURL_INFO_EX(HTTP_CODE
, RESPONSE_CODE
);
430 HTTP_CURL_INFO(HTTP_CONNECTCODE
);
432 #if LIBCURL_VERSION_NUM >= 0x070500
433 HTTP_CURL_INFO(FILETIME
);
435 HTTP_CURL_INFO(TOTAL_TIME
);
436 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
437 HTTP_CURL_INFO(CONNECT_TIME
);
438 HTTP_CURL_INFO(PRETRANSFER_TIME
);
439 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
440 #if LIBCURL_VERSION_NUM >= 0x070907
441 HTTP_CURL_INFO(REDIRECT_TIME
);
442 HTTP_CURL_INFO(REDIRECT_COUNT
);
445 HTTP_CURL_INFO(SIZE_UPLOAD
);
446 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
447 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
448 HTTP_CURL_INFO(SPEED_UPLOAD
);
450 HTTP_CURL_INFO(HEADER_SIZE
);
451 HTTP_CURL_INFO(REQUEST_SIZE
);
453 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
454 #if LIBCURL_VERSION_NUM >= 0x070c03
455 /*HTTP_CURL_INFO(SSL_ENGINES);
456 todo: CURLINFO_SLIST */
459 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
460 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
461 HTTP_CURL_INFO(CONTENT_TYPE
);
463 #if LIBCURL_VERSION_NUM >= 0x070a03
464 /*HTTP_CURL_INFO(PRIVATE);*/
467 #if LIBCURL_VERSION_NUM >= 0x070a08
468 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
469 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
472 #if LIBCURL_VERSION_NUM >= 0x070c02
473 /*HTTP_CURL_INFO(OS_ERRNO);*/
476 #if LIBCURL_VERSION_NUM >= 0x070c03
477 HTTP_CURL_INFO(NUM_CONNECTS
);
482 /* {{{ STATUS http_get_ex(CURL *, char *, HashTable *, HashTable *, phpstr *) */
483 PHP_HTTP_API STATUS
_http_get_ex(CURL
*ch
, const char *URL
, HashTable
*options
, HashTable
*info
, phpstr
*response TSRMLS_DC
)
485 zend_bool clean_curl
= 0;
487 http_curl_startup(ch
, clean_curl
, URL
, options
, response
);
488 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1);
489 http_curl_perform(ch
, clean_curl
, response
);
492 http_curl_getinfo(ch
, info
);
495 http_curl_cleanup(ch
, clean_curl
, response
);
500 /* {{{ STATUS http_head_ex(CURL *, char *, HashTable *, HashTable *, phpstr *) */
501 PHP_HTTP_API STATUS
_http_head_ex(CURL
*ch
, const char *URL
, HashTable
*options
,HashTable
*info
, phpstr
*response TSRMLS_DC
)
503 zend_bool clean_curl
= 0;
505 http_curl_startup(ch
, clean_curl
, URL
, options
, response
);
506 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
507 http_curl_perform(ch
, clean_curl
, response
);
510 http_curl_getinfo(ch
, info
);
513 http_curl_cleanup(ch
, clean_curl
, response
);
518 /* {{{ STATUS http_post_data_ex(CURL *, char *, char *, size_t, HashTable *, HashTable *, phpstr *) */
519 PHP_HTTP_API STATUS
_http_post_data_ex(CURL
*ch
, const char *URL
, char *postdata
,
520 size_t postdata_len
, HashTable
*options
, HashTable
*info
, phpstr
*response TSRMLS_DC
)
522 zend_bool clean_curl
= 0;
524 http_curl_startup(ch
, clean_curl
, URL
, options
, response
);
525 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
526 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, postdata
);
527 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, postdata_len
);
528 http_curl_perform(ch
, clean_curl
, response
);
531 http_curl_getinfo(ch
, info
);
534 http_curl_cleanup(ch
, clean_curl
, response
);
540 /* {{{ STATUS http_post_array_ex(CURL *, char *, HashTable *, HashTable *, HashTable *, phpstr *) */
541 PHP_HTTP_API STATUS
_http_post_array_ex(CURL
*ch
, const char *URL
, HashTable
*postarray
,
542 HashTable
*options
, HashTable
*info
, phpstr
*response TSRMLS_DC
)
548 if (SUCCESS
!= http_urlencode_hash_ex(postarray
, 1, NULL
, 0, &encoded
, &encoded_len
)) {
549 http_error(E_WARNING
, HTTP_E_ENCODE
, "Could not encode post data");
553 status
= http_post_data_ex(ch
, URL
, encoded
, encoded_len
, options
, info
, response
);
560 /* {{{ STATUS http_post_curldata_ex(CURL *, char *, curl_httppost *, HashTable *, HashTable *, phpstr *) */
561 PHP_HTTP_API STATUS
_http_post_curldata_ex(CURL
*ch
, const char *URL
, struct curl_httppost
*curldata
,
562 HashTable
*options
, HashTable
*info
, phpstr
*response TSRMLS_DC
)
564 zend_bool clean_curl
= 0;
566 http_curl_startup(ch
, clean_curl
, URL
, options
, response
);
567 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
568 curl_easy_setopt(ch
, CURLOPT_HTTPPOST
, curldata
);
569 http_curl_perform(ch
, clean_curl
, response
);
572 http_curl_getinfo(ch
, info
);
575 http_curl_cleanup(ch
, clean_curl
, response
);
586 * vim600: noet sw=4 ts=4 fdm=marker
587 * vim<600: noet sw=4 ts=4