-/* {{{ HAVE_CURL */
-#ifdef HTTP_HAVE_CURL
-
-/* {{{ static inline void http_curl_initbuf(http_curlbuf_member) */
-static inline void _http_curl_initbuf(http_curlbuf_member member TSRMLS_DC)
-{
- http_curl_freebuf(member);
-
- if (member & CURLBUF_HDRS) {
- HTTP_G(curlbuf).hdrs.data = emalloc(HTTP_CURLBUF_HDRSSIZE);
- HTTP_G(curlbuf).hdrs.free = HTTP_CURLBUF_HDRSSIZE;
- }
- if (member & CURLBUF_BODY) {
- HTTP_G(curlbuf).body.data = emalloc(HTTP_CURLBUF_BODYSIZE);
- HTTP_G(curlbuf).body.free = HTTP_CURLBUF_BODYSIZE;
- }
-}
-/* }}} */
-
-/* {{{ static inline void http_curl_freebuf(http_curlbuf_member) */
-static inline void _http_curl_freebuf(http_curlbuf_member member TSRMLS_DC)
-{
- if (member & CURLBUF_HDRS) {
- if (HTTP_G(curlbuf).hdrs.data) {
- efree(HTTP_G(curlbuf).hdrs.data);
- HTTP_G(curlbuf).hdrs.data = NULL;
- }
- HTTP_G(curlbuf).hdrs.used = 0;
- HTTP_G(curlbuf).hdrs.free = 0;
- }
- if (member & CURLBUF_BODY) {
- if (HTTP_G(curlbuf).body.data) {
- efree(HTTP_G(curlbuf).body.data);
- HTTP_G(curlbuf).body.data = NULL;
- }
- HTTP_G(curlbuf).body.used = 0;
- HTTP_G(curlbuf).body.free = 0;
- }
-}
-/* }}} */
-
-/* {{{ static inline void http_curl_copybuf(http_curlbuf_member, char **,
- size_t *) */
-static inline void _http_curl_copybuf(http_curlbuf_member member, char **data,
- size_t *data_len TSRMLS_DC)
-{
- *data = NULL;
- *data_len = 0;
-
- if ((member & CURLBUF_HDRS) && HTTP_G(curlbuf).hdrs.used) {
- if ((member & CURLBUF_BODY) && HTTP_G(curlbuf).body.used) {
- *data = emalloc(HTTP_G(curlbuf).hdrs.used + HTTP_G(curlbuf).body.used + 1);
- } else {
- *data = emalloc(HTTP_G(curlbuf).hdrs.used + 1);
- }
- memcpy(*data, HTTP_G(curlbuf).hdrs.data, HTTP_G(curlbuf).hdrs.used);
- *data_len = HTTP_G(curlbuf).hdrs.used;
- }
-
- if ((member & CURLBUF_BODY) && HTTP_G(curlbuf).body.used) {
- if (*data) {
- memcpy((*data) + HTTP_G(curlbuf).hdrs.used,
- HTTP_G(curlbuf).body.data, HTTP_G(curlbuf).body.used);
- *data_len = HTTP_G(curlbuf).hdrs.used + HTTP_G(curlbuf).body.used;
- } else {
- emalloc(HTTP_G(curlbuf).body.used + 1);
- memcpy(*data, HTTP_G(curlbuf).body.data, HTTP_G(curlbuf).body.used);
- *data_len = HTTP_G(curlbuf).body.used;
- }
- }
- if (*data) {
- (*data)[*data_len] = 0;
- } else {
- *data = "";
- }
-}
-/* }}} */
-
-/* {{{ static inline void http_curl_movebuf(http_curlbuf_member, char **,
- size_t *) */
-static inline void _http_curl_movebuf(http_curlbuf_member member, char **data,
- size_t *data_len TSRMLS_DC)
-{
- http_curl_copybuf(member, data, data_len);
- http_curl_freebuf(member);
-}
-/* }}} */
-
-/* {{{ static size_t http_curl_body_callback(char *, size_t, size_t, void *) */
-static size_t http_curl_body_callback(char *buf, size_t len, size_t n, void *s)
-{
- TSRMLS_FETCH();
-
- if ((len *= n) > HTTP_G(curlbuf).body.free) {
- size_t bsize = HTTP_CURLBUF_BODYSIZE;
- while (bsize < len) {
- bsize *= 2;
- }
- HTTP_G(curlbuf).body.data = erealloc(HTTP_G(curlbuf).body.data,
- HTTP_G(curlbuf).body.used + bsize);
- HTTP_G(curlbuf).body.free += bsize;
- }
-
- memcpy(HTTP_G(curlbuf).body.data + HTTP_G(curlbuf).body.used, buf, len);
- HTTP_G(curlbuf).body.free -= len;
- HTTP_G(curlbuf).body.used += len;
-
- return len;
-}
-/* }}} */
-
-/* {{{ static size_t http_curl_hdrs_callback(char*, size_t, size_t, void *) */
-static size_t http_curl_hdrs_callback(char *buf, size_t len, size_t n, void *s)
-{
- TSRMLS_FETCH();
-
- /* discard previous headers */
- if ((HTTP_G(curlbuf).hdrs.used) && (!strncmp(buf, "HTTP/1.", strlen("HTTP/1.")))) {
- http_curl_initbuf(CURLBUF_HDRS);
- }
-
- if ((len *= n) > HTTP_G(curlbuf).hdrs.free) {
- size_t bsize = HTTP_CURLBUF_HDRSSIZE;
- while (bsize < len) {
- bsize *= 2;
- }
- HTTP_G(curlbuf).hdrs.data = erealloc(HTTP_G(curlbuf).hdrs.data,
- HTTP_G(curlbuf).hdrs.used + bsize);
- HTTP_G(curlbuf).hdrs.free += bsize;
- }
-
- memcpy(HTTP_G(curlbuf).hdrs.data + HTTP_G(curlbuf).hdrs.used, buf, len);
- HTTP_G(curlbuf).hdrs.free -= len;
- HTTP_G(curlbuf).hdrs.used += len;
-
- return len;
-}
-/* }}} */
-
-/* {{{ static inline zval *http_curl_getopt(HashTable *, char *, int, ...) */
-static inline zval *_http_curl_getopt(HashTable *options, char *key TSRMLS_DC, int checks, ...)
-{
- zval **zoption;
- va_list types;
- int i;
-
- if (SUCCESS != zend_hash_find(options, key, strlen(key) + 1, (void **) &zoption)) {
- return NULL;
- }
- if (checks < 1) {
- return *zoption;
- }
-
- va_start(types, checks);
- for (i = 0; i < checks; ++i) {
- if ((va_arg(types, int)) == (Z_TYPE_PP(zoption))) {
- va_end(types);
- return *zoption;
- }
- }
- va_end(types);
- return NULL;
-}
-/* }}} */
-
-/* {{{ static inline void http_curl_setopts(CURL *, char *, HashTable *) */
-static inline void _http_curl_setopts(CURL *ch, const char *url, HashTable *options TSRMLS_DC)
-{
- zval *zoption;
-
- /* standard options */
- curl_easy_setopt(ch, CURLOPT_URL, url);
- curl_easy_setopt(ch, CURLOPT_HEADER, 0);
- curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1);
- curl_easy_setopt(ch, CURLOPT_AUTOREFERER, 1);
- curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, http_curl_body_callback);
- curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, http_curl_hdrs_callback);
-#ifdef ZTS
- curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1);
-#endif
-
- if ((!options) || (1 > zend_hash_num_elements(options))) {
- return;
- }
-
- /* redirects, defaults to 0 */
- if (zoption = http_curl_getopt1(options, "redirect", IS_LONG)) {
- curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
- curl_easy_setopt(ch, CURLOPT_MAXREDIRS, Z_LVAL_P(zoption));
- if (zoption = http_curl_getopt2(options, "unrestrictedauth", IS_LONG, IS_BOOL)) {
- curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
- }
- } else {
- curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 0);
- }
-
- /* proxy */
- if (zoption = http_curl_getopt1(options, "proxyhost", IS_STRING)) {
- curl_easy_setopt(ch, CURLOPT_PROXY, Z_STRVAL_P(zoption));
- /* port */
- if (zoption = http_curl_getopt1(options, "proxyport", IS_LONG)) {
- curl_easy_setopt(ch, CURLOPT_PROXYPORT, Z_LVAL_P(zoption));
- }
- /* user:pass */
- if (zoption = http_curl_getopt1(options, "proxyauth", IS_STRING)) {
- curl_easy_setopt(ch, CURLOPT_PROXYUSERPWD, Z_STRVAL_P(zoption));
- }
- /* auth method */
- if (zoption = http_curl_getopt1(options, "proxyauthtype", IS_LONG)) {
- curl_easy_setopt(ch, CURLOPT_PROXYAUTH, Z_LVAL_P(zoption));
- }
- }
-
- /* auth */
- if (zoption = http_curl_getopt1(options, "httpauth", IS_STRING)) {
- curl_easy_setopt(ch, CURLOPT_USERPWD, Z_STRVAL_P(zoption));
- }
- if (zoption = http_curl_getopt1(options, "httpauthtype", IS_LONG)) {
- curl_easy_setopt(ch, CURLOPT_HTTPAUTH, Z_LVAL_P(zoption));
- }
-
- /* compress, enabled by default (empty string enables deflate and gzip) */
- if (zoption = http_curl_getopt2(options, "compress", IS_LONG, IS_BOOL)) {
- if (Z_LVAL_P(zoption)) {
- curl_easy_setopt(ch, CURLOPT_ENCODING, "");
- }
- } else {
- curl_easy_setopt(ch, CURLOPT_ENCODING, "");
- }
-
- /* another port */
- if (zoption = http_curl_getopt1(options, "port", IS_LONG)) {
- curl_easy_setopt(ch, CURLOPT_PORT, Z_LVAL_P(zoption));
- }
-
- /* referer */
- if (zoption = http_curl_getopt1(options, "referer", IS_STRING)) {
- curl_easy_setopt(ch, CURLOPT_REFERER, Z_STRVAL_P(zoption));
- }
-
- /* useragent, default "PECL::HTTP/version (PHP/version)" */
- if (zoption = http_curl_getopt1(options, "useragent", IS_STRING)) {
- curl_easy_setopt(ch, CURLOPT_USERAGENT, Z_STRVAL_P(zoption));
- } else {
- curl_easy_setopt(ch, CURLOPT_USERAGENT,
- "PECL::HTTP/" PHP_EXT_HTTP_VERSION " (PHP/" PHP_VERSION ")");
- }
-
- /* cookies, array('name' => 'value') */
- if (zoption = http_curl_getopt1(options, "cookies", IS_ARRAY)) {
- char *cookie_key;
- zval **cookie_val;
- int key_type;
- smart_str qstr = {0};
-
- zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
- while (HASH_KEY_NON_EXISTANT != (key_type = zend_hash_get_current_key_type(Z_ARRVAL_P(zoption)))) {
- if (key_type == HASH_KEY_IS_STRING) {
- zend_hash_get_current_key(Z_ARRVAL_P(zoption), &cookie_key, NULL, 0);
- zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &cookie_val);
- smart_str_appends(&qstr, cookie_key);
- smart_str_appendl(&qstr, "=", 1);
- smart_str_appendl(&qstr, Z_STRVAL_PP(cookie_val), Z_STRLEN_PP(cookie_val));
- smart_str_appendl(&qstr, "; ", 2);
- zend_hash_move_forward(Z_ARRVAL_P(zoption));
- }
- }
- smart_str_0(&qstr);
-
- if (qstr.c) {
- curl_easy_setopt(ch, CURLOPT_COOKIE, qstr.c);
- }
- }
-
- /* cookiestore */
- if (zoption = http_curl_getopt1(options, "cookiestore", IS_STRING)) {
- curl_easy_setopt(ch, CURLOPT_COOKIEFILE, Z_STRVAL_P(zoption));
- curl_easy_setopt(ch, CURLOPT_COOKIEJAR, Z_STRVAL_P(zoption));
- }
-
- /* additional headers, array('name' => 'value') */
- if (zoption = http_curl_getopt1(options, "headers", IS_ARRAY)) {
- int key_type;
- char *header_key, header[1024] = {0};
- zval **header_val;
- struct curl_slist *headers = NULL;
-
- zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
- while (HASH_KEY_NON_EXISTANT != (key_type = zend_hash_get_current_key_type(Z_ARRVAL_P(zoption)))) {
- if (key_type == HASH_KEY_IS_STRING) {
- zend_hash_get_current_key(Z_ARRVAL_P(zoption), &header_key, NULL, 0);
- zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &header_val);
- snprintf(header, 1024, "%s: %s", header_key, Z_STRVAL_PP(header_val));
- headers = curl_slist_append(headers, header);
- zend_hash_move_forward(Z_ARRVAL_P(zoption));
- }
- }
- if (headers) {
- curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headers);
- }
- }
-}
-/* }}} */
-
-/* {{{ static inline char *http_curl_getinfoname(CURLINFO) */
-static inline char *_http_curl_getinfoname(CURLINFO i TSRMLS_DC)
-{
-#define CASE(I) case CURLINFO_ ##I : return #I
- switch (i)
- {
- /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
- CASE(EFFECTIVE_URL);
- /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
- CASE(RESPONSE_CODE);
- /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
- CASE(TOTAL_TIME);
- /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
- CASE(NAMELOOKUP_TIME);
- /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
- CASE(CONNECT_TIME);
- /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
- CASE(PRETRANSFER_TIME);
- /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
- CASE(SIZE_UPLOAD);
- /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
- CASE(SIZE_DOWNLOAD);
- /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
- CASE(SPEED_DOWNLOAD);
- /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
- CASE(SPEED_UPLOAD);
- /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
- CASE(HEADER_SIZE);
- /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
- CASE(REQUEST_SIZE);
- /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
- CASE(SSL_VERIFYRESULT);
- /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
- CASE(FILETIME);
- /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
- CASE(CONTENT_LENGTH_DOWNLOAD);
- /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
- CASE(CONTENT_LENGTH_UPLOAD);
- /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
- CASE(STARTTRANSFER_TIME);
- /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
- CASE(CONTENT_TYPE);
- /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
- CASE(REDIRECT_TIME);
- /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
- CASE(REDIRECT_COUNT);
- /* CURLINFO_PRIVATE = CURLINFO_STRING +21, */
- CASE(PRIVATE);
- /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
- CASE(HTTP_CONNECTCODE);
- /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
- CASE(HTTPAUTH_AVAIL);
- /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
- CASE(PROXYAUTH_AVAIL);
- }
-#undef CASE
- return NULL;
-}
-/* }}} */
-
-/* {{{ static inline void http_curl_getinfo_ex(CURL, CURLINFO, zval *) */
-static inline void _http_curl_getinfo_ex(CURL *ch, CURLINFO i, zval *array TSRMLS_DC)
-{
- char *key;
- if (key = http_curl_getinfoname(i)) {
- switch (i & ~CURLINFO_MASK)
- {
- case CURLINFO_STRING:
- {
- char *c;
- if (CURLE_OK == curl_easy_getinfo(ch, i, &c)) {
- add_assoc_string(array, key, c ? c : "", 1);
- }
- }
- break;
-
- case CURLINFO_DOUBLE:
- {
- double d;
- if (CURLE_OK == curl_easy_getinfo(ch, i, &d)) {
- add_assoc_double(array, key, (double) d);
- }
- }
- break;
-
- case CURLINFO_LONG:
- {
- long l;
- if (CURLE_OK == curl_easy_getinfo(ch, i, &l)) {
- add_assoc_long(array, key, (long) l);
- }
- }
- break;
- }
- }
-}
-/* }}} */
-
-/* {{{ static inline http_curl_getinfo(CURL, HashTable *) */
-static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC)
-{
- zval *array;
-
- MAKE_STD_ZVAL(array);
- Z_ARRVAL_P(array) = info;
-
-#define INFO(I) http_curl_getinfo_ex(ch, CURLINFO_ ##I , array)
- /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
- INFO(EFFECTIVE_URL);
- /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
- INFO(RESPONSE_CODE);
- /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
- INFO(TOTAL_TIME);
- /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
- INFO(NAMELOOKUP_TIME);
- /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
- INFO(CONNECT_TIME);
- /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
- INFO(PRETRANSFER_TIME);
- /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
- INFO(SIZE_UPLOAD);
- /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
- INFO(SIZE_DOWNLOAD);
- /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
- INFO(SPEED_DOWNLOAD);
- /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
- INFO(SPEED_UPLOAD);
- /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
- INFO(HEADER_SIZE);
- /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
- INFO(REQUEST_SIZE);
- /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
- INFO(SSL_VERIFYRESULT);
- /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
- INFO(FILETIME);
- /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
- INFO(CONTENT_LENGTH_DOWNLOAD);
- /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
- INFO(CONTENT_LENGTH_UPLOAD);
- /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
- INFO(STARTTRANSFER_TIME);
- /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
- INFO(CONTENT_TYPE);
- /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
- INFO(REDIRECT_TIME);
- /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
- INFO(REDIRECT_COUNT);
- /* CURLINFO_PRIVATE = CURLINFO_STRING +21, */
- INFO(PRIVATE);
- /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
- INFO(HTTP_CONNECTCODE);
- /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
- INFO(HTTPAUTH_AVAIL);
- /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
- INFO(PROXYAUTH_AVAIL);
-#undef INFO
- efree(array);
-}
-/* }}} */
-