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 +----------------------------------------------------------------------+
24 #include "php_http_std_defs.h"
25 #include "php_http_api.h"
26 #include "php_http_request_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 ZEND_EXTERN_MODULE_GLOBALS(http
);
42 #if LIBCURL_VERSION_NUM < 0x070c00
43 # define curl_easy_strerror(code) HTTP_G(request).error
46 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
47 #define HTTP_CURL_INFO_EX(I, X) \
48 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
50 case CURLINFO_STRING: \
53 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
54 add_assoc_string(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
59 case CURLINFO_DOUBLE: \
62 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
63 add_assoc_double(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), d); \
71 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
72 add_assoc_long(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), l); \
78 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
79 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
80 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
81 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
82 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
83 if (!strcasecmp(key, #keyname)) { \
84 convert_to_string_ex(param); \
85 HTTP_CURL_OPT(optname, http_request_data_copy(COPY_STRING, Z_STRVAL_PP(param))); \
89 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
90 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
91 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
92 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
93 if (!strcasecmp(key, #keyname)) { \
94 convert_to_long_ex(param); \
95 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
100 static const char *const http_request_methods
[HTTP_MAX_REQUEST_METHOD
+ 1];
101 #define http_curl_getopt(o, k, t) _http_curl_getopt_ex((o), (k), sizeof(k), (t) TSRMLS_CC)
102 #define http_curl_getopt_ex(o, k, l, t) _http_curl_getopt_ex((o), (k), (l), (t) TSRMLS_CC)
103 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
);
104 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
105 static int http_curl_progress_callback(void *, double, double, double, double);
106 static int http_curl_raw_callback(CURL
*, curl_infotype
, char *, size_t, void *);
107 static int http_curl_dummy_callback(char *data
, size_t n
, size_t l
, void *s
) { return n
*l
; }
109 #define HTTP_CURL_CALLBACK_DATA(from, type, var) \
110 http_curl_callback_ctx *__CTX = (http_curl_callback_ctx *) (from); \
111 TSRMLS_FETCH_FROM_CTX(__CTX->tsrm_ctx); \
112 type (var) = (type) (__CTX->data)
114 #define http_curl_callback_data(data) _http_curl_callback_data((data) TSRMLS_CC)
115 static http_curl_callback_ctx
*_http_curl_callback_data(void *data TSRMLS_DC
);
118 /* {{{ void *http_request_data_copy(int, void *) */
119 void *_http_request_data_copy(int type
, void *data TSRMLS_DC
)
125 char *new_str
= estrdup(data
);
126 zend_llist_add_element(&HTTP_G(request
).copies
.strings
, &new_str
);
132 zend_llist_add_element(&HTTP_G(request
).copies
.slists
, &data
);
138 zend_llist_add_element(&HTTP_G(request
).copies
.contexts
, &data
);
144 zend_llist_add_element(&HTTP_G(request
).copies
.convs
, &data
);
156 /* {{{ void http_request_data_free_string(char **) */
157 void _http_request_data_free_string(void *string
)
159 efree(*((char **)string
));
163 /* {{{ void http_request_data_free_slist(struct curl_slist **) */
164 void _http_request_data_free_slist(void *list
)
166 curl_slist_free_all(*((struct curl_slist
**) list
));
170 /* {{{ _http_request_data_free_context(http_curl_callback_ctx **) */
171 void _http_request_data_free_context(void *context
)
173 efree(*((http_curl_callback_ctx
**) context
));
177 /* {{{ _http_request_data_free_conv(http_curl_conv **) */
178 void _http_request_data_free_conv(void *conv
)
180 efree(*((http_curl_conv
**) conv
));
184 /* {{{ http_request_body *http_request_body_new() */
185 PHP_HTTP_API http_request_body
*_http_request_body_new(TSRMLS_D
)
187 http_request_body
*body
= ecalloc(1, sizeof(http_request_body
));
192 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
193 PHP_HTTP_API STATUS
_http_request_body_fill(http_request_body
*body
, HashTable
*fields
, HashTable
*files TSRMLS_DC
)
195 if (files
&& (zend_hash_num_elements(files
) > 0)) {
199 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
202 FOREACH_HASH_KEYVAL(fields
, key
, idx
, data
) {
205 convert_to_string_ex(data
);
206 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
207 CURLFORM_COPYNAME
, key
,
208 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
209 CURLFORM_CONTENTSLENGTH
, Z_STRLEN_PP(data
),
212 if (CURLE_OK
!= err
) {
213 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post fields: %s", curl_easy_strerror(err
));
214 curl_formfree(http_post_data
[0]);
224 FOREACH_HASH_VAL(files
, data
) {
226 zval
**file
, **type
, **name
;
227 if ( SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) &&
228 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) &&
229 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
230 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
231 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
232 CURLFORM_FILE
, Z_STRVAL_PP(file
),
233 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
236 if (CURLE_OK
!= err
) {
237 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post files: %s", curl_easy_strerror(err
));
238 curl_formfree(http_post_data
[0]);
242 http_error(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Post file array entry misses either 'name', 'type' or 'file' entry");
246 body
->type
= HTTP_REQUEST_BODY_CURLPOST
;
247 body
->data
= http_post_data
[0];
254 if (SUCCESS
!= http_urlencode_hash_ex(fields
, 1, NULL
, 0, &encoded
, &encoded_len
)) {
255 http_error(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post data");
259 body
->type
= HTTP_REQUEST_BODY_CSTRING
;
260 body
->data
= encoded
;
261 body
->size
= encoded_len
;
268 /* {{{ void http_request_body_dtor(http_request_body *) */
269 PHP_HTTP_API
void _http_request_body_dtor(http_request_body
*body TSRMLS_DC
)
274 case HTTP_REQUEST_BODY_CSTRING
:
280 case HTTP_REQUEST_BODY_CURLPOST
:
281 curl_formfree(body
->data
);
284 case HTTP_REQUEST_BODY_UPLOADFILE
:
285 php_stream_close(body
->data
);
292 /* {{{ void http_request_body_free(http_request_body *) */
293 PHP_HTTP_API
void _http_request_body_free(http_request_body
*body TSRMLS_DC
)
296 http_request_body_dtor(body
);
302 /* {{{ STATUS http_request_init(CURL *, http_request_method, char *, http_request_body *, HashTable *) */
303 PHP_HTTP_API STATUS
_http_request_init(CURL
*ch
, http_request_method meth
, char *url
, http_request_body
*body
, HashTable
*options TSRMLS_DC
)
306 zend_bool range_req
= 0;
308 /* reset CURL handle */
309 #if LIBCURL_VERSION_NUM >= 0x070c01
315 HTTP_CURL_OPT(URL
, http_request_data_copy(COPY_STRING
, url
));
318 HTTP_CURL_OPT(HEADER
, 0);
319 HTTP_CURL_OPT(FILETIME
, 1);
320 HTTP_CURL_OPT(AUTOREFERER
, 1);
321 HTTP_CURL_OPT(READFUNCTION
, http_curl_read_callback
);
322 /* we'll get all data through the debug function */
323 HTTP_CURL_OPT(WRITEFUNCTION
, http_curl_dummy_callback
);
324 HTTP_CURL_OPT(HEADERFUNCTION
, NULL
);
326 HTTP_CURL_OPT(VERBOSE
, 1);
327 HTTP_CURL_OPT(DEBUGFUNCTION
, http_curl_raw_callback
);
329 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
330 HTTP_CURL_OPT(NOSIGNAL
, 1);
332 #if LIBCURL_VERSION_NUM < 0x070c00
333 HTTP_CURL_OPT(ERRORBUFFER
, HTTP_G(request
).error
);
336 /* progress callback */
337 if (zoption
= http_curl_getopt(options
, "onprogress", 0)) {
338 HTTP_CURL_OPT(NOPROGRESS
, 0);
339 HTTP_CURL_OPT(PROGRESSFUNCTION
, http_curl_progress_callback
);
340 HTTP_CURL_OPT(PROGRESSDATA
, http_curl_callback_data(zoption
));
342 HTTP_CURL_OPT(NOPROGRESS
, 1);
346 if (zoption
= http_curl_getopt(options
, "proxyhost", IS_STRING
)) {
347 HTTP_CURL_OPT(PROXY
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
349 if (zoption
= http_curl_getopt(options
, "proxyport", IS_LONG
)) {
350 HTTP_CURL_OPT(PROXYPORT
, Z_LVAL_P(zoption
));
353 if (zoption
= http_curl_getopt(options
, "proxyauth", IS_STRING
)) {
354 HTTP_CURL_OPT(PROXYUSERPWD
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
356 #if LIBCURL_VERSION_NUM >= 0x070a07
358 if (zoption
= http_curl_getopt(options
, "proxyauthtype", IS_LONG
)) {
359 HTTP_CURL_OPT(PROXYAUTH
, Z_LVAL_P(zoption
));
364 /* outgoing interface */
365 if (zoption
= http_curl_getopt(options
, "interface", IS_STRING
)) {
366 HTTP_CURL_OPT(INTERFACE
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
370 if (zoption
= http_curl_getopt(options
, "port", IS_LONG
)) {
371 HTTP_CURL_OPT(PORT
, Z_LVAL_P(zoption
));
375 if (zoption
= http_curl_getopt(options
, "httpauth", IS_STRING
)) {
376 HTTP_CURL_OPT(USERPWD
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
378 #if LIBCURL_VERSION_NUM >= 0x070a06
379 if (zoption
= http_curl_getopt(options
, "httpauthtype", IS_LONG
)) {
380 HTTP_CURL_OPT(HTTPAUTH
, Z_LVAL_P(zoption
));
384 /* compress, empty string enables deflate and gzip */
385 if ((zoption
= http_curl_getopt(options
, "compress", IS_BOOL
)) && Z_LVAL_P(zoption
)) {
386 HTTP_CURL_OPT(ENCODING
, "");
388 HTTP_CURL_OPT(ENCODING
, 0);
391 /* redirects, defaults to 0 */
392 if (zoption
= http_curl_getopt(options
, "redirect", IS_LONG
)) {
393 HTTP_CURL_OPT(FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
394 HTTP_CURL_OPT(MAXREDIRS
, Z_LVAL_P(zoption
));
395 if (zoption
= http_curl_getopt(options
, "unrestrictedauth", IS_BOOL
)) {
396 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
399 HTTP_CURL_OPT(FOLLOWLOCATION
, 0);
403 if (zoption
= http_curl_getopt(options
, "referer", IS_STRING
)) {
404 HTTP_CURL_OPT(REFERER
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
406 HTTP_CURL_OPT(REFERER
, NULL
);
409 /* useragent, default "PECL::HTTP/version (PHP/version)" */
410 if (zoption
= http_curl_getopt(options
, "useragent", IS_STRING
)) {
411 HTTP_CURL_OPT(USERAGENT
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
413 HTTP_CURL_OPT(USERAGENT
, "PECL::HTTP/" HTTP_PEXT_VERSION
" (PHP/" PHP_VERSION
")");
416 /* additional headers, array('name' => 'value') */
417 if (zoption
= http_curl_getopt(options
, "headers", IS_ARRAY
)) {
420 struct curl_slist
*headers
= NULL
;
422 FOREACH_KEY(zoption
, header_key
, header_idx
) {
425 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &header_val
)) {
426 char header
[1024] = {0};
427 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
428 headers
= curl_slist_append(headers
, http_request_data_copy(COPY_STRING
, header
));
437 HTTP_CURL_OPT(HTTPHEADER
, http_request_data_copy(COPY_SLIST
, headers
));
440 HTTP_CURL_OPT(HTTPHEADER
, NULL
);
443 /* cookies, array('name' => 'value') */
444 if (zoption
= http_curl_getopt(options
, "cookies", IS_ARRAY
)) {
445 char *cookie_key
= NULL
;
446 ulong cookie_idx
= 0;
447 phpstr
*qstr
= phpstr_new();
449 FOREACH_KEY(zoption
, cookie_key
, cookie_idx
) {
452 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &cookie_val
)) {
453 phpstr_appendf(qstr
, "%s=%s; ", cookie_key
, Z_STRVAL_PP(cookie_val
));
463 HTTP_CURL_OPT(COOKIE
, http_request_data_copy(COPY_STRING
, qstr
->data
));
467 HTTP_CURL_OPT(COOKIE
, NULL
);
470 /* session cookies */
471 if (zoption
= http_curl_getopt(options
, "cookiesession", IS_BOOL
)) {
472 if (Z_LVAL_P(zoption
)) {
473 /* accept cookies for this session */
474 HTTP_CURL_OPT(COOKIEFILE
, "");
476 /* reset session cookies */
477 HTTP_CURL_OPT(COOKIESESSION
, 1);
480 HTTP_CURL_OPT(COOKIEFILE
, NULL
);
483 /* cookiestore, read initial cookies from that file and store cookies back into that file */
484 if ((zoption
= http_curl_getopt(options
, "cookiestore", IS_STRING
)) && Z_STRLEN_P(zoption
)) {
485 HTTP_CURL_OPT(COOKIEFILE
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
486 HTTP_CURL_OPT(COOKIEJAR
, http_request_data_copy(COPY_STRING
, Z_STRVAL_P(zoption
)));
488 HTTP_CURL_OPT(COOKIEFILE
, NULL
);
489 HTTP_CURL_OPT(COOKIEJAR
, NULL
);
493 if (zoption
= http_curl_getopt(options
, "resume", IS_LONG
)) {
495 HTTP_CURL_OPT(RESUME_FROM
, Z_LVAL_P(zoption
));
497 HTTP_CURL_OPT(RESUME_FROM
, 0);
501 if (zoption
= http_curl_getopt(options
, "maxfilesize", IS_LONG
)) {
502 HTTP_CURL_OPT(MAXFILESIZE
, Z_LVAL_P(zoption
));
504 HTTP_CURL_OPT(MAXFILESIZE
, 0);
508 if (zoption
= http_curl_getopt(options
, "lastmodified", IS_LONG
)) {
509 HTTP_CURL_OPT(TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
510 HTTP_CURL_OPT(TIMEVALUE
, Z_LVAL_P(zoption
));
512 HTTP_CURL_OPT(TIMEVALUE
, 0);
515 /* timeout, defaults to 3 */
516 if (zoption
= http_curl_getopt(options
, "timeout", IS_LONG
)) {
517 HTTP_CURL_OPT(TIMEOUT
, Z_LVAL_P(zoption
));
519 HTTP_CURL_OPT(TIMEOUT
, 3);
522 /* connecttimeout, defaults to 3 */
523 if (zoption
= http_curl_getopt(options
, "connecttimeout", IS_LONG
)) {
524 HTTP_CURL_OPT(CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
526 HTTP_CURL_OPT(CONNECTTIMEOUT
, 3);
530 if (zoption
= http_curl_getopt(options
, "ssl", IS_ARRAY
)) {
535 FOREACH_KEYVAL(zoption
, key
, idx
, param
) {
537 HTTP_CURL_OPT_SSL_STRING(CERT
);
538 #if LIBCURL_VERSION_NUM >= 0x070903
539 HTTP_CURL_OPT_SSL_STRING(CERTTYPE
);
541 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD
);
543 HTTP_CURL_OPT_SSL_STRING(KEY
);
544 HTTP_CURL_OPT_SSL_STRING(KEYTYPE
);
545 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD
);
547 HTTP_CURL_OPT_SSL_STRING(ENGINE
);
548 HTTP_CURL_OPT_SSL_LONG(VERSION
);
550 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER
);
551 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST
);
552 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST
);
555 HTTP_CURL_OPT_STRING(CAINFO
);
556 #if LIBCURL_VERSION_NUM >= 0x070908
557 HTTP_CURL_OPT_STRING(CAPATH
);
559 HTTP_CURL_OPT_STRING(RANDOM_FILE
);
560 HTTP_CURL_OPT_STRING(EGDSOCKET
);
567 /* disable SSL verification by default */
568 HTTP_CURL_OPT(SSL_VERIFYPEER
, 0);
569 HTTP_CURL_OPT(SSL_VERIFYHOST
, 0);
576 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1);
580 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
584 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
588 curl_easy_setopt(ch
, CURLOPT_UPLOAD
, 1);
592 if (http_request_method_exists(0, meth
, NULL
)) {
593 curl_easy_setopt(ch
, CURLOPT_CUSTOMREQUEST
, http_request_method_name(meth
));
595 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_METHOD
, "Unsupported request method: %d", meth
);
601 /* attach request body */
602 if (body
&& (meth
!= HTTP_GET
) && (meth
!= HTTP_HEAD
)) {
605 case HTTP_REQUEST_BODY_CSTRING
:
606 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, body
->data
);
607 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, body
->size
);
610 case HTTP_REQUEST_BODY_CURLPOST
:
611 curl_easy_setopt(ch
, CURLOPT_HTTPPOST
, (struct curl_httppost
*) body
->data
);
614 case HTTP_REQUEST_BODY_UPLOADFILE
:
615 curl_easy_setopt(ch
, CURLOPT_READDATA
, http_curl_callback_data(body
));
616 curl_easy_setopt(ch
, CURLOPT_INFILESIZE
, body
->size
);
620 /* shouldn't ever happen */
621 http_error_ex(HE_ERROR
, 0, "Unknown request body type: %d", body
->type
);
631 /* {{{ void http_request_conv(CURL *, phpstr *, phpstr *) */
632 void _http_request_conv(CURL
*ch
, phpstr
* response
, phpstr
*request TSRMLS_DC
)
634 http_curl_conv
*conv
= emalloc(sizeof(http_curl_conv
));
635 conv
->response
= response
;
636 conv
->request
= request
;
637 conv
->last_info
= -1;
638 HTTP_CURL_OPT(DEBUGDATA
, http_curl_callback_data(http_request_data_copy(COPY_CONV
, conv
)));
642 /* {{{ STATUS http_request_exec(CURL *, HashTable *) */
643 PHP_HTTP_API STATUS
_http_request_exec(CURL
*ch
, HashTable
*info
, phpstr
*response
, phpstr
*request TSRMLS_DC
)
647 http_request_conv(ch
, response
, request
);
649 /* perform request */
650 if (CURLE_OK
!= (result
= curl_easy_perform(ch
))) {
651 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "Could not perform request: %s", curl_easy_strerror(result
));
656 http_request_info(ch
, info
);
663 /* {{{ void http_request_info(CURL *, HashTable *) */
664 PHP_HTTP_API
void _http_request_info(CURL
*ch
, HashTable
*info TSRMLS_DC
)
667 Z_ARRVAL(array
) = info
;
669 HTTP_CURL_INFO(EFFECTIVE_URL
);
670 #if LIBCURL_VERSION_NUM >= 0x070a07
671 HTTP_CURL_INFO(RESPONSE_CODE
);
673 HTTP_CURL_INFO_EX(HTTP_CODE
, RESPONSE_CODE
);
675 HTTP_CURL_INFO(HTTP_CONNECTCODE
);
676 #if LIBCURL_VERSION_NUM >= 0x070500
677 HTTP_CURL_INFO(FILETIME
);
679 HTTP_CURL_INFO(TOTAL_TIME
);
680 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
681 HTTP_CURL_INFO(CONNECT_TIME
);
682 HTTP_CURL_INFO(PRETRANSFER_TIME
);
683 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
684 #if LIBCURL_VERSION_NUM >= 0x070907
685 HTTP_CURL_INFO(REDIRECT_TIME
);
686 HTTP_CURL_INFO(REDIRECT_COUNT
);
688 HTTP_CURL_INFO(SIZE_UPLOAD
);
689 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
690 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
691 HTTP_CURL_INFO(SPEED_UPLOAD
);
692 HTTP_CURL_INFO(HEADER_SIZE
);
693 HTTP_CURL_INFO(REQUEST_SIZE
);
694 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
695 #if LIBCURL_VERSION_NUM >= 0x070c03
696 /*HTTP_CURL_INFO(SSL_ENGINES); todo: CURLINFO_SLIST */
698 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
699 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
700 HTTP_CURL_INFO(CONTENT_TYPE
);
701 #if LIBCURL_VERSION_NUM >= 0x070a03
702 /*HTTP_CURL_INFO(PRIVATE);*/
704 #if LIBCURL_VERSION_NUM >= 0x070a08
705 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
706 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
708 #if LIBCURL_VERSION_NUM >= 0x070c02
709 /*HTTP_CURL_INFO(OS_ERRNO);*/
711 #if LIBCURL_VERSION_NUM >= 0x070c03
712 HTTP_CURL_INFO(NUM_CONNECTS
);
717 /* {{{ STATUS http_request_ex(CURL *, http_request_method, char *, http_request_body, HashTable, HashTable, phpstr *) */
718 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
)
721 zend_bool clean_curl
;
723 if ((clean_curl
= (!ch
))) {
724 if (!(ch
= curl_easy_init())) {
725 http_error(HE_WARNING
, HTTP_E_REQUEST
, "Could not initialize curl.");
730 status
= ((SUCCESS
== http_request_init(ch
, meth
, url
, body
, options
)) &&
731 (SUCCESS
== http_request_exec(ch
, info
, response
, NULL
))) ? SUCCESS
: FAILURE
;
734 curl_easy_cleanup(ch
);
740 /* {{{ char *http_request_method_name(http_request_method) */
741 PHP_HTTP_API
const char *_http_request_method_name(http_request_method m TSRMLS_DC
)
745 if (HTTP_STD_REQUEST_METHOD(m
)) {
746 return http_request_methods
[m
];
749 if (SUCCESS
== zend_hash_index_find(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(m
), (void **) &meth
)) {
750 return Z_STRVAL_PP(meth
);
753 return http_request_methods
[0];
757 /* {{{ unsigned long http_request_method_exists(zend_bool, unsigned long, char *) */
758 PHP_HTTP_API
unsigned long _http_request_method_exists(zend_bool by_name
, unsigned long id
, const char *name TSRMLS_DC
)
763 for (i
= HTTP_NO_REQUEST_METHOD
+ 1; i
< HTTP_MAX_REQUEST_METHOD
; ++i
) {
764 if (!strcmp(name
, http_request_methods
[i
])) {
773 FOREACH_HASH_KEYVAL(&HTTP_G(request
).methods
.custom
, key
, idx
, data
) {
774 if (!strcmp(name
, Z_STRVAL_PP(data
))) {
775 return idx
+ HTTP_MAX_REQUEST_METHOD
;
781 return HTTP_STD_REQUEST_METHOD(id
) || zend_hash_index_exists(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(id
)) ? id
: 0;
786 /* {{{ unsigned long http_request_method_register(char *) */
787 PHP_HTTP_API
unsigned long _http_request_method_register(const char *method TSRMLS_DC
)
791 unsigned long meth_num
= HTTP_G(request
).methods
.custom
.nNextFreeElement
+ HTTP_MAX_REQUEST_METHOD
;
793 Z_ARRVAL(array
) = &HTTP_G(request
).methods
.custom
;
794 add_next_index_string(&array
, estrdup(method
), 0);
796 spprintf(&http_method
, 0, "HTTP_%s", method
);
797 zend_register_long_constant(http_method
, strlen(http_method
) + 1, meth_num
, CONST_CS
, http_module_number TSRMLS_CC
);
804 /* {{{ STATUS http_request_method_unregister(usngigned long) */
805 PHP_HTTP_API STATUS
_http_request_method_unregister(unsigned long method TSRMLS_DC
)
810 if (SUCCESS
!= zend_hash_index_find(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(method
), (void **) &zmethod
)) {
811 http_error_ex(HE_NOTICE
, HTTP_E_REQUEST_METHOD
, "Request method with id %lu does not exist", method
);
815 spprintf(&http_method
, 0, "HTTP_%s", Z_STRVAL_PP(zmethod
));
817 if ( (SUCCESS
!= zend_hash_index_del(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(method
)))
818 || (SUCCESS
!= zend_hash_del(EG(zend_constants
), http_method
, strlen(http_method
) + 1))) {
819 http_error_ex(HE_NOTICE
, HTTP_E_REQUEST_METHOD
, "Could not unregister request method: %s", http_method
);
830 /* {{{ char *http_request_methods[] */
831 static const char *const http_request_methods
[] = {
842 /* WebDAV - RFC 2518 */
850 /* WebDAV Versioning - RFC 3253 */
862 /* WebDAV Access Control - RFC 3744 */
868 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
869 static size_t http_curl_read_callback(void *data
, size_t len
, size_t n
, void *s
)
871 HTTP_CURL_CALLBACK_DATA(s
, http_request_body
*, body
);
873 if (body
->type
!= HTTP_REQUEST_BODY_UPLOADFILE
) {
876 return php_stream_read((php_stream
*) body
->data
, data
, len
* n
);
880 /* {{{ http_curl_callback_ctx http_curl_callback_data(void *) */
881 static http_curl_callback_ctx
*_http_curl_callback_data(void *data TSRMLS_DC
)
883 http_curl_callback_ctx
*ctx
= emalloc(sizeof(http_curl_callback_ctx
));
884 TSRMLS_SET_CTX(ctx
->tsrm_ctx
);
886 return http_request_data_copy(COPY_CONTEXT
, ctx
);
890 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
891 static int http_curl_progress_callback(void *data
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
893 zval
*params_pass
[4], params_local
[4], retval
;
894 HTTP_CURL_CALLBACK_DATA(data
, zval
*, func
);
896 params_pass
[0] = ¶ms_local
[0];
897 params_pass
[1] = ¶ms_local
[1];
898 params_pass
[2] = ¶ms_local
[2];
899 params_pass
[3] = ¶ms_local
[3];
901 INIT_PZVAL(params_pass
[0]);
902 INIT_PZVAL(params_pass
[1]);
903 INIT_PZVAL(params_pass
[2]);
904 INIT_PZVAL(params_pass
[3]);
905 ZVAL_DOUBLE(params_pass
[0], dltotal
);
906 ZVAL_DOUBLE(params_pass
[1], dlnow
);
907 ZVAL_DOUBLE(params_pass
[2], ultotal
);
908 ZVAL_DOUBLE(params_pass
[3], ulnow
);
910 return call_user_function(EG(function_table
), NULL
, func
, &retval
, 4, params_pass TSRMLS_CC
);
914 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
915 static int http_curl_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
917 HTTP_CURL_CALLBACK_DATA(ctx
, http_curl_conv
*, conv
);
921 case CURLINFO_DATA_IN
:
922 if (conv
->response
&& conv
->last_info
== CURLINFO_HEADER_IN
) {
923 phpstr_appends(conv
->response
, HTTP_CRLF
);
925 case CURLINFO_HEADER_IN
:
926 if (conv
->response
) {
927 phpstr_append(conv
->response
, data
, length
);
930 case CURLINFO_DATA_OUT
:
931 if (conv
->request
&& conv
->last_info
== CURLINFO_HEADER_OUT
) {
932 phpstr_appends(conv
->request
, HTTP_CRLF
);
934 case CURLINFO_HEADER_OUT
:
936 phpstr_append(conv
->request
, data
, length
);
942 conv
->last_info
= type
;
948 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
949 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
)
953 if (!options
|| (SUCCESS
!= zend_hash_find(options
, key
, keylen
, (void **) &zoption
))) {
957 if (Z_TYPE_PP(zoption
) != type
) {
960 case IS_BOOL
: convert_to_boolean_ex(zoption
); break;
961 case IS_LONG
: convert_to_long_ex(zoption
); break;
962 case IS_DOUBLE
: convert_to_double_ex(zoption
); break;
963 case IS_STRING
: convert_to_string_ex(zoption
); break;
964 case IS_ARRAY
: convert_to_array_ex(zoption
); break;
965 case IS_OBJECT
: convert_to_object_ex(zoption
); break;
980 * vim600: noet sw=4 ts=4 fdm=marker
981 * vim<600: noet sw=4 ts=4