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-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #define HTTP_WANT_SAPI
16 #define HTTP_WANT_CURL
21 #include "php_http_api.h"
22 #include "php_http_request_api.h"
23 #include "php_http_url_api.h"
26 # include "php_http_request_object.h"
29 /* {{{ cruft for thread safe SSL crypto locks */
30 #if defined(ZTS) && defined(HTTP_HAVE_SSL)
32 # define HTTP_NEED_SSL_TSL
33 # define HTTP_NEED_OPENSSL_TSL
34 # include <openssl/crypto.h>
35 # else /* !PHP_WIN32 */
36 # if defined(HTTP_HAVE_OPENSSL)
37 # if defined(HAVE_OPENSSL_CRYPTO_H)
38 # define HTTP_NEED_SSL_TSL
39 # define HTTP_NEED_OPENSSL_TSL
40 # include <openssl/crypto.h>
43 "libcurl was compiled with OpenSSL support, but configure could not find " \
44 "openssl/crypto.h; thus no SSL crypto locking callbacks will be set, which may " \
45 "cause random crashes on SSL requests"
47 # elif defined(HTTP_HAVE_GNUTLS)
48 # if defined(HAVE_GCRYPT_H)
49 # define HTTP_NEED_SSL_TSL
50 # define HTTP_NEED_GNUTLS_TSL
54 "libcurl was compiled with GnuTLS support, but configure could not find " \
55 "gcrypt.h; thus no SSL crypto locking callbacks will be set, which may " \
56 "cause random crashes on SSL requests"
60 "libcurl was compiled with SSL support, but configure could not determine which" \
61 "library was used; thus no SSL crypto locking callbacks will be set, which may " \
62 "cause random crashes on SSL requests"
63 # endif /* HTTP_HAVE_OPENSSL || HTTP_HAVE_GNUTLS */
64 # endif /* PHP_WIN32 */
65 #endif /* ZTS && HTTP_HAVE_SSL */
67 #ifdef HTTP_NEED_SSL_TSL
68 static inline void http_ssl_init(void);
69 static inline void http_ssl_cleanup(void);
74 PHP_MINIT_FUNCTION(http_request
)
76 #ifdef HTTP_NEED_SSL_TSL
80 if (CURLE_OK
!= curl_global_init(CURL_GLOBAL_ALL
)) {
84 HTTP_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC
);
85 HTTP_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST
);
86 HTTP_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM
);
87 HTTP_LONG_CONSTANT("HTTP_AUTH_ANY", CURLAUTH_ANY
);
89 HTTP_LONG_CONSTANT("HTTP_VERSION_NONE", CURL_HTTP_VERSION_NONE
);
90 HTTP_LONG_CONSTANT("HTTP_VERSION_1_0", CURL_HTTP_VERSION_1_0
);
91 HTTP_LONG_CONSTANT("HTTP_VERSION_1_1", CURL_HTTP_VERSION_1_1
);
98 PHP_MSHUTDOWN_FUNCTION(http_request
)
100 curl_global_cleanup();
101 #ifdef HTTP_NEED_SSL_TSL
109 #ifndef HAVE_CURL_EASY_STRERROR
110 # define curl_easy_strerror(dummy) "unkown error"
113 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
114 #define HTTP_CURL_INFO_EX(I, X) \
115 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
117 case CURLINFO_STRING: \
120 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &c)) { \
121 char *key = estrndup(#X, lenof(#X)); \
122 add_assoc_string(&array, pretty_key(key, lenof(#X), 0, 0), c ? c : "", 1); \
128 case CURLINFO_DOUBLE: \
131 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &d)) { \
132 char *key = estrndup(#X, lenof(#X)); \
133 add_assoc_double(&array, pretty_key(key, lenof(#X), 0, 0), d); \
139 case CURLINFO_LONG: \
142 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &l)) { \
143 char *key = estrndup(#X, lenof(#X)); \
144 add_assoc_long(&array, pretty_key(key, lenof(#X), 0, 0), l); \
150 case CURLINFO_SLIST: \
152 struct curl_slist *l, *p; \
153 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &l)) { \
155 char *key = estrndup(#X, lenof(#X)); \
156 MAKE_STD_ZVAL(subarray); \
157 array_init(subarray); \
158 for (p = l; p; p = p->next) { \
159 add_next_index_string(subarray, p->data, 1); \
161 add_assoc_zval(&array, pretty_key(key, lenof(#X), 0, 0), subarray); \
162 curl_slist_free_all(l); \
168 #define HTTP_CURL_OPT(OPTION, p) HTTP_CURL_OPT_EX(request->ch, OPTION, (p))
169 #define HTTP_CURL_OPT_EX(ch, OPTION, p) curl_easy_setopt((ch), CURLOPT_##OPTION, (p))
170 #define HTTP_CURL_OPT_STRING(keyname, obdc) HTTP_CURL_OPT_STRING_EX(keyname, keyname, obdc)
171 #define HTTP_CURL_OPT_SSL_STRING(keyname, obdc) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname, obdc)
172 #define HTTP_CURL_OPT_SSL_STRING_(keyname,obdc ) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname, obdc)
173 #define HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \
174 if (!strcasecmp(key, #keyname)) { \
175 zval *copy = http_request_option_cache(request, #keyname, zval_copy(IS_STRING, *param)); \
177 HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(copy), return FAILURE); \
179 HTTP_CURL_OPT(optname, Z_STRVAL_P(copy)); \
183 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
184 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
185 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
186 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
187 if (!strcasecmp(key, #keyname)) { \
188 zval *copy = http_request_option_cache(request, #keyname, zval_copy(IS_LONG, *param)); \
189 HTTP_CURL_OPT(optname, Z_LVAL_P(copy)); \
195 /* {{{ forward declarations */
196 #define http_request_option(r, o, k, t) _http_request_option_ex((r), (o), (k), sizeof(k), (t) TSRMLS_CC)
197 #define http_request_option_ex(r, o, k, l, t) _http_request_option_ex((r), (o), (k), (l), (t) TSRMLS_CC)
198 static inline zval
*_http_request_option_ex(http_request
*request
, HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
);
199 #define http_request_option_cache(r, k, z) _http_request_option_cache_ex((r), (k), sizeof(k), 0, (z) TSRMLS_CC)
200 #define http_request_option_cache_ex(r, k, kl, h, z) _http_request_option_cache_ex((r), (k), (kl), (h), (z) TSRMLS_CC)
201 static inline zval
*_http_request_option_cache_ex(http_request
*r
, char *key
, size_t keylen
, ulong h
, zval
*opt TSRMLS_DC
);
203 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
204 static int http_curl_progress_callback(void *, double, double, double, double);
205 static int http_curl_raw_callback(CURL
*, curl_infotype
, char *, size_t, void *);
206 static int http_curl_dummy_callback(char *data
, size_t n
, size_t l
, void *s
) { return n
*l
; }
207 static curlioerr
http_curl_ioctl_callback(CURL
*, curliocmd
, void *);
210 /* {{{ CURL *http_curl_init(http_request *) */
211 PHP_HTTP_API CURL
* _http_curl_init_ex(CURL
*ch
, http_request
*request
)
213 if (ch
|| (ch
= curl_easy_init())) {
215 HTTP_CURL_OPT_EX(ch
, NOSIGNAL
, 1);
217 HTTP_CURL_OPT_EX(ch
, HEADER
, 0);
218 HTTP_CURL_OPT_EX(ch
, FILETIME
, 1);
219 HTTP_CURL_OPT_EX(ch
, AUTOREFERER
, 1);
220 HTTP_CURL_OPT_EX(ch
, VERBOSE
, 1);
221 HTTP_CURL_OPT_EX(ch
, HEADERFUNCTION
, NULL
);
222 HTTP_CURL_OPT_EX(ch
, DEBUGFUNCTION
, http_curl_raw_callback
);
223 HTTP_CURL_OPT_EX(ch
, READFUNCTION
, http_curl_read_callback
);
224 HTTP_CURL_OPT_EX(ch
, IOCTLFUNCTION
, http_curl_ioctl_callback
);
225 HTTP_CURL_OPT_EX(ch
, WRITEFUNCTION
, http_curl_dummy_callback
);
229 HTTP_CURL_OPT_EX(ch
, PRIVATE
, request
);
230 HTTP_CURL_OPT_EX(ch
, DEBUGDATA
, request
);
231 HTTP_CURL_OPT_EX(ch
, ERRORBUFFER
, request
->_error
);
233 /* attach curl handle */
235 /* set defaults (also in http_request_reset()) */
236 http_request_defaults(request
);
244 /* {{{ void http_curl_free(CURL **) */
245 PHP_HTTP_API
void _http_curl_free(CURL
**ch
)
248 /* avoid nasty segfaults with already cleaned up callbacks */
249 HTTP_CURL_OPT_EX(*ch
, NOPROGRESS
, 1);
250 HTTP_CURL_OPT_EX(*ch
, PROGRESSFUNCTION
, NULL
);
251 HTTP_CURL_OPT_EX(*ch
, VERBOSE
, 0);
252 HTTP_CURL_OPT_EX(*ch
, DEBUGFUNCTION
, NULL
);
253 curl_easy_cleanup(*ch
);
259 /* {{{ http_request *http_request_init(http_request *) */
260 PHP_HTTP_API http_request
*_http_request_init_ex(http_request
*request
, CURL
*ch
, http_request_method meth
, const char *url ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC
)
267 r
= emalloc_rel(sizeof(http_request
));
269 memset(r
, 0, sizeof(http_request
));
272 r
->url
= (url
) ? http_absolute_url(url
) : NULL
;
273 r
->meth
= (meth
> 0) ? meth
: HTTP_GET
;
275 phpstr_init(&r
->conv
.request
);
276 phpstr_init_ex(&r
->conv
.response
, HTTP_CURLBUF_SIZE
, 0);
277 phpstr_init(&r
->_cache
.cookies
);
278 zend_hash_init(&r
->_cache
.options
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
280 TSRMLS_SET_CTX(r
->tsrm_ls
);
286 /* {{{ void http_request_dtor(http_request *) */
287 PHP_HTTP_API
void _http_request_dtor(http_request
*request
)
289 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
291 http_curl_free(&request
->ch
);
292 http_request_reset(request
);
294 phpstr_dtor(&request
->_cache
.cookies
);
295 zend_hash_destroy(&request
->_cache
.options
);
296 if (request
->_cache
.headers
) {
297 curl_slist_free_all(request
->_cache
.headers
);
298 request
->_cache
.headers
= NULL
;
300 if (request
->_progress_callback
) {
301 zval_ptr_dtor(&request
->_progress_callback
);
302 request
->_progress_callback
= NULL
;
307 /* {{{ void http_request_free(http_request **) */
308 PHP_HTTP_API
void _http_request_free(http_request
**request
)
311 TSRMLS_FETCH_FROM_CTX((*request
)->tsrm_ls
);
312 http_request_body_free(&(*request
)->body
);
313 http_request_dtor(*request
);
320 /* {{{ void http_request_reset(http_request *) */
321 PHP_HTTP_API
void _http_request_reset(http_request
*request
)
323 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
324 STR_SET(request
->url
, NULL
);
325 request
->conv
.last_type
= 0;
326 phpstr_dtor(&request
->conv
.request
);
327 phpstr_dtor(&request
->conv
.response
);
328 http_request_body_dtor(request
->body
);
331 http_request_defaults(request
);
336 /* {{{ void http_request_defaults(http_request *) */
337 PHP_HTTP_API
void _http_request_defaults(http_request
*request
)
340 HTTP_CURL_OPT(PROGRESSFUNCTION
, NULL
);
341 HTTP_CURL_OPT(URL
, NULL
);
342 HTTP_CURL_OPT(NOPROGRESS
, 1);
343 HTTP_CURL_OPT(PROXY
, NULL
);
344 HTTP_CURL_OPT(PROXYPORT
, 0);
345 HTTP_CURL_OPT(PROXYUSERPWD
, NULL
);
346 HTTP_CURL_OPT(PROXYAUTH
, 0);
347 HTTP_CURL_OPT(INTERFACE
, NULL
);
348 HTTP_CURL_OPT(PORT
, 0);
349 HTTP_CURL_OPT(USERPWD
, NULL
);
350 HTTP_CURL_OPT(HTTPAUTH
, 0);
351 HTTP_CURL_OPT(ENCODING
, NULL
);
352 HTTP_CURL_OPT(FOLLOWLOCATION
, 0);
353 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, 0);
354 HTTP_CURL_OPT(REFERER
, NULL
);
355 HTTP_CURL_OPT(USERAGENT
, "PECL::HTTP/" PHP_EXT_HTTP_VERSION
" (PHP/" PHP_VERSION
")");
356 HTTP_CURL_OPT(HTTPHEADER
, NULL
);
357 HTTP_CURL_OPT(COOKIE
, NULL
);
358 #if LIBCURL_VERSION_NUM >= 0x070e01
359 HTTP_CURL_OPT(COOKIELIST
, NULL
);
361 HTTP_CURL_OPT(COOKIEFILE
, NULL
);
362 HTTP_CURL_OPT(COOKIEJAR
, NULL
);
363 HTTP_CURL_OPT(RESUME_FROM
, 0);
364 HTTP_CURL_OPT(MAXFILESIZE
, 0);
365 HTTP_CURL_OPT(TIMECONDITION
, 0);
366 HTTP_CURL_OPT(TIMEVALUE
, 0);
367 HTTP_CURL_OPT(TIMEOUT
, 0);
368 HTTP_CURL_OPT(CONNECTTIMEOUT
, 3);
369 HTTP_CURL_OPT(SSLCERT
, NULL
);
370 HTTP_CURL_OPT(SSLCERTTYPE
, NULL
);
371 HTTP_CURL_OPT(SSLCERTPASSWD
, NULL
);
372 HTTP_CURL_OPT(SSLKEY
, NULL
);
373 HTTP_CURL_OPT(SSLKEYTYPE
, NULL
);
374 HTTP_CURL_OPT(SSLKEYPASSWD
, NULL
);
375 HTTP_CURL_OPT(SSLENGINE
, NULL
);
376 HTTP_CURL_OPT(SSLVERSION
, 0);
377 HTTP_CURL_OPT(SSL_VERIFYPEER
, 0);
378 HTTP_CURL_OPT(SSL_VERIFYHOST
, 0);
379 HTTP_CURL_OPT(SSL_CIPHER_LIST
, NULL
);
380 HTTP_CURL_OPT(CAINFO
, NULL
);
381 HTTP_CURL_OPT(CAPATH
, NULL
);
382 HTTP_CURL_OPT(RANDOM_FILE
, NULL
);
383 HTTP_CURL_OPT(EGDSOCKET
, NULL
);
384 HTTP_CURL_OPT(POSTFIELDS
, NULL
);
385 HTTP_CURL_OPT(POSTFIELDSIZE
, 0);
386 HTTP_CURL_OPT(HTTPPOST
, NULL
);
387 HTTP_CURL_OPT(IOCTLDATA
, NULL
);
388 HTTP_CURL_OPT(READDATA
, NULL
);
389 HTTP_CURL_OPT(INFILESIZE
, 0);
390 HTTP_CURL_OPT(HTTP_VERSION
, CURL_HTTP_VERSION_NONE
);
391 HTTP_CURL_OPT(CUSTOMREQUEST
, NULL
);
392 HTTP_CURL_OPT(NOBODY
, 0);
393 HTTP_CURL_OPT(POST
, 0);
394 HTTP_CURL_OPT(UPLOAD
, 0);
395 HTTP_CURL_OPT(HTTPGET
, 1);
400 PHP_HTTP_API
void _http_request_set_progress_callback(http_request
*request
, zval
*cb
)
402 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
404 if (request
->_progress_callback
) {
405 zval_ptr_dtor(&request
->_progress_callback
);
407 if ((request
->_progress_callback
= cb
)) {
409 HTTP_CURL_OPT(NOPROGRESS
, 0);
410 HTTP_CURL_OPT(PROGRESSDATA
, request
);
411 HTTP_CURL_OPT(PROGRESSFUNCTION
, http_curl_progress_callback
);
413 HTTP_CURL_OPT(NOPROGRESS
, 1);
414 HTTP_CURL_OPT(PROGRESSDATA
, NULL
);
415 HTTP_CURL_OPT(PROGRESSFUNCTION
, NULL
);
419 /* {{{ STATUS http_request_prepare(http_request *, HashTable *) */
420 PHP_HTTP_API STATUS
_http_request_prepare(http_request
*request
, HashTable
*options
)
423 zend_bool range_req
= 0;
425 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
427 HTTP_CHECK_CURL_INIT(request
->ch
, http_curl_init(request
), return FAILURE
);
430 HTTP_CURL_OPT(URL
, request
->url
);
432 /* progress callback */
433 if ((zoption
= http_request_option(request
, options
, "onprogress", -1))) {
434 http_request_set_progress_callback(request
, zoption
);
438 if ((zoption
= http_request_option(request
, options
, "proxyhost", IS_STRING
))) {
439 if (Z_STRLEN_P(zoption
)) {
440 HTTP_CURL_OPT(PROXY
, Z_STRVAL_P(zoption
));
444 if ((zoption
= http_request_option(request
, options
, "proxyport", IS_LONG
))) {
445 HTTP_CURL_OPT(PROXYPORT
, Z_LVAL_P(zoption
));
448 if ((zoption
= http_request_option(request
, options
, "proxyauth", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
449 HTTP_CURL_OPT(PROXYUSERPWD
, Z_STRVAL_P(zoption
));
452 if ((zoption
= http_request_option(request
, options
, "proxyauthtype", IS_LONG
))) {
453 HTTP_CURL_OPT(PROXYAUTH
, Z_LVAL_P(zoption
));
457 /* outgoing interface */
458 if ((zoption
= http_request_option(request
, options
, "interface", IS_STRING
))) {
459 HTTP_CURL_OPT(INTERFACE
, Z_STRVAL_P(zoption
));
463 if ((zoption
= http_request_option(request
, options
, "port", IS_LONG
))) {
464 HTTP_CURL_OPT(PORT
, Z_LVAL_P(zoption
));
468 if ((zoption
= http_request_option(request
, options
, "httpauth", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
469 HTTP_CURL_OPT(USERPWD
, Z_STRVAL_P(zoption
));
471 if ((zoption
= http_request_option(request
, options
, "httpauthtype", IS_LONG
))) {
472 HTTP_CURL_OPT(HTTPAUTH
, Z_LVAL_P(zoption
));
475 /* redirects, defaults to 0 */
476 if ((zoption
= http_request_option(request
, options
, "redirect", IS_LONG
))) {
477 HTTP_CURL_OPT(FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
478 HTTP_CURL_OPT(MAXREDIRS
, Z_LVAL_P(zoption
));
479 if ((zoption
= http_request_option(request
, options
, "unrestrictedauth", IS_BOOL
))) {
480 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
485 if ((zoption
= http_request_option(request
, options
, "referer", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
486 HTTP_CURL_OPT(REFERER
, Z_STRVAL_P(zoption
));
489 /* useragent, default "PECL::HTTP/version (PHP/version)" */
490 if ((zoption
= http_request_option(request
, options
, "useragent", IS_STRING
))) {
491 /* allow to send no user agent, not even default one */
492 if (Z_STRLEN_P(zoption
)) {
493 HTTP_CURL_OPT(USERAGENT
, Z_STRVAL_P(zoption
));
495 HTTP_CURL_OPT(USERAGENT
, NULL
);
499 /* additional headers, array('name' => 'value') */
500 if (request
->_cache
.headers
) {
501 curl_slist_free_all(request
->_cache
.headers
);
502 request
->_cache
.headers
= NULL
;
504 if ((zoption
= http_request_option(request
, options
, "headers", IS_ARRAY
))) {
509 FOREACH_KEY(pos
, zoption
, header_key
, header_idx
) {
512 if (SUCCESS
== zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption
), (void **) &header_val
, &pos
)) {
513 char header
[1024] = {0};
515 ZVAL_ADDREF(*header_val
);
516 convert_to_string_ex(header_val
);
517 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
518 request
->_cache
.headers
= curl_slist_append(request
->_cache
.headers
, header
);
519 zval_ptr_dtor(header_val
);
527 if ((zoption
= http_request_option(request
, options
, "compress", IS_BOOL
)) && Z_LVAL_P(zoption
)) {
528 request
->_cache
.headers
= curl_slist_append(request
->_cache
.headers
, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
530 HTTP_CURL_OPT(HTTPHEADER
, request
->_cache
.headers
);
532 /* cookies, array('name' => 'value') */
533 if ((zoption
= http_request_option(request
, options
, "cookies", IS_ARRAY
))) {
534 phpstr_dtor(&request
->_cache
.cookies
);
535 if (zend_hash_num_elements(Z_ARRVAL_P(zoption
))) {
536 if (SUCCESS
== http_urlencode_hash_recursive(HASH_OF(zoption
), &request
->_cache
.cookies
, "; ", sizeof("; ")-1, NULL
, 0)) {
537 phpstr_fix(&request
->_cache
.cookies
);
538 HTTP_CURL_OPT(COOKIE
, request
->_cache
.cookies
.data
);
543 #if LIBCURL_VERSION_NUM >= 0x070e01
545 if ((zoption
= http_request_option(request
, options
, "resetcookies", IS_BOOL
)) && Z_LVAL_P(zoption
)) {
546 HTTP_CURL_OPT(COOKIELIST
, "ALL");
550 /* session cookies */
551 if ((zoption
= http_request_option(request
, options
, "cookiesession", IS_BOOL
))) {
552 if (Z_LVAL_P(zoption
)) {
553 /* accept cookies for this session */
554 HTTP_CURL_OPT(COOKIEFILE
, "");
556 /* reset session cookies */
557 HTTP_CURL_OPT(COOKIESESSION
, 1);
561 /* cookiestore, read initial cookies from that file and store cookies back into that file */
562 if ((zoption
= http_request_option(request
, options
, "cookiestore", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
563 HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(zoption
), return FAILURE
);
564 HTTP_CURL_OPT(COOKIEFILE
, Z_STRVAL_P(zoption
));
565 HTTP_CURL_OPT(COOKIEJAR
, Z_STRVAL_P(zoption
));
569 if ((zoption
= http_request_option(request
, options
, "resume", IS_LONG
)) && (Z_LVAL_P(zoption
) != 0)) {
571 HTTP_CURL_OPT(RESUME_FROM
, Z_LVAL_P(zoption
));
575 if ((zoption
= http_request_option(request
, options
, "maxfilesize", IS_LONG
))) {
576 HTTP_CURL_OPT(MAXFILESIZE
, Z_LVAL_P(zoption
));
580 if ((zoption
= http_request_option(request
, options
, "protocol", IS_LONG
))) {
581 HTTP_CURL_OPT(HTTP_VERSION
, Z_LVAL_P(zoption
));
585 if ((zoption
= http_request_option(request
, options
, "lastmodified", IS_LONG
))) {
586 if (Z_LVAL_P(zoption
)) {
587 if (Z_LVAL_P(zoption
) > 0) {
588 HTTP_CURL_OPT(TIMEVALUE
, Z_LVAL_P(zoption
));
590 HTTP_CURL_OPT(TIMEVALUE
, HTTP_GET_REQUEST_TIME() + Z_LVAL_P(zoption
));
592 HTTP_CURL_OPT(TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
594 HTTP_CURL_OPT(TIMECONDITION
, CURL_TIMECOND_NONE
);
598 /* timeout, defaults to 0 */
599 if ((zoption
= http_request_option(request
, options
, "timeout", IS_LONG
))) {
600 HTTP_CURL_OPT(TIMEOUT
, Z_LVAL_P(zoption
));
603 /* connecttimeout, defaults to 3 */
604 if ((zoption
= http_request_option(request
, options
, "connecttimeout", IS_LONG
))) {
605 HTTP_CURL_OPT(CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
609 if ((zoption
= http_request_option(request
, options
, "ssl", IS_ARRAY
))) {
615 FOREACH_KEYVAL(pos
, zoption
, key
, idx
, param
) {
617 HTTP_CURL_OPT_SSL_STRING(CERT
, 1);
618 HTTP_CURL_OPT_SSL_STRING(CERTTYPE
, 0);
619 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD
, 0);
621 HTTP_CURL_OPT_SSL_STRING(KEY
, 0);
622 HTTP_CURL_OPT_SSL_STRING(KEYTYPE
, 0);
623 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD
, 0);
625 HTTP_CURL_OPT_SSL_STRING(ENGINE
, 0);
626 HTTP_CURL_OPT_SSL_LONG(VERSION
);
628 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER
);
629 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST
);
630 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST
, 0);
632 HTTP_CURL_OPT_STRING(CAINFO
, 1);
633 HTTP_CURL_OPT_STRING(CAPATH
, 1);
634 HTTP_CURL_OPT_STRING(RANDOM_FILE
, 1);
635 HTTP_CURL_OPT_STRING(EGDSOCKET
, 1);
644 switch (request
->meth
)
647 HTTP_CURL_OPT(HTTPGET
, 1);
651 HTTP_CURL_OPT(NOBODY
, 1);
655 HTTP_CURL_OPT(POST
, 1);
659 HTTP_CURL_OPT(UPLOAD
, 1);
663 if (http_request_method_exists(0, request
->meth
, NULL
)) {
664 HTTP_CURL_OPT(CUSTOMREQUEST
, http_request_method_name(request
->meth
));
666 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unsupported request method: %d (%s)", request
->meth
, request
->url
);
672 /* attach request body */
673 if (request
->body
&& (request
->meth
!= HTTP_GET
) && (request
->meth
!= HTTP_HEAD
) && (request
->meth
!= HTTP_OPTIONS
)) {
674 switch (request
->body
->type
)
676 case HTTP_REQUEST_BODY_EMPTY
:
680 case HTTP_REQUEST_BODY_CSTRING
:
681 HTTP_CURL_OPT(POSTFIELDS
, request
->body
->data
);
682 HTTP_CURL_OPT(POSTFIELDSIZE
, request
->body
->size
);
685 case HTTP_REQUEST_BODY_CURLPOST
:
686 HTTP_CURL_OPT(HTTPPOST
, (struct curl_httppost
*) request
->body
->data
);
689 case HTTP_REQUEST_BODY_UPLOADFILE
:
690 HTTP_CURL_OPT(IOCTLDATA
, request
);
691 HTTP_CURL_OPT(READDATA
, request
);
692 HTTP_CURL_OPT(INFILESIZE
, request
->body
->size
);
696 /* shouldn't ever happen */
697 http_error_ex(HE_ERROR
, 0, "Unknown request body type: %d (%s)", request
->body
->type
, request
->url
);
707 /* {{{ void http_request_exec(http_request *) */
708 PHP_HTTP_API
void _http_request_exec(http_request
*request
)
711 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
713 if (CURLE_OK
!= (result
= curl_easy_perform(request
->ch
))) {
714 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "%s; %s (%s)", curl_easy_strerror(result
), request
->_error
, request
->url
);
719 /* {{{ void http_request_info(http_request *, HashTable *) */
720 PHP_HTTP_API
void _http_request_info(http_request
*request
, HashTable
*info
)
723 INIT_ZARR(array
, info
);
725 HTTP_CURL_INFO(EFFECTIVE_URL
);
726 HTTP_CURL_INFO(RESPONSE_CODE
);
727 HTTP_CURL_INFO_EX(HTTP_CONNECTCODE
, connect_code
);
728 HTTP_CURL_INFO(FILETIME
);
729 HTTP_CURL_INFO(TOTAL_TIME
);
730 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
731 HTTP_CURL_INFO(CONNECT_TIME
);
732 HTTP_CURL_INFO(PRETRANSFER_TIME
);
733 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
734 HTTP_CURL_INFO(REDIRECT_TIME
);
735 HTTP_CURL_INFO(REDIRECT_COUNT
);
736 HTTP_CURL_INFO(SIZE_UPLOAD
);
737 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
738 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
739 HTTP_CURL_INFO(SPEED_UPLOAD
);
740 HTTP_CURL_INFO(HEADER_SIZE
);
741 HTTP_CURL_INFO(REQUEST_SIZE
);
742 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
743 HTTP_CURL_INFO(SSL_ENGINES
);
744 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
745 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
746 HTTP_CURL_INFO(CONTENT_TYPE
);
747 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
748 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
749 /*HTTP_CURL_INFO(OS_ERRNO);*/
750 HTTP_CURL_INFO(NUM_CONNECTS
);
751 #if LIBCURL_VERSION_NUM >= 0x070e01
752 HTTP_CURL_INFO_EX(COOKIELIST
, cookies
);
757 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
758 static size_t http_curl_read_callback(void *data
, size_t len
, size_t n
, void *ctx
)
760 http_request
*request
= (http_request
*) ctx
;
761 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
763 if (request
->body
== NULL
|| request
->body
->type
!= HTTP_REQUEST_BODY_UPLOADFILE
) {
766 return php_stream_read((php_stream
*) request
->body
->data
, data
, len
* n
);
770 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
771 static int http_curl_progress_callback(void *ctx
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
774 http_request
*request
= (http_request
*) ctx
;
775 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
780 MAKE_STD_ZVAL(param
);
782 add_assoc_double(param
, "dltotal", dltotal
);
783 add_assoc_double(param
, "dlnow", dlnow
);
784 add_assoc_double(param
, "ultotal", ultotal
);
785 add_assoc_double(param
, "ulnow", ulnow
);
787 call_user_function(EG(function_table
), NULL
, request
->_progress_callback
, &retval
, 1, ¶m TSRMLS_CC
);
789 zval_ptr_dtor(¶m
);
796 /* {{{ static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *) */
797 static curlioerr
http_curl_ioctl_callback(CURL
*ch
, curliocmd cmd
, void *ctx
)
799 http_request
*request
= (http_request
*) ctx
;
800 TSRMLS_FETCH_FROM_CTX(request
->tsrm_ls
);
802 if (cmd
!= CURLIOCMD_RESTARTREAD
) {
803 return CURLIOE_UNKNOWNCMD
;
805 if ( request
->body
== NULL
||
806 request
->body
->type
!= HTTP_REQUEST_BODY_UPLOADFILE
||
807 SUCCESS
!= php_stream_rewind((php_stream
*) request
->body
->data
)) {
808 return CURLIOE_FAILRESTART
;
814 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
815 static int http_curl_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
817 http_request
*request
= (http_request
*) ctx
;
821 case CURLINFO_DATA_IN
:
822 if (request
->conv
.last_type
== CURLINFO_HEADER_IN
) {
823 phpstr_appends(&request
->conv
.response
, HTTP_CRLF
);
825 case CURLINFO_HEADER_IN
:
826 phpstr_append(&request
->conv
.response
, data
, length
);
828 case CURLINFO_DATA_OUT
:
829 if (request
->conv
.last_type
== CURLINFO_HEADER_OUT
) {
830 phpstr_appends(&request
->conv
.request
, HTTP_CRLF
);
832 case CURLINFO_HEADER_OUT
:
833 phpstr_append(&request
->conv
.request
, data
, length
);
837 fprintf(stderr
, "## ", type
);
839 fprintf(stderr
, "%s", data
);
842 for (i
= 1; i
<= length
; ++i
) {
843 fprintf(stderr
, "%02X ", data
[i
-1] & 0xFF);
845 fprintf(stderr
, "\n## ");
848 fprintf(stderr
, "\n");
850 if (data
[length
-1] != 0xa) {
851 fprintf(stderr
, "\n");
858 request
->conv
.last_type
= type
;
864 /* {{{ static inline zval *http_request_option(http_request *, HashTable *, char *, size_t, int) */
865 static inline zval
*_http_request_option_ex(http_request
*r
, HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
)
869 ulong h
= zend_get_hash_value(key
, keylen
);
876 (SUCCESS
!= zend_hash_quick_find(options
, key
, keylen
, h
, (void **) &zoption
))
878 (SUCCESS
!= zend_hash_find(options
, key
, keylen
, (void **) &zoption
))
884 return http_request_option_cache_ex(r
, key
, keylen
, h
, zval_copy(type
, *zoption
));
888 /* {{{ static inline zval *http_request_option_cache(http_request *, char *key, zval *) */
889 static inline zval
*_http_request_option_cache_ex(http_request
*r
, char *key
, size_t keylen
, ulong h
, zval
*opt TSRMLS_DC
)
895 _zend_hash_quick_add_or_update(&r
->_cache
.options
, key
, keylen
, h
, &opt
, sizeof(zval
*), NULL
,
896 zend_hash_quick_exists(&r
->_cache
.options
, key
, keylen
, h
)?HASH_UPDATE
:HASH_ADD ZEND_FILE_LINE_CC
);
901 if (zend_hash_exists(&r
->_cache
.options
, key
, keylen
)) {
902 zend_hash_update(&r
->_cache
.options
, key
, keylen
, &opt
, sizeof(zval
*), NULL
);
904 zend_hash_add(&r
->_cache
.options
, key
, keylen
, &opt
, sizeof(zval
*), NULL
);
912 #ifdef HTTP_NEED_OPENSSL_TSL
914 static MUTEX_T
*http_openssl_tsl
= NULL
;
916 static void http_ssl_lock(int mode
, int n
, const char * file
, int line
)
918 if (mode
& CRYPTO_LOCK
) {
919 tsrm_mutex_lock(http_openssl_tsl
[n
]);
921 tsrm_mutex_unlock(http_openssl_tsl
[n
]);
925 static ulong
http_ssl_id(void)
927 return (ulong
) tsrm_thread_id();
930 static inline void http_ssl_init(void)
932 int i
, c
= CRYPTO_num_locks();
934 http_openssl_tsl
= malloc(c
* sizeof(MUTEX_T
));
936 for (i
= 0; i
< c
; ++i
) {
937 http_openssl_tsl
[i
] = tsrm_mutex_alloc();
940 CRYPTO_set_id_callback(http_ssl_id
);
941 CRYPTO_set_locking_callback(http_ssl_lock
);
944 static inline void http_ssl_cleanup(void)
946 if (http_openssl_tsl
) {
947 int i
, c
= CRYPTO_num_locks();
949 CRYPTO_set_id_callback(NULL
);
950 CRYPTO_set_locking_callback(NULL
);
952 for (i
= 0; i
< c
; ++i
) {
953 tsrm_mutex_free(http_openssl_tsl
[i
]);
956 free(http_openssl_tsl
);
957 http_openssl_tsl
= NULL
;
960 #endif /* HTTP_NEED_OPENSSL_TSL */
963 #ifdef HTTP_NEED_GNUTLS_TSL
965 static int http_ssl_mutex_create(void **m
)
967 if (*((MUTEX_T
*) m
) = tsrm_mutex_alloc()) {
974 static int http_ssl_mutex_destroy(void **m
)
976 tsrm_mutex_free(*((MUTEX_T
*) m
));
980 static int http_ssl_mutex_lock(void **m
)
982 return tsrm_mutex_lock(*((MUTEX_T
*) m
));
985 static int http_ssl_mutex_unlock(void **m
)
987 return tsrm_mutex_unlock(*((MUTEX_T
*) m
));
990 static struct gcry_thread_cbs http_gnutls_tsl
= {
991 GCRY_THREAD_OPTION_USER
,
993 http_ssl_mutex_create
,
994 http_ssl_mutex_destroy
,
996 http_ssl_mutex_unlock
999 static inline void http_ssl_init(void)
1001 gcry_control(GCRYCTL_SET_THREAD_CBS
, &http_gnutls_tsl
);
1004 static inline void http_ssl_cleanup(void)
1008 #endif /* HTTP_NEED_GNUTLS_TSL */
1011 #endif /* HTTP_HAVE_CURL */
1018 * vim600: noet sw=4 ts=4 fdm=marker
1019 * vim<600: noet sw=4 ts=4