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
)
197 /* standard options */
198 curl_easy_setopt(ch
, CURLOPT_URL
, url
);
199 curl_easy_setopt(ch
, CURLOPT_HEADER
, 0);
200 curl_easy_setopt(ch
, CURLOPT_NOPROGRESS
, 1);
201 curl_easy_setopt(ch
, CURLOPT_AUTOREFERER
, 1);
202 curl_easy_setopt(ch
, CURLOPT_WRITEFUNCTION
, http_curl_body_callback
);
203 curl_easy_setopt(ch
, CURLOPT_HEADERFUNCTION
, http_curl_hdrs_callback
);
204 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
205 curl_easy_setopt(ch
, CURLOPT_NOSIGNAL
, 1);
208 if ((!options
) || (1 > zend_hash_num_elements(options
))) {
212 /* redirects, defaults to 0 */
213 if (zoption
= http_curl_getopt1(options
, "redirect", IS_LONG
)) {
214 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
215 curl_easy_setopt(ch
, CURLOPT_MAXREDIRS
, Z_LVAL_P(zoption
));
216 if (zoption
= http_curl_getopt2(options
, "unrestrictedauth", IS_LONG
, IS_BOOL
)) {
217 curl_easy_setopt(ch
, CURLOPT_UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
220 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, 0);
224 if (zoption
= http_curl_getopt1(options
, "proxyhost", IS_STRING
)) {
225 curl_easy_setopt(ch
, CURLOPT_PROXY
, http_curl_copystr(Z_STRVAL_P(zoption
)));
227 if (zoption
= http_curl_getopt1(options
, "proxyport", IS_LONG
)) {
228 curl_easy_setopt(ch
, CURLOPT_PROXYPORT
, Z_LVAL_P(zoption
));
231 if (zoption
= http_curl_getopt1(options
, "proxyauth", IS_STRING
)) {
232 curl_easy_setopt(ch
, CURLOPT_PROXYUSERPWD
, http_curl_copystr(Z_STRVAL_P(zoption
)));
234 #if LIBCURL_VERSION_NUM > 0x070a06
236 if (zoption
= http_curl_getopt1(options
, "proxyauthtype", IS_LONG
)) {
237 curl_easy_setopt(ch
, CURLOPT_PROXYAUTH
, Z_LVAL_P(zoption
));
243 if (zoption
= http_curl_getopt1(options
, "httpauth", IS_STRING
)) {
244 curl_easy_setopt(ch
, CURLOPT_USERPWD
, http_curl_copystr(Z_STRVAL_P(zoption
)));
246 #if LIBCURL_VERSION_NUM > 0x070a05
247 if (zoption
= http_curl_getopt1(options
, "httpauthtype", IS_LONG
)) {
248 curl_easy_setopt(ch
, CURLOPT_HTTPAUTH
, Z_LVAL_P(zoption
));
252 /* compress, enabled by default (empty string enables deflate and gzip) */
253 if (zoption
= http_curl_getopt2(options
, "compress", IS_LONG
, IS_BOOL
)) {
254 if (Z_LVAL_P(zoption
)) {
255 curl_easy_setopt(ch
, CURLOPT_ENCODING
, "");
260 if (zoption
= http_curl_getopt1(options
, "port", IS_LONG
)) {
261 curl_easy_setopt(ch
, CURLOPT_PORT
, Z_LVAL_P(zoption
));
265 if (zoption
= http_curl_getopt1(options
, "referer", IS_STRING
)) {
266 curl_easy_setopt(ch
, CURLOPT_REFERER
, http_curl_copystr(Z_STRVAL_P(zoption
)));
269 /* useragent, default "PECL::HTTP/version (PHP/version)" */
270 if (zoption
= http_curl_getopt1(options
, "useragent", IS_STRING
)) {
271 curl_easy_setopt(ch
, CURLOPT_USERAGENT
, http_curl_copystr(Z_STRVAL_P(zoption
)));
273 curl_easy_setopt(ch
, CURLOPT_USERAGENT
,
274 "PECL::HTTP/" PHP_EXT_HTTP_VERSION
" (PHP/" PHP_VERSION
")");
277 /* cookies, array('name' => 'value') */
278 if (zoption
= http_curl_getopt1(options
, "cookies", IS_ARRAY
)) {
279 char *cookie_key
= NULL
;
281 smart_str qstr
= {0};
283 FOREACH_KEY(zoption
, cookie_key
, cookie_idx
) {
286 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &cookie_val
)) {
287 smart_str_appends(&qstr
, cookie_key
);
288 smart_str_appendl(&qstr
, "=", 1);
289 smart_str_appendl(&qstr
, Z_STRVAL_PP(cookie_val
), Z_STRLEN_PP(cookie_val
));
290 smart_str_appendl(&qstr
, "; ", 2);
300 curl_easy_setopt(ch
, CURLOPT_COOKIE
, http_curl_copystr(qstr
.c
));
306 if (zoption
= http_curl_getopt1(options
, "cookiestore", IS_STRING
)) {
307 curl_easy_setopt(ch
, CURLOPT_COOKIEFILE
, http_curl_copystr(Z_STRVAL_P(zoption
)));
308 curl_easy_setopt(ch
, CURLOPT_COOKIEJAR
, http_curl_copystr(Z_STRVAL_P(zoption
)));
311 /* additional headers, array('name' => 'value') */
312 if (zoption
= http_curl_getopt1(options
, "headers", IS_ARRAY
)) {
315 struct curl_slist
*headers
= NULL
;
317 FOREACH_KEY(zoption
, header_key
, header_idx
) {
320 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &header_val
)) {
321 char header
[1024] = {0};
322 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
323 headers
= curl_slist_append(headers
, http_curl_copystr(header
));
332 curl_easy_setopt(ch
, CURLOPT_HTTPHEADER
, headers
);
338 /* {{{ static inline http_curl_getinfo(CURL, HashTable *) */
339 static inline void _http_curl_getinfo(CURL
*ch
, HashTable
*info TSRMLS_DC
)
342 Z_ARRVAL(array
) = info
;
344 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
345 #define HTTP_CURL_INFO_EX(I, X) \
346 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
348 case CURLINFO_STRING: \
351 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
352 add_assoc_string(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
357 case CURLINFO_DOUBLE: \
360 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
361 add_assoc_double(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), d); \
366 case CURLINFO_LONG: \
369 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
370 add_assoc_long(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), l); \
376 /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
377 HTTP_CURL_INFO(EFFECTIVE_URL
);
378 #if LIBCURL_VERSION_NUM > 0x070a06
379 /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
380 HTTP_CURL_INFO(RESPONSE_CODE
);
382 HTTP_CURL_INFO_EX(HTTP_CODE
, RESPONSE_CODE
);
384 /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
385 HTTP_CURL_INFO(TOTAL_TIME
);
386 /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
387 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
388 /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
389 HTTP_CURL_INFO(CONNECT_TIME
);
390 /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
391 HTTP_CURL_INFO(PRETRANSFER_TIME
);
392 /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
393 HTTP_CURL_INFO(SIZE_UPLOAD
);
394 /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
395 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
396 /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
397 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
398 /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
399 HTTP_CURL_INFO(SPEED_UPLOAD
);
400 /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
401 HTTP_CURL_INFO(HEADER_SIZE
);
402 /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
403 HTTP_CURL_INFO(REQUEST_SIZE
);
404 /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
405 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
406 /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
407 HTTP_CURL_INFO(FILETIME
);
408 /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
409 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
410 /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
411 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
412 /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
413 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
414 /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
415 HTTP_CURL_INFO(CONTENT_TYPE
);
416 /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
417 HTTP_CURL_INFO(REDIRECT_TIME
);
418 /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
419 HTTP_CURL_INFO(REDIRECT_COUNT
);
420 /* CURLINFO_PRIVATE = CURLINFO_STRING +21, */
421 HTTP_CURL_INFO(PRIVATE
);
422 /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
423 HTTP_CURL_INFO(HTTP_CONNECTCODE
);
424 #if LIBCURL_VERSION_NUM > 0x070a07
425 /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
426 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
427 /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
428 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
433 /* {{{ STATUS http_get_ex(CURL *, char *, HashTable *, HashTable *, char **, size_t *) */
434 PHP_HTTP_API STATUS
_http_get_ex(CURL
*ch
, const char *URL
, HashTable
*options
,
435 HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
437 zend_bool clean_curl
= 0;
439 http_curl_startup(ch
, clean_curl
, URL
, options
);
440 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 0);
441 curl_easy_setopt(ch
, CURLOPT_POST
, 0);
443 if (CURLE_OK
!= curl_easy_perform(ch
)) {
444 http_curl_cleanup(ch
, clean_curl
);
445 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
450 http_curl_getinfo(ch
, info
);
453 http_curl_copybuf(data
, data_len
);
454 http_curl_cleanup(ch
, clean_curl
);
459 /* {{{ STATUS http_head_ex(CURL *, char *, HashTable *, HashTable *, char **data, size_t *) */
460 PHP_HTTP_API STATUS
_http_head_ex(CURL
*ch
, const char *URL
, HashTable
*options
,
461 HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
463 zend_bool clean_curl
= 0;
465 http_curl_startup(ch
, clean_curl
, URL
, options
);
466 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
467 curl_easy_setopt(ch
, CURLOPT_POST
, 0);
469 if (CURLE_OK
!= curl_easy_perform(ch
)) {
470 http_curl_cleanup(ch
, clean_curl
);
471 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
476 http_curl_getinfo(ch
, info
);
479 http_curl_copybuf(data
, data_len
);
480 http_curl_cleanup(ch
, clean_curl
);
485 /* {{{ STATUS http_post_data_ex(CURL *, char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
486 PHP_HTTP_API STATUS
_http_post_data_ex(CURL
*ch
, const char *URL
, char *postdata
,
487 size_t postdata_len
, HashTable
*options
, HashTable
*info
, char **data
,
488 size_t *data_len TSRMLS_DC
)
490 zend_bool clean_curl
= 0;
492 http_curl_startup(ch
, clean_curl
, URL
, options
);
493 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
494 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, postdata
);
495 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, postdata_len
);
497 if (CURLE_OK
!= curl_easy_perform(ch
)) {
498 http_curl_cleanup(ch
, clean_curl
);
499 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
504 http_curl_getinfo(ch
, info
);
507 http_curl_copybuf(data
, data_len
);
508 http_curl_cleanup(ch
, clean_curl
);
514 /* {{{ STATUS http_post_array_ex(CURL *, char *, HashTable *, HashTable *, HashTable *, char **, size_t *) */
515 PHP_HTTP_API STATUS
_http_post_array_ex(CURL
*ch
, const char *URL
, HashTable
*postarray
,
516 HashTable
*options
, HashTable
*info
, char **data
, size_t *data_len TSRMLS_DC
)
518 smart_str qstr
= {0};
521 HTTP_URL_ARGSEP_OVERRIDE
;
522 if (php_url_encode_hash_ex(postarray
, &qstr
, NULL
,0,NULL
,0,NULL
,0,NULL TSRMLS_CC
) != SUCCESS
) {
526 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not encode post data");
527 HTTP_URL_ARGSEP_RESTORE
;
531 HTTP_URL_ARGSEP_RESTORE
;
533 status
= http_post_data_ex(ch
, URL
, qstr
.c
, qstr
.len
, options
, info
, data
, data_len
);
542 /* {{{ STATUS http_post_curldata_ex(CURL *, char *, curl_httppost *, HashTable *, HashTable *, char **, size_t *) */
543 PHP_HTTP_API STATUS
_http_post_curldata_ex(CURL
*ch
, const char *URL
,
544 struct curl_httppost
*curldata
, HashTable
*options
, HashTable
*info
,
545 char **data
, size_t *data_len TSRMLS_DC
)
547 zend_bool clean_curl
= 0;
549 http_curl_startup(ch
, clean_curl
, URL
, options
);
550 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
551 curl_easy_setopt(ch
, CURLOPT_HTTPPOST
, curldata
);
553 if (CURLE_OK
!= curl_easy_perform(ch
)) {
554 http_curl_cleanup(ch
, clean_curl
);
555 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
560 http_curl_getinfo(ch
, info
);
563 http_curl_copybuf(data
, data_len
);
564 http_curl_cleanup(ch
, clean_curl
);
575 * vim600: noet sw=4 ts=4 fdm=marker
576 * vim<600: noet sw=4 ts=4