2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
19 #define HTTP_WANT_CURL
24 #include "php_http_api.h"
25 #include "php_http_request_api.h"
26 #include "php_http_url_api.h"
29 # include "php_http_request_object.h"
32 ZEND_EXTERN_MODULE_GLOBALS(http
);
34 /* {{{ cruft for thread safe SSL crypto locks */
35 #if defined(ZTS) && defined(HTTP_HAVE_SSL)
37 # define HTTP_NEED_SSL_TSL
38 # define HTTP_NEED_OPENSSL_TSL
39 # include <openssl/crypto.h>
40 # else /* !PHP_WIN32 */
41 # if defined(HTTP_HAVE_OPENSSL)
42 # if defined(HAVE_OPENSSL_CRYPTO_H)
43 # define HTTP_NEED_SSL_TSL
44 # define HTTP_NEED_OPENSSL_TSL
45 # include <openssl/crypto.h>
48 "libcurl was compiled with OpenSSL support, but configure could not find " \
49 "openssl/crypto.h; thus no SSL crypto locking callbacks will be set, which may " \
50 "cause random crashes on SSL requests"
52 # elif defined(HTTP_HAVE_GNUTLS)
53 # if defined(HAVE_GCRYPT_H)
54 # define HTTP_NEED_SSL_TSL
55 # define HTTP_NEED_GNUTLS_TSL
59 "libcurl was compiled with GnuTLS support, but configure could not find " \
60 "gcrypt.h; thus no SSL crypto locking callbacks will be set, which may " \
61 "cause random crashes on SSL requests"
65 "libcurl was compiled with SSL support, but configure could not determine which" \
66 "library was used; thus no SSL crypto locking callbacks will be set, which may " \
67 "cause random crashes on SSL requests"
68 # endif /* HTTP_HAVE_OPENSSL || HTTP_HAVE_GNUTLS */
69 # endif /* PHP_WIN32 */
70 #endif /* ZTS && HTTP_HAVE_SSL */
72 #ifdef HTTP_NEED_SSL_TSL
73 static inline void http_ssl_init(void);
74 static inline void http_ssl_cleanup(void);
79 PHP_MINIT_FUNCTION(http_request
)
81 #ifdef HTTP_NEED_SSL_TSL
85 if (CURLE_OK
!= curl_global_init(CURL_GLOBAL_ALL
)) {
89 HTTP_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC
);
90 HTTP_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST
);
91 HTTP_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM
);
92 HTTP_LONG_CONSTANT("HTTP_AUTH_ANY", CURLAUTH_ANY
);
99 PHP_MSHUTDOWN_FUNCTION(http_request
)
101 curl_global_cleanup();
102 #ifdef HTTP_NEED_SSL_TSL
110 #ifndef HAVE_CURL_EASY_STRERROR
111 # define curl_easy_strerror(dummy) "unkown error"
114 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
115 #define HTTP_CURL_INFO_EX(I, X) \
116 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
118 case CURLINFO_STRING: \
121 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &c)) { \
122 char *key = estrndup(#X, lenof(#X)); \
123 add_assoc_string(&array, pretty_key(key, lenof(#X), 0, 0), c ? c : "", 1); \
129 case CURLINFO_DOUBLE: \
132 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &d)) { \
133 char *key = estrndup(#X, lenof(#X)); \
134 add_assoc_double(&array, pretty_key(key, lenof(#X), 0, 0), d); \
140 case CURLINFO_LONG: \
143 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &l)) { \
144 char *key = estrndup(#X, lenof(#X)); \
145 add_assoc_long(&array, pretty_key(key, lenof(#X), 0, 0), l); \
151 case CURLINFO_SLIST: \
153 struct curl_slist *l, *p; \
154 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &l)) { \
156 char *key = estrndup(#X, lenof(#X)); \
157 MAKE_STD_ZVAL(subarray); \
158 array_init(subarray); \
159 for (p = l; p; p = p->next) { \
160 add_next_index_string(subarray, p->data, 1); \
162 add_assoc_zval(&array, pretty_key(key, lenof(#X), 0, 0), subarray); \
163 curl_slist_free_all(l); \
169 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(request->ch, CURLOPT_##OPTION, (p))
170 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
171 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
172 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
173 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
174 if (!strcasecmp(key, #keyname)) { \
175 convert_to_string(*param); \
176 HTTP_CURL_OPT(optname, Z_STRVAL_PP(param)); \
180 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
181 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
182 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
183 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
184 if (!strcasecmp(key, #keyname)) { \
185 convert_to_long(*param); \
186 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
192 /* {{{ forward declarations */
193 #define http_request_option(r, o, k, t) _http_request_option_ex((r), (o), (k), sizeof(k), (t) TSRMLS_CC)
194 #define http_request_option_ex(r, o, k, l, t) _http_request_option_ex((r), (o), (k), (l), (t) TSRMLS_CC)
195 static inline zval
*_http_request_option_ex(http_request
*request
, HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
);
197 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
198 static int http_curl_progress_callback(void *, double, double, double, double);
199 static int http_curl_raw_callback(CURL
*, curl_infotype
, char *, size_t, void *);
200 static int http_curl_dummy_callback(char *data
, size_t n
, size_t l
, void *s
) { return n
*l
; }
201 static curlioerr
http_curl_ioctl_callback(CURL
*, curliocmd
, void *);
204 /* {{{ http_request *http_request_init(http_request *) */
205 PHP_HTTP_API http_request
*_http_request_init_ex(http_request
*request
, CURL
*ch
, http_request_method meth
, const char *url TSRMLS_DC
)
212 r
= emalloc(sizeof(http_request
));
214 memset(r
, 0, sizeof(http_request
));
217 r
->url
= (url
&& *url
) ? estrdup(url
) : NULL
;
218 r
->meth
= (meth
> 0) ? meth
: HTTP_GET
;
220 phpstr_init(&r
->conv
.request
);
221 phpstr_init_ex(&r
->conv
.response
, HTTP_CURLBUF_SIZE
, 0);
223 zend_hash_init(&r
->info
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
225 phpstr_init(&r
->_cache
.cookies
);
226 zend_hash_init(&r
->_cache
.options
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
228 TSRMLS_SET_CTX(request
->tsrm_ls
);
234 /* {{{ void http_request_dtor(http_request *) */
235 PHP_HTTP_API
void _http_request_dtor(http_request
*request
)
237 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
240 /* avoid nasty segfaults with already cleaned up callbacks */
241 curl_easy_setopt(request
->ch
, CURLOPT_NOPROGRESS
, 1);
242 curl_easy_setopt(request
->ch
, CURLOPT_PROGRESSFUNCTION
, NULL
);
243 curl_easy_setopt(request
->ch
, CURLOPT_VERBOSE
, 0);
244 curl_easy_setopt(request
->ch
, CURLOPT_DEBUGFUNCTION
, NULL
);
245 curl_easy_cleanup(request
->ch
);
249 http_request_reset(request
);
251 zend_hash_destroy(&request
->info
);
253 phpstr_dtor(&request
->_cache
.cookies
);
254 zend_hash_destroy(&request
->_cache
.options
);
255 if (request
->_cache
.headers
) {
256 curl_slist_free_all(request
->_cache
.headers
);
257 request
->_cache
.headers
= NULL
;
262 /* {{{ void http_request_free(http_request **) */
263 PHP_HTTP_API
void _http_request_free(http_request
**request
)
266 TSRMLS_FETCH_FROM_CTX((*request
)->tsrm_ls
);
267 http_request_dtor(*request
);
274 /* {{{ void http_request_reset(http_request *) */
275 PHP_HTTP_API
void _http_request_reset(http_request
*request
)
277 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
278 STR_SET(request
->url
, NULL
);
279 request
->conv
.last_type
= 0;
280 phpstr_dtor(&request
->conv
.request
);
281 phpstr_dtor(&request
->conv
.response
);
282 http_request_body_free(&request
->body
);
286 /* {{{ void http_request_defaults(http_request *) */
287 PHP_HTTP_API
void _http_request_defaults(http_request
*request
)
290 #ifdef HAVE_CURL_EASY_RESET
291 curl_easy_reset(request
->ch
);
294 HTTP_CURL_OPT(NOSIGNAL
, 1);
296 HTTP_CURL_OPT(HEADER
, 0);
297 HTTP_CURL_OPT(FILETIME
, 1);
298 HTTP_CURL_OPT(AUTOREFERER
, 1);
299 HTTP_CURL_OPT(VERBOSE
, 1);
300 HTTP_CURL_OPT(HEADERFUNCTION
, NULL
);
301 HTTP_CURL_OPT(DEBUGFUNCTION
, http_curl_raw_callback
);
302 HTTP_CURL_OPT(READFUNCTION
, http_curl_read_callback
);
303 HTTP_CURL_OPT(IOCTLFUNCTION
, http_curl_ioctl_callback
);
304 HTTP_CURL_OPT(WRITEFUNCTION
, http_curl_dummy_callback
);
305 HTTP_CURL_OPT(URL
, NULL
);
306 HTTP_CURL_OPT(NOPROGRESS
, 1);
307 HTTP_CURL_OPT(PROXY
, NULL
);
308 HTTP_CURL_OPT(PROXYPORT
, 0);
309 HTTP_CURL_OPT(PROXYUSERPWD
, NULL
);
310 HTTP_CURL_OPT(PROXYAUTH
, 0);
311 HTTP_CURL_OPT(INTERFACE
, NULL
);
312 HTTP_CURL_OPT(PORT
, 0);
313 HTTP_CURL_OPT(USERPWD
, NULL
);
314 HTTP_CURL_OPT(HTTPAUTH
, 0);
315 HTTP_CURL_OPT(ENCODING
, 0);
316 HTTP_CURL_OPT(FOLLOWLOCATION
, 0);
317 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, 0);
318 HTTP_CURL_OPT(REFERER
, NULL
);
319 HTTP_CURL_OPT(USERAGENT
, "PECL::HTTP/" PHP_EXT_HTTP_VERSION
" (PHP/" PHP_VERSION
")");
320 HTTP_CURL_OPT(HTTPHEADER
, NULL
);
321 HTTP_CURL_OPT(COOKIE
, NULL
);
322 HTTP_CURL_OPT(COOKIEFILE
, NULL
);
323 HTTP_CURL_OPT(COOKIEJAR
, NULL
);
324 HTTP_CURL_OPT(RESUME_FROM
, 0);
325 HTTP_CURL_OPT(MAXFILESIZE
, 0);
326 HTTP_CURL_OPT(TIMECONDITION
, 0);
327 HTTP_CURL_OPT(TIMEVALUE
, 0);
328 HTTP_CURL_OPT(TIMEOUT
, 0);
329 HTTP_CURL_OPT(CONNECTTIMEOUT
, 3);
330 HTTP_CURL_OPT(SSLCERT
, NULL
);
331 HTTP_CURL_OPT(SSLCERTTYPE
, NULL
);
332 HTTP_CURL_OPT(SSLCERTPASSWD
, NULL
);
333 HTTP_CURL_OPT(SSLKEY
, NULL
);
334 HTTP_CURL_OPT(SSLKEYTYPE
, NULL
);
335 HTTP_CURL_OPT(SSLKEYPASSWD
, NULL
);
336 HTTP_CURL_OPT(SSLENGINE
, NULL
);
337 HTTP_CURL_OPT(SSLVERSION
, 0);
338 HTTP_CURL_OPT(SSL_VERIFYPEER
, 0);
339 HTTP_CURL_OPT(SSL_VERIFYHOST
, 0);
340 HTTP_CURL_OPT(SSL_CIPHER_LIST
, NULL
);
341 HTTP_CURL_OPT(CAINFO
, NULL
);
342 HTTP_CURL_OPT(CAPATH
, NULL
);
343 HTTP_CURL_OPT(RANDOM_FILE
, NULL
);
344 HTTP_CURL_OPT(EGDSOCKET
, NULL
);
345 HTTP_CURL_OPT(POSTFIELDS
, NULL
);
346 HTTP_CURL_OPT(POSTFIELDSIZE
, 0);
347 HTTP_CURL_OPT(HTTPPOST
, NULL
);
348 HTTP_CURL_OPT(IOCTLDATA
, NULL
);
349 HTTP_CURL_OPT(READDATA
, NULL
);
350 HTTP_CURL_OPT(INFILESIZE
, 0);
355 /* {{{ STATUS http_request_prepare(http_request *, HashTable *) */
356 PHP_HTTP_API STATUS
_http_request_prepare(http_request
*request
, HashTable
*options
)
359 zend_bool range_req
= 0;
361 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
363 HTTP_CHECK_CURL_INIT(request
->ch
, curl_easy_init(), return FAILURE
);
365 http_request_defaults(request
);
368 HTTP_CURL_OPT(DEBUGDATA
, request
);
369 HTTP_CURL_OPT(URL
, request
->url
);
370 HTTP_CURL_OPT(PRIVATE
, request
->url
);
372 /* progress callback * /
373 if ((zoption = http_request_option(request, options, "onprogress", 0))) {
374 HTTP_CURL_OPT(NOPROGRESS, 0);
375 HTTP_CURL_OPT(PROGRESSFUNCTION, http_curl_progress_callback);
376 HTTP_CURL_OPT(PROGRESSDATA, http_request_callback_data(zoption));
380 if ((zoption
= http_request_option(request
, options
, "proxyhost", IS_STRING
))) {
381 HTTP_CURL_OPT(PROXY
, Z_STRVAL_P(zoption
));
383 if ((zoption
= http_request_option(request
, options
, "proxyport", IS_LONG
))) {
384 HTTP_CURL_OPT(PROXYPORT
, Z_LVAL_P(zoption
));
387 if ((zoption
= http_request_option(request
, options
, "proxyauth", IS_STRING
))) {
388 HTTP_CURL_OPT(PROXYUSERPWD
, Z_STRVAL_P(zoption
));
391 if ((zoption
= http_request_option(request
, options
, "proxyauthtype", IS_LONG
))) {
392 HTTP_CURL_OPT(PROXYAUTH
, Z_LVAL_P(zoption
));
396 /* outgoing interface */
397 if ((zoption
= http_request_option(request
, options
, "interface", IS_STRING
))) {
398 HTTP_CURL_OPT(INTERFACE
, Z_STRVAL_P(zoption
));
402 if ((zoption
= http_request_option(request
, options
, "port", IS_LONG
))) {
403 HTTP_CURL_OPT(PORT
, Z_LVAL_P(zoption
));
407 if ((zoption
= http_request_option(request
, options
, "httpauth", IS_STRING
))) {
408 HTTP_CURL_OPT(USERPWD
, Z_STRVAL_P(zoption
));
410 if ((zoption
= http_request_option(request
, options
, "httpauthtype", IS_LONG
))) {
411 HTTP_CURL_OPT(HTTPAUTH
, Z_LVAL_P(zoption
));
414 /* compress, empty string enables all supported if libcurl was build with zlib support */
415 if ((zoption
= http_request_option(request
, options
, "compress", IS_BOOL
)) && Z_LVAL_P(zoption
)) {
416 #ifdef HTTP_HAVE_CURL_ZLIB
417 HTTP_CURL_OPT(ENCODING
, "gzip, deflate");
419 HTTP_CURL_OPT(ENCODING
, "gzip;q=1.0, deflate;q=0.5, *;q=0.1");
423 /* redirects, defaults to 0 */
424 if ((zoption
= http_request_option(request
, options
, "redirect", IS_LONG
))) {
425 HTTP_CURL_OPT(FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
426 HTTP_CURL_OPT(MAXREDIRS
, Z_LVAL_P(zoption
));
427 if ((zoption
= http_request_option(request
, options
, "unrestrictedauth", IS_BOOL
))) {
428 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
433 if ((zoption
= http_request_option(request
, options
, "referer", IS_STRING
))) {
434 HTTP_CURL_OPT(REFERER
, Z_STRVAL_P(zoption
));
437 /* useragent, default "PECL::HTTP/version (PHP/version)" */
438 if ((zoption
= http_request_option(request
, options
, "useragent", IS_STRING
))) {
439 HTTP_CURL_OPT(USERAGENT
, Z_STRVAL_P(zoption
));
442 /* additional headers, array('name' => 'value') */
443 if ((zoption
= http_request_option(request
, options
, "headers", IS_ARRAY
))) {
448 if (request
->_cache
.headers
) {
449 curl_slist_free_all(request
->_cache
.headers
);
450 request
->_cache
.headers
= NULL
;
453 FOREACH_KEY(pos
, zoption
, header_key
, header_idx
) {
456 if (SUCCESS
== zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption
), (void **) &header_val
, &pos
)) {
457 char header
[1024] = {0};
458 zval
*cpy
, *val
= convert_to_type_ex(IS_STRING
, *header_val
, &cpy
);
460 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_P(val
));
461 request
->_cache
.headers
= curl_slist_append(request
->_cache
.headers
, header
);
474 /* cookies, array('name' => 'value') */
475 if ((zoption
= http_request_option(request
, options
, "cookies", IS_ARRAY
))) {
476 char *cookie_key
= NULL
;
477 ulong cookie_idx
= 0;
480 phpstr_dtor(&request
->_cache
.cookies
);
482 FOREACH_KEY(pos
, zoption
, cookie_key
, cookie_idx
) {
485 if (SUCCESS
== zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption
), (void **) &cookie_val
, &pos
)) {
486 zval
*cpy
, *val
= convert_to_type_ex(IS_STRING
, *cookie_val
, &cpy
);
488 phpstr_appendf(&request
->_cache
.cookies
, "%s=%s; ", cookie_key
, Z_STRVAL_P(val
));
500 if (request
->_cache
.cookies
.used
) {
501 phpstr_fix(&request
->_cache
.cookies
);
502 HTTP_CURL_OPT(COOKIE
, request
->_cache
.cookies
.data
);
506 /* session cookies */
507 if ((zoption
= http_request_option(request
, options
, "cookiesession", IS_BOOL
))) {
508 if (Z_LVAL_P(zoption
)) {
509 /* accept cookies for this session */
510 HTTP_CURL_OPT(COOKIEFILE
, "");
512 /* reset session cookies */
513 HTTP_CURL_OPT(COOKIESESSION
, 1);
517 /* cookiestore, read initial cookies from that file and store cookies back into that file */
518 if ((zoption
= http_request_option(request
, options
, "cookiestore", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
519 HTTP_CURL_OPT(COOKIEFILE
, Z_STRVAL_P(zoption
));
520 HTTP_CURL_OPT(COOKIEJAR
, Z_STRVAL_P(zoption
));
524 if ((zoption
= http_request_option(request
, options
, "resume", IS_LONG
)) && (Z_LVAL_P(zoption
) != 0)) {
526 HTTP_CURL_OPT(RESUME_FROM
, Z_LVAL_P(zoption
));
530 if ((zoption
= http_request_option(request
, options
, "maxfilesize", IS_LONG
))) {
531 HTTP_CURL_OPT(MAXFILESIZE
, Z_LVAL_P(zoption
));
535 if ((zoption
= http_request_option(request
, options
, "lastmodified", IS_LONG
))) {
536 if (Z_LVAL_P(zoption
)) {
537 if (Z_LVAL_P(zoption
) > 0) {
538 HTTP_CURL_OPT(TIMEVALUE
, Z_LVAL_P(zoption
));
540 HTTP_CURL_OPT(TIMEVALUE
, time(NULL
) + Z_LVAL_P(zoption
));
542 HTTP_CURL_OPT(TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
544 HTTP_CURL_OPT(TIMECONDITION
, CURL_TIMECOND_NONE
);
548 /* timeout, defaults to 0 */
549 if ((zoption
= http_request_option(request
, options
, "timeout", IS_LONG
))) {
550 HTTP_CURL_OPT(TIMEOUT
, Z_LVAL_P(zoption
));
553 /* connecttimeout, defaults to 3 */
554 if ((zoption
= http_request_option(request
, options
, "connecttimeout", IS_LONG
))) {
555 HTTP_CURL_OPT(CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
559 if ((zoption
= http_request_option(request
, options
, "ssl", IS_ARRAY
))) {
565 FOREACH_KEYVAL(pos
, zoption
, key
, idx
, param
) {
567 HTTP_CURL_OPT_SSL_STRING(CERT
);
568 HTTP_CURL_OPT_SSL_STRING(CERTTYPE
);
569 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD
);
571 HTTP_CURL_OPT_SSL_STRING(KEY
);
572 HTTP_CURL_OPT_SSL_STRING(KEYTYPE
);
573 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD
);
575 HTTP_CURL_OPT_SSL_STRING(ENGINE
);
576 HTTP_CURL_OPT_SSL_LONG(VERSION
);
578 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER
);
579 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST
);
580 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST
);
582 HTTP_CURL_OPT_STRING(CAINFO
);
583 HTTP_CURL_OPT_STRING(CAPATH
);
584 HTTP_CURL_OPT_STRING(RANDOM_FILE
);
585 HTTP_CURL_OPT_STRING(EGDSOCKET
);
594 switch (request
->meth
)
597 curl_easy_setopt(request
->ch
, CURLOPT_HTTPGET
, 1);
601 curl_easy_setopt(request
->ch
, CURLOPT_NOBODY
, 1);
605 curl_easy_setopt(request
->ch
, CURLOPT_POST
, 1);
609 curl_easy_setopt(request
->ch
, CURLOPT_UPLOAD
, 1);
613 if (http_request_method_exists(0, request
->meth
, NULL
)) {
614 curl_easy_setopt(request
->ch
, CURLOPT_CUSTOMREQUEST
, http_request_method_name(request
->meth
));
616 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unsupported request method: %d (%s)", request
->meth
, request
->url
);
622 /* attach request body */
623 if (request
->body
&& (request
->meth
!= HTTP_GET
) && (request
->meth
!= HTTP_HEAD
)) {
624 switch (request
->body
->type
)
626 case HTTP_REQUEST_BODY_CSTRING
:
627 curl_easy_setopt(request
->ch
, CURLOPT_POSTFIELDS
, request
->body
->data
);
628 curl_easy_setopt(request
->ch
, CURLOPT_POSTFIELDSIZE
, request
->body
->size
);
631 case HTTP_REQUEST_BODY_CURLPOST
:
632 curl_easy_setopt(request
->ch
, CURLOPT_HTTPPOST
, (struct curl_httppost
*) request
->body
->data
);
635 case HTTP_REQUEST_BODY_UPLOADFILE
:
636 curl_easy_setopt(request
->ch
, CURLOPT_IOCTLDATA
, request
);
637 curl_easy_setopt(request
->ch
, CURLOPT_READDATA
, request
);
638 curl_easy_setopt(request
->ch
, CURLOPT_INFILESIZE
, request
->body
->size
);
642 /* shouldn't ever happen */
643 http_error_ex(HE_ERROR
, 0, "Unknown request body type: %d (%s)", request
->body
->type
, request
->url
);
653 /* {{{ void http_request_exec(http_request *) */
654 PHP_HTTP_API
void _http_request_exec(http_request
*request
)
657 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
659 if (CURLE_OK
!= (result
= curl_easy_perform(request
->ch
))) {
660 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "%s (%s)", curl_easy_strerror(result
), request
->url
);
665 /* {{{ void http_request_info(http_request *, HashTable *) */
666 PHP_HTTP_API
void _http_request_info(http_request
*request
, HashTable
*info
)
669 INIT_ZARR(array
, &request
->info
);
671 HTTP_CURL_INFO(EFFECTIVE_URL
);
672 HTTP_CURL_INFO(RESPONSE_CODE
);
673 HTTP_CURL_INFO_EX(HTTP_CONNECTCODE
, connect_code
);
674 HTTP_CURL_INFO(FILETIME
);
675 HTTP_CURL_INFO(TOTAL_TIME
);
676 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
677 HTTP_CURL_INFO(CONNECT_TIME
);
678 HTTP_CURL_INFO(PRETRANSFER_TIME
);
679 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
680 HTTP_CURL_INFO(REDIRECT_TIME
);
681 HTTP_CURL_INFO(REDIRECT_COUNT
);
682 HTTP_CURL_INFO(SIZE_UPLOAD
);
683 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
684 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
685 HTTP_CURL_INFO(SPEED_UPLOAD
);
686 HTTP_CURL_INFO(HEADER_SIZE
);
687 HTTP_CURL_INFO(REQUEST_SIZE
);
688 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
689 HTTP_CURL_INFO(SSL_ENGINES
);
690 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
691 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
692 HTTP_CURL_INFO(CONTENT_TYPE
);
693 /*HTTP_CURL_INFO(PRIVATE);*/
694 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
695 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
696 /*HTTP_CURL_INFO(OS_ERRNO);*/
697 HTTP_CURL_INFO(NUM_CONNECTS
);
698 #if LIBCURL_VERSION_NUM >= 0x070e01
699 HTTP_CURL_INFO_EX(COOKIELIST
, cookies
);
703 zend_hash_copy(info
, &request
->info
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
708 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
709 static size_t http_curl_read_callback(void *data
, size_t len
, size_t n
, void *ctx
)
711 http_request
*request
= (http_request
*) ctx
;
712 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
714 if (request
->body
== NULL
|| request
->body
->type
!= HTTP_REQUEST_BODY_UPLOADFILE
) {
717 return php_stream_read((php_stream
*) request
->body
->data
, data
, len
* n
);
721 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) * /
722 static int http_curl_progress_callback(void *data, double dltotal, double dlnow, double ultotal, double ulnow)
724 zval *params_pass[4], params_local[4], retval;
725 HTTP_REQUEST_CALLBACK_DATA(data, zval *, func);
727 params_pass[0] = ¶ms_local[0];
728 params_pass[1] = ¶ms_local[1];
729 params_pass[2] = ¶ms_local[2];
730 params_pass[3] = ¶ms_local[3];
732 INIT_PZVAL(params_pass[0]);
733 INIT_PZVAL(params_pass[1]);
734 INIT_PZVAL(params_pass[2]);
735 INIT_PZVAL(params_pass[3]);
736 ZVAL_DOUBLE(params_pass[0], dltotal);
737 ZVAL_DOUBLE(params_pass[1], dlnow);
738 ZVAL_DOUBLE(params_pass[2], ultotal);
739 ZVAL_DOUBLE(params_pass[3], ulnow);
741 return call_user_function(EG(function_table), NULL, func, &retval, 4, params_pass TSRMLS_CC);
745 /* {{{ static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *) */
746 static curlioerr
http_curl_ioctl_callback(CURL
*ch
, curliocmd cmd
, void *ctx
)
748 http_request
*request
= (http_request
*) ctx
;
749 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
751 if (cmd
!= CURLIOCMD_RESTARTREAD
) {
752 return CURLIOE_UNKNOWNCMD
;
754 if ( request
->body
== NULL
||
755 request
->body
->type
!= HTTP_REQUEST_BODY_UPLOADFILE
||
756 SUCCESS
!= php_stream_rewind((php_stream
*) request
->body
->data
)) {
757 return CURLIOE_FAILRESTART
;
763 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
764 static int http_curl_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
766 http_request
*request
= (http_request
*) ctx
;
770 case CURLINFO_DATA_IN
:
771 if (request
->conv
.last_type
== CURLINFO_HEADER_IN
) {
772 phpstr_appends(&request
->conv
.response
, HTTP_CRLF
);
774 case CURLINFO_HEADER_IN
:
775 phpstr_append(&request
->conv
.response
, data
, length
);
777 case CURLINFO_DATA_OUT
:
778 if (request
->conv
.last_type
== CURLINFO_HEADER_OUT
) {
779 phpstr_appends(&request
->conv
.request
, HTTP_CRLF
);
781 case CURLINFO_HEADER_OUT
:
782 phpstr_append(&request
->conv
.request
, data
, length
);
786 fprintf(stderr
, "## ", type
);
788 fprintf(stderr
, "%s", data
);
791 for (i
= 1; i
<= length
; ++i
) {
792 fprintf(stderr
, "%02X ", data
[i
-1] & 0xFF);
794 fprintf(stderr
, "\n## ");
797 fprintf(stderr
, "\n");
799 if (data
[length
-1] != 0xa) {
800 fprintf(stderr
, "\n");
807 request
->conv
.last_type
= type
;
813 /* {{{ static inline zval *http_request_option(http_request *, HashTable *, char *, size_t, int) */
814 static inline zval
*_http_request_option_ex(http_request
*r
, HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
)
817 ulong h
= zend_get_hash_value(key
, keylen
);
819 if (!options
|| (SUCCESS
!= zend_hash_quick_find(options
, key
, keylen
, h
, (void **) &zoption
))) {
823 if (Z_TYPE_PP(zoption
) != type
) {
826 case IS_BOOL
: convert_to_boolean_ex(zoption
); break;
827 case IS_LONG
: convert_to_long_ex(zoption
); break;
828 case IS_DOUBLE
: convert_to_double_ex(zoption
); break;
829 case IS_STRING
: convert_to_string_ex(zoption
); break;
830 case IS_ARRAY
: convert_to_array_ex(zoption
); break;
831 case IS_OBJECT
: convert_to_object_ex(zoption
); break;
837 ZVAL_ADDREF(*zoption
);
838 _zend_hash_quick_add_or_update(&r
->_cache
.options
, key
, keylen
, h
, zoption
, sizeof(zval
*), NULL
,
839 zend_hash_quick_exists(&r
->_cache
.options
, key
, keylen
, h
)?HASH_UPDATE
:HASH_ADD ZEND_FILE_LINE_CC
);
845 #ifdef HTTP_NEED_OPENSSL_TSL
847 static MUTEX_T
*http_openssl_tsl
= NULL
;
849 static void http_ssl_lock(int mode
, int n
, const char * file
, int line
)
851 if (mode
& CRYPTO_LOCK
) {
852 tsrm_mutex_lock(http_openssl_tsl
[n
]);
854 tsrm_mutex_unlock(http_openssl_tsl
[n
]);
858 static ulong
http_ssl_id(void)
860 return (ulong
) tsrm_thread_id();
863 static inline void http_ssl_init(void)
865 int i
, c
= CRYPTO_num_locks();
867 http_openssl_tsl
= malloc(c
* sizeof(MUTEX_T
));
869 for (i
= 0; i
< c
; ++i
) {
870 http_openssl_tsl
[i
] = tsrm_mutex_alloc();
873 CRYPTO_set_id_callback(http_ssl_id
);
874 CRYPTO_set_locking_callback(http_ssl_lock
);
877 static inline void http_ssl_cleanup(void)
879 if (http_openssl_tsl
) {
880 int i
, c
= CRYPTO_num_locks();
882 CRYPTO_set_id_callback(NULL
);
883 CRYPTO_set_locking_callback(NULL
);
885 for (i
= 0; i
< c
; ++i
) {
886 tsrm_mutex_free(http_openssl_tsl
[i
]);
889 free(http_openssl_tsl
);
890 http_openssl_tsl
= NULL
;
893 #endif /* HTTP_NEED_OPENSSL_TSL */
896 #ifdef HTTP_NEED_GNUTLS_TSL
898 static int http_ssl_mutex_create(void **m
)
900 if (*((MUTEX_T
*) m
) = tsrm_mutex_alloc()) {
907 static int http_ssl_mutex_destroy(void **m
)
909 tsrm_mutex_free(*((MUTEX_T
*) m
));
913 static int http_ssl_mutex_lock(void **m
)
915 return tsrm_mutex_lock(*((MUTEX_T
*) m
));
918 static int http_ssl_mutex_unlock(void **m
)
920 return tsrm_mutex_unlock(*((MUTEX_T
*) m
));
923 static struct gcry_thread_cbs http_gnutls_tsl
= {
924 GCRY_THREAD_OPTIONS_USER
,
926 http_ssl_mutex_create
,
927 http_ssl_mutex_destroy
,
929 http_ssl_mutex_unlock
932 static inline void http_ssl_init(void)
934 gcry_control(GCRYCTL_SET_THREAD_CBS
, &http_gnutls_tsl
);
937 static inline void http_ssl_cleanup(void)
941 #endif /* HTTP_NEED_GNUTLS_TSL */
944 #endif /* HTTP_HAVE_CURL */
951 * vim600: noet sw=4 ts=4 fdm=marker
952 * vim<600: noet sw=4 ts=4