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 +--------------------------------------------------------------------+
23 #include "php_http_std_defs.h"
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"
29 # include "php_http_request_object.h"
32 #include "phpstr/phpstr.h"
35 # include <winsock2.h>
38 #include <curl/curl.h>
40 /* {{{ cruft for thread safe SSL crypto locks */
41 #if defined(ZTS) && defined(HTTP_HAVE_SSL)
43 # define HTTP_NEED_SSL_TSL
44 # define HTTP_NEED_OPENSSL_TSL
45 # include <openssl/crypto.h>
46 # else /* !PHP_WIN32 */
47 # if defined(HTTP_HAVE_OPENSSL)
48 # if defined(HAVE_OPENSSL_CRYPTO_H)
49 # define HTTP_NEED_SSL_TSL
50 # define HTTP_NEED_OPENSSL_TSL
51 # include <openssl/crypto.h>
54 "libcurl was compiled with OpenSSL support, but configure could not find " \
55 "openssl/crypto.h; thus no SSL crypto locking callbacks will be set, which may " \
56 "cause random crashes on SSL requests"
58 # elif defined(HTTP_HAVE_GNUTLS)
59 # if defined(HAVE_GCRYPT_H)
60 # define HTTP_NEED_SSL_TSL
61 # define HTTP_NEED_GNUTLS_TSL
65 "libcurl was compiled with GnuTLS support, but configure could not find " \
66 "gcrypt.h; thus no SSL crypto locking callbacks will be set, which may " \
67 "cause random crashes on SSL requests"
71 "libcurl was compiled with SSL support, but configure could not determine which" \
72 "library was used; thus no SSL crypto locking callbacks will be set, which may " \
73 "cause random crashes on SSL requests"
74 # endif /* HTTP_HAVE_OPENSSL || HTTP_HAVE_GNUTLS */
75 # endif /* PHP_WIN32 */
76 #endif /* ZTS && HTTP_HAVE_SSL */
79 ZEND_EXTERN_MODULE_GLOBALS(http
);
81 #ifdef HTTP_NEED_SSL_TSL
82 static inline void http_ssl_init(void);
83 static inline void http_ssl_cleanup(void);
86 PHP_MINIT_FUNCTION(http_request
)
88 #ifdef HTTP_NEED_SSL_TSL
92 if (CURLE_OK
!= curl_global_init(CURL_GLOBAL_ALL
)) {
96 #if LIBCURL_VERSION_NUM >= 0x070a05
97 HTTP_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC
);
98 HTTP_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST
);
99 HTTP_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM
);
100 HTTP_LONG_CONSTANT("HTTP_AUTH_ANY", CURLAUTH_ANY
);
101 #endif /* LIBCURL_VERSION_NUM */
106 PHP_MSHUTDOWN_FUNCTION(http_request
)
108 curl_global_cleanup();
109 #ifdef HTTP_NEED_SSL_TSL
115 #ifndef HAVE_CURL_EASY_STRERROR
116 # define curl_easy_strerror(code) HTTP_G(request).error
119 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
120 #define HTTP_CURL_INFO_EX(I, X) \
121 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
123 case CURLINFO_STRING: \
126 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
127 add_assoc_string(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
132 case CURLINFO_DOUBLE: \
135 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
136 add_assoc_double(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), d); \
141 case CURLINFO_LONG: \
144 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
145 add_assoc_long(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), l); \
151 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
152 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
153 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
154 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
155 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
156 if (!strcasecmp(key, #keyname)) { \
157 convert_to_string_ex(param); \
158 HTTP_CURL_OPT(optname, http_request_data_copy(COPY_STRING, Z_STRVAL_PP(param))); \
162 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
163 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
164 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
165 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
166 if (!strcasecmp(key, #keyname)) { \
167 convert_to_long_ex(param); \
168 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
173 #define http_curl_getopt(o, k, t) _http_curl_getopt_ex((o), (k), sizeof(k), (t) TSRMLS_CC)
174 #define http_curl_getopt_ex(o, k, l, t) _http_curl_getopt_ex((o), (k), (l), (t) TSRMLS_CC)
175 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
);
176 #define http_curl_defaults(ch) _http_curl_defaults((ch))
177 static inline void _http_curl_defaults(CURL
*ch
);
178 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
179 static int http_curl_progress_callback(void *, double, double, double, double);
180 static int http_curl_raw_callback(CURL
*, curl_infotype
, char *, size_t, void *);
181 static int http_curl_dummy_callback(char *data
, size_t n
, size_t l
, void *s
) { return n
*l
; }
183 /* {{{ http_request_callback_ctx http_request_callback_data(void *) */
184 http_request_callback_ctx
*_http_request_callback_data_ex(void *data
, zend_bool cpy TSRMLS_DC
)
186 http_request_callback_ctx
*ctx
= emalloc(sizeof(http_request_callback_ctx
));
188 TSRMLS_SET_CTX(ctx
->tsrm_ctx
);
192 return http_request_data_copy(COPY_CONTEXT
, ctx
);
199 /* {{{ void *http_request_data_copy(int, void *) */
200 void *_http_request_data_copy(int type
, void *data TSRMLS_DC
)
206 char *new_str
= estrdup(data
);
207 zend_llist_add_element(&HTTP_G(request
).copies
.strings
, &new_str
);
213 zend_llist_add_element(&HTTP_G(request
).copies
.slists
, &data
);
219 zend_llist_add_element(&HTTP_G(request
).copies
.contexts
, &data
);
225 zend_llist_add_element(&HTTP_G(request
).copies
.convs
, &data
);
237 /* {{{ void http_request_data_free_string(char **) */
238 void _http_request_data_free_string(void *string
)
240 efree(*((char **)string
));
244 /* {{{ void http_request_data_free_slist(struct curl_slist **) */
245 void _http_request_data_free_slist(void *list
)
247 curl_slist_free_all(*((struct curl_slist
**) list
));
251 /* {{{ _http_request_data_free_context(http_request_callback_ctx **) */
252 void _http_request_data_free_context(void *context
)
254 efree(*((http_request_callback_ctx
**) context
));
258 /* {{{ _http_request_data_free_conv(http_request_conv **) */
259 void _http_request_data_free_conv(void *conv
)
261 efree(*((http_request_conv
**) conv
));
265 /* {{{ http_request_body *http_request_body_new() */
266 PHP_HTTP_API http_request_body
*_http_request_body_new(TSRMLS_D
)
268 http_request_body
*body
= ecalloc(1, sizeof(http_request_body
));
273 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
274 PHP_HTTP_API STATUS
_http_request_body_fill(http_request_body
*body
, HashTable
*fields
, HashTable
*files TSRMLS_DC
)
276 if (files
&& (zend_hash_num_elements(files
) > 0)) {
280 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
283 FOREACH_HASH_KEYVAL(fields
, key
, idx
, data
) {
286 convert_to_string_ex(data
);
287 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
288 CURLFORM_COPYNAME
, key
,
289 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
290 CURLFORM_CONTENTSLENGTH
, (long) Z_STRLEN_PP(data
),
293 if (CURLE_OK
!= err
) {
294 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post fields: %s", curl_easy_strerror(err
));
295 curl_formfree(http_post_data
[0]);
305 FOREACH_HASH_VAL(files
, data
) {
306 zval
**file
, **type
, **name
;
308 if ( SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) ||
309 SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) ||
310 SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
311 http_error(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Post file array entry misses either 'name', 'type' or 'file' entry");
313 CURLcode err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
314 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
315 CURLFORM_FILE
, Z_STRVAL_PP(file
),
316 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
319 if (CURLE_OK
!= err
) {
320 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post files: %s", curl_easy_strerror(err
));
321 curl_formfree(http_post_data
[0]);
327 body
->type
= HTTP_REQUEST_BODY_CURLPOST
;
328 body
->data
= http_post_data
[0];
335 if (SUCCESS
!= http_urlencode_hash_ex(fields
, 1, NULL
, 0, &encoded
, &encoded_len
)) {
336 http_error(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post data");
340 body
->type
= HTTP_REQUEST_BODY_CSTRING
;
341 body
->data
= encoded
;
342 body
->size
= encoded_len
;
349 /* {{{ void http_request_body_dtor(http_request_body *) */
350 PHP_HTTP_API
void _http_request_body_dtor(http_request_body
*body TSRMLS_DC
)
355 case HTTP_REQUEST_BODY_CSTRING
:
361 case HTTP_REQUEST_BODY_CURLPOST
:
362 curl_formfree(body
->data
);
365 case HTTP_REQUEST_BODY_UPLOADFILE
:
366 php_stream_close(body
->data
);
373 /* {{{ void http_request_body_free(http_request_body *) */
374 PHP_HTTP_API
void _http_request_body_free(http_request_body
*body TSRMLS_DC
)
377 http_request_body_dtor(body
);
383 /* {{{ STATUS http_request_init(CURL *, http_request_method, char *, http_request_body *, HashTable *) */
384 PHP_HTTP_API STATUS
_http_request_init(CURL
*ch
, http_request_method meth
, char *url
, http_request_body
*body
, HashTable
*options TSRMLS_DC
)
387 zend_bool range_req
= 0;
389 /* reset CURL handle */
390 #ifdef HAVE_CURL_EASY_RESET
393 http_curl_defaults(ch
);
397 HTTP_CURL_OPT(URL
, http_request_data_copy(COPY_STRING
, url
));
400 HTTP_CURL_OPT(HEADER
, 0);
401 HTTP_CURL_OPT(FILETIME
, 1);
402 HTTP_CURL_OPT(AUTOREFERER
, 1);
403 HTTP_CURL_OPT(READFUNCTION
, http_curl_read_callback
);
404 /* we'll get all data through the debug function */
405 HTTP_CURL_OPT(WRITEFUNCTION
, http_curl_dummy_callback
);
406 HTTP_CURL_OPT(HEADERFUNCTION
, NULL
);
408 HTTP_CURL_OPT(VERBOSE
, 1);
409 HTTP_CURL_OPT(DEBUGFUNCTION
, http_curl_raw_callback
);
411 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
412 HTTP_CURL_OPT(NOSIGNAL
, 1);
414 #if LIBCURL_VERSION_NUM < 0x070c00
415 HTTP_CURL_OPT(ERRORBUFFER
, HTTP_G(request
).error
);
418 /* progress callback */
419 if (zoption
= http_curl_getopt(options
, "onprogress", 0)) {
420 HTTP_CURL_OPT(NOPROGRESS
, 0);
421 HTTP_CURL_OPT(PROGRESSFUNCTION
, http_curl_progress_callback
);
422 HTTP_CURL_OPT(PROGRESSDATA
, http_request_callback_data(zoption
));
426 if (zoption
= http_curl_getopt(options
, "proxyhost", IS_STRING
)) {
427 HTTP_CURL_OPT(PROXY
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
429 if (zoption
= http_curl_getopt(options
, "proxyport", IS_LONG
)) {
430 HTTP_CURL_OPT(PROXYPORT
, Z_LVAL_P(zoption
));
433 if (zoption
= http_curl_getopt(options
, "proxyauth", IS_STRING
)) {
434 HTTP_CURL_OPT(PROXYUSERPWD
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
436 #if LIBCURL_VERSION_NUM >= 0x070a07
438 if (zoption
= http_curl_getopt(options
, "proxyauthtype", IS_LONG
)) {
439 HTTP_CURL_OPT(PROXYAUTH
, Z_LVAL_P(zoption
));
444 /* outgoing interface */
445 if (zoption
= http_curl_getopt(options
, "interface", IS_STRING
)) {
446 HTTP_CURL_OPT(INTERFACE
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
450 if (zoption
= http_curl_getopt(options
, "port", IS_LONG
)) {
451 HTTP_CURL_OPT(PORT
, Z_LVAL_P(zoption
));
455 if (zoption
= http_curl_getopt(options
, "httpauth", IS_STRING
)) {
456 HTTP_CURL_OPT(USERPWD
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
458 #if LIBCURL_VERSION_NUM >= 0x070a06
459 if (zoption
= http_curl_getopt(options
, "httpauthtype", IS_LONG
)) {
460 HTTP_CURL_OPT(HTTPAUTH
, Z_LVAL_P(zoption
));
464 /* compress, empty string enables all supported if libcurl was build with zlib support */
465 if ((zoption
= http_curl_getopt(options
, "compress", IS_BOOL
)) && Z_LVAL_P(zoption
)) {
466 #if defined(HTTP_HAVE_ZLIB) || defined(HAVE_ZLIB)
467 HTTP_CURL_OPT(ENCODING
, "gzip;q=1.0, deflate;q=0.5, *;q=0");
469 HTTP_CURL_OPT(ENCODING
, "");
473 /* redirects, defaults to 0 */
474 if (zoption
= http_curl_getopt(options
, "redirect", IS_LONG
)) {
475 HTTP_CURL_OPT(FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
476 HTTP_CURL_OPT(MAXREDIRS
, Z_LVAL_P(zoption
));
477 if (zoption
= http_curl_getopt(options
, "unrestrictedauth", IS_BOOL
)) {
478 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
483 if (zoption
= http_curl_getopt(options
, "referer", IS_STRING
)) {
484 HTTP_CURL_OPT(REFERER
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
487 /* useragent, default "PECL::HTTP/version (PHP/version)" */
488 if (zoption
= http_curl_getopt(options
, "useragent", IS_STRING
)) {
489 HTTP_CURL_OPT(USERAGENT
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
492 /* additional headers, array('name' => 'value') */
493 if (zoption
= http_curl_getopt(options
, "headers", IS_ARRAY
)) {
496 struct curl_slist
*headers
= NULL
;
498 FOREACH_KEY(zoption
, header_key
, header_idx
) {
501 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &header_val
)) {
502 char header
[1024] = {0};
503 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
504 headers
= curl_slist_append(headers
, http_request_data_copy(COPY_STRING
, header
));
513 HTTP_CURL_OPT(HTTPHEADER
, http_request_data_copy(COPY_SLIST
, headers
));
517 /* cookies, array('name' => 'value') */
518 if (zoption
= http_curl_getopt(options
, "cookies", IS_ARRAY
)) {
519 char *cookie_key
= NULL
;
520 ulong cookie_idx
= 0;
521 phpstr
*qstr
= phpstr_new();
523 FOREACH_KEY(zoption
, cookie_key
, cookie_idx
) {
526 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &cookie_val
)) {
527 phpstr_appendf(qstr
, "%s=%s; ", cookie_key
, Z_STRVAL_PP(cookie_val
));
537 HTTP_CURL_OPT(COOKIE
, http_request_data_copy(COPY_STRING
, qstr
->data
));
542 /* session cookies */
543 if (zoption
= http_curl_getopt(options
, "cookiesession", IS_BOOL
)) {
544 if (Z_LVAL_P(zoption
)) {
545 /* accept cookies for this session */
546 HTTP_CURL_OPT(COOKIEFILE
, "");
548 /* reset session cookies */
549 HTTP_CURL_OPT(COOKIESESSION
, 1);
553 /* cookiestore, read initial cookies from that file and store cookies back into that file */
554 if ((zoption
= http_curl_getopt(options
, "cookiestore", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
555 HTTP_CURL_OPT(COOKIEFILE
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
556 HTTP_CURL_OPT(COOKIEJAR
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
560 if ((zoption
= http_curl_getopt(options
, "resume", IS_LONG
)) && (Z_LVAL_P(zoption
) != 0)) {
562 HTTP_CURL_OPT(RESUME_FROM
, Z_LVAL_P(zoption
));
566 if (zoption
= http_curl_getopt(options
, "maxfilesize", IS_LONG
)) {
567 HTTP_CURL_OPT(MAXFILESIZE
, Z_LVAL_P(zoption
));
571 if (zoption
= http_curl_getopt(options
, "lastmodified", IS_LONG
)) {
572 if (Z_LVAL_P(zoption
)) {
573 if (Z_LVAL_P(zoption
) > 0) {
574 HTTP_CURL_OPT(TIMEVALUE
, Z_LVAL_P(zoption
));
576 HTTP_CURL_OPT(TIMEVALUE
, time(NULL
) + Z_LVAL_P(zoption
));
578 HTTP_CURL_OPT(TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
580 HTTP_CURL_OPT(TIMECONDITION
, CURL_TIMECOND_NONE
);
584 /* timeout, defaults to 0 */
585 if (zoption
= http_curl_getopt(options
, "timeout", IS_LONG
)) {
586 HTTP_CURL_OPT(TIMEOUT
, Z_LVAL_P(zoption
));
589 /* connecttimeout, defaults to 3 */
590 if (zoption
= http_curl_getopt(options
, "connecttimeout", IS_LONG
)) {
591 HTTP_CURL_OPT(CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
595 if (zoption
= http_curl_getopt(options
, "ssl", IS_ARRAY
)) {
600 FOREACH_KEYVAL(zoption
, key
, idx
, param
) {
602 HTTP_CURL_OPT_SSL_STRING(CERT
);
603 #if LIBCURL_VERSION_NUM >= 0x070903
604 HTTP_CURL_OPT_SSL_STRING(CERTTYPE
);
606 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD
);
608 HTTP_CURL_OPT_SSL_STRING(KEY
);
609 HTTP_CURL_OPT_SSL_STRING(KEYTYPE
);
610 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD
);
612 HTTP_CURL_OPT_SSL_STRING(ENGINE
);
613 HTTP_CURL_OPT_SSL_LONG(VERSION
);
615 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER
);
616 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST
);
617 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST
);
620 HTTP_CURL_OPT_STRING(CAINFO
);
621 #if LIBCURL_VERSION_NUM >= 0x070908
622 HTTP_CURL_OPT_STRING(CAPATH
);
624 HTTP_CURL_OPT_STRING(RANDOM_FILE
);
625 HTTP_CURL_OPT_STRING(EGDSOCKET
);
637 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1);
641 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
645 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
649 curl_easy_setopt(ch
, CURLOPT_UPLOAD
, 1);
653 if (http_request_method_exists(0, meth
, NULL
)) {
654 curl_easy_setopt(ch
, CURLOPT_CUSTOMREQUEST
, http_request_method_name(meth
));
656 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unsupported request method: %d", meth
);
662 /* attach request body */
663 if (body
&& (meth
!= HTTP_GET
) && (meth
!= HTTP_HEAD
)) {
666 case HTTP_REQUEST_BODY_CSTRING
:
667 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, body
->data
);
668 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, body
->size
);
671 case HTTP_REQUEST_BODY_CURLPOST
:
672 curl_easy_setopt(ch
, CURLOPT_HTTPPOST
, (struct curl_httppost
*) body
->data
);
675 case HTTP_REQUEST_BODY_UPLOADFILE
:
676 curl_easy_setopt(ch
, CURLOPT_READDATA
, http_request_callback_data(body
));
677 curl_easy_setopt(ch
, CURLOPT_INFILESIZE
, body
->size
);
681 /* shouldn't ever happen */
682 http_error_ex(HE_ERROR
, 0, "Unknown request body type: %d", body
->type
);
692 /* {{{ void http_request_conv(CURL *, phpstr *, phpstr *) */
693 void _http_request_conv(CURL
*ch
, phpstr
* response
, phpstr
*request TSRMLS_DC
)
695 http_request_conv
*conv
= emalloc(sizeof(http_request_conv
));
696 conv
->response
= response
;
697 conv
->request
= request
;
698 conv
->last_info
= -1;
699 HTTP_CURL_OPT(DEBUGDATA
, http_request_callback_data(http_request_data_copy(COPY_CONV
, conv
)));
703 /* {{{ STATUS http_request_exec(CURL *, HashTable *) */
704 PHP_HTTP_API STATUS
_http_request_exec(CURL
*ch
, HashTable
*info
, phpstr
*response
, phpstr
*request TSRMLS_DC
)
708 http_request_conv(ch
, response
, request
);
710 /* perform request */
711 if (CURLE_OK
!= (result
= curl_easy_perform(ch
))) {
712 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "Could not perform request: %s", curl_easy_strerror(result
));
717 http_request_info(ch
, info
);
724 /* {{{ void http_request_info(CURL *, HashTable *) */
725 PHP_HTTP_API
void _http_request_info(CURL
*ch
, HashTable
*info TSRMLS_DC
)
728 INIT_ZARR(array
, info
);
730 HTTP_CURL_INFO(EFFECTIVE_URL
);
731 #if LIBCURL_VERSION_NUM >= 0x070a07
732 HTTP_CURL_INFO(RESPONSE_CODE
);
734 HTTP_CURL_INFO_EX(HTTP_CODE
, RESPONSE_CODE
);
736 HTTP_CURL_INFO(HTTP_CONNECTCODE
);
737 #if LIBCURL_VERSION_NUM >= 0x070500
738 HTTP_CURL_INFO(FILETIME
);
740 HTTP_CURL_INFO(TOTAL_TIME
);
741 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
742 HTTP_CURL_INFO(CONNECT_TIME
);
743 HTTP_CURL_INFO(PRETRANSFER_TIME
);
744 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
745 #if LIBCURL_VERSION_NUM >= 0x070907
746 HTTP_CURL_INFO(REDIRECT_TIME
);
747 HTTP_CURL_INFO(REDIRECT_COUNT
);
749 HTTP_CURL_INFO(SIZE_UPLOAD
);
750 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
751 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
752 HTTP_CURL_INFO(SPEED_UPLOAD
);
753 HTTP_CURL_INFO(HEADER_SIZE
);
754 HTTP_CURL_INFO(REQUEST_SIZE
);
755 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
756 #if LIBCURL_VERSION_NUM >= 0x070c03
757 /*HTTP_CURL_INFO(SSL_ENGINES); todo: CURLINFO_SLIST */
759 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
760 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
761 HTTP_CURL_INFO(CONTENT_TYPE
);
762 #if LIBCURL_VERSION_NUM >= 0x070a03
763 /*HTTP_CURL_INFO(PRIVATE);*/
765 #if LIBCURL_VERSION_NUM >= 0x070a08
766 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
767 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
769 #if LIBCURL_VERSION_NUM >= 0x070c02
770 /*HTTP_CURL_INFO(OS_ERRNO);*/
772 #if LIBCURL_VERSION_NUM >= 0x070c03
773 HTTP_CURL_INFO(NUM_CONNECTS
);
778 /* {{{ STATUS http_request_ex(CURL *, http_request_method, char *, http_request_body, HashTable, HashTable, phpstr *) */
779 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
)
782 zend_bool clean_curl
= !ch
;
784 HTTP_CHECK_CURL_INIT(ch
, curl_easy_init(), return FAILURE
);
786 status
= ((SUCCESS
== http_request_init(ch
, meth
, url
, body
, options
)) &&
787 (SUCCESS
== http_request_exec(ch
, info
, response
, NULL
))) ? SUCCESS
: FAILURE
;
790 curl_easy_cleanup(ch
);
796 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
797 static size_t http_curl_read_callback(void *data
, size_t len
, size_t n
, void *s
)
799 HTTP_REQUEST_CALLBACK_DATA(s
, http_request_body
*, body
);
801 if (body
->type
!= HTTP_REQUEST_BODY_UPLOADFILE
) {
804 return php_stream_read((php_stream
*) body
->data
, data
, len
* n
);
808 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
809 static int http_curl_progress_callback(void *data
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
811 zval
*params_pass
[4], params_local
[4], retval
;
812 HTTP_REQUEST_CALLBACK_DATA(data
, zval
*, func
);
814 params_pass
[0] = ¶ms_local
[0];
815 params_pass
[1] = ¶ms_local
[1];
816 params_pass
[2] = ¶ms_local
[2];
817 params_pass
[3] = ¶ms_local
[3];
819 INIT_PZVAL(params_pass
[0]);
820 INIT_PZVAL(params_pass
[1]);
821 INIT_PZVAL(params_pass
[2]);
822 INIT_PZVAL(params_pass
[3]);
823 ZVAL_DOUBLE(params_pass
[0], dltotal
);
824 ZVAL_DOUBLE(params_pass
[1], dlnow
);
825 ZVAL_DOUBLE(params_pass
[2], ultotal
);
826 ZVAL_DOUBLE(params_pass
[3], ulnow
);
828 return call_user_function(EG(function_table
), NULL
, func
, &retval
, 4, params_pass TSRMLS_CC
);
832 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
833 static int http_curl_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
835 HTTP_REQUEST_CALLBACK_DATA(ctx
, http_request_conv
*, conv
);
838 fprintf(stderr
, "DEBUG: %s\n", data
);
843 case CURLINFO_DATA_IN
:
844 if (conv
->response
&& conv
->last_info
== CURLINFO_HEADER_IN
) {
845 phpstr_appends(conv
->response
, HTTP_CRLF
);
847 case CURLINFO_HEADER_IN
:
848 if (conv
->response
) {
849 phpstr_append(conv
->response
, data
, length
);
852 case CURLINFO_DATA_OUT
:
853 if (conv
->request
&& conv
->last_info
== CURLINFO_HEADER_OUT
) {
854 phpstr_appends(conv
->request
, HTTP_CRLF
);
856 case CURLINFO_HEADER_OUT
:
858 phpstr_append(conv
->request
, data
, length
);
864 conv
->last_info
= type
;
870 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
871 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
)
875 if (!options
|| (SUCCESS
!= zend_hash_find(options
, key
, keylen
, (void **) &zoption
))) {
879 if (Z_TYPE_PP(zoption
) != type
) {
882 case IS_BOOL
: convert_to_boolean_ex(zoption
); break;
883 case IS_LONG
: convert_to_long_ex(zoption
); break;
884 case IS_DOUBLE
: convert_to_double_ex(zoption
); break;
885 case IS_STRING
: convert_to_string_ex(zoption
); break;
886 case IS_ARRAY
: convert_to_array_ex(zoption
); break;
887 case IS_OBJECT
: convert_to_object_ex(zoption
); break;
897 #ifdef HTTP_NEED_OPENSSL_TSL
899 static MUTEX_T
*http_openssl_tsl
= NULL
;
901 static void http_ssl_lock(int mode
, int n
, const char * file
, int line
)
903 if (mode
& CRYPTO_LOCK
) {
904 tsrm_mutex_lock(http_openssl_tsl
[n
]);
906 tsrm_mutex_unlock(http_openssl_tsl
[n
]);
910 static unsigned long http_ssl_id(void)
912 return (unsigned long) tsrm_thread_id();
915 static inline void http_ssl_init(void)
917 int i
, c
= CRYPTO_num_locks();
919 http_openssl_tsl
= malloc(c
* sizeof(MUTEX_T
));
921 for (i
= 0; i
< c
; ++i
) {
922 http_openssl_tsl
[i
] = tsrm_mutex_alloc();
925 CRYPTO_set_id_callback(http_ssl_id
);
926 CRYPTO_set_locking_callback(http_ssl_lock
);
929 static inline void http_ssl_cleanup(void)
931 if (http_openssl_tsl
) {
932 int i
, c
= CRYPTO_num_locks();
934 CRYPTO_set_id_callback(NULL
);
935 CRYPTO_set_locking_callback(NULL
);
937 for (i
= 0; i
< c
; ++i
) {
938 tsrm_mutex_free(http_openssl_tsl
[i
]);
941 free(http_openssl_tsl
);
942 http_openssl_tsl
= NULL
;
945 #endif /* HTTP_NEED_OPENSSL_TSL */
948 #ifdef HTTP_NEED_GNUTLS_TSL
950 static int http_ssl_mutex_create(void **m
)
952 if (*((MUTEX_T
*) m
) = tsrm_mutex_alloc()) {
959 static int http_ssl_mutex_destroy(void **m
)
961 tsrm_mutex_free(*((MUTEX_T
*) m
));
965 static int http_ssl_mutex_lock(void **m
)
967 return tsrm_mutex_lock(*((MUTEX_T
*) m
));
970 static int http_ssl_mutex_unlock(void **m
)
972 return tsrm_mutex_unlock(*((MUTEX_T
*) m
));
975 static struct gcry_thread_cbs http_gnutls_tsl
= {
976 GCRY_THREAD_OPTIONS_USER
,
978 http_ssl_mutex_create
,
979 http_ssl_mutex_destroy
,
981 http_ssl_mutex_unlock
984 static inline void http_ssl_init(void)
986 gcry_control(GCRYCTL_SET_THREAD_CBS
, &http_gnutls_tsl
);
989 static inline void http_ssl_cleanup(void)
993 #endif /* HTTP_NEED_GNUTLS_TSL */
996 /* {{{ http_curl_defaults(CURL *) */
997 static inline void _http_curl_defaults(CURL
*ch
)
999 HTTP_CURL_OPT(URL
, NULL
);
1000 HTTP_CURL_OPT(NOPROGRESS
, 1);
1001 HTTP_CURL_OPT(PROXY
, NULL
);
1002 HTTP_CURL_OPT(PROXYPORT
, 0);
1003 HTTP_CURL_OPT(PROXYUSERPWD
, NULL
);
1004 #if LIBCURL_VERSION_NUM >= 0x070a07
1005 HTTP_CURL_OPT(PROXYAUTH
, 0);
1007 HTTP_CURL_OPT(INTERFACE
, NULL
);
1008 HTTP_CURL_OPT(PORT
, 0);
1009 HTTP_CURL_OPT(USERPWD
, NULL
);
1010 #if LIBCURL_VERSION_NUM >= 0x070a06
1011 HTTP_CURL_OPT(HTTPAUTH
, 0);
1013 HTTP_CURL_OPT(ENCODING
, 0);
1014 HTTP_CURL_OPT(FOLLOWLOCATION
, 0);
1015 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, 0);
1016 HTTP_CURL_OPT(REFERER
, NULL
);
1017 HTTP_CURL_OPT(USERAGENT
, "PECL::HTTP/" HTTP_PEXT_VERSION
" (PHP/" PHP_VERSION
")");
1018 HTTP_CURL_OPT(HTTPHEADER
, NULL
);
1019 HTTP_CURL_OPT(COOKIE
, NULL
);
1020 HTTP_CURL_OPT(COOKIEFILE
, NULL
);
1021 HTTP_CURL_OPT(COOKIEJAR
, NULL
);
1022 HTTP_CURL_OPT(RESUME_FROM
, 0);
1023 HTTP_CURL_OPT(MAXFILESIZE
, 0);
1024 HTTP_CURL_OPT(TIMECONDITION
, 0);
1025 HTTP_CURL_OPT(TIMEVALUE
, 0);
1026 HTTP_CURL_OPT(TIMEOUT
, 0);
1027 HTTP_CURL_OPT(CONNECTTIMEOUT
, 3);
1028 HTTP_CURL_OPT(SSLCERT
, NULL
);
1029 #if LIBCURL_VERSION_NUM >= 0x070903
1030 HTTP_CURL_OPT(SSLCERTTYPE
, NULL
);
1032 HTTP_CURL_OPT(SSLCERTPASSWD
, NULL
);
1033 HTTP_CURL_OPT(SSLKEY
, NULL
);
1034 HTTP_CURL_OPT(SSLKEYTYPE
, NULL
);
1035 HTTP_CURL_OPT(SSLKEYPASSWD
, NULL
);
1036 HTTP_CURL_OPT(SSLENGINE
, NULL
);
1037 HTTP_CURL_OPT(SSLVERSION
, 0);
1038 HTTP_CURL_OPT(SSL_VERIFYPEER
, 0);
1039 HTTP_CURL_OPT(SSL_VERIFYHOST
, 0);
1040 HTTP_CURL_OPT(SSL_CIPHER_LIST
, NULL
);
1041 HTTP_CURL_OPT(CAINFO
, NULL
);
1042 #if LIBCURL_VERSION_NUM >= 0x070908
1043 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