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 +----------------------------------------------------------------------+
23 # include <winsock2.h>
26 #include <curl/curl.h>
28 #include "phpstr/phpstr.h"
32 #include "php_http_std_defs.h"
33 #include "php_http_api.h"
34 #include "php_http_request_api.h"
35 #include "php_http_request_object.h"
36 #include "php_http_requestpool_object.h"
37 #include "php_http_url_api.h"
39 ZEND_EXTERN_MODULE_GLOBALS(http
)
41 #if LIBCURL_VERSION_NUM < 0x070c00
42 # define curl_easy_strerror(code) HTTP_G(request).curl.error
45 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
46 #define HTTP_CURL_INFO_EX(I, X) \
47 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
49 case CURLINFO_STRING: \
52 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
53 add_assoc_string(&array, pretty_key(http_request_copystr(#X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
58 case CURLINFO_DOUBLE: \
61 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
62 add_assoc_double(&array, pretty_key(http_request_copystr(#X), sizeof(#X)-1, 0, 0), d); \
70 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
71 add_assoc_long(&array, pretty_key(http_request_copystr(#X), sizeof(#X)-1, 0, 0), l); \
77 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
78 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
79 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
80 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
81 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
82 if (!strcasecmp(key, #keyname)) { \
83 convert_to_string_ex(param); \
84 HTTP_CURL_OPT(optname, http_request_copystr(Z_STRVAL_PP(param))); \
88 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
89 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
90 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
91 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
92 if (!strcasecmp(key, #keyname)) { \
93 convert_to_long_ex(param); \
94 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_write_callback(char *, size_t, size_t, void *);
105 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
106 static int http_curl_progress_callback(void *, double, double, double, double);
107 static int http_curl_debug_callback(CURL
*, curl_infotype
, char *, size_t, void *);
109 static void http_request_pool_freebody(http_request_body
**body
);
110 static void http_request_pool_responsehandler(zval
**req TSRMLS_DC
);
111 static inline STATUS
http_request_pool_select(http_request_pool
*pool
);
112 static inline void http_request_pool_perform(http_request_pool
*pool
);
114 /* {{{ char *http_request_copystr(char *) */
115 PHP_HTTP_API
char *_http_request_copystr(const char *str TSRMLS_DC
)
117 char *new_str
= estrdup(str
);
118 zend_llist_add_element(&HTTP_G(request
).curl
.copies
, &new_str
);
123 /* {{{ http_request_body *http_request_body_new() */
124 PHP_HTTP_API http_request_body
*_http_request_body_new(TSRMLS_D
)
126 http_request_body
*body
= ecalloc(1, sizeof(http_request_body
));
131 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
132 PHP_HTTP_API STATUS
_http_request_body_fill(http_request_body
*body
, HashTable
*fields
, HashTable
*files TSRMLS_DC
)
134 if (files
&& (zend_hash_num_elements(files
) > 0)) {
138 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
141 FOREACH_HASH_KEYVAL(fields
, key
, idx
, data
) {
144 convert_to_string_ex(data
);
145 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
146 CURLFORM_COPYNAME
, key
,
147 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
148 CURLFORM_CONTENTSLENGTH
, Z_STRLEN_PP(data
),
151 if (CURLE_OK
!= err
) {
152 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not encode post fields: %s", curl_easy_strerror(err
));
153 curl_formfree(http_post_data
[0]);
163 FOREACH_HASH_VAL(files
, data
) {
165 zval
**file
, **type
, **name
;
166 if ( SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) &&
167 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) &&
168 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
169 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
170 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
171 CURLFORM_FILE
, Z_STRVAL_PP(file
),
172 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
175 if (CURLE_OK
!= err
) {
176 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not encode post files: %s", curl_easy_strerror(err
));
177 curl_formfree(http_post_data
[0]);
181 http_error(E_NOTICE
, HTTP_E_PARAM
, "Post file array entry misses either 'name', 'type' or 'file' entry");
185 body
->type
= HTTP_REQUEST_BODY_CURLPOST
;
186 body
->data
= http_post_data
[0];
193 if (SUCCESS
!= http_urlencode_hash_ex(fields
, 1, NULL
, 0, &encoded
, &encoded_len
)) {
194 http_error(E_WARNING
, HTTP_E_ENCODE
, "Could not encode post data");
198 body
->type
= HTTP_REQUEST_BODY_CSTRING
;
199 body
->data
= encoded
;
200 body
->size
= encoded_len
;
207 /* {{{ void http_request_body_dtor(http_request_body *) */
208 PHP_HTTP_API
void _http_request_body_dtor(http_request_body
*body TSRMLS_DC
)
213 case HTTP_REQUEST_BODY_CSTRING
:
219 case HTTP_REQUEST_BODY_CURLPOST
:
220 curl_formfree(body
->data
);
223 case HTTP_REQUEST_BODY_UPLOADFILE
:
224 php_stream_close(body
->data
);
231 /* {{{ void http_request_body_free(http_request_body *) */
232 PHP_HTTP_API
void _http_request_body_free(http_request_body
*body TSRMLS_DC
)
235 http_request_body_dtor(body
);
241 /* {{{ STATUS http_request_init(CURL *, http_request_method, char *, http_request_body *, HashTable *, phpstr *) */
242 PHP_HTTP_API STATUS
_http_request_init(CURL
*ch
, http_request_method meth
, const char *url
, http_request_body
*body
, HashTable
*options
, phpstr
*response TSRMLS_DC
)
245 zend_bool range_req
= 0;
247 /* reset CURL handle */
248 #if LIBCURL_VERSION_NUM >= 0x070c01
254 HTTP_CURL_OPT(URL
, url
);
258 HTTP_CURL_OPT(WRITEDATA
, response
);
259 HTTP_CURL_OPT(WRITEHEADER
, response
);
262 HTTP_CURL_OPT(HEADER
, 0);
263 HTTP_CURL_OPT(FILETIME
, 1);
264 HTTP_CURL_OPT(AUTOREFERER
, 1);
265 HTTP_CURL_OPT(READFUNCTION
, http_curl_read_callback
);
266 HTTP_CURL_OPT(WRITEFUNCTION
, http_curl_write_callback
);
267 HTTP_CURL_OPT(HEADERFUNCTION
, http_curl_write_callback
);
269 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
270 HTTP_CURL_OPT(NOSIGNAL
, 1);
272 #if LIBCURL_VERSION_NUM < 0x070c00
273 HTTP_CURL_OPT(ERRORBUFFER
, HTTP_G(request
).curl
.error
);
276 /* progress callback */
277 if (zoption
= http_curl_getopt(options
, "onprogress", 0)) {
278 HTTP_CURL_OPT(NOPROGRESS
, 0);
279 HTTP_CURL_OPT(PROGRESSFUNCTION
, http_curl_progress_callback
);
280 HTTP_CURL_OPT(PROGRESSDATA
, zoption
);
282 HTTP_CURL_OPT(NOPROGRESS
, 1);
286 if (zoption
= http_curl_getopt(options
, "ondebug", 0)) {
287 HTTP_CURL_OPT(VERBOSE
, 1);
288 HTTP_CURL_OPT(DEBUGFUNCTION
, http_curl_debug_callback
);
289 HTTP_CURL_OPT(DEBUGDATA
, zoption
);
291 HTTP_CURL_OPT(VERBOSE
, 0);
295 if (zoption
= http_curl_getopt(options
, "proxyhost", IS_STRING
)) {
296 HTTP_CURL_OPT(PROXY
, http_request_copystr(Z_STRVAL_P(zoption
)));
298 if (zoption
= http_curl_getopt(options
, "proxyport", IS_LONG
)) {
299 HTTP_CURL_OPT(PROXYPORT
, Z_LVAL_P(zoption
));
302 if (zoption
= http_curl_getopt(options
, "proxyauth", IS_STRING
)) {
303 HTTP_CURL_OPT(PROXYUSERPWD
, http_request_copystr(Z_STRVAL_P(zoption
)));
305 #if LIBCURL_VERSION_NUM >= 0x070a07
307 if (zoption
= http_curl_getopt(options
, "proxyauthtype", IS_LONG
)) {
308 HTTP_CURL_OPT(PROXYAUTH
, Z_LVAL_P(zoption
));
313 /* outgoing interface */
314 if (zoption
= http_curl_getopt(options
, "interface", IS_STRING
)) {
315 HTTP_CURL_OPT(INTERFACE
, http_request_copystr(Z_STRVAL_P(zoption
)));
319 if (zoption
= http_curl_getopt(options
, "port", IS_LONG
)) {
320 HTTP_CURL_OPT(PORT
, Z_LVAL_P(zoption
));
324 if (zoption
= http_curl_getopt(options
, "httpauth", IS_STRING
)) {
325 HTTP_CURL_OPT(USERPWD
, http_request_copystr(Z_STRVAL_P(zoption
)));
327 #if LIBCURL_VERSION_NUM >= 0x070a06
328 if (zoption
= http_curl_getopt(options
, "httpauthtype", IS_LONG
)) {
329 HTTP_CURL_OPT(HTTPAUTH
, Z_LVAL_P(zoption
));
333 /* compress, empty string enables deflate and gzip */
334 if (zoption
= http_curl_getopt(options
, "compress", IS_BOOL
)) {
335 if (Z_LVAL_P(zoption
)) {
336 HTTP_CURL_OPT(ENCODING
, http_request_copystr(""));
340 /* redirects, defaults to 0 */
341 if (zoption
= http_curl_getopt(options
, "redirect", IS_LONG
)) {
342 HTTP_CURL_OPT(FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
343 HTTP_CURL_OPT(MAXREDIRS
, Z_LVAL_P(zoption
));
344 if (zoption
= http_curl_getopt(options
, "unrestrictedauth", IS_BOOL
)) {
345 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
348 HTTP_CURL_OPT(FOLLOWLOCATION
, 0);
352 if (zoption
= http_curl_getopt(options
, "referer", IS_STRING
)) {
353 HTTP_CURL_OPT(REFERER
, http_request_copystr(Z_STRVAL_P(zoption
)));
356 /* useragent, default "PECL::HTTP/version (PHP/version)" */
357 if (zoption
= http_curl_getopt(options
, "useragent", IS_STRING
)) {
358 HTTP_CURL_OPT(USERAGENT
, http_request_copystr(Z_STRVAL_P(zoption
)));
360 HTTP_CURL_OPT(USERAGENT
, "PECL::HTTP/" HTTP_PEXT_VERSION
" (PHP/" PHP_VERSION
")");
363 /* additional headers, array('name' => 'value') */
364 if (zoption
= http_curl_getopt(options
, "headers", IS_ARRAY
)) {
367 struct curl_slist
*headers
= NULL
;
369 FOREACH_KEY(zoption
, header_key
, header_idx
) {
372 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &header_val
)) {
373 char header
[1024] = {0};
374 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
375 headers
= curl_slist_append(headers
, http_request_copystr(header
));
384 HTTP_CURL_OPT(HTTPHEADER
, headers
);
388 /* cookies, array('name' => 'value') */
389 if (zoption
= http_curl_getopt(options
, "cookies", IS_ARRAY
)) {
390 char *cookie_key
= NULL
;
391 ulong cookie_idx
= 0;
392 phpstr
*qstr
= phpstr_new();
394 FOREACH_KEY(zoption
, cookie_key
, cookie_idx
) {
397 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &cookie_val
)) {
398 phpstr_appendf(qstr
, "%s=%s; ", cookie_key
, Z_STRVAL_PP(cookie_val
));
408 HTTP_CURL_OPT(COOKIE
, http_request_copystr(qstr
->data
));
414 if (zoption
= http_curl_getopt(options
, "cookiestore", IS_STRING
)) {
415 HTTP_CURL_OPT(COOKIEFILE
, http_request_copystr(Z_STRVAL_P(zoption
)));
416 HTTP_CURL_OPT(COOKIEJAR
, http_request_copystr(Z_STRVAL_P(zoption
)));
420 if (zoption
= http_curl_getopt(options
, "resume", IS_LONG
)) {
422 HTTP_CURL_OPT(RESUME_FROM
, Z_LVAL_P(zoption
));
426 if (zoption
= http_curl_getopt(options
, "maxfilesize", IS_LONG
)) {
427 HTTP_CURL_OPT(MAXFILESIZE
, Z_LVAL_P(zoption
));
431 if (zoption
= http_curl_getopt(options
, "lastmodified", IS_LONG
)) {
432 HTTP_CURL_OPT(TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
433 HTTP_CURL_OPT(TIMEVALUE
, Z_LVAL_P(zoption
));
437 if (zoption
= http_curl_getopt(options
, "timeout", IS_LONG
)) {
438 HTTP_CURL_OPT(TIMEOUT
, Z_LVAL_P(zoption
));
441 /* connecttimeout, defaults to 1 */
442 if (zoption
= http_curl_getopt(options
, "connecttimeout", IS_LONG
)) {
443 HTTP_CURL_OPT(CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
445 HTTP_CURL_OPT(CONNECTTIMEOUT
, 1);
449 if (zoption
= http_curl_getopt(options
, "ssl", IS_ARRAY
)) {
454 FOREACH_KEYVAL(zoption
, key
, idx
, param
) {
456 HTTP_CURL_OPT_SSL_STRING(CERT
);
457 #if LIBCURL_VERSION_NUM >= 0x070903
458 HTTP_CURL_OPT_SSL_STRING(CERTTYPE
);
460 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD
);
462 HTTP_CURL_OPT_SSL_STRING(KEY
);
463 HTTP_CURL_OPT_SSL_STRING(KEYTYPE
);
464 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD
);
466 HTTP_CURL_OPT_SSL_STRING(ENGINE
);
467 HTTP_CURL_OPT_SSL_LONG(VERSION
);
469 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER
);
470 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST
);
471 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST
);
474 HTTP_CURL_OPT_STRING(CAINFO
);
475 #if LIBCURL_VERSION_NUM >= 0x070908
476 HTTP_CURL_OPT_STRING(CAPATH
);
478 HTTP_CURL_OPT_STRING(RANDOM_FILE
);
479 HTTP_CURL_OPT_STRING(EGDSOCKET
);
486 /* disable SSL verification by default */
487 HTTP_CURL_OPT(SSL_VERIFYPEER
, 0);
488 HTTP_CURL_OPT(SSL_VERIFYHOST
, 0);
495 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1);
499 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
503 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
507 curl_easy_setopt(ch
, CURLOPT_UPLOAD
, 1);
511 if (http_request_method_exists(0, meth
, NULL
)) {
512 curl_easy_setopt(ch
, CURLOPT_CUSTOMREQUEST
, http_request_method_name(meth
));
514 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Unsupported request method: %d", meth
);
520 /* attach request body */
521 if (body
&& (meth
!= HTTP_GET
) && (meth
!= HTTP_HEAD
)) {
524 case HTTP_REQUEST_BODY_CSTRING
:
525 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, (char *) body
->data
);
526 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, body
->size
);
529 case HTTP_REQUEST_BODY_CURLPOST
:
530 curl_easy_setopt(ch
, CURLOPT_HTTPPOST
, (struct curl_httppost
*) body
->data
);
533 case HTTP_REQUEST_BODY_UPLOADFILE
:
534 case HTTP_REQUEST_BODY_UPLOADDATA
:
535 curl_easy_setopt(ch
, CURLOPT_READDATA
, body
);
536 curl_easy_setopt(ch
, CURLOPT_INFILESIZE
, body
->size
);
540 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Unknown request body type: %d", body
->type
);
550 /* {{{ STATUS http_request_exec(CURL *, HashTable *) */
551 PHP_HTTP_API STATUS
_http_request_exec(CURL
*ch
, HashTable
*info TSRMLS_DC
)
555 /* perform request */
556 if (CURLE_OK
!= (result
= curl_easy_perform(ch
))) {
557 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not perform request: %s", curl_easy_strerror(result
));
562 http_request_info(ch
, info
);
569 /* {{{ void http_request_info(CURL *, HashTable *) */
570 PHP_HTTP_API
void _http_request_info(CURL
*ch
, HashTable
*info TSRMLS_DC
)
573 Z_ARRVAL(array
) = info
;
575 HTTP_CURL_INFO(EFFECTIVE_URL
);
576 #if LIBCURL_VERSION_NUM >= 0x070a07
577 HTTP_CURL_INFO(RESPONSE_CODE
);
579 HTTP_CURL_INFO_EX(HTTP_CODE
, RESPONSE_CODE
);
581 HTTP_CURL_INFO(HTTP_CONNECTCODE
);
582 #if LIBCURL_VERSION_NUM >= 0x070500
583 HTTP_CURL_INFO(FILETIME
);
585 HTTP_CURL_INFO(TOTAL_TIME
);
586 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
587 HTTP_CURL_INFO(CONNECT_TIME
);
588 HTTP_CURL_INFO(PRETRANSFER_TIME
);
589 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
590 #if LIBCURL_VERSION_NUM >= 0x070907
591 HTTP_CURL_INFO(REDIRECT_TIME
);
592 HTTP_CURL_INFO(REDIRECT_COUNT
);
594 HTTP_CURL_INFO(SIZE_UPLOAD
);
595 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
596 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
597 HTTP_CURL_INFO(SPEED_UPLOAD
);
598 HTTP_CURL_INFO(HEADER_SIZE
);
599 HTTP_CURL_INFO(REQUEST_SIZE
);
600 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
601 #if LIBCURL_VERSION_NUM >= 0x070c03
602 /*HTTP_CURL_INFO(SSL_ENGINES); todo: CURLINFO_SLIST */
604 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
605 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
606 HTTP_CURL_INFO(CONTENT_TYPE
);
607 #if LIBCURL_VERSION_NUM >= 0x070a03
608 /*HTTP_CURL_INFO(PRIVATE);*/
610 #if LIBCURL_VERSION_NUM >= 0x070a08
611 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
612 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
614 #if LIBCURL_VERSION_NUM >= 0x070c02
615 /*HTTP_CURL_INFO(OS_ERRNO);*/
617 #if LIBCURL_VERSION_NUM >= 0x070c03
618 HTTP_CURL_INFO(NUM_CONNECTS
);
623 /* {{{ STATUS http_request_ex(CURL *, http_request_method, char *, http_request_body, HashTable, HashTable, phpstr *) */
624 PHP_HTTP_API STATUS
_http_request_ex(CURL
*ch
, http_request_method meth
, const char *url
, http_request_body
*body
, HashTable
*options
, HashTable
*info
, phpstr
*response TSRMLS_DC
)
626 if (SUCCESS
!= http_request_init(ch
, meth
, url
, body
, options
, response
)) {
629 return http_request_exec(ch
, info
);
633 /* {{{ char *http_request_method_name(http_request_method) */
634 PHP_HTTP_API
const char *_http_request_method_name(http_request_method m TSRMLS_DC
)
638 if (HTTP_STD_REQUEST_METHOD(m
)) {
639 return http_request_methods
[m
];
642 if (SUCCESS
== zend_hash_index_find(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(m
), (void **) &meth
)) {
643 return Z_STRVAL_PP(meth
);
646 return http_request_methods
[0];
650 /* {{{ unsigned long http_request_method_exists(zend_bool, unsigned long, char *) */
651 PHP_HTTP_API
unsigned long _http_request_method_exists(zend_bool by_name
, unsigned long id
, const char *name TSRMLS_DC
)
656 for (i
= HTTP_NO_REQUEST_METHOD
+ 1; i
< HTTP_MAX_REQUEST_METHOD
; ++i
) {
657 if (!strcmp(name
, http_request_methods
[i
])) {
666 FOREACH_HASH_KEYVAL(&HTTP_G(request
).methods
.custom
, key
, idx
, data
) {
667 if (!strcmp(name
, Z_STRVAL_PP(data
))) {
668 return idx
+ HTTP_MAX_REQUEST_METHOD
;
674 return HTTP_STD_REQUEST_METHOD(id
) || zend_hash_index_exists(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(id
)) ? id
: 0;
679 /* {{{ unsigned long http_request_method_register(char *) */
680 PHP_HTTP_API
unsigned long _http_request_method_register(const char *method TSRMLS_DC
)
684 unsigned long meth_num
= HTTP_G(request
).methods
.custom
.nNextFreeElement
+ HTTP_MAX_REQUEST_METHOD
;
686 Z_ARRVAL(array
) = &HTTP_G(request
).methods
.custom
;
687 add_next_index_string(&array
, estrdup(method
), 0);
689 spprintf(&http_method
, 0, "HTTP_%s", method
);
690 zend_register_long_constant(http_method
, strlen(http_method
) + 1, meth_num
, CONST_CS
, http_module_number TSRMLS_CC
);
697 /* {{{ STATUS http_request_method_unregister(usngigned long) */
698 PHP_HTTP_API STATUS
_http_request_method_unregister(unsigned long method TSRMLS_DC
)
703 if (SUCCESS
!= zend_hash_index_find(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(method
), (void **) &zmethod
)) {
704 http_error_ex(E_NOTICE
, HTTP_E_PARAM
, "Request method with id %lu does not exist", method
);
708 spprintf(&http_method
, 0, "HTTP_%s", Z_STRVAL_PP(zmethod
));
710 if ( (SUCCESS
!= zend_hash_index_del(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(method
)))
711 || (SUCCESS
!= zend_hash_del(EG(zend_constants
), http_method
, strlen(http_method
) + 1))) {
712 http_error_ex(E_NOTICE
, 0, "Could not unregister request method: %s", http_method
);
723 /* {{{ http_request_pool *http_request_pool_init(http_request_pool *) */
724 PHP_HTTP_API http_request_pool
*_http_request_pool_init(http_request_pool
*pool TSRMLS_DC
)
728 if ((free_pool
= (!pool
))) {
729 pool
= emalloc(sizeof(http_request_pool
));
734 if (!(pool
->ch
= curl_multi_init())) {
735 http_error(E_WARNING
, HTTP_E_CURL
, "Could not initialize curl");
743 pool
->unfinished
= 0;
744 zend_llist_init(&pool
->handles
, sizeof(zval
*), (llist_dtor_func_t
) ZVAL_PTR_DTOR
, 0);
745 zend_llist_init(&pool
->bodies
, sizeof(http_request_body
*), (llist_dtor_func_t
) http_request_pool_freebody
, 0);
751 /* {{{ STATUS http_request_pool_attach(http_request_pool *, zval *) */
752 PHP_HTTP_API STATUS
_http_request_pool_attach(http_request_pool
*pool
, zval
*request TSRMLS_DC
)
754 getObjectEx(http_request_object
, req
, request
);
757 http_error(E_WARNING
, HTTP_E_CURL
, "HttpRequest object is already member of an HttpRequestPool");
760 http_request_body
*body
= http_request_body_new();
761 zval
*info
= GET_PROP_EX(req
, request
, responseInfo
);
763 if (SUCCESS
!= http_request_object_requesthandler(req
, request
, body
)) {
765 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not initialize HttpRequest object for attaching to the HttpRequestPool");
766 } else if (CURLM_OK
!= (code
= curl_multi_add_handle(pool
->ch
, req
->ch
))) {
767 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not attach HttpRequest object to the HttpRequestPool: %s", curl_multi_strerror(code
));
770 zval_add_ref(&request
);
771 zend_llist_add_element(&pool
->handles
, &request
);
772 zend_llist_add_element(&pool
->bodies
, &body
);
780 /* {{{ STATUS http_request_pool_detach(http_request_pool *, zval *) */
781 PHP_HTTP_API STATUS
_http_request_pool_detach(http_request_pool
*pool
, zval
*request TSRMLS_DC
)
783 getObjectEx(http_request_object
, req
, request
);
785 if (!req
->attached
) {
786 http_error(E_WARNING
, HTTP_E_CURL
, "HttpRequest object is not attached to an HttpRequestPool");
790 if (CURLM_OK
!= (code
= curl_multi_remove_handle(pool
->ch
, req
->ch
))) {
791 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not detach HttpRequest object from the HttpRequestPool: %s", curl_multi_strerror(code
));
794 zval_ptr_dtor(&request
);
802 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
803 PHP_HTTP_API STATUS
_http_request_pool_send(http_request_pool
*pool TSRMLS_DC
)
805 http_request_pool_perform(pool
);
806 while (pool
->unfinished
) {
807 if (SUCCESS
!= http_request_pool_select(pool
)) {
808 http_error(E_WARNING
, HTTP_E_CURL
, "Socket error");
811 http_request_pool_perform(pool
);
813 zend_llist_apply(&pool
->handles
, (llist_apply_func_t
) http_request_pool_responsehandler TSRMLS_CC
);
820 /* {{{ static void http_request_pool_free_body(http_request_body *) */
821 static void http_request_pool_freebody(http_request_body
**body
)
824 http_request_body_free(*body
);
828 /* {{{ static void http_request_pool_responsehandler(zval **) */
829 static void http_request_pool_responsehandler(zval
**req TSRMLS_DC
)
831 getObjectEx(http_request_object
, obj
, *req
);
832 http_request_object_responsehandler(obj
, *req
, NULL
);
836 /* {{{ static inline STATUS http_request_pool_select(http_request_pool *) */
837 static inline STATUS
http_request_pool_select(http_request_pool
*pool
)
841 struct timeval timeout
= {1, 0};
847 curl_multi_fdset(pool
->ch
, &R
, &W
, &E
, &MAX
);
848 return (-1 != select(MAX
+ 1, &R
, &W
, &E
, &timeout
)) ? SUCCESS
: FAILURE
;
852 /* {{{ static inline void http_request_pool_perform(http_request_pool *) */
853 static inline void http_request_pool_perform(http_request_pool
*pool
)
855 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(pool
->ch
, &pool
->unfinished
));
859 /* {{{ char *http_request_methods[] */
860 static const char *const http_request_methods
[] = {
871 /* WebDAV - RFC 2518 */
879 /* WebDAV Versioning - RFC 3253 */
891 /* WebDAV Access Control - RFC 3744 */
897 /* {{{ static size_t http_curl_write_callback(char *, size_t, size_t, void *) */
898 static size_t http_curl_write_callback(char *buf
, size_t len
, size_t n
, void *s
)
900 return s
? phpstr_append(PHPSTR(s
), buf
, len
* n
) : len
* n
;
904 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
905 static size_t http_curl_read_callback(void *data
, size_t len
, size_t n
, void *s
)
907 static char *offset
= NULL
, *original
= NULL
;
908 http_request_body
*body
= (http_request_body
*) s
;
912 case HTTP_REQUEST_BODY_UPLOADFILE
:
915 return php_stream_read((php_stream
*) body
->data
, data
, len
* n
);
919 case HTTP_REQUEST_BODY_UPLOADDATA
:
923 original
= offset
= s
;
925 if ((avail
= body
->size
- (offset
- original
)) < 1) {
928 if (avail
< (len
* n
)) {
929 memcpy(data
, offset
, avail
);
933 memcpy(data
, offset
, len
* n
);
947 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
948 static int http_curl_progress_callback(void *data
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
950 zval
*params_pass
[4], params_local
[4], retval
, *func
= (zval
*) data
;
953 params_pass
[0] = ¶ms_local
[0];
954 params_pass
[1] = ¶ms_local
[1];
955 params_pass
[2] = ¶ms_local
[2];
956 params_pass
[3] = ¶ms_local
[3];
958 ZVAL_DOUBLE(params_pass
[0], dltotal
);
959 ZVAL_DOUBLE(params_pass
[1], dlnow
);
960 ZVAL_DOUBLE(params_pass
[2], ultotal
);
961 ZVAL_DOUBLE(params_pass
[3], ulnow
);
963 return call_user_function(EG(function_table
), NULL
, func
, &retval
, 4, params_pass TSRMLS_CC
);
967 static int http_curl_debug_callback(CURL
*ch
, curl_infotype type
, char *string
, size_t length
, void *data
)
969 zval
*params_pass
[2], params_local
[2], retval
, *func
= (zval
*) data
;
972 params_pass
[0] = ¶ms_local
[0];
973 params_pass
[1] = ¶ms_local
[1];
975 ZVAL_LONG(params_pass
[0], type
);
976 ZVAL_STRINGL(params_pass
[1], string
, length
, 1);
978 call_user_function(EG(function_table
), NULL
, func
, &retval
, 2, params_pass TSRMLS_CC
);
982 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
983 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
)
987 if (!options
|| (SUCCESS
!= zend_hash_find(options
, key
, keylen
, (void **) &zoption
))) {
991 if (Z_TYPE_PP(zoption
) != type
) {
994 case IS_BOOL
: convert_to_boolean_ex(zoption
); break;
995 case IS_LONG
: convert_to_long_ex(zoption
); break;
996 case IS_DOUBLE
: convert_to_double_ex(zoption
); break;
997 case IS_STRING
: convert_to_string_ex(zoption
); break;
998 case IS_ARRAY
: convert_to_array_ex(zoption
); break;
999 case IS_OBJECT
: convert_to_object_ex(zoption
); break;
1014 * vim600: noet sw=4 ts=4 fdm=marker
1015 * vim<600: noet sw=4 ts=4