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_request_method_api.h"
27 #include "php_http_url_api.h"
30 # include "php_http_request_object.h"
33 /* {{{ cruft for thread safe SSL crypto locks */
34 #if defined(ZTS) && defined(HTTP_HAVE_SSL)
36 # define HTTP_NEED_SSL_TSL
37 # define HTTP_NEED_OPENSSL_TSL
38 # include <openssl/crypto.h>
39 # else /* !PHP_WIN32 */
40 # if defined(HTTP_HAVE_OPENSSL)
41 # if defined(HAVE_OPENSSL_CRYPTO_H)
42 # define HTTP_NEED_SSL_TSL
43 # define HTTP_NEED_OPENSSL_TSL
44 # include <openssl/crypto.h>
47 "libcurl was compiled with OpenSSL support, but configure could not find " \
48 "openssl/crypto.h; thus no SSL crypto locking callbacks will be set, which may " \
49 "cause random crashes on SSL requests"
51 # elif defined(HTTP_HAVE_GNUTLS)
52 # if defined(HAVE_GCRYPT_H)
53 # define HTTP_NEED_SSL_TSL
54 # define HTTP_NEED_GNUTLS_TSL
58 "libcurl was compiled with GnuTLS support, but configure could not find " \
59 "gcrypt.h; thus no SSL crypto locking callbacks will be set, which may " \
60 "cause random crashes on SSL requests"
64 "libcurl was compiled with SSL support, but configure could not determine which" \
65 "library was used; thus no SSL crypto locking callbacks will be set, which may " \
66 "cause random crashes on SSL requests"
67 # endif /* HTTP_HAVE_OPENSSL || HTTP_HAVE_GNUTLS */
68 # endif /* PHP_WIN32 */
69 #endif /* ZTS && HTTP_HAVE_SSL */
72 ZEND_EXTERN_MODULE_GLOBALS(http
);
74 #ifdef HTTP_NEED_SSL_TSL
75 static inline void http_ssl_init(void);
76 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
);
97 PHP_MSHUTDOWN_FUNCTION(http_request
)
99 curl_global_cleanup();
100 #ifdef HTTP_NEED_SSL_TSL
106 #ifndef HAVE_CURL_EASY_STRERROR
107 # define curl_easy_strerror(dummy) "unkown error"
110 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
111 #define HTTP_CURL_INFO_EX(I, X) \
112 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
114 case CURLINFO_STRING: \
117 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
118 add_assoc_string(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
123 case CURLINFO_DOUBLE: \
126 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
127 add_assoc_double(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), d); \
132 case CURLINFO_LONG: \
135 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
136 add_assoc_long(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), l); \
142 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
143 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
144 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
145 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
146 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
147 if (!strcasecmp(key, #keyname)) { \
148 convert_to_string_ex(param); \
149 HTTP_CURL_OPT(optname, http_request_data_copy(COPY_STRING, Z_STRVAL_PP(param))); \
153 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
154 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
155 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
156 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
157 if (!strcasecmp(key, #keyname)) { \
158 convert_to_long_ex(param); \
159 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
164 #define http_curl_getopt(o, k, t) _http_curl_getopt_ex((o), (k), sizeof(k), (t) TSRMLS_CC)
165 #define http_curl_getopt_ex(o, k, l, t) _http_curl_getopt_ex((o), (k), (l), (t) TSRMLS_CC)
166 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
);
167 #define http_curl_defaults(ch) _http_curl_defaults((ch))
168 static inline void _http_curl_defaults(CURL
*ch
);
169 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
170 static int http_curl_progress_callback(void *, double, double, double, double);
171 static int http_curl_raw_callback(CURL
*, curl_infotype
, char *, size_t, void *);
172 static int http_curl_dummy_callback(char *data
, size_t n
, size_t l
, void *s
) { return n
*l
; }
174 /* {{{ http_request_callback_ctx http_request_callback_data(void *) */
175 http_request_callback_ctx
*_http_request_callback_data_ex(void *data
, zend_bool cpy TSRMLS_DC
)
177 http_request_callback_ctx
*ctx
= emalloc(sizeof(http_request_callback_ctx
));
179 TSRMLS_SET_CTX(ctx
->tsrm_ctx
);
183 return http_request_data_copy(COPY_CONTEXT
, ctx
);
190 /* {{{ void *http_request_data_copy(int, void *) */
191 void *_http_request_data_copy(int type
, void *data TSRMLS_DC
)
197 char *new_str
= estrdup(data
);
198 zend_llist_add_element(&HTTP_G(request
).copies
.strings
, &new_str
);
204 zend_llist_add_element(&HTTP_G(request
).copies
.slists
, &data
);
210 zend_llist_add_element(&HTTP_G(request
).copies
.contexts
, &data
);
216 zend_llist_add_element(&HTTP_G(request
).copies
.convs
, &data
);
228 /* {{{ void http_request_data_free_string(char **) */
229 void _http_request_data_free_string(void *string
)
231 efree(*((char **)string
));
235 /* {{{ void http_request_data_free_slist(struct curl_slist **) */
236 void _http_request_data_free_slist(void *list
)
238 curl_slist_free_all(*((struct curl_slist
**) list
));
242 /* {{{ _http_request_data_free_context(http_request_callback_ctx **) */
243 void _http_request_data_free_context(void *context
)
245 efree(*((http_request_callback_ctx
**) context
));
249 /* {{{ _http_request_data_free_conv(http_request_conv **) */
250 void _http_request_data_free_conv(void *conv
)
252 efree(*((http_request_conv
**) conv
));
256 /* {{{ http_request_body *http_request_body_new() */
257 PHP_HTTP_API http_request_body
*_http_request_body_new(TSRMLS_D
)
259 http_request_body
*body
= ecalloc(1, sizeof(http_request_body
));
264 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
265 PHP_HTTP_API STATUS
_http_request_body_fill(http_request_body
*body
, HashTable
*fields
, HashTable
*files TSRMLS_DC
)
267 if (files
&& (zend_hash_num_elements(files
) > 0)) {
272 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
275 FOREACH_HASH_KEYVAL(pos
, fields
, key
, idx
, data
) {
280 convert_to_string_ex(data
);
281 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
282 CURLFORM_COPYNAME
, key
,
283 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
284 CURLFORM_CONTENTSLENGTH
, (long) Z_STRLEN_PP(data
),
292 if (CURLE_OK
!= err
) {
293 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post fields: %s", curl_easy_strerror(err
));
294 curl_formfree(http_post_data
[0]);
304 FOREACH_HASH_VAL(pos
, files
, data
) {
305 zval
**file
, **type
, **name
;
307 if (Z_TYPE_PP(data
) != IS_ARRAY
) {
308 http_error(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Unrecognized type of post file array entry");
309 } else if ( SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) ||
310 SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) ||
311 SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
312 http_error(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Post file array entry misses either 'name', 'type' or 'file' entry");
314 CURLcode err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
315 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
316 CURLFORM_FILE
, Z_STRVAL_PP(file
),
317 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
320 if (CURLE_OK
!= err
) {
321 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post files: %s", curl_easy_strerror(err
));
322 curl_formfree(http_post_data
[0]);
328 body
->type
= HTTP_REQUEST_BODY_CURLPOST
;
329 body
->data
= http_post_data
[0];
336 if (SUCCESS
!= http_urlencode_hash_ex(fields
, 1, NULL
, 0, &encoded
, &encoded_len
)) {
337 http_error(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post data");
341 body
->type
= HTTP_REQUEST_BODY_CSTRING
;
342 body
->data
= encoded
;
343 body
->size
= encoded_len
;
350 /* {{{ void http_request_body_dtor(http_request_body *) */
351 PHP_HTTP_API
void _http_request_body_dtor(http_request_body
*body TSRMLS_DC
)
356 case HTTP_REQUEST_BODY_CSTRING
:
362 case HTTP_REQUEST_BODY_CURLPOST
:
363 curl_formfree(body
->data
);
366 case HTTP_REQUEST_BODY_UPLOADFILE
:
367 php_stream_close(body
->data
);
374 /* {{{ void http_request_body_free(http_request_body *) */
375 PHP_HTTP_API
void _http_request_body_free(http_request_body
**body TSRMLS_DC
)
378 http_request_body_dtor(*body
);
385 /* {{{ STATUS http_request_init(CURL *, http_request_method, char *, http_request_body *, HashTable *) */
386 PHP_HTTP_API STATUS
_http_request_init(CURL
*ch
, http_request_method meth
, char *url
, http_request_body
*body
, HashTable
*options TSRMLS_DC
)
389 zend_bool range_req
= 0;
391 /* reset CURL handle */
392 #ifdef HAVE_CURL_EASY_RESET
395 http_curl_defaults(ch
);
399 HTTP_CURL_OPT(URL
, http_request_data_copy(COPY_STRING
, url
));
402 HTTP_CURL_OPT(HEADER
, 0);
403 HTTP_CURL_OPT(FILETIME
, 1);
404 HTTP_CURL_OPT(AUTOREFERER
, 1);
405 HTTP_CURL_OPT(READFUNCTION
, http_curl_read_callback
);
406 /* we'll get all data through the debug function */
407 HTTP_CURL_OPT(WRITEFUNCTION
, http_curl_dummy_callback
);
408 HTTP_CURL_OPT(HEADERFUNCTION
, NULL
);
410 HTTP_CURL_OPT(VERBOSE
, 1);
411 HTTP_CURL_OPT(DEBUGFUNCTION
, http_curl_raw_callback
);
414 HTTP_CURL_OPT(NOSIGNAL
, 1);
417 /* progress callback */
418 if ((zoption
= http_curl_getopt(options
, "onprogress", 0))) {
419 HTTP_CURL_OPT(NOPROGRESS
, 0);
420 HTTP_CURL_OPT(PROGRESSFUNCTION
, http_curl_progress_callback
);
421 HTTP_CURL_OPT(PROGRESSDATA
, http_request_callback_data(zoption
));
425 if ((zoption
= http_curl_getopt(options
, "proxyhost", IS_STRING
))) {
426 HTTP_CURL_OPT(PROXY
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
428 if ((zoption
= http_curl_getopt(options
, "proxyport", IS_LONG
))) {
429 HTTP_CURL_OPT(PROXYPORT
, Z_LVAL_P(zoption
));
432 if ((zoption
= http_curl_getopt(options
, "proxyauth", IS_STRING
))) {
433 HTTP_CURL_OPT(PROXYUSERPWD
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
436 if ((zoption
= http_curl_getopt(options
, "proxyauthtype", IS_LONG
))) {
437 HTTP_CURL_OPT(PROXYAUTH
, Z_LVAL_P(zoption
));
441 /* outgoing interface */
442 if ((zoption
= http_curl_getopt(options
, "interface", IS_STRING
))) {
443 HTTP_CURL_OPT(INTERFACE
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
447 if ((zoption
= http_curl_getopt(options
, "port", IS_LONG
))) {
448 HTTP_CURL_OPT(PORT
, Z_LVAL_P(zoption
));
452 if ((zoption
= http_curl_getopt(options
, "httpauth", IS_STRING
))) {
453 HTTP_CURL_OPT(USERPWD
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
455 if ((zoption
= http_curl_getopt(options
, "httpauthtype", IS_LONG
))) {
456 HTTP_CURL_OPT(HTTPAUTH
, Z_LVAL_P(zoption
));
459 /* compress, empty string enables all supported if libcurl was build with zlib support */
460 if ((zoption
= http_curl_getopt(options
, "compress", IS_BOOL
)) && Z_LVAL_P(zoption
)) {
461 #ifdef HTTP_HAVE_CURL_ZLIB
462 HTTP_CURL_OPT(ENCODING
, "");
464 HTTP_CURL_OPT(ENCODING
, "gzip;q=1.0, deflate;q=0.5, *;q=0.1");
468 /* redirects, defaults to 0 */
469 if ((zoption
= http_curl_getopt(options
, "redirect", IS_LONG
))) {
470 HTTP_CURL_OPT(FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
471 HTTP_CURL_OPT(MAXREDIRS
, Z_LVAL_P(zoption
));
472 if ((zoption
= http_curl_getopt(options
, "unrestrictedauth", IS_BOOL
))) {
473 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
478 if ((zoption
= http_curl_getopt(options
, "referer", IS_STRING
))) {
479 HTTP_CURL_OPT(REFERER
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
482 /* useragent, default "PECL::HTTP/version (PHP/version)" */
483 if ((zoption
= http_curl_getopt(options
, "useragent", IS_STRING
))) {
484 HTTP_CURL_OPT(USERAGENT
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
487 /* additional headers, array('name' => 'value') */
488 if ((zoption
= http_curl_getopt(options
, "headers", IS_ARRAY
))) {
492 struct curl_slist
*headers
= NULL
;
494 FOREACH_KEY(pos
, zoption
, header_key
, header_idx
) {
497 if (SUCCESS
== zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption
), (void **) &header_val
, &pos
)) {
498 char header
[1024] = {0};
499 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
500 headers
= curl_slist_append(headers
, http_request_data_copy(COPY_STRING
, header
));
509 HTTP_CURL_OPT(HTTPHEADER
, http_request_data_copy(COPY_SLIST
, headers
));
513 /* cookies, array('name' => 'value') */
514 if ((zoption
= http_curl_getopt(options
, "cookies", IS_ARRAY
))) {
515 char *cookie_key
= NULL
;
516 ulong cookie_idx
= 0;
518 phpstr
*qstr
= phpstr_new();
520 FOREACH_KEY(pos
, zoption
, cookie_key
, cookie_idx
) {
523 if (SUCCESS
== zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption
), (void **) &cookie_val
, &pos
)) {
524 phpstr_appendf(qstr
, "%s=%s; ", cookie_key
, Z_STRVAL_PP(cookie_val
));
534 HTTP_CURL_OPT(COOKIE
, http_request_data_copy(COPY_STRING
, qstr
->data
));
539 /* session cookies */
540 if ((zoption
= http_curl_getopt(options
, "cookiesession", IS_BOOL
))) {
541 if (Z_LVAL_P(zoption
)) {
542 /* accept cookies for this session */
543 HTTP_CURL_OPT(COOKIEFILE
, "");
545 /* reset session cookies */
546 HTTP_CURL_OPT(COOKIESESSION
, 1);
550 /* cookiestore, read initial cookies from that file and store cookies back into that file */
551 if ((zoption
= http_curl_getopt(options
, "cookiestore", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
552 HTTP_CURL_OPT(COOKIEFILE
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
553 HTTP_CURL_OPT(COOKIEJAR
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
557 if ((zoption
= http_curl_getopt(options
, "resume", IS_LONG
)) && (Z_LVAL_P(zoption
) != 0)) {
559 HTTP_CURL_OPT(RESUME_FROM
, Z_LVAL_P(zoption
));
563 if ((zoption
= http_curl_getopt(options
, "maxfilesize", IS_LONG
))) {
564 HTTP_CURL_OPT(MAXFILESIZE
, Z_LVAL_P(zoption
));
568 if ((zoption
= http_curl_getopt(options
, "lastmodified", IS_LONG
))) {
569 if (Z_LVAL_P(zoption
)) {
570 if (Z_LVAL_P(zoption
) > 0) {
571 HTTP_CURL_OPT(TIMEVALUE
, Z_LVAL_P(zoption
));
573 HTTP_CURL_OPT(TIMEVALUE
, time(NULL
) + Z_LVAL_P(zoption
));
575 HTTP_CURL_OPT(TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
577 HTTP_CURL_OPT(TIMECONDITION
, CURL_TIMECOND_NONE
);
581 /* timeout, defaults to 0 */
582 if ((zoption
= http_curl_getopt(options
, "timeout", IS_LONG
))) {
583 HTTP_CURL_OPT(TIMEOUT
, Z_LVAL_P(zoption
));
586 /* connecttimeout, defaults to 3 */
587 if ((zoption
= http_curl_getopt(options
, "connecttimeout", IS_LONG
))) {
588 HTTP_CURL_OPT(CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
592 if ((zoption
= http_curl_getopt(options
, "ssl", IS_ARRAY
))) {
598 FOREACH_KEYVAL(pos
, zoption
, key
, idx
, param
) {
600 HTTP_CURL_OPT_SSL_STRING(CERT
);
601 HTTP_CURL_OPT_SSL_STRING(CERTTYPE
);
602 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD
);
604 HTTP_CURL_OPT_SSL_STRING(KEY
);
605 HTTP_CURL_OPT_SSL_STRING(KEYTYPE
);
606 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD
);
608 HTTP_CURL_OPT_SSL_STRING(ENGINE
);
609 HTTP_CURL_OPT_SSL_LONG(VERSION
);
611 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER
);
612 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST
);
613 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST
);
615 HTTP_CURL_OPT_STRING(CAINFO
);
616 HTTP_CURL_OPT_STRING(CAPATH
);
617 HTTP_CURL_OPT_STRING(RANDOM_FILE
);
618 HTTP_CURL_OPT_STRING(EGDSOCKET
);
630 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1);
634 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
638 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
642 curl_easy_setopt(ch
, CURLOPT_UPLOAD
, 1);
646 if (http_request_method_exists(0, meth
, NULL
)) {
647 curl_easy_setopt(ch
, CURLOPT_CUSTOMREQUEST
, http_request_method_name(meth
));
649 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unsupported request method: %d", meth
);
655 /* attach request body */
656 if (body
&& (meth
!= HTTP_GET
) && (meth
!= HTTP_HEAD
)) {
659 case HTTP_REQUEST_BODY_CSTRING
:
660 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, body
->data
);
661 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, body
->size
);
664 case HTTP_REQUEST_BODY_CURLPOST
:
665 curl_easy_setopt(ch
, CURLOPT_HTTPPOST
, (struct curl_httppost
*) body
->data
);
668 case HTTP_REQUEST_BODY_UPLOADFILE
:
669 curl_easy_setopt(ch
, CURLOPT_READDATA
, http_request_callback_data(body
));
670 curl_easy_setopt(ch
, CURLOPT_INFILESIZE
, body
->size
);
674 /* shouldn't ever happen */
675 http_error_ex(HE_ERROR
, 0, "Unknown request body type: %d", body
->type
);
685 /* {{{ void http_request_conv(CURL *, phpstr *, phpstr *) */
686 void _http_request_conv(CURL
*ch
, phpstr
* response
, phpstr
*request TSRMLS_DC
)
688 http_request_conv
*conv
= emalloc(sizeof(http_request_conv
));
689 conv
->response
= response
;
690 conv
->request
= request
;
691 conv
->last_info
= -1;
692 HTTP_CURL_OPT(DEBUGDATA
, http_request_callback_data(http_request_data_copy(COPY_CONV
, conv
)));
696 /* {{{ STATUS http_request_exec(CURL *, HashTable *) */
697 PHP_HTTP_API STATUS
_http_request_exec(CURL
*ch
, HashTable
*info
, phpstr
*response
, phpstr
*request TSRMLS_DC
)
701 http_request_conv(ch
, response
, request
);
703 /* perform request */
704 if (CURLE_OK
!= (result
= curl_easy_perform(ch
))) {
705 http_error(HE_WARNING
, HTTP_E_REQUEST
, curl_easy_strerror(result
));
709 http_request_info(ch
, info
);
711 /* always succeeds */
716 /* {{{ void http_request_info(CURL *, HashTable *) */
717 PHP_HTTP_API
void _http_request_info(CURL
*ch
, HashTable
*info TSRMLS_DC
)
720 INIT_ZARR(array
, info
);
722 HTTP_CURL_INFO(EFFECTIVE_URL
);
723 HTTP_CURL_INFO(RESPONSE_CODE
);
724 HTTP_CURL_INFO_EX(HTTP_CODE
, RESPONSE_CODE
);
725 HTTP_CURL_INFO(HTTP_CONNECTCODE
);
726 HTTP_CURL_INFO(FILETIME
);
727 HTTP_CURL_INFO(TOTAL_TIME
);
728 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
729 HTTP_CURL_INFO(CONNECT_TIME
);
730 HTTP_CURL_INFO(PRETRANSFER_TIME
);
731 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
732 HTTP_CURL_INFO(REDIRECT_TIME
);
733 HTTP_CURL_INFO(REDIRECT_COUNT
);
734 HTTP_CURL_INFO(SIZE_UPLOAD
);
735 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
736 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
737 HTTP_CURL_INFO(SPEED_UPLOAD
);
738 HTTP_CURL_INFO(HEADER_SIZE
);
739 HTTP_CURL_INFO(REQUEST_SIZE
);
740 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
741 /*HTTP_CURL_INFO(SSL_ENGINES); todo: CURLINFO_SLIST */
742 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
743 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
744 HTTP_CURL_INFO(CONTENT_TYPE
);
745 /*HTTP_CURL_INFO(PRIVATE);*/
746 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
747 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
748 /*HTTP_CURL_INFO(OS_ERRNO);*/
749 HTTP_CURL_INFO(NUM_CONNECTS
);
753 /* {{{ STATUS http_request_ex(CURL *, http_request_method, char *, http_request_body, HashTable, HashTable, phpstr *) */
754 PHP_HTTP_API STATUS
_http_request_ex(CURL
*ch
, http_request_method meth
, char *url
, http_request_body
*body
, HashTable
*options
, HashTable
*info
, phpstr
*response TSRMLS_DC
)
757 zend_bool clean_curl
= !ch
;
759 HTTP_CHECK_CURL_INIT(ch
, curl_easy_init(), return FAILURE
);
761 status
= ((SUCCESS
== http_request_init(ch
, meth
, url
, body
, options
)) &&
762 (SUCCESS
== http_request_exec(ch
, info
, response
, NULL
))) ? SUCCESS
: FAILURE
;
765 curl_easy_cleanup(ch
);
771 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
772 static size_t http_curl_read_callback(void *data
, size_t len
, size_t n
, void *s
)
774 HTTP_REQUEST_CALLBACK_DATA(s
, http_request_body
*, body
);
776 if (body
->type
!= HTTP_REQUEST_BODY_UPLOADFILE
) {
779 return php_stream_read((php_stream
*) body
->data
, data
, len
* n
);
783 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
784 static int http_curl_progress_callback(void *data
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
786 zval
*params_pass
[4], params_local
[4], retval
;
787 HTTP_REQUEST_CALLBACK_DATA(data
, zval
*, func
);
789 params_pass
[0] = ¶ms_local
[0];
790 params_pass
[1] = ¶ms_local
[1];
791 params_pass
[2] = ¶ms_local
[2];
792 params_pass
[3] = ¶ms_local
[3];
794 INIT_PZVAL(params_pass
[0]);
795 INIT_PZVAL(params_pass
[1]);
796 INIT_PZVAL(params_pass
[2]);
797 INIT_PZVAL(params_pass
[3]);
798 ZVAL_DOUBLE(params_pass
[0], dltotal
);
799 ZVAL_DOUBLE(params_pass
[1], dlnow
);
800 ZVAL_DOUBLE(params_pass
[2], ultotal
);
801 ZVAL_DOUBLE(params_pass
[3], ulnow
);
803 return call_user_function(EG(function_table
), NULL
, func
, &retval
, 4, params_pass TSRMLS_CC
);
807 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
808 static int http_curl_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
810 HTTP_REQUEST_CALLBACK_DATA(ctx
, http_request_conv
*, conv
);
814 case CURLINFO_DATA_IN
:
815 if (conv
->response
&& conv
->last_info
== CURLINFO_HEADER_IN
) {
816 phpstr_appends(conv
->response
, HTTP_CRLF
);
818 case CURLINFO_HEADER_IN
:
819 if (conv
->response
) {
820 phpstr_append(conv
->response
, data
, length
);
823 case CURLINFO_DATA_OUT
:
824 if (conv
->request
&& conv
->last_info
== CURLINFO_HEADER_OUT
) {
825 phpstr_appends(conv
->request
, HTTP_CRLF
);
827 case CURLINFO_HEADER_OUT
:
829 phpstr_append(conv
->request
, data
, length
);
834 fprintf(stderr
, "## ", type
);
836 fprintf(stderr
, "%s", data
);
839 for (i
= 1; i
<= length
; ++i
) {
840 fprintf(stderr
, "%02X ", data
[i
-1] & 0xFF);
842 fprintf(stderr
, "\n## ");
845 fprintf(stderr
, "\n");
847 if (data
[length
-1] != 0xa) {
848 fprintf(stderr
, "\n");
855 conv
->last_info
= type
;
861 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
862 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
)
866 if (!options
|| (SUCCESS
!= zend_hash_find(options
, key
, keylen
, (void **) &zoption
))) {
870 if (Z_TYPE_PP(zoption
) != type
) {
873 case IS_BOOL
: convert_to_boolean_ex(zoption
); break;
874 case IS_LONG
: convert_to_long_ex(zoption
); break;
875 case IS_DOUBLE
: convert_to_double_ex(zoption
); break;
876 case IS_STRING
: convert_to_string_ex(zoption
); break;
877 case IS_ARRAY
: convert_to_array_ex(zoption
); break;
878 case IS_OBJECT
: convert_to_object_ex(zoption
); break;
888 #ifdef HTTP_NEED_OPENSSL_TSL
890 static MUTEX_T
*http_openssl_tsl
= NULL
;
892 static void http_ssl_lock(int mode
, int n
, const char * file
, int line
)
894 if (mode
& CRYPTO_LOCK
) {
895 tsrm_mutex_lock(http_openssl_tsl
[n
]);
897 tsrm_mutex_unlock(http_openssl_tsl
[n
]);
901 static ulong
http_ssl_id(void)
903 return (ulong
) tsrm_thread_id();
906 static inline void http_ssl_init(void)
908 int i
, c
= CRYPTO_num_locks();
910 http_openssl_tsl
= malloc(c
* sizeof(MUTEX_T
));
912 for (i
= 0; i
< c
; ++i
) {
913 http_openssl_tsl
[i
] = tsrm_mutex_alloc();
916 CRYPTO_set_id_callback(http_ssl_id
);
917 CRYPTO_set_locking_callback(http_ssl_lock
);
920 static inline void http_ssl_cleanup(void)
922 if (http_openssl_tsl
) {
923 int i
, c
= CRYPTO_num_locks();
925 CRYPTO_set_id_callback(NULL
);
926 CRYPTO_set_locking_callback(NULL
);
928 for (i
= 0; i
< c
; ++i
) {
929 tsrm_mutex_free(http_openssl_tsl
[i
]);
932 free(http_openssl_tsl
);
933 http_openssl_tsl
= NULL
;
936 #endif /* HTTP_NEED_OPENSSL_TSL */
939 #ifdef HTTP_NEED_GNUTLS_TSL
941 static int http_ssl_mutex_create(void **m
)
943 if (*((MUTEX_T
*) m
) = tsrm_mutex_alloc()) {
950 static int http_ssl_mutex_destroy(void **m
)
952 tsrm_mutex_free(*((MUTEX_T
*) m
));
956 static int http_ssl_mutex_lock(void **m
)
958 return tsrm_mutex_lock(*((MUTEX_T
*) m
));
961 static int http_ssl_mutex_unlock(void **m
)
963 return tsrm_mutex_unlock(*((MUTEX_T
*) m
));
966 static struct gcry_thread_cbs http_gnutls_tsl
= {
967 GCRY_THREAD_OPTIONS_USER
,
969 http_ssl_mutex_create
,
970 http_ssl_mutex_destroy
,
972 http_ssl_mutex_unlock
975 static inline void http_ssl_init(void)
977 gcry_control(GCRYCTL_SET_THREAD_CBS
, &http_gnutls_tsl
);
980 static inline void http_ssl_cleanup(void)
984 #endif /* HTTP_NEED_GNUTLS_TSL */
987 /* {{{ http_curl_defaults(CURL *) */
988 static inline void _http_curl_defaults(CURL
*ch
)
990 HTTP_CURL_OPT(URL
, NULL
);
991 HTTP_CURL_OPT(NOPROGRESS
, 1);
992 HTTP_CURL_OPT(PROXY
, NULL
);
993 HTTP_CURL_OPT(PROXYPORT
, 0);
994 HTTP_CURL_OPT(PROXYUSERPWD
, NULL
);
995 HTTP_CURL_OPT(PROXYAUTH
, 0);
996 HTTP_CURL_OPT(INTERFACE
, NULL
);
997 HTTP_CURL_OPT(PORT
, 0);
998 HTTP_CURL_OPT(USERPWD
, NULL
);
999 HTTP_CURL_OPT(HTTPAUTH
, 0);
1000 HTTP_CURL_OPT(ENCODING
, 0);
1001 HTTP_CURL_OPT(FOLLOWLOCATION
, 0);
1002 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, 0);
1003 HTTP_CURL_OPT(REFERER
, NULL
);
1004 HTTP_CURL_OPT(USERAGENT
, "PECL::HTTP/" PHP_EXT_HTTP_VERSION
" (PHP/" PHP_VERSION
")");
1005 HTTP_CURL_OPT(HTTPHEADER
, NULL
);
1006 HTTP_CURL_OPT(COOKIE
, NULL
);
1007 HTTP_CURL_OPT(COOKIEFILE
, NULL
);
1008 HTTP_CURL_OPT(COOKIEJAR
, NULL
);
1009 HTTP_CURL_OPT(RESUME_FROM
, 0);
1010 HTTP_CURL_OPT(MAXFILESIZE
, 0);
1011 HTTP_CURL_OPT(TIMECONDITION
, 0);
1012 HTTP_CURL_OPT(TIMEVALUE
, 0);
1013 HTTP_CURL_OPT(TIMEOUT
, 0);
1014 HTTP_CURL_OPT(CONNECTTIMEOUT
, 3);
1015 HTTP_CURL_OPT(SSLCERT
, NULL
);
1016 HTTP_CURL_OPT(SSLCERTTYPE
, NULL
);
1017 HTTP_CURL_OPT(SSLCERTPASSWD
, NULL
);
1018 HTTP_CURL_OPT(SSLKEY
, NULL
);
1019 HTTP_CURL_OPT(SSLKEYTYPE
, NULL
);
1020 HTTP_CURL_OPT(SSLKEYPASSWD
, NULL
);
1021 HTTP_CURL_OPT(SSLENGINE
, NULL
);
1022 HTTP_CURL_OPT(SSLVERSION
, 0);
1023 HTTP_CURL_OPT(SSL_VERIFYPEER
, 0);
1024 HTTP_CURL_OPT(SSL_VERIFYHOST
, 0);
1025 HTTP_CURL_OPT(SSL_CIPHER_LIST
, NULL
);
1026 HTTP_CURL_OPT(CAINFO
, NULL
);
1027 HTTP_CURL_OPT(CAPATH
, NULL
);
1028 HTTP_CURL_OPT(RANDOM_FILE
, NULL
);
1029 HTTP_CURL_OPT(EGDSOCKET
, NULL
);
1030 HTTP_CURL_OPT(POSTFIELDS
, NULL
);
1031 HTTP_CURL_OPT(POSTFIELDSIZE
, 0);
1032 HTTP_CURL_OPT(HTTPPOST
, NULL
);
1033 HTTP_CURL_OPT(READDATA
, NULL
);
1034 HTTP_CURL_OPT(INFILESIZE
, 0);
1038 #endif /* HTTP_HAVE_CURL */
1045 * vim600: noet sw=4 ts=4 fdm=marker
1046 * vim<600: noet sw=4 ts=4