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>
30 #include "php_http_api.h"
31 #include "php_http_curl_api.h"
32 #include "php_http_std_defs.h"
34 #include "phpstr/phpstr.h"
36 ZEND_DECLARE_MODULE_GLOBALS(http
)
38 #if LIBCURL_VERSION_NUM >= 0x070c01
39 # define http_curl_reset(ch) curl_easy_reset(ch)
41 # define http_curl_reset(ch)
44 #if LIBCURL_VERSION_NUM < 0x070c00
45 # define curl_easy_strerror(code) "unkown error"
48 #define http_curl_startup(ch, clean_curl, URL, options) \
50 if (!(ch = curl_easy_init())) { \
51 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl"); \
56 http_curl_reset(ch); \
58 http_curl_setopts(ch, URL, options);
60 #define http_curl_perform(ch, clean_curl) \
63 if (CURLE_OK != (result = curl_easy_perform(ch))) { \
64 http_curl_cleanup(ch, clean_curl); \
65 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request: %s", curl_easy_strerror(result)); \
70 #define http_curl_cleanup(ch, clean_curl) \
71 phpstr_dtor(&HTTP_G(curlbuf)); \
72 zend_llist_clean(&HTTP_G(to_free)); \
74 curl_easy_cleanup(ch); \
78 #define http_curl_copybuf(d, l) \
79 phpstr_data(&HTTP_G(curlbuf), d, l)
81 #define http_curl_copystr(s) _http_curl_copystr((s) TSRMLS_CC)
82 static inline char *_http_curl_copystr(const char *str TSRMLS_DC
);
84 #define http_curl_setopts(c, u, o) _http_curl_setopts((c), (u), (o) TSRMLS_CC)
85 static void _http_curl_setopts(CURL
*ch
, const char *url
, HashTable
*options TSRMLS_DC
);
87 #define http_curl_getopt(o, k) _http_curl_getopt((o), (k) TSRMLS_CC, 0)
88 #define http_curl_getopt1(o, k, t1) _http_curl_getopt((o), (k) TSRMLS_CC, 1, (t1))
89 #define http_curl_getopt2(o, k, t1, t2) _http_curl_getopt((o), (k) TSRMLS_CC, 2, (t1), (t2))
90 static inline zval
*_http_curl_getopt(HashTable
*options
, char *key TSRMLS_DC
, int checks
, ...);
92 static size_t http_curl_body_callback(char *, size_t, size_t, void *);
93 static size_t http_curl_hdrs_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_body_callback(char *, size_t, size_t, void *) */
108 static size_t http_curl_body_callback(char *buf
, size_t len
, size_t n
, void *s
)
112 phpstr_append(&HTTP_G(curlbuf
), buf
, len
*= n
);
117 /* {{{ static size_t http_curl_hdrs_callback(char *, size_t, size_t, void *) */
118 static size_t http_curl_hdrs_callback(char *buf
, size_t len
, size_t n
, void *s
)
122 /* discard previous headers */
123 if (HTTP_G(curlbuf
).used
&& (!strncmp(buf
, "HTTP/1.", sizeof("HTTP/1.") - 1))) {
124 phpstr_free(&HTTP_G(curlbuf
));
127 phpstr_append(&HTTP_G(curlbuf
), buf
, len
*= n
);
132 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, int, ...) */
133 static inline zval
*_http_curl_getopt(HashTable
*options
, char *key TSRMLS_DC
, int checks
, ...)
139 if (SUCCESS
!= zend_hash_find(options
, key
, strlen(key
) + 1, (void **) &zoption
)) {
146 va_start(types
, checks
);
147 for (i
= 0; i
< checks
; ++i
) {
148 if ((va_arg(types
, int)) == (Z_TYPE_PP(zoption
))) {
158 /* {{{ static void http_curl_setopts(CURL *, char *, HashTable *) */
159 static void _http_curl_setopts(CURL
*ch
, const char *url
, HashTable
*options TSRMLS_DC
)
162 zend_bool range_req
= 0;
164 /* standard options */
165 curl_easy_setopt(ch
, CURLOPT_URL
, url
);
166 curl_easy_setopt(ch
, CURLOPT_HEADER
, 0);
167 curl_easy_setopt(ch
, CURLOPT_FILETIME
, 1);
168 curl_easy_setopt(ch
, CURLOPT_NOPROGRESS
, 1);
169 curl_easy_setopt(ch
, CURLOPT_AUTOREFERER
, 1);
170 curl_easy_setopt(ch
, CURLOPT_WRITEFUNCTION
, http_curl_body_callback
);
171 curl_easy_setopt(ch
, CURLOPT_HEADERFUNCTION
, http_curl_hdrs_callback
);
172 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
173 curl_easy_setopt(ch
, CURLOPT_NOSIGNAL
, 1);
176 if ((!options
) || (1 > zend_hash_num_elements(options
))) {
181 if (zoption
= http_curl_getopt1(options
, "proxyhost", IS_STRING
)) {
182 curl_easy_setopt(ch
, CURLOPT_PROXY
, http_curl_copystr(Z_STRVAL_P(zoption
)));
184 if (zoption
= http_curl_getopt1(options
, "proxyport", IS_LONG
)) {
185 curl_easy_setopt(ch
, CURLOPT_PROXYPORT
, Z_LVAL_P(zoption
));
188 if (zoption
= http_curl_getopt1(options
, "proxyauth", IS_STRING
)) {
189 curl_easy_setopt(ch
, CURLOPT_PROXYUSERPWD
, http_curl_copystr(Z_STRVAL_P(zoption
)));
191 #if LIBCURL_VERSION_NUM >= 0x070a07
193 if (zoption
= http_curl_getopt1(options
, "proxyauthtype", IS_LONG
)) {
194 curl_easy_setopt(ch
, CURLOPT_PROXYAUTH
, Z_LVAL_P(zoption
));
199 /* outgoing interface */
200 if (zoption
= http_curl_getopt1(options
, "interface", IS_STRING
)) {
201 curl_easy_setopt(ch
, CURLOPT_INTERFACE
, http_curl_copystr(Z_STRVAL_P(zoption
)));
205 if (zoption
= http_curl_getopt1(options
, "port", IS_LONG
)) {
206 curl_easy_setopt(ch
, CURLOPT_PORT
, Z_LVAL_P(zoption
));
210 if (zoption
= http_curl_getopt1(options
, "httpauth", IS_STRING
)) {
211 curl_easy_setopt(ch
, CURLOPT_USERPWD
, http_curl_copystr(Z_STRVAL_P(zoption
)));
213 #if LIBCURL_VERSION_NUM >= 0x070a06
214 if (zoption
= http_curl_getopt1(options
, "httpauthtype", IS_LONG
)) {
215 curl_easy_setopt(ch
, CURLOPT_HTTPAUTH
, Z_LVAL_P(zoption
));
219 /* compress, empty string enables deflate and gzip */
220 if (zoption
= http_curl_getopt2(options
, "compress", IS_LONG
, IS_BOOL
)) {
221 if (Z_LVAL_P(zoption
)) {
222 curl_easy_setopt(ch
, CURLOPT_ENCODING
, "");
226 /* redirects, defaults to 0 */
227 if (zoption
= http_curl_getopt1(options
, "redirect", IS_LONG
)) {
228 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
229 curl_easy_setopt(ch
, CURLOPT_MAXREDIRS
, Z_LVAL_P(zoption
));
230 if (zoption
= http_curl_getopt2(options
, "unrestrictedauth", IS_LONG
, IS_BOOL
)) {
231 curl_easy_setopt(ch
, CURLOPT_UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
234 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, 0);
238 if (zoption
= http_curl_getopt1(options
, "referer", IS_STRING
)) {
239 curl_easy_setopt(ch
, CURLOPT_REFERER
, http_curl_copystr(Z_STRVAL_P(zoption
)));
242 /* useragent, default "PECL::HTTP/version (PHP/version)" */
243 if (zoption
= http_curl_getopt1(options
, "useragent", IS_STRING
)) {
244 curl_easy_setopt(ch
, CURLOPT_USERAGENT
, http_curl_copystr(Z_STRVAL_P(zoption
)));
246 curl_easy_setopt(ch
, CURLOPT_USERAGENT
,
247 "PECL::HTTP/" HTTP_PEXT_VERSION
" (PHP/" PHP_VERSION
")");
250 /* additional headers, array('name' => 'value') */
251 if (zoption
= http_curl_getopt1(options
, "headers", IS_ARRAY
)) {
254 struct curl_slist
*headers
= NULL
;
256 FOREACH_KEY(zoption
, header_key
, header_idx
) {
259 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &header_val
)) {
260 char header
[1024] = {0};
261 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
262 headers
= curl_slist_append(headers
, http_curl_copystr(header
));
271 curl_easy_setopt(ch
, CURLOPT_HTTPHEADER
, headers
);
275 /* cookies, array('name' => 'value') */
276 if (zoption
= http_curl_getopt1(options
, "cookies", IS_ARRAY
)) {
277 char *cookie_key
= NULL
;
279 phpstr
*qstr
= phpstr_new();
281 FOREACH_KEY(zoption
, cookie_key
, cookie_idx
) {
284 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &cookie_val
)) {
285 phpstr_appendf(qstr
, "%s=%s; ", cookie_key
, Z_STRVAL_PP(cookie_val
));
295 curl_easy_setopt(ch
, CURLOPT_COOKIE
, http_curl_copystr(qstr
->data
));
301 if (zoption
= http_curl_getopt1(options
, "cookiestore", IS_STRING
)) {
302 curl_easy_setopt(ch
, CURLOPT_COOKIEFILE
, http_curl_copystr(Z_STRVAL_P(zoption
)));
303 curl_easy_setopt(ch
, CURLOPT_COOKIEJAR
, http_curl_copystr(Z_STRVAL_P(zoption
)));
307 if (zoption
= http_curl_getopt1(options
, "resume", IS_LONG
)) {
309 curl_easy_setopt(ch
, CURLOPT_RESUME_FROM
, Z_LVAL_P(zoption
));
313 if (zoption
= http_curl_getopt1(options
, "maxfilesize", IS_LONG
)) {
314 curl_easy_setopt(ch
, CURLOPT_MAXFILESIZE
, Z_LVAL_P(zoption
));
318 if (zoption
= http_curl_getopt1(options
, "lastmodified", IS_LONG
)) {
319 curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
320 curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, Z_LVAL_P(zoption
));
324 if (zoption
= http_curl_getopt1(options
, "timeout", IS_LONG
)) {
325 curl_easy_setopt(ch
, CURLOPT_TIMEOUT
, Z_LVAL_P(zoption
));
329 if (zoption
= http_curl_getopt1(options
, "connecttimeout", IS_LONG
)) {
330 curl_easy_setopt(ch
, CURLOPT_CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
335 /* {{{ static inline http_curl_getinfo(CURL, HashTable *) */
336 static inline void _http_curl_getinfo(CURL
*ch
, HashTable
*info TSRMLS_DC
)
339 Z_ARRVAL(array
) = info
;
341 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
342 #define HTTP_CURL_INFO_EX(I, X) \
343 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
345 case CURLINFO_STRING: \
348 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
349 add_assoc_string(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
354 case CURLINFO_DOUBLE: \
357 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
358 add_assoc_double(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), d); \
363 case CURLINFO_LONG: \
366 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
367 add_assoc_long(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), l); \
373 HTTP_CURL_INFO(EFFECTIVE_URL
);
375 #if LIBCURL_VERSION_NUM >= 0x070a07
376 HTTP_CURL_INFO(RESPONSE_CODE
);
378 HTTP_CURL_INFO_EX(HTTP_CODE
, RESPONSE_CODE
);
380 HTTP_CURL_INFO(HTTP_CONNECTCODE
);
382 #if LIBCURL_VERSION_NUM >= 0x070500
383 HTTP_CURL_INFO(FILETIME
);
385 HTTP_CURL_INFO(TOTAL_TIME
);
386 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
387 HTTP_CURL_INFO(CONNECT_TIME
);
388 HTTP_CURL_INFO(PRETRANSFER_TIME
);
389 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
390 #if LIBCURL_VERSION_NUM >= 0x070907
391 HTTP_CURL_INFO(REDIRECT_TIME
);
392 HTTP_CURL_INFO(REDIRECT_COUNT
);
395 HTTP_CURL_INFO(SIZE_UPLOAD
);
396 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
397 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
398 HTTP_CURL_INFO(SPEED_UPLOAD
);
400 HTTP_CURL_INFO(HEADER_SIZE
);
401 HTTP_CURL_INFO(REQUEST_SIZE
);
403 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
404 #if LIBCURL_VERSION_NUM >= 0x070c03
405 /*HTTP_CURL_INFO(SSL_ENGINES);
406 todo: CURLINFO_SLIST */
409 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
410 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
411 HTTP_CURL_INFO(CONTENT_TYPE
);
413 #if LIBCURL_VERSION_NUM >= 0x070a03
414 /*HTTP_CURL_INFO(PRIVATE);*/
417 #if LIBCURL_VERSION_NUM >= 0x070a08
418 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
419 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
422 #if LIBCURL_VERSION_NUM >= 0x070c02
423 /*HTTP_CURL_INFO(OS_ERRNO);*/
426 #if LIBCURL_VERSION_NUM >= 0x070c03
427 HTTP_CURL_INFO(NUM_CONNECTS
);
432 /* {{{ STATUS http_get_ex(CURL *, char *, HashTable *, HashTable *, char **, size_t *) */
433 PHP_HTTP_API STATUS
_http_get_ex(CURL
*ch
, const char *URL
, HashTable
*options
,
434 HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
436 zend_bool clean_curl
= 0;
438 http_curl_startup(ch
, clean_curl
, URL
, options
);
439 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1);
440 http_curl_perform(ch
, clean_curl
);
443 http_curl_getinfo(ch
, info
);
446 http_curl_copybuf(data
, data_len
);
447 http_curl_cleanup(ch
, clean_curl
);
452 /* {{{ STATUS http_head_ex(CURL *, char *, HashTable *, HashTable *, char **data, size_t *) */
453 PHP_HTTP_API STATUS
_http_head_ex(CURL
*ch
, const char *URL
, HashTable
*options
,
454 HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
456 zend_bool clean_curl
= 0;
458 http_curl_startup(ch
, clean_curl
, URL
, options
);
459 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
460 http_curl_perform(ch
, clean_curl
);
463 http_curl_getinfo(ch
, info
);
466 http_curl_copybuf(data
, data_len
);
467 http_curl_cleanup(ch
, clean_curl
);
472 /* {{{ STATUS http_post_data_ex(CURL *, char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
473 PHP_HTTP_API STATUS
_http_post_data_ex(CURL
*ch
, const char *URL
, char *postdata
,
474 size_t postdata_len
, HashTable
*options
, HashTable
*info
, char **data
,
475 size_t *data_len TSRMLS_DC
)
477 zend_bool clean_curl
= 0;
479 http_curl_startup(ch
, clean_curl
, URL
, options
);
480 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
481 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, postdata
);
482 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, postdata_len
);
483 http_curl_perform(ch
, clean_curl
);
486 http_curl_getinfo(ch
, info
);
489 http_curl_copybuf(data
, data_len
);
490 http_curl_cleanup(ch
, clean_curl
);
496 /* {{{ STATUS http_post_array_ex(CURL *, char *, HashTable *, HashTable *, HashTable *, char **, size_t *) */
497 PHP_HTTP_API STATUS
_http_post_array_ex(CURL
*ch
, const char *URL
, HashTable
*postarray
,
498 HashTable
*options
, HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
504 if (SUCCESS
!= http_urlencode_hash_ex(postarray
, 1, NULL
, 0, &encoded
, &encoded_len
)) {
505 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not encode post data");
509 status
= http_post_data_ex(ch
, URL
, encoded
, encoded_len
, options
, info
, data
, data_len
);
516 /* {{{ STATUS http_post_curldata_ex(CURL *, char *, curl_httppost *, HashTable *, HashTable *, char **, size_t *) */
517 PHP_HTTP_API STATUS
_http_post_curldata_ex(CURL
*ch
, const char *URL
,
518 struct curl_httppost
*curldata
, HashTable
*options
, HashTable
*info
,
519 char **data
, size_t *data_len TSRMLS_DC
)
521 zend_bool clean_curl
= 0;
523 http_curl_startup(ch
, clean_curl
, URL
, options
);
524 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
525 curl_easy_setopt(ch
, CURLOPT_HTTPPOST
, curldata
);
526 http_curl_perform(ch
, clean_curl
);
529 http_curl_getinfo(ch
, info
);
532 http_curl_copybuf(data
, data_len
);
533 http_curl_cleanup(ch
, clean_curl
);
544 * vim600: noet sw=4 ts=4 fdm=marker
545 * vim<600: noet sw=4 ts=4