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); \
141 case CURLINFO_SLIST: \
143 struct curl_slist *l, *p; \
144 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
146 MAKE_STD_ZVAL(subarray); \
147 array_init(subarray); \
148 for (p = l; p; p = p->next) { \
149 add_next_index_string(subarray, p->data, 1); \
151 add_assoc_zval(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), subarray); \
152 curl_slist_free_all(l); \
157 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
158 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
159 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
160 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
161 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
162 if (!strcasecmp(key, #keyname)) { \
163 convert_to_string_ex(param); \
164 HTTP_CURL_OPT(optname, http_request_data_copy(COPY_STRING, Z_STRVAL_PP(param))); \
168 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
169 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
170 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
171 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
172 if (!strcasecmp(key, #keyname)) { \
173 convert_to_long_ex(param); \
174 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
179 #define http_curl_getopt(o, k, t) _http_curl_getopt_ex((o), (k), sizeof(k), (t) TSRMLS_CC)
180 #define http_curl_getopt_ex(o, k, l, t) _http_curl_getopt_ex((o), (k), (l), (t) TSRMLS_CC)
181 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
);
182 #define http_curl_defaults(ch) _http_curl_defaults((ch))
183 static inline void _http_curl_defaults(CURL
*ch
);
184 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
185 static int http_curl_progress_callback(void *, double, double, double, double);
186 static int http_curl_raw_callback(CURL
*, curl_infotype
, char *, size_t, void *);
187 static int http_curl_dummy_callback(char *data
, size_t n
, size_t l
, void *s
) { return n
*l
; }
189 /* {{{ http_request_callback_ctx http_request_callback_data(void *) */
190 http_request_callback_ctx
*_http_request_callback_data_ex(void *data
, zend_bool cpy TSRMLS_DC
)
192 http_request_callback_ctx
*ctx
= emalloc(sizeof(http_request_callback_ctx
));
194 TSRMLS_SET_CTX(ctx
->tsrm_ctx
);
198 return http_request_data_copy(COPY_CONTEXT
, ctx
);
205 /* {{{ void *http_request_data_copy(int, void *) */
206 void *_http_request_data_copy(int type
, void *data TSRMLS_DC
)
212 char *new_str
= estrdup(data
);
213 zend_llist_add_element(&HTTP_G(request
).copies
.strings
, &new_str
);
219 zend_llist_add_element(&HTTP_G(request
).copies
.slists
, &data
);
225 zend_llist_add_element(&HTTP_G(request
).copies
.contexts
, &data
);
231 zend_llist_add_element(&HTTP_G(request
).copies
.convs
, &data
);
243 /* {{{ void http_request_data_free_string(char **) */
244 void _http_request_data_free_string(void *string
)
246 efree(*((char **)string
));
250 /* {{{ void http_request_data_free_slist(struct curl_slist **) */
251 void _http_request_data_free_slist(void *list
)
253 curl_slist_free_all(*((struct curl_slist
**) list
));
257 /* {{{ _http_request_data_free_context(http_request_callback_ctx **) */
258 void _http_request_data_free_context(void *context
)
260 efree(*((http_request_callback_ctx
**) context
));
264 /* {{{ _http_request_data_free_conv(http_request_conv **) */
265 void _http_request_data_free_conv(void *conv
)
267 efree(*((http_request_conv
**) conv
));
271 /* {{{ http_request_body *http_request_body_new() */
272 PHP_HTTP_API http_request_body
*_http_request_body_new(TSRMLS_D
)
274 http_request_body
*body
= ecalloc(1, sizeof(http_request_body
));
279 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
280 PHP_HTTP_API STATUS
_http_request_body_fill(http_request_body
*body
, HashTable
*fields
, HashTable
*files TSRMLS_DC
)
282 if (files
&& (zend_hash_num_elements(files
) > 0)) {
287 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
290 FOREACH_HASH_KEYVAL(pos
, fields
, key
, idx
, data
) {
295 convert_to_string_ex(data
);
296 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
297 CURLFORM_COPYNAME
, key
,
298 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
299 CURLFORM_CONTENTSLENGTH
, (long) Z_STRLEN_PP(data
),
307 if (CURLE_OK
!= err
) {
308 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post fields: %s", curl_easy_strerror(err
));
309 curl_formfree(http_post_data
[0]);
319 FOREACH_HASH_VAL(pos
, files
, data
) {
320 zval
**file
, **type
, **name
;
322 if (Z_TYPE_PP(data
) != IS_ARRAY
) {
323 http_error(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Unrecognized type of post file array entry");
324 } else if ( SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) ||
325 SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) ||
326 SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
327 http_error(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Post file array entry misses either 'name', 'type' or 'file' entry");
329 CURLcode err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
330 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
331 CURLFORM_FILE
, Z_STRVAL_PP(file
),
332 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
335 if (CURLE_OK
!= err
) {
336 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post files: %s", curl_easy_strerror(err
));
337 curl_formfree(http_post_data
[0]);
343 body
->type
= HTTP_REQUEST_BODY_CURLPOST
;
344 body
->data
= http_post_data
[0];
351 if (SUCCESS
!= http_urlencode_hash_ex(fields
, 1, NULL
, 0, &encoded
, &encoded_len
)) {
352 http_error(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post data");
356 body
->type
= HTTP_REQUEST_BODY_CSTRING
;
357 body
->data
= encoded
;
358 body
->size
= encoded_len
;
365 /* {{{ void http_request_body_dtor(http_request_body *) */
366 PHP_HTTP_API
void _http_request_body_dtor(http_request_body
*body TSRMLS_DC
)
371 case HTTP_REQUEST_BODY_CSTRING
:
377 case HTTP_REQUEST_BODY_CURLPOST
:
378 curl_formfree(body
->data
);
381 case HTTP_REQUEST_BODY_UPLOADFILE
:
382 php_stream_close(body
->data
);
389 /* {{{ void http_request_body_free(http_request_body *) */
390 PHP_HTTP_API
void _http_request_body_free(http_request_body
**body TSRMLS_DC
)
393 http_request_body_dtor(*body
);
400 /* {{{ STATUS http_request_init(CURL *, http_request_method, char *, http_request_body *, HashTable *) */
401 PHP_HTTP_API STATUS
_http_request_init(CURL
*ch
, http_request_method meth
, char *url
, http_request_body
*body
, HashTable
*options TSRMLS_DC
)
404 zend_bool range_req
= 0;
406 /* reset CURL handle */
407 #ifdef HAVE_CURL_EASY_RESET
410 http_curl_defaults(ch
);
414 HTTP_CURL_OPT(URL
, http_request_data_copy(COPY_STRING
, url
));
417 HTTP_CURL_OPT(HEADER
, 0);
418 HTTP_CURL_OPT(FILETIME
, 1);
419 HTTP_CURL_OPT(AUTOREFERER
, 1);
420 HTTP_CURL_OPT(READFUNCTION
, http_curl_read_callback
);
421 /* we'll get all data through the debug function */
422 HTTP_CURL_OPT(WRITEFUNCTION
, http_curl_dummy_callback
);
423 HTTP_CURL_OPT(HEADERFUNCTION
, NULL
);
425 HTTP_CURL_OPT(VERBOSE
, 1);
426 HTTP_CURL_OPT(DEBUGFUNCTION
, http_curl_raw_callback
);
429 HTTP_CURL_OPT(NOSIGNAL
, 1);
432 /* progress callback */
433 if ((zoption
= http_curl_getopt(options
, "onprogress", 0))) {
434 HTTP_CURL_OPT(NOPROGRESS
, 0);
435 HTTP_CURL_OPT(PROGRESSFUNCTION
, http_curl_progress_callback
);
436 HTTP_CURL_OPT(PROGRESSDATA
, http_request_callback_data(zoption
));
440 if ((zoption
= http_curl_getopt(options
, "proxyhost", IS_STRING
))) {
441 HTTP_CURL_OPT(PROXY
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
443 if ((zoption
= http_curl_getopt(options
, "proxyport", IS_LONG
))) {
444 HTTP_CURL_OPT(PROXYPORT
, Z_LVAL_P(zoption
));
447 if ((zoption
= http_curl_getopt(options
, "proxyauth", IS_STRING
))) {
448 HTTP_CURL_OPT(PROXYUSERPWD
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
451 if ((zoption
= http_curl_getopt(options
, "proxyauthtype", IS_LONG
))) {
452 HTTP_CURL_OPT(PROXYAUTH
, Z_LVAL_P(zoption
));
456 /* outgoing interface */
457 if ((zoption
= http_curl_getopt(options
, "interface", IS_STRING
))) {
458 HTTP_CURL_OPT(INTERFACE
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
462 if ((zoption
= http_curl_getopt(options
, "port", IS_LONG
))) {
463 HTTP_CURL_OPT(PORT
, Z_LVAL_P(zoption
));
467 if ((zoption
= http_curl_getopt(options
, "httpauth", IS_STRING
))) {
468 HTTP_CURL_OPT(USERPWD
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
470 if ((zoption
= http_curl_getopt(options
, "httpauthtype", IS_LONG
))) {
471 HTTP_CURL_OPT(HTTPAUTH
, Z_LVAL_P(zoption
));
474 /* compress, empty string enables all supported if libcurl was build with zlib support */
475 if ((zoption
= http_curl_getopt(options
, "compress", IS_BOOL
)) && Z_LVAL_P(zoption
)) {
476 #ifdef HTTP_HAVE_CURL_ZLIB
477 HTTP_CURL_OPT(ENCODING
, "");
479 HTTP_CURL_OPT(ENCODING
, "gzip;q=1.0, deflate;q=0.5, *;q=0.1");
483 /* redirects, defaults to 0 */
484 if ((zoption
= http_curl_getopt(options
, "redirect", IS_LONG
))) {
485 HTTP_CURL_OPT(FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
486 HTTP_CURL_OPT(MAXREDIRS
, Z_LVAL_P(zoption
));
487 if ((zoption
= http_curl_getopt(options
, "unrestrictedauth", IS_BOOL
))) {
488 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
493 if ((zoption
= http_curl_getopt(options
, "referer", IS_STRING
))) {
494 HTTP_CURL_OPT(REFERER
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
497 /* useragent, default "PECL::HTTP/version (PHP/version)" */
498 if ((zoption
= http_curl_getopt(options
, "useragent", IS_STRING
))) {
499 HTTP_CURL_OPT(USERAGENT
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
502 /* additional headers, array('name' => 'value') */
503 if ((zoption
= http_curl_getopt(options
, "headers", IS_ARRAY
))) {
507 struct curl_slist
*headers
= NULL
;
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};
514 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
515 headers
= curl_slist_append(headers
, http_request_data_copy(COPY_STRING
, header
));
524 HTTP_CURL_OPT(HTTPHEADER
, http_request_data_copy(COPY_SLIST
, headers
));
528 /* cookies, array('name' => 'value') */
529 if ((zoption
= http_curl_getopt(options
, "cookies", IS_ARRAY
))) {
530 char *cookie_key
= NULL
;
531 ulong cookie_idx
= 0;
533 phpstr
*qstr
= phpstr_new();
535 FOREACH_KEY(pos
, zoption
, cookie_key
, cookie_idx
) {
538 if (SUCCESS
== zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption
), (void **) &cookie_val
, &pos
)) {
539 phpstr_appendf(qstr
, "%s=%s; ", cookie_key
, Z_STRVAL_PP(cookie_val
));
549 HTTP_CURL_OPT(COOKIE
, http_request_data_copy(COPY_STRING
, qstr
->data
));
554 /* session cookies */
555 if ((zoption
= http_curl_getopt(options
, "cookiesession", IS_BOOL
))) {
556 if (Z_LVAL_P(zoption
)) {
557 /* accept cookies for this session */
558 HTTP_CURL_OPT(COOKIEFILE
, "");
560 /* reset session cookies */
561 HTTP_CURL_OPT(COOKIESESSION
, 1);
565 /* cookiestore, read initial cookies from that file and store cookies back into that file */
566 if ((zoption
= http_curl_getopt(options
, "cookiestore", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
567 HTTP_CURL_OPT(COOKIEFILE
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
568 HTTP_CURL_OPT(COOKIEJAR
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
572 if ((zoption
= http_curl_getopt(options
, "resume", IS_LONG
)) && (Z_LVAL_P(zoption
) != 0)) {
574 HTTP_CURL_OPT(RESUME_FROM
, Z_LVAL_P(zoption
));
578 if ((zoption
= http_curl_getopt(options
, "maxfilesize", IS_LONG
))) {
579 HTTP_CURL_OPT(MAXFILESIZE
, Z_LVAL_P(zoption
));
583 if ((zoption
= http_curl_getopt(options
, "lastmodified", IS_LONG
))) {
584 if (Z_LVAL_P(zoption
)) {
585 if (Z_LVAL_P(zoption
) > 0) {
586 HTTP_CURL_OPT(TIMEVALUE
, Z_LVAL_P(zoption
));
588 HTTP_CURL_OPT(TIMEVALUE
, time(NULL
) + Z_LVAL_P(zoption
));
590 HTTP_CURL_OPT(TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
592 HTTP_CURL_OPT(TIMECONDITION
, CURL_TIMECOND_NONE
);
596 /* timeout, defaults to 0 */
597 if ((zoption
= http_curl_getopt(options
, "timeout", IS_LONG
))) {
598 HTTP_CURL_OPT(TIMEOUT
, Z_LVAL_P(zoption
));
601 /* connecttimeout, defaults to 3 */
602 if ((zoption
= http_curl_getopt(options
, "connecttimeout", IS_LONG
))) {
603 HTTP_CURL_OPT(CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
607 if ((zoption
= http_curl_getopt(options
, "ssl", IS_ARRAY
))) {
613 FOREACH_KEYVAL(pos
, zoption
, key
, idx
, param
) {
615 HTTP_CURL_OPT_SSL_STRING(CERT
);
616 HTTP_CURL_OPT_SSL_STRING(CERTTYPE
);
617 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD
);
619 HTTP_CURL_OPT_SSL_STRING(KEY
);
620 HTTP_CURL_OPT_SSL_STRING(KEYTYPE
);
621 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD
);
623 HTTP_CURL_OPT_SSL_STRING(ENGINE
);
624 HTTP_CURL_OPT_SSL_LONG(VERSION
);
626 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER
);
627 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST
);
628 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST
);
630 HTTP_CURL_OPT_STRING(CAINFO
);
631 HTTP_CURL_OPT_STRING(CAPATH
);
632 HTTP_CURL_OPT_STRING(RANDOM_FILE
);
633 HTTP_CURL_OPT_STRING(EGDSOCKET
);
645 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1);
649 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
653 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
657 curl_easy_setopt(ch
, CURLOPT_UPLOAD
, 1);
661 if (http_request_method_exists(0, meth
, NULL
)) {
662 curl_easy_setopt(ch
, CURLOPT_CUSTOMREQUEST
, http_request_method_name(meth
));
664 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unsupported request method: %d", meth
);
670 /* attach request body */
671 if (body
&& (meth
!= HTTP_GET
) && (meth
!= HTTP_HEAD
)) {
674 case HTTP_REQUEST_BODY_CSTRING
:
675 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, body
->data
);
676 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, body
->size
);
679 case HTTP_REQUEST_BODY_CURLPOST
:
680 curl_easy_setopt(ch
, CURLOPT_HTTPPOST
, (struct curl_httppost
*) body
->data
);
683 case HTTP_REQUEST_BODY_UPLOADFILE
:
684 curl_easy_setopt(ch
, CURLOPT_READDATA
, http_request_callback_data(body
));
685 curl_easy_setopt(ch
, CURLOPT_INFILESIZE
, body
->size
);
689 /* shouldn't ever happen */
690 http_error_ex(HE_ERROR
, 0, "Unknown request body type: %d", body
->type
);
700 /* {{{ void http_request_conv(CURL *, phpstr *, phpstr *) */
701 void _http_request_conv(CURL
*ch
, phpstr
* response
, phpstr
*request TSRMLS_DC
)
703 http_request_conv
*conv
= emalloc(sizeof(http_request_conv
));
704 conv
->response
= response
;
705 conv
->request
= request
;
706 conv
->last_info
= -1;
707 HTTP_CURL_OPT(DEBUGDATA
, http_request_callback_data(http_request_data_copy(COPY_CONV
, conv
)));
711 /* {{{ STATUS http_request_exec(CURL *, HashTable *) */
712 PHP_HTTP_API STATUS
_http_request_exec(CURL
*ch
, HashTable
*info
, phpstr
*response
, phpstr
*request TSRMLS_DC
)
716 http_request_conv(ch
, response
, request
);
718 /* perform request */
719 if (CURLE_OK
!= (result
= curl_easy_perform(ch
))) {
720 http_error(HE_WARNING
, HTTP_E_REQUEST
, curl_easy_strerror(result
));
724 http_request_info(ch
, info
);
726 /* always succeeds */
731 /* {{{ void http_request_info(CURL *, HashTable *) */
732 PHP_HTTP_API
void _http_request_info(CURL
*ch
, HashTable
*info TSRMLS_DC
)
735 INIT_ZARR(array
, info
);
737 HTTP_CURL_INFO(EFFECTIVE_URL
);
738 HTTP_CURL_INFO(RESPONSE_CODE
);
739 HTTP_CURL_INFO_EX(HTTP_CONNECTCODE
, connect_code
);
740 HTTP_CURL_INFO(FILETIME
);
741 HTTP_CURL_INFO(TOTAL_TIME
);
742 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
743 HTTP_CURL_INFO(CONNECT_TIME
);
744 HTTP_CURL_INFO(PRETRANSFER_TIME
);
745 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
746 HTTP_CURL_INFO(REDIRECT_TIME
);
747 HTTP_CURL_INFO(REDIRECT_COUNT
);
748 HTTP_CURL_INFO(SIZE_UPLOAD
);
749 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
750 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
751 HTTP_CURL_INFO(SPEED_UPLOAD
);
752 HTTP_CURL_INFO(HEADER_SIZE
);
753 HTTP_CURL_INFO(REQUEST_SIZE
);
754 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
755 HTTP_CURL_INFO(SSL_ENGINES
);
756 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
757 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
758 HTTP_CURL_INFO(CONTENT_TYPE
);
759 /*HTTP_CURL_INFO(PRIVATE);*/
760 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
761 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
762 /*HTTP_CURL_INFO(OS_ERRNO);*/
763 HTTP_CURL_INFO(NUM_CONNECTS
);
764 #if LIBCURL_VERSION_NUM >= 0x070e01
765 HTTP_CURL_INFO_EX(COOKIELIST
, cookies
);
770 /* {{{ STATUS http_request_ex(CURL *, http_request_method, char *, http_request_body, HashTable, HashTable, phpstr *) */
771 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
)
774 zend_bool clean_curl
= !ch
;
776 HTTP_CHECK_CURL_INIT(ch
, curl_easy_init(), return FAILURE
);
778 status
= ((SUCCESS
== http_request_init(ch
, meth
, url
, body
, options
)) &&
779 (SUCCESS
== http_request_exec(ch
, info
, response
, NULL
))) ? SUCCESS
: FAILURE
;
782 curl_easy_cleanup(ch
);
788 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
789 static size_t http_curl_read_callback(void *data
, size_t len
, size_t n
, void *s
)
791 HTTP_REQUEST_CALLBACK_DATA(s
, http_request_body
*, body
);
793 if (body
->type
!= HTTP_REQUEST_BODY_UPLOADFILE
) {
796 return php_stream_read((php_stream
*) body
->data
, data
, len
* n
);
800 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
801 static int http_curl_progress_callback(void *data
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
803 zval
*params_pass
[4], params_local
[4], retval
;
804 HTTP_REQUEST_CALLBACK_DATA(data
, zval
*, func
);
806 params_pass
[0] = ¶ms_local
[0];
807 params_pass
[1] = ¶ms_local
[1];
808 params_pass
[2] = ¶ms_local
[2];
809 params_pass
[3] = ¶ms_local
[3];
811 INIT_PZVAL(params_pass
[0]);
812 INIT_PZVAL(params_pass
[1]);
813 INIT_PZVAL(params_pass
[2]);
814 INIT_PZVAL(params_pass
[3]);
815 ZVAL_DOUBLE(params_pass
[0], dltotal
);
816 ZVAL_DOUBLE(params_pass
[1], dlnow
);
817 ZVAL_DOUBLE(params_pass
[2], ultotal
);
818 ZVAL_DOUBLE(params_pass
[3], ulnow
);
820 return call_user_function(EG(function_table
), NULL
, func
, &retval
, 4, params_pass TSRMLS_CC
);
824 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
825 static int http_curl_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
827 HTTP_REQUEST_CALLBACK_DATA(ctx
, http_request_conv
*, conv
);
831 case CURLINFO_DATA_IN
:
832 if (conv
->response
&& conv
->last_info
== CURLINFO_HEADER_IN
) {
833 phpstr_appends(conv
->response
, HTTP_CRLF
);
835 case CURLINFO_HEADER_IN
:
836 if (conv
->response
) {
837 phpstr_append(conv
->response
, data
, length
);
840 case CURLINFO_DATA_OUT
:
841 if (conv
->request
&& conv
->last_info
== CURLINFO_HEADER_OUT
) {
842 phpstr_appends(conv
->request
, HTTP_CRLF
);
844 case CURLINFO_HEADER_OUT
:
846 phpstr_append(conv
->request
, data
, length
);
851 fprintf(stderr
, "## ", type
);
853 fprintf(stderr
, "%s", data
);
856 for (i
= 1; i
<= length
; ++i
) {
857 fprintf(stderr
, "%02X ", data
[i
-1] & 0xFF);
859 fprintf(stderr
, "\n## ");
862 fprintf(stderr
, "\n");
864 if (data
[length
-1] != 0xa) {
865 fprintf(stderr
, "\n");
872 conv
->last_info
= type
;
878 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
879 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
)
883 if (!options
|| (SUCCESS
!= zend_hash_find(options
, key
, keylen
, (void **) &zoption
))) {
887 if (Z_TYPE_PP(zoption
) != type
) {
890 case IS_BOOL
: convert_to_boolean_ex(zoption
); break;
891 case IS_LONG
: convert_to_long_ex(zoption
); break;
892 case IS_DOUBLE
: convert_to_double_ex(zoption
); break;
893 case IS_STRING
: convert_to_string_ex(zoption
); break;
894 case IS_ARRAY
: convert_to_array_ex(zoption
); break;
895 case IS_OBJECT
: convert_to_object_ex(zoption
); break;
905 #ifdef HTTP_NEED_OPENSSL_TSL
907 static MUTEX_T
*http_openssl_tsl
= NULL
;
909 static void http_ssl_lock(int mode
, int n
, const char * file
, int line
)
911 if (mode
& CRYPTO_LOCK
) {
912 tsrm_mutex_lock(http_openssl_tsl
[n
]);
914 tsrm_mutex_unlock(http_openssl_tsl
[n
]);
918 static ulong
http_ssl_id(void)
920 return (ulong
) tsrm_thread_id();
923 static inline void http_ssl_init(void)
925 int i
, c
= CRYPTO_num_locks();
927 http_openssl_tsl
= malloc(c
* sizeof(MUTEX_T
));
929 for (i
= 0; i
< c
; ++i
) {
930 http_openssl_tsl
[i
] = tsrm_mutex_alloc();
933 CRYPTO_set_id_callback(http_ssl_id
);
934 CRYPTO_set_locking_callback(http_ssl_lock
);
937 static inline void http_ssl_cleanup(void)
939 if (http_openssl_tsl
) {
940 int i
, c
= CRYPTO_num_locks();
942 CRYPTO_set_id_callback(NULL
);
943 CRYPTO_set_locking_callback(NULL
);
945 for (i
= 0; i
< c
; ++i
) {
946 tsrm_mutex_free(http_openssl_tsl
[i
]);
949 free(http_openssl_tsl
);
950 http_openssl_tsl
= NULL
;
953 #endif /* HTTP_NEED_OPENSSL_TSL */
956 #ifdef HTTP_NEED_GNUTLS_TSL
958 static int http_ssl_mutex_create(void **m
)
960 if (*((MUTEX_T
*) m
) = tsrm_mutex_alloc()) {
967 static int http_ssl_mutex_destroy(void **m
)
969 tsrm_mutex_free(*((MUTEX_T
*) m
));
973 static int http_ssl_mutex_lock(void **m
)
975 return tsrm_mutex_lock(*((MUTEX_T
*) m
));
978 static int http_ssl_mutex_unlock(void **m
)
980 return tsrm_mutex_unlock(*((MUTEX_T
*) m
));
983 static struct gcry_thread_cbs http_gnutls_tsl
= {
984 GCRY_THREAD_OPTIONS_USER
,
986 http_ssl_mutex_create
,
987 http_ssl_mutex_destroy
,
989 http_ssl_mutex_unlock
992 static inline void http_ssl_init(void)
994 gcry_control(GCRYCTL_SET_THREAD_CBS
, &http_gnutls_tsl
);
997 static inline void http_ssl_cleanup(void)
1001 #endif /* HTTP_NEED_GNUTLS_TSL */
1004 /* {{{ http_curl_defaults(CURL *) */
1005 static inline void _http_curl_defaults(CURL
*ch
)
1007 HTTP_CURL_OPT(URL
, NULL
);
1008 HTTP_CURL_OPT(NOPROGRESS
, 1);
1009 HTTP_CURL_OPT(PROXY
, NULL
);
1010 HTTP_CURL_OPT(PROXYPORT
, 0);
1011 HTTP_CURL_OPT(PROXYUSERPWD
, NULL
);
1012 HTTP_CURL_OPT(PROXYAUTH
, 0);
1013 HTTP_CURL_OPT(INTERFACE
, NULL
);
1014 HTTP_CURL_OPT(PORT
, 0);
1015 HTTP_CURL_OPT(USERPWD
, NULL
);
1016 HTTP_CURL_OPT(HTTPAUTH
, 0);
1017 HTTP_CURL_OPT(ENCODING
, 0);
1018 HTTP_CURL_OPT(FOLLOWLOCATION
, 0);
1019 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, 0);
1020 HTTP_CURL_OPT(REFERER
, NULL
);
1021 HTTP_CURL_OPT(USERAGENT
, "PECL::HTTP/" PHP_EXT_HTTP_VERSION
" (PHP/" PHP_VERSION
")");
1022 HTTP_CURL_OPT(HTTPHEADER
, NULL
);
1023 HTTP_CURL_OPT(COOKIE
, NULL
);
1024 HTTP_CURL_OPT(COOKIEFILE
, NULL
);
1025 HTTP_CURL_OPT(COOKIEJAR
, NULL
);
1026 HTTP_CURL_OPT(RESUME_FROM
, 0);
1027 HTTP_CURL_OPT(MAXFILESIZE
, 0);
1028 HTTP_CURL_OPT(TIMECONDITION
, 0);
1029 HTTP_CURL_OPT(TIMEVALUE
, 0);
1030 HTTP_CURL_OPT(TIMEOUT
, 0);
1031 HTTP_CURL_OPT(CONNECTTIMEOUT
, 3);
1032 HTTP_CURL_OPT(SSLCERT
, NULL
);
1033 HTTP_CURL_OPT(SSLCERTTYPE
, NULL
);
1034 HTTP_CURL_OPT(SSLCERTPASSWD
, NULL
);
1035 HTTP_CURL_OPT(SSLKEY
, NULL
);
1036 HTTP_CURL_OPT(SSLKEYTYPE
, NULL
);
1037 HTTP_CURL_OPT(SSLKEYPASSWD
, NULL
);
1038 HTTP_CURL_OPT(SSLENGINE
, NULL
);
1039 HTTP_CURL_OPT(SSLVERSION
, 0);
1040 HTTP_CURL_OPT(SSL_VERIFYPEER
, 0);
1041 HTTP_CURL_OPT(SSL_VERIFYHOST
, 0);
1042 HTTP_CURL_OPT(SSL_CIPHER_LIST
, NULL
);
1043 HTTP_CURL_OPT(CAINFO
, NULL
);
1044 HTTP_CURL_OPT(CAPATH
, NULL
);
1045 HTTP_CURL_OPT(RANDOM_FILE
, NULL
);
1046 HTTP_CURL_OPT(EGDSOCKET
, NULL
);
1047 HTTP_CURL_OPT(POSTFIELDS
, NULL
);
1048 HTTP_CURL_OPT(POSTFIELDSIZE
, 0);
1049 HTTP_CURL_OPT(HTTPPOST
, NULL
);
1050 HTTP_CURL_OPT(READDATA
, NULL
);
1051 HTTP_CURL_OPT(INFILESIZE
, 0);
1055 #endif /* HTTP_HAVE_CURL */
1062 * vim600: noet sw=4 ts=4 fdm=marker
1063 * vim<600: noet sw=4 ts=4