2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
26 #include "php_http_std_defs.h"
27 #include "php_http_api.h"
28 #include "php_http_request_api.h"
29 #include "php_http_url_api.h"
31 # include "php_http_request_object.h"
34 #include "phpstr/phpstr.h"
37 # include <winsock2.h>
40 #include <curl/curl.h>
42 ZEND_EXTERN_MODULE_GLOBALS(http
);
44 #if LIBCURL_VERSION_NUM < 0x070c00
45 # define curl_easy_strerror(code) HTTP_G(request).error
48 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
49 #define HTTP_CURL_INFO_EX(I, X) \
50 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
52 case CURLINFO_STRING: \
55 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
56 add_assoc_string(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
61 case CURLINFO_DOUBLE: \
64 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
65 add_assoc_double(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), d); \
73 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
74 add_assoc_long(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), l); \
80 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
81 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
82 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
83 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
84 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
85 if (!strcasecmp(key, #keyname)) { \
86 convert_to_string_ex(param); \
87 HTTP_CURL_OPT(optname, http_request_data_copy(COPY_STRING, Z_STRVAL_PP(param))); \
91 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
92 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
93 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
94 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
95 if (!strcasecmp(key, #keyname)) { \
96 convert_to_long_ex(param); \
97 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
102 static const char *const http_request_methods
[HTTP_MAX_REQUEST_METHOD
+ 1];
103 #define http_curl_getopt(o, k, t) _http_curl_getopt_ex((o), (k), sizeof(k), (t) TSRMLS_CC)
104 #define http_curl_getopt_ex(o, k, l, t) _http_curl_getopt_ex((o), (k), (l), (t) TSRMLS_CC)
105 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
);
106 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
107 static int http_curl_progress_callback(void *, double, double, double, double);
108 static int http_curl_raw_callback(CURL
*, curl_infotype
, char *, size_t, void *);
109 static int http_curl_dummy_callback(char *data
, size_t n
, size_t l
, void *s
) { return n
*l
; }
111 #define HTTP_CURL_CALLBACK_DATA(from, type, var) \
112 http_curl_callback_ctx *__CTX = (http_curl_callback_ctx *) (from); \
113 TSRMLS_FETCH_FROM_CTX(__CTX->tsrm_ctx); \
114 type (var) = (type) (__CTX->data)
116 #define http_curl_callback_data(data) _http_curl_callback_data((data) TSRMLS_CC)
117 static http_curl_callback_ctx
*_http_curl_callback_data(void *data TSRMLS_DC
);
120 /* {{{ void *http_request_data_copy(int, void *) */
121 void *_http_request_data_copy(int type
, void *data TSRMLS_DC
)
127 char *new_str
= estrdup(data
);
128 zend_llist_add_element(&HTTP_G(request
).copies
.strings
, &new_str
);
134 zend_llist_add_element(&HTTP_G(request
).copies
.slists
, &data
);
140 zend_llist_add_element(&HTTP_G(request
).copies
.contexts
, &data
);
146 zend_llist_add_element(&HTTP_G(request
).copies
.convs
, &data
);
158 /* {{{ void http_request_data_free_string(char **) */
159 void _http_request_data_free_string(void *string
)
161 efree(*((char **)string
));
165 /* {{{ void http_request_data_free_slist(struct curl_slist **) */
166 void _http_request_data_free_slist(void *list
)
168 curl_slist_free_all(*((struct curl_slist
**) list
));
172 /* {{{ _http_request_data_free_context(http_curl_callback_ctx **) */
173 void _http_request_data_free_context(void *context
)
175 efree(*((http_curl_callback_ctx
**) context
));
179 /* {{{ _http_request_data_free_conv(http_curl_conv **) */
180 void _http_request_data_free_conv(void *conv
)
182 efree(*((http_curl_conv
**) conv
));
186 /* {{{ http_request_body *http_request_body_new() */
187 PHP_HTTP_API http_request_body
*_http_request_body_new(TSRMLS_D
)
189 http_request_body
*body
= ecalloc(1, sizeof(http_request_body
));
194 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
195 PHP_HTTP_API STATUS
_http_request_body_fill(http_request_body
*body
, HashTable
*fields
, HashTable
*files TSRMLS_DC
)
197 if (files
&& (zend_hash_num_elements(files
) > 0)) {
201 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
204 FOREACH_HASH_KEYVAL(fields
, key
, idx
, data
) {
207 convert_to_string_ex(data
);
208 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
209 CURLFORM_COPYNAME
, key
,
210 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
211 CURLFORM_CONTENTSLENGTH
, Z_STRLEN_PP(data
),
214 if (CURLE_OK
!= err
) {
215 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post fields: %s", curl_easy_strerror(err
));
216 curl_formfree(http_post_data
[0]);
226 FOREACH_HASH_VAL(files
, data
) {
228 zval
**file
, **type
, **name
;
229 if ( SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) &&
230 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) &&
231 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
232 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
233 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
234 CURLFORM_FILE
, Z_STRVAL_PP(file
),
235 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
238 if (CURLE_OK
!= err
) {
239 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post files: %s", curl_easy_strerror(err
));
240 curl_formfree(http_post_data
[0]);
244 http_error(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Post file array entry misses either 'name', 'type' or 'file' entry");
248 body
->type
= HTTP_REQUEST_BODY_CURLPOST
;
249 body
->data
= http_post_data
[0];
256 if (SUCCESS
!= http_urlencode_hash_ex(fields
, 1, NULL
, 0, &encoded
, &encoded_len
)) {
257 http_error(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post data");
261 body
->type
= HTTP_REQUEST_BODY_CSTRING
;
262 body
->data
= encoded
;
263 body
->size
= encoded_len
;
270 /* {{{ void http_request_body_dtor(http_request_body *) */
271 PHP_HTTP_API
void _http_request_body_dtor(http_request_body
*body TSRMLS_DC
)
276 case HTTP_REQUEST_BODY_CSTRING
:
282 case HTTP_REQUEST_BODY_CURLPOST
:
283 curl_formfree(body
->data
);
286 case HTTP_REQUEST_BODY_UPLOADFILE
:
287 php_stream_close(body
->data
);
294 /* {{{ void http_request_body_free(http_request_body *) */
295 PHP_HTTP_API
void _http_request_body_free(http_request_body
*body TSRMLS_DC
)
298 http_request_body_dtor(body
);
304 /* {{{ STATUS http_request_init(CURL *, http_request_method, char *, http_request_body *, HashTable *) */
305 PHP_HTTP_API STATUS
_http_request_init(CURL
*ch
, http_request_method meth
, char *url
, http_request_body
*body
, HashTable
*options TSRMLS_DC
)
308 zend_bool range_req
= 0;
310 /* reset CURL handle */
311 #if LIBCURL_VERSION_NUM >= 0x070c01
317 HTTP_CURL_OPT(URL
, http_request_data_copy(COPY_STRING
, url
));
320 HTTP_CURL_OPT(HEADER
, 0);
321 HTTP_CURL_OPT(FILETIME
, 1);
322 HTTP_CURL_OPT(AUTOREFERER
, 1);
323 HTTP_CURL_OPT(READFUNCTION
, http_curl_read_callback
);
324 /* we'll get all data through the debug function */
325 HTTP_CURL_OPT(WRITEFUNCTION
, http_curl_dummy_callback
);
326 HTTP_CURL_OPT(HEADERFUNCTION
, NULL
);
328 HTTP_CURL_OPT(VERBOSE
, 1);
329 HTTP_CURL_OPT(DEBUGFUNCTION
, http_curl_raw_callback
);
331 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
332 HTTP_CURL_OPT(NOSIGNAL
, 1);
334 #if LIBCURL_VERSION_NUM < 0x070c00
335 HTTP_CURL_OPT(ERRORBUFFER
, HTTP_G(request
).error
);
338 /* progress callback */
339 if (zoption
= http_curl_getopt(options
, "onprogress", 0)) {
340 HTTP_CURL_OPT(NOPROGRESS
, 0);
341 HTTP_CURL_OPT(PROGRESSFUNCTION
, http_curl_progress_callback
);
342 HTTP_CURL_OPT(PROGRESSDATA
, http_curl_callback_data(zoption
));
344 HTTP_CURL_OPT(NOPROGRESS
, 1);
348 if (zoption
= http_curl_getopt(options
, "proxyhost", IS_STRING
)) {
349 HTTP_CURL_OPT(PROXY
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
351 if (zoption
= http_curl_getopt(options
, "proxyport", IS_LONG
)) {
352 HTTP_CURL_OPT(PROXYPORT
, Z_LVAL_P(zoption
));
355 if (zoption
= http_curl_getopt(options
, "proxyauth", IS_STRING
)) {
356 HTTP_CURL_OPT(PROXYUSERPWD
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
358 #if LIBCURL_VERSION_NUM >= 0x070a07
360 if (zoption
= http_curl_getopt(options
, "proxyauthtype", IS_LONG
)) {
361 HTTP_CURL_OPT(PROXYAUTH
, Z_LVAL_P(zoption
));
366 /* outgoing interface */
367 if (zoption
= http_curl_getopt(options
, "interface", IS_STRING
)) {
368 HTTP_CURL_OPT(INTERFACE
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
372 if (zoption
= http_curl_getopt(options
, "port", IS_LONG
)) {
373 HTTP_CURL_OPT(PORT
, Z_LVAL_P(zoption
));
377 if (zoption
= http_curl_getopt(options
, "httpauth", IS_STRING
)) {
378 HTTP_CURL_OPT(USERPWD
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
380 #if LIBCURL_VERSION_NUM >= 0x070a06
381 if (zoption
= http_curl_getopt(options
, "httpauthtype", IS_LONG
)) {
382 HTTP_CURL_OPT(HTTPAUTH
, Z_LVAL_P(zoption
));
386 /* compress, empty string enables deflate and gzip */
387 if ((zoption
= http_curl_getopt(options
, "compress", IS_BOOL
)) && Z_LVAL_P(zoption
)) {
388 HTTP_CURL_OPT(ENCODING
, "");
390 HTTP_CURL_OPT(ENCODING
, 0);
393 /* redirects, defaults to 0 */
394 if (zoption
= http_curl_getopt(options
, "redirect", IS_LONG
)) {
395 HTTP_CURL_OPT(FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
396 HTTP_CURL_OPT(MAXREDIRS
, Z_LVAL_P(zoption
));
397 if (zoption
= http_curl_getopt(options
, "unrestrictedauth", IS_BOOL
)) {
398 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
401 HTTP_CURL_OPT(FOLLOWLOCATION
, 0);
405 if (zoption
= http_curl_getopt(options
, "referer", IS_STRING
)) {
406 HTTP_CURL_OPT(REFERER
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
408 HTTP_CURL_OPT(REFERER
, NULL
);
411 /* useragent, default "PECL::HTTP/version (PHP/version)" */
412 if (zoption
= http_curl_getopt(options
, "useragent", IS_STRING
)) {
413 HTTP_CURL_OPT(USERAGENT
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
415 HTTP_CURL_OPT(USERAGENT
, "PECL::HTTP/" HTTP_PEXT_VERSION
" (PHP/" PHP_VERSION
")");
418 /* additional headers, array('name' => 'value') */
419 if (zoption
= http_curl_getopt(options
, "headers", IS_ARRAY
)) {
422 struct curl_slist
*headers
= NULL
;
424 FOREACH_KEY(zoption
, header_key
, header_idx
) {
427 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &header_val
)) {
428 char header
[1024] = {0};
429 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
430 headers
= curl_slist_append(headers
, http_request_data_copy(COPY_STRING
, header
));
439 HTTP_CURL_OPT(HTTPHEADER
, http_request_data_copy(COPY_SLIST
, headers
));
442 HTTP_CURL_OPT(HTTPHEADER
, NULL
);
445 /* cookies, array('name' => 'value') */
446 if (zoption
= http_curl_getopt(options
, "cookies", IS_ARRAY
)) {
447 char *cookie_key
= NULL
;
448 ulong cookie_idx
= 0;
449 phpstr
*qstr
= phpstr_new();
451 FOREACH_KEY(zoption
, cookie_key
, cookie_idx
) {
454 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &cookie_val
)) {
455 phpstr_appendf(qstr
, "%s=%s; ", cookie_key
, Z_STRVAL_PP(cookie_val
));
465 HTTP_CURL_OPT(COOKIE
, http_request_data_copy(COPY_STRING
, qstr
->data
));
469 HTTP_CURL_OPT(COOKIE
, NULL
);
472 /* session cookies */
473 if (zoption
= http_curl_getopt(options
, "cookiesession", IS_BOOL
)) {
474 if (Z_LVAL_P(zoption
)) {
475 /* accept cookies for this session */
476 HTTP_CURL_OPT(COOKIEFILE
, "");
478 /* reset session cookies */
479 HTTP_CURL_OPT(COOKIESESSION
, 1);
482 HTTP_CURL_OPT(COOKIEFILE
, NULL
);
485 /* cookiestore, read initial cookies from that file and store cookies back into that file */
486 if ((zoption
= http_curl_getopt(options
, "cookiestore", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
487 HTTP_CURL_OPT(COOKIEFILE
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
488 HTTP_CURL_OPT(COOKIEJAR
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
490 HTTP_CURL_OPT(COOKIEFILE
, NULL
);
491 HTTP_CURL_OPT(COOKIEJAR
, NULL
);
495 if (zoption
= http_curl_getopt(options
, "resume", IS_LONG
)) {
497 HTTP_CURL_OPT(RESUME_FROM
, Z_LVAL_P(zoption
));
499 HTTP_CURL_OPT(RESUME_FROM
, 0);
503 if (zoption
= http_curl_getopt(options
, "maxfilesize", IS_LONG
)) {
504 HTTP_CURL_OPT(MAXFILESIZE
, Z_LVAL_P(zoption
));
506 HTTP_CURL_OPT(MAXFILESIZE
, 0);
510 if (zoption
= http_curl_getopt(options
, "lastmodified", IS_LONG
)) {
511 HTTP_CURL_OPT(TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
512 HTTP_CURL_OPT(TIMEVALUE
, Z_LVAL_P(zoption
));
514 HTTP_CURL_OPT(TIMEVALUE
, 0);
517 /* timeout, defaults to 3 */
518 if (zoption
= http_curl_getopt(options
, "timeout", IS_LONG
)) {
519 HTTP_CURL_OPT(TIMEOUT
, Z_LVAL_P(zoption
));
521 HTTP_CURL_OPT(TIMEOUT
, 3);
524 /* connecttimeout, defaults to 3 */
525 if (zoption
= http_curl_getopt(options
, "connecttimeout", IS_LONG
)) {
526 HTTP_CURL_OPT(CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
528 HTTP_CURL_OPT(CONNECTTIMEOUT
, 3);
532 if (zoption
= http_curl_getopt(options
, "ssl", IS_ARRAY
)) {
537 FOREACH_KEYVAL(zoption
, key
, idx
, param
) {
539 HTTP_CURL_OPT_SSL_STRING(CERT
);
540 #if LIBCURL_VERSION_NUM >= 0x070903
541 HTTP_CURL_OPT_SSL_STRING(CERTTYPE
);
543 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD
);
545 HTTP_CURL_OPT_SSL_STRING(KEY
);
546 HTTP_CURL_OPT_SSL_STRING(KEYTYPE
);
547 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD
);
549 HTTP_CURL_OPT_SSL_STRING(ENGINE
);
550 HTTP_CURL_OPT_SSL_LONG(VERSION
);
552 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER
);
553 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST
);
554 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST
);
557 HTTP_CURL_OPT_STRING(CAINFO
);
558 #if LIBCURL_VERSION_NUM >= 0x070908
559 HTTP_CURL_OPT_STRING(CAPATH
);
561 HTTP_CURL_OPT_STRING(RANDOM_FILE
);
562 HTTP_CURL_OPT_STRING(EGDSOCKET
);
569 /* disable SSL verification by default */
570 HTTP_CURL_OPT(SSL_VERIFYPEER
, 0);
571 HTTP_CURL_OPT(SSL_VERIFYHOST
, 0);
578 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1);
582 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
586 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
590 curl_easy_setopt(ch
, CURLOPT_UPLOAD
, 1);
594 if (http_request_method_exists(0, meth
, NULL
)) {
595 curl_easy_setopt(ch
, CURLOPT_CUSTOMREQUEST
, http_request_method_name(meth
));
597 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unsupported request method: %d", meth
);
603 /* attach request body */
604 if (body
&& (meth
!= HTTP_GET
) && (meth
!= HTTP_HEAD
)) {
607 case HTTP_REQUEST_BODY_CSTRING
:
608 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, body
->data
);
609 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, body
->size
);
612 case HTTP_REQUEST_BODY_CURLPOST
:
613 curl_easy_setopt(ch
, CURLOPT_HTTPPOST
, (struct curl_httppost
*) body
->data
);
616 case HTTP_REQUEST_BODY_UPLOADFILE
:
617 curl_easy_setopt(ch
, CURLOPT_READDATA
, http_curl_callback_data(body
));
618 curl_easy_setopt(ch
, CURLOPT_INFILESIZE
, body
->size
);
622 /* shouldn't ever happen */
623 http_error_ex(HE_ERROR
, 0, "Unknown request body type: %d", body
->type
);
633 /* {{{ void http_request_conv(CURL *, phpstr *, phpstr *) */
634 void _http_request_conv(CURL
*ch
, phpstr
* response
, phpstr
*request TSRMLS_DC
)
636 http_curl_conv
*conv
= emalloc(sizeof(http_curl_conv
));
637 conv
->response
= response
;
638 conv
->request
= request
;
639 conv
->last_info
= -1;
640 HTTP_CURL_OPT(DEBUGDATA
, http_curl_callback_data(http_request_data_copy(COPY_CONV
, conv
)));
644 /* {{{ STATUS http_request_exec(CURL *, HashTable *) */
645 PHP_HTTP_API STATUS
_http_request_exec(CURL
*ch
, HashTable
*info
, phpstr
*response
, phpstr
*request TSRMLS_DC
)
649 http_request_conv(ch
, response
, request
);
651 /* perform request */
652 if (CURLE_OK
!= (result
= curl_easy_perform(ch
))) {
653 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "Could not perform request: %s", curl_easy_strerror(result
));
658 http_request_info(ch
, info
);
665 /* {{{ void http_request_info(CURL *, HashTable *) */
666 PHP_HTTP_API
void _http_request_info(CURL
*ch
, HashTable
*info TSRMLS_DC
)
669 Z_ARRVAL(array
) = info
;
671 HTTP_CURL_INFO(EFFECTIVE_URL
);
672 #if LIBCURL_VERSION_NUM >= 0x070a07
673 HTTP_CURL_INFO(RESPONSE_CODE
);
675 HTTP_CURL_INFO_EX(HTTP_CODE
, RESPONSE_CODE
);
677 HTTP_CURL_INFO(HTTP_CONNECTCODE
);
678 #if LIBCURL_VERSION_NUM >= 0x070500
679 HTTP_CURL_INFO(FILETIME
);
681 HTTP_CURL_INFO(TOTAL_TIME
);
682 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
683 HTTP_CURL_INFO(CONNECT_TIME
);
684 HTTP_CURL_INFO(PRETRANSFER_TIME
);
685 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
686 #if LIBCURL_VERSION_NUM >= 0x070907
687 HTTP_CURL_INFO(REDIRECT_TIME
);
688 HTTP_CURL_INFO(REDIRECT_COUNT
);
690 HTTP_CURL_INFO(SIZE_UPLOAD
);
691 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
692 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
693 HTTP_CURL_INFO(SPEED_UPLOAD
);
694 HTTP_CURL_INFO(HEADER_SIZE
);
695 HTTP_CURL_INFO(REQUEST_SIZE
);
696 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
697 #if LIBCURL_VERSION_NUM >= 0x070c03
698 /*HTTP_CURL_INFO(SSL_ENGINES); todo: CURLINFO_SLIST */
700 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
701 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
702 HTTP_CURL_INFO(CONTENT_TYPE
);
703 #if LIBCURL_VERSION_NUM >= 0x070a03
704 /*HTTP_CURL_INFO(PRIVATE);*/
706 #if LIBCURL_VERSION_NUM >= 0x070a08
707 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
708 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
710 #if LIBCURL_VERSION_NUM >= 0x070c02
711 /*HTTP_CURL_INFO(OS_ERRNO);*/
713 #if LIBCURL_VERSION_NUM >= 0x070c03
714 HTTP_CURL_INFO(NUM_CONNECTS
);
719 /* {{{ STATUS http_request_ex(CURL *, http_request_method, char *, http_request_body, HashTable, HashTable, phpstr *) */
720 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
)
723 zend_bool clean_curl
;
725 if ((clean_curl
= (!ch
))) {
726 if (!(ch
= curl_easy_init())) {
727 http_error(HE_WARNING
, HTTP_E_REQUEST
, "Could not initialize curl.");
732 status
= ((SUCCESS
== http_request_init(ch
, meth
, url
, body
, options
)) &&
733 (SUCCESS
== http_request_exec(ch
, info
, response
, NULL
))) ? SUCCESS
: FAILURE
;
736 curl_easy_cleanup(ch
);
742 /* {{{ char *http_request_method_name(http_request_method) */
743 PHP_HTTP_API
const char *_http_request_method_name(http_request_method m TSRMLS_DC
)
747 if (HTTP_STD_REQUEST_METHOD(m
)) {
748 return http_request_methods
[m
];
751 if (SUCCESS
== zend_hash_index_find(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(m
), (void **) &meth
)) {
752 return Z_STRVAL_PP(meth
);
755 return http_request_methods
[0];
759 /* {{{ unsigned long http_request_method_exists(zend_bool, unsigned long, char *) */
760 PHP_HTTP_API
unsigned long _http_request_method_exists(zend_bool by_name
, unsigned long id
, const char *name TSRMLS_DC
)
765 for (i
= HTTP_NO_REQUEST_METHOD
+ 1; i
< HTTP_MAX_REQUEST_METHOD
; ++i
) {
766 if (!strcmp(name
, http_request_methods
[i
])) {
775 FOREACH_HASH_KEYVAL(&HTTP_G(request
).methods
.custom
, key
, idx
, data
) {
776 if (!strcmp(name
, Z_STRVAL_PP(data
))) {
777 return idx
+ HTTP_MAX_REQUEST_METHOD
;
783 return HTTP_STD_REQUEST_METHOD(id
) || zend_hash_index_exists(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(id
)) ? id
: 0;
788 /* {{{ unsigned long http_request_method_register(char *) */
789 PHP_HTTP_API
unsigned long _http_request_method_register(const char *method TSRMLS_DC
)
793 unsigned long meth_num
= HTTP_G(request
).methods
.custom
.nNextFreeElement
+ HTTP_MAX_REQUEST_METHOD
;
795 Z_ARRVAL(array
) = &HTTP_G(request
).methods
.custom
;
796 add_next_index_string(&array
, estrdup(method
), 0);
798 spprintf(&http_method
, 0, "HTTP_%s", method
);
799 zend_register_long_constant(http_method
, strlen(http_method
) + 1, meth_num
, CONST_CS
, http_module_number TSRMLS_CC
);
806 /* {{{ STATUS http_request_method_unregister(usngigned long) */
807 PHP_HTTP_API STATUS
_http_request_method_unregister(unsigned long method TSRMLS_DC
)
812 if (SUCCESS
!= zend_hash_index_find(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(method
), (void **) &zmethod
)) {
813 http_error_ex(HE_NOTICE
, HTTP_E_REQUEST_METHOD
, "Request method with id %lu does not exist", method
);
817 spprintf(&http_method
, 0, "HTTP_%s", Z_STRVAL_PP(zmethod
));
819 if ( (SUCCESS
!= zend_hash_index_del(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(method
)))
820 || (SUCCESS
!= zend_hash_del(EG(zend_constants
), http_method
, strlen(http_method
) + 1))) {
821 http_error_ex(HE_NOTICE
, HTTP_E_REQUEST_METHOD
, "Could not unregister request method: %s", http_method
);
832 /* {{{ char *http_request_methods[] */
833 static const char *const http_request_methods
[] = {
844 /* WebDAV - RFC 2518 */
852 /* WebDAV Versioning - RFC 3253 */
864 /* WebDAV Access Control - RFC 3744 */
870 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
871 static size_t http_curl_read_callback(void *data
, size_t len
, size_t n
, void *s
)
873 HTTP_CURL_CALLBACK_DATA(s
, http_request_body
*, body
);
875 if (body
->type
!= HTTP_REQUEST_BODY_UPLOADFILE
) {
878 return php_stream_read((php_stream
*) body
->data
, data
, len
* n
);
882 /* {{{ http_curl_callback_ctx http_curl_callback_data(void *) */
883 static http_curl_callback_ctx
*_http_curl_callback_data(void *data TSRMLS_DC
)
885 http_curl_callback_ctx
*ctx
= emalloc(sizeof(http_curl_callback_ctx
));
886 TSRMLS_SET_CTX(ctx
->tsrm_ctx
);
888 return http_request_data_copy(COPY_CONTEXT
, ctx
);
892 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
893 static int http_curl_progress_callback(void *data
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
895 zval
*params_pass
[4], params_local
[4], retval
;
896 HTTP_CURL_CALLBACK_DATA(data
, zval
*, func
);
898 params_pass
[0] = ¶ms_local
[0];
899 params_pass
[1] = ¶ms_local
[1];
900 params_pass
[2] = ¶ms_local
[2];
901 params_pass
[3] = ¶ms_local
[3];
903 INIT_PZVAL(params_pass
[0]);
904 INIT_PZVAL(params_pass
[1]);
905 INIT_PZVAL(params_pass
[2]);
906 INIT_PZVAL(params_pass
[3]);
907 ZVAL_DOUBLE(params_pass
[0], dltotal
);
908 ZVAL_DOUBLE(params_pass
[1], dlnow
);
909 ZVAL_DOUBLE(params_pass
[2], ultotal
);
910 ZVAL_DOUBLE(params_pass
[3], ulnow
);
912 return call_user_function(EG(function_table
), NULL
, func
, &retval
, 4, params_pass TSRMLS_CC
);
916 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
917 static int http_curl_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
919 HTTP_CURL_CALLBACK_DATA(ctx
, http_curl_conv
*, conv
);
923 case CURLINFO_DATA_IN
:
924 if (conv
->response
&& conv
->last_info
== CURLINFO_HEADER_IN
) {
925 phpstr_appends(conv
->response
, HTTP_CRLF
);
927 case CURLINFO_HEADER_IN
:
928 if (conv
->response
) {
929 phpstr_append(conv
->response
, data
, length
);
932 case CURLINFO_DATA_OUT
:
933 if (conv
->request
&& conv
->last_info
== CURLINFO_HEADER_OUT
) {
934 phpstr_appends(conv
->request
, HTTP_CRLF
);
936 case CURLINFO_HEADER_OUT
:
938 phpstr_append(conv
->request
, data
, length
);
944 conv
->last_info
= type
;
950 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
951 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
)
955 if (!options
|| (SUCCESS
!= zend_hash_find(options
, key
, keylen
, (void **) &zoption
))) {
959 if (Z_TYPE_PP(zoption
) != type
) {
962 case IS_BOOL
: convert_to_boolean_ex(zoption
); break;
963 case IS_LONG
: convert_to_long_ex(zoption
); break;
964 case IS_DOUBLE
: convert_to_double_ex(zoption
); break;
965 case IS_STRING
: convert_to_string_ex(zoption
); break;
966 case IS_ARRAY
: convert_to_array_ex(zoption
); break;
967 case IS_OBJECT
: convert_to_object_ex(zoption
); break;
984 * vim600: noet sw=4 ts=4 fdm=marker
985 * vim<600: noet sw=4 ts=4