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 # define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
25 # include <winsock2.h>
26 # include <sys/types.h>
29 #include <curl/curl.h>
30 #include <curl/easy.h>
34 #include "php_http_api.h"
35 #include "php_http_curl_api.h"
36 #include "php_http_std_defs.h"
39 # include "ext/standard/php_http.h"
42 #include "ext/standard/php_smart_str.h"
44 ZEND_DECLARE_MODULE_GLOBALS(http
)
46 #define http_curl_startup(ch, clean_curl, URL, options) \
48 if (!(ch = curl_easy_init())) { \
49 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl"); \
54 http_curl_initbuf(); \
55 http_curl_setopts(ch, URL, options);
58 #define http_curl_cleanup(ch, clean_curl) \
59 http_curl_freestr(); \
60 http_curl_freebuf(); \
62 curl_easy_cleanup(ch); \
66 #define http_curl_freestr() \
67 zend_llist_clean(&HTTP_G(to_free))
69 #define http_curl_initbuf() http_curl_initbuf_ex(0)
71 #define http_curl_initbuf_ex(chunk_size) \
73 size_t size = (chunk_size > 0) ? chunk_size : HTTP_CURLBUF_SIZE; \
74 http_curl_freebuf(); \
75 HTTP_G(curlbuf).data = emalloc(size); \
76 HTTP_G(curlbuf).free = size; \
77 HTTP_G(curlbuf).size = size; \
80 #define http_curl_freebuf() \
81 if (HTTP_G(curlbuf).data) { \
82 efree(HTTP_G(curlbuf).data); \
83 HTTP_G(curlbuf).data = NULL; \
85 HTTP_G(curlbuf).used = 0; \
86 HTTP_G(curlbuf).free = 0; \
87 HTTP_G(curlbuf).size = 0;
89 #define http_curl_copybuf(data, size) \
90 * size = HTTP_G(curlbuf).used; \
91 * data = ecalloc(1, HTTP_G(curlbuf).used + 1); \
92 memcpy(* data, HTTP_G(curlbuf).data, * size);
94 #define http_curl_sizebuf(for_size) \
96 size_t size = (for_size); \
97 if (size > HTTP_G(curlbuf).free) { \
98 size_t bsize = HTTP_G(curlbuf).size; \
99 while (size > bsize) { \
102 HTTP_G(curlbuf).data = erealloc(HTTP_G(curlbuf).data, HTTP_G(curlbuf).used + bsize); \
103 HTTP_G(curlbuf).free += bsize; \
108 #define http_curl_copystr(s) _http_curl_copystr((s) TSRMLS_CC)
109 static inline char *_http_curl_copystr(const char *str TSRMLS_DC
);
111 #define http_curl_setopts(c, u, o) _http_curl_setopts((c), (u), (o) TSRMLS_CC)
112 static inline void _http_curl_setopts(CURL
*ch
, const char *url
, HashTable
*options TSRMLS_DC
);
114 #define http_curl_getopt(o, k) _http_curl_getopt((o), (k) TSRMLS_CC, 0)
115 #define http_curl_getopt1(o, k, t1) _http_curl_getopt((o), (k) TSRMLS_CC, 1, (t1))
116 #define http_curl_getopt2(o, k, t1, t2) _http_curl_getopt((o), (k) TSRMLS_CC, 2, (t1), (t2))
117 static inline zval
*_http_curl_getopt(HashTable
*options
, char *key TSRMLS_DC
, int checks
, ...);
119 static size_t http_curl_body_callback(char *, size_t, size_t, void *);
120 static size_t http_curl_hdrs_callback(char *, size_t, size_t, void *);
122 #define http_curl_getinfo(c, h) _http_curl_getinfo((c), (h) TSRMLS_CC)
123 static inline void _http_curl_getinfo(CURL
*ch
, HashTable
*info TSRMLS_DC
);
125 /* {{{ static inline char *http_curl_copystr(char *) */
126 static inline char *_http_curl_copystr(const char *str TSRMLS_DC
)
128 char *new_str
= estrdup(str
);
129 zend_llist_add_element(&HTTP_G(to_free
), &new_str
);
134 /* {{{ static size_t http_curl_body_callback(char *, size_t, size_t, void *) */
135 static size_t http_curl_body_callback(char *buf
, size_t len
, size_t n
, void *s
)
139 http_curl_sizebuf(len
*= n
);
141 memcpy(HTTP_G(curlbuf
).data
+ HTTP_G(curlbuf
).used
, buf
, len
);
142 HTTP_G(curlbuf
).free
-= len
;
143 HTTP_G(curlbuf
).used
+= len
;
148 /* {{{ static size_t http_curl_hdrs_callback(char *, size_t, size_t, void *) */
149 static size_t http_curl_hdrs_callback(char *buf
, size_t len
, size_t n
, void *s
)
153 /* discard previous headers */
154 if ((HTTP_G(curlbuf
).used
) && (!strncmp(buf
, "HTTP/1.", sizeof("HTTP/1.") - 1))) {
157 http_curl_sizebuf(len
*= n
);
159 memcpy(HTTP_G(curlbuf
).data
+ HTTP_G(curlbuf
).used
, buf
, len
);
160 HTTP_G(curlbuf
).free
-= len
;
161 HTTP_G(curlbuf
).used
+= len
;
166 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, int, ...) */
167 static inline zval
*_http_curl_getopt(HashTable
*options
, char *key TSRMLS_DC
, int checks
, ...)
173 if (SUCCESS
!= zend_hash_find(options
, key
, strlen(key
) + 1, (void **) &zoption
)) {
180 va_start(types
, checks
);
181 for (i
= 0; i
< checks
; ++i
) {
182 if ((va_arg(types
, int)) == (Z_TYPE_PP(zoption
))) {
192 /* {{{ static inline void http_curl_setopts(CURL *, char *, HashTable *) */
193 static inline void _http_curl_setopts(CURL
*ch
, const char *url
, HashTable
*options TSRMLS_DC
)
196 zend_bool range_req
= 0;
198 /* standard options */
199 curl_easy_setopt(ch
, CURLOPT_URL
, url
);
200 curl_easy_setopt(ch
, CURLOPT_HEADER
, 0);
201 curl_easy_setopt(ch
, CURLOPT_FILETIME
, 1);
202 curl_easy_setopt(ch
, CURLOPT_NOPROGRESS
, 1);
203 curl_easy_setopt(ch
, CURLOPT_AUTOREFERER
, 1);
204 curl_easy_setopt(ch
, CURLOPT_WRITEFUNCTION
, http_curl_body_callback
);
205 curl_easy_setopt(ch
, CURLOPT_HEADERFUNCTION
, http_curl_hdrs_callback
);
206 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 71000)
207 curl_easy_setopt(ch
, CURLOPT_NOSIGNAL
, 1);
210 if ((!options
) || (1 > zend_hash_num_elements(options
))) {
215 if (zoption
= http_curl_getopt1(options
, "proxyhost", IS_STRING
)) {
216 curl_easy_setopt(ch
, CURLOPT_PROXY
, http_curl_copystr(Z_STRVAL_P(zoption
)));
218 if (zoption
= http_curl_getopt1(options
, "proxyport", IS_LONG
)) {
219 curl_easy_setopt(ch
, CURLOPT_PROXYPORT
, Z_LVAL_P(zoption
));
222 if (zoption
= http_curl_getopt1(options
, "proxyauth", IS_STRING
)) {
223 curl_easy_setopt(ch
, CURLOPT_PROXYUSERPWD
, http_curl_copystr(Z_STRVAL_P(zoption
)));
225 #if LIBCURL_VERSION_NUM >= 71007
227 if (zoption
= http_curl_getopt1(options
, "proxyauthtype", IS_LONG
)) {
228 curl_easy_setopt(ch
, CURLOPT_PROXYAUTH
, Z_LVAL_P(zoption
));
233 /* outgoing interface */
234 if (zoption
= http_curl_getopt1(options
, "interface", IS_STRING
)) {
235 curl_easy_setopt(ch
, CURLOPT_INTERFACE
, http_curl_copystr(Z_STRVAL_P(zoption
)));
239 if (zoption
= http_curl_getopt1(options
, "port", IS_LONG
)) {
240 curl_easy_setopt(ch
, CURLOPT_PORT
, Z_LVAL_P(zoption
));
244 if (zoption
= http_curl_getopt1(options
, "httpauth", IS_STRING
)) {
245 curl_easy_setopt(ch
, CURLOPT_USERPWD
, http_curl_copystr(Z_STRVAL_P(zoption
)));
247 #if LIBCURL_VERSION_NUM >= 71006
248 if (zoption
= http_curl_getopt1(options
, "httpauthtype", IS_LONG
)) {
249 curl_easy_setopt(ch
, CURLOPT_HTTPAUTH
, Z_LVAL_P(zoption
));
253 /* compress, empty string enables deflate and gzip */
254 if (zoption
= http_curl_getopt2(options
, "compress", IS_LONG
, IS_BOOL
)) {
255 if (Z_LVAL_P(zoption
)) {
256 curl_easy_setopt(ch
, CURLOPT_ENCODING
, "");
260 /* redirects, defaults to 0 */
261 if (zoption
= http_curl_getopt1(options
, "redirect", IS_LONG
)) {
262 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
263 curl_easy_setopt(ch
, CURLOPT_MAXREDIRS
, Z_LVAL_P(zoption
));
264 if (zoption
= http_curl_getopt2(options
, "unrestrictedauth", IS_LONG
, IS_BOOL
)) {
265 curl_easy_setopt(ch
, CURLOPT_UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
268 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, 0);
272 if (zoption
= http_curl_getopt1(options
, "referer", IS_STRING
)) {
273 curl_easy_setopt(ch
, CURLOPT_REFERER
, http_curl_copystr(Z_STRVAL_P(zoption
)));
276 /* useragent, default "PECL::HTTP/version (PHP/version)" */
277 if (zoption
= http_curl_getopt1(options
, "useragent", IS_STRING
)) {
278 curl_easy_setopt(ch
, CURLOPT_USERAGENT
, http_curl_copystr(Z_STRVAL_P(zoption
)));
280 curl_easy_setopt(ch
, CURLOPT_USERAGENT
,
281 "PECL::HTTP/" PHP_EXT_HTTP_VERSION
" (PHP/" PHP_VERSION
")");
284 /* additional headers, array('name' => 'value') */
285 if (zoption
= http_curl_getopt1(options
, "headers", IS_ARRAY
)) {
288 struct curl_slist
*headers
= NULL
;
290 FOREACH_KEY(zoption
, header_key
, header_idx
) {
293 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &header_val
)) {
294 char header
[1024] = {0};
295 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
296 headers
= curl_slist_append(headers
, http_curl_copystr(header
));
305 curl_easy_setopt(ch
, CURLOPT_HTTPHEADER
, headers
);
309 /* cookies, array('name' => 'value') */
310 if (zoption
= http_curl_getopt1(options
, "cookies", IS_ARRAY
)) {
311 char *cookie_key
= NULL
;
313 smart_str qstr
= {0};
315 FOREACH_KEY(zoption
, cookie_key
, cookie_idx
) {
318 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &cookie_val
)) {
319 smart_str_appends(&qstr
, cookie_key
);
320 smart_str_appendl(&qstr
, "=", 1);
321 smart_str_appendl(&qstr
, Z_STRVAL_PP(cookie_val
), Z_STRLEN_PP(cookie_val
));
322 smart_str_appendl(&qstr
, "; ", 2);
332 curl_easy_setopt(ch
, CURLOPT_COOKIE
, http_curl_copystr(qstr
.c
));
338 if (zoption
= http_curl_getopt1(options
, "cookiestore", IS_STRING
)) {
339 curl_easy_setopt(ch
, CURLOPT_COOKIEFILE
, http_curl_copystr(Z_STRVAL_P(zoption
)));
340 curl_easy_setopt(ch
, CURLOPT_COOKIEJAR
, http_curl_copystr(Z_STRVAL_P(zoption
)));
344 if (zoption
= http_curl_getopt1(options
, "resume", IS_LONG
)) {
346 curl_easy_setopt(ch
, CURLOPT_RESUME_FROM
, Z_LVAL_P(zoption
));
350 if (zoption
= http_curl_getopt1(options
, "maxfilesize", IS_LONG
)) {
351 curl_easy_setopt(ch
, CURLOPT_MAXFILESIZE
, Z_LVAL_P(zoption
));
355 if (zoption
= http_curl_getopt1(options
, "lastmodified", IS_LONG
)) {
356 curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
357 curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, Z_LVAL_P(zoption
));
361 if (zoption
= http_curl_getopt1(options
, "timeout", IS_LONG
)) {
362 curl_easy_setopt(ch
, CURLOPT_TIMEOUT
, Z_LVAL_P(zoption
));
366 if (zoption
= http_curl_getopt1(options
, "connecttimeout", IS_LONG
)) {
367 curl_easy_setopt(ch
, CURLOPT_CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
372 /* {{{ static inline http_curl_getinfo(CURL, HashTable *) */
373 static inline void _http_curl_getinfo(CURL
*ch
, HashTable
*info TSRMLS_DC
)
376 Z_ARRVAL(array
) = info
;
378 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
379 #define HTTP_CURL_INFO_EX(I, X) \
380 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
382 case CURLINFO_STRING: \
385 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
386 add_assoc_string(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
391 case CURLINFO_DOUBLE: \
394 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
395 add_assoc_double(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), d); \
400 case CURLINFO_LONG: \
403 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
404 add_assoc_long(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), l); \
410 HTTP_CURL_INFO(EFFECTIVE_URL
);
412 #if LIBCURL_VERSION_NUM >= 71007
413 HTTP_CURL_INFO(RESPONSE_CODE
);
415 HTTP_CURL_INFO_EX(HTTP_CODE
, RESPONSE_CODE
);
417 HTTP_CURL_INFO(HTTP_CONNECTCODE
);
419 #if LIBCURL_VERSION_NUM >= 70500
420 HTTP_CURL_INFO(FILETIME
);
422 HTTP_CURL_INFO(TOTAL_TIME
);
423 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
424 HTTP_CURL_INFO(CONNECT_TIME
);
425 HTTP_CURL_INFO(PRETRANSFER_TIME
);
426 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
427 #if LIBCURL_VERSION_NUM >= 70907
428 HTTP_CURL_INFO(REDIRECT_TIME
);
429 HTTP_CURL_INFO(REDIRECT_COUNT
);
432 HTTP_CURL_INFO(SIZE_UPLOAD
);
433 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
434 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
435 HTTP_CURL_INFO(SPEED_UPLOAD
);
437 HTTP_CURL_INFO(HEADER_SIZE
);
438 HTTP_CURL_INFO(REQUEST_SIZE
);
440 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
441 #if LIBCURL_VERSION_NUM >= 71203
442 /*HTTP_CURL_INFO(SSL_ENGINES);
443 todo: CURLINFO_SLIST */
446 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
447 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
448 HTTP_CURL_INFO(CONTENT_TYPE
);
450 #if LIBCURL_VERSION_NUM >= 71003
451 /*HTTP_CURL_INFO(PRIVATE);*/
454 #if LIBCURL_VERSION_NUM >= 71008
455 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
456 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
459 #if LIBCURL_VERSION_NUM >= 71202
460 /*HTTP_CURL_INFO(OS_ERRNO);*/
463 #if LIBCURL_VERSION_NUM >= 71203
464 HTTP_CURL_INFO(NUM_CONNECTS
);
469 /* {{{ STATUS http_get_ex(CURL *, char *, HashTable *, HashTable *, char **, size_t *) */
470 PHP_HTTP_API STATUS
_http_get_ex(CURL
*ch
, const char *URL
, HashTable
*options
,
471 HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
473 zend_bool clean_curl
= 0;
475 http_curl_startup(ch
, clean_curl
, URL
, options
);
476 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1);
478 if (CURLE_OK
!= curl_easy_perform(ch
)) {
479 http_curl_cleanup(ch
, clean_curl
);
480 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
485 http_curl_getinfo(ch
, info
);
488 http_curl_copybuf(data
, data_len
);
489 http_curl_cleanup(ch
, clean_curl
);
494 /* {{{ STATUS http_head_ex(CURL *, char *, HashTable *, HashTable *, char **data, size_t *) */
495 PHP_HTTP_API STATUS
_http_head_ex(CURL
*ch
, const char *URL
, HashTable
*options
,
496 HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
498 zend_bool clean_curl
= 0;
500 http_curl_startup(ch
, clean_curl
, URL
, options
);
501 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
503 if (CURLE_OK
!= curl_easy_perform(ch
)) {
504 http_curl_cleanup(ch
, clean_curl
);
505 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
510 http_curl_getinfo(ch
, info
);
513 http_curl_copybuf(data
, data_len
);
514 http_curl_cleanup(ch
, clean_curl
);
519 /* {{{ STATUS http_post_data_ex(CURL *, char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
520 PHP_HTTP_API STATUS
_http_post_data_ex(CURL
*ch
, const char *URL
, char *postdata
,
521 size_t postdata_len
, HashTable
*options
, HashTable
*info
, char **data
,
522 size_t *data_len TSRMLS_DC
)
524 zend_bool clean_curl
= 0;
526 http_curl_startup(ch
, clean_curl
, URL
, options
);
527 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
528 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, postdata
);
529 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, postdata_len
);
531 if (CURLE_OK
!= curl_easy_perform(ch
)) {
532 http_curl_cleanup(ch
, clean_curl
);
533 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
538 http_curl_getinfo(ch
, info
);
541 http_curl_copybuf(data
, data_len
);
542 http_curl_cleanup(ch
, clean_curl
);
548 /* {{{ STATUS http_post_array_ex(CURL *, char *, HashTable *, HashTable *, HashTable *, char **, size_t *) */
549 PHP_HTTP_API STATUS
_http_post_array_ex(CURL
*ch
, const char *URL
, HashTable
*postarray
,
550 HashTable
*options
, HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
552 smart_str qstr
= {0};
555 HTTP_URL_ARGSEP_OVERRIDE
;
556 if (php_url_encode_hash_ex(postarray
, &qstr
, NULL
,0,NULL
,0,NULL
,0,NULL TSRMLS_CC
) != SUCCESS
) {
560 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not encode post data");
561 HTTP_URL_ARGSEP_RESTORE
;
565 HTTP_URL_ARGSEP_RESTORE
;
567 status
= http_post_data_ex(ch
, URL
, qstr
.c
, qstr
.len
, options
, info
, data
, data_len
);
576 /* {{{ STATUS http_post_curldata_ex(CURL *, char *, curl_httppost *, HashTable *, HashTable *, char **, size_t *) */
577 PHP_HTTP_API STATUS
_http_post_curldata_ex(CURL
*ch
, const char *URL
,
578 struct curl_httppost
*curldata
, HashTable
*options
, HashTable
*info
,
579 char **data
, size_t *data_len TSRMLS_DC
)
581 zend_bool clean_curl
= 0;
583 http_curl_startup(ch
, clean_curl
, URL
, options
);
584 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
585 curl_easy_setopt(ch
, CURLOPT_HTTPPOST
, curldata
);
587 if (CURLE_OK
!= curl_easy_perform(ch
)) {
588 http_curl_cleanup(ch
, clean_curl
);
589 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
594 http_curl_getinfo(ch
, info
);
597 http_curl_copybuf(data
, data_len
);
598 http_curl_cleanup(ch
, clean_curl
);
609 * vim600: noet sw=4 ts=4 fdm=marker
610 * vim<600: noet sw=4 ts=4