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_url_api.h"
37 ZEND_EXTERN_MODULE_GLOBALS(http
)
39 #if LIBCURL_VERSION_NUM < 0x070c00
40 # define curl_easy_strerror(code) HTTP_G(request).curl.error
43 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
44 #define HTTP_CURL_INFO_EX(I, X) \
45 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
47 case CURLINFO_STRING: \
50 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
51 add_assoc_string(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
56 case CURLINFO_DOUBLE: \
59 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
60 add_assoc_double(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), d); \
68 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
69 add_assoc_long(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), l); \
75 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
76 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
77 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
78 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
79 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
80 if (!strcasecmp(key, #keyname)) { \
81 convert_to_string_ex(param); \
82 HTTP_CURL_OPT(optname, http_curl_copystr(Z_STRVAL_PP(param))); \
86 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
87 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
88 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
89 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
90 if (!strcasecmp(key, #keyname)) { \
91 convert_to_long_ex(param); \
92 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
98 static const char *const http_request_methods
[HTTP_MAX_REQUEST_METHOD
+ 1];
99 #define http_curl_copystr(s) _http_curl_copystr((s) TSRMLS_CC)
100 static inline char *_http_curl_copystr(const char *str TSRMLS_DC
);
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 *);
110 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
111 PHP_HTTP_API STATUS
_http_request_body_fill(http_request_body
*body
, HashTable
*fields
, HashTable
*files TSRMLS_DC
)
113 if (files
&& (zend_hash_num_elements(files
) > 0)) {
117 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
120 FOREACH_HASH_KEYVAL(fields
, key
, idx
, data
) {
123 convert_to_string_ex(data
);
124 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
125 CURLFORM_COPYNAME
, key
,
126 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
127 CURLFORM_CONTENTSLENGTH
, Z_STRLEN_PP(data
),
130 if (CURLE_OK
!= err
) {
131 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not encode post fields: %s", curl_easy_strerror(err
));
132 curl_formfree(http_post_data
[0]);
142 FOREACH_HASH_VAL(files
, data
) {
144 zval
**file
, **type
, **name
;
145 if ( SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) &&
146 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) &&
147 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
148 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
149 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
150 CURLFORM_FILE
, Z_STRVAL_PP(file
),
151 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
154 if (CURLE_OK
!= err
) {
155 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not encode post files: %s", curl_easy_strerror(err
));
156 curl_formfree(http_post_data
[0]);
160 http_error(E_NOTICE
, HTTP_E_PARAM
, "Post file array entry misses either 'name', 'type' or 'file' entry");
164 body
->type
= HTTP_REQUEST_BODY_CURLPOST
;
165 body
->data
= http_post_data
[0];
172 if (SUCCESS
!= http_urlencode_hash_ex(fields
, 1, NULL
, 0, &encoded
, &encoded_len
)) {
173 http_error(E_WARNING
, HTTP_E_ENCODE
, "Could not encode post data");
177 body
->type
= HTTP_REQUEST_BODY_CSTRING
;
178 body
->data
= encoded
;
179 body
->size
= encoded_len
;
186 /* {{{ void http_request_body_dtor(http_request_body *) */
187 PHP_HTTP_API
void _http_request_body_dtor(http_request_body
*body TSRMLS_DC
)
191 case HTTP_REQUEST_BODY_CSTRING
:
195 case HTTP_REQUEST_BODY_CURLPOST
:
196 curl_formfree(body
->data
);
199 case HTTP_REQUEST_BODY_UPLOADFILE
:
200 php_stream_close(body
->data
);
206 /* {{{ STATUS http_request_ex(CURL *, http_request_method, char *, http_request_body, HashTable, HashTable, phpstr *) */
207 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
)
209 CURLcode result
= CURLE_OK
;
210 STATUS status
= SUCCESS
;
211 zend_bool clean_curl
= 0, range_req
= 0;
214 /* check/init CURL handle */
216 #if LIBCURL_VERSION_NUM >= 0x070c01
220 if (ch
= curl_easy_init()) {
223 http_error(E_WARNING
, HTTP_E_CURL
, "Could not initialize curl");
230 HTTP_CURL_OPT(URL
, url
);
233 HTTP_CURL_OPT(HEADER
, 0);
234 HTTP_CURL_OPT(FILETIME
, 1);
235 HTTP_CURL_OPT(AUTOREFERER
, 1);
236 HTTP_CURL_OPT(READFUNCTION
, http_curl_read_callback
);
237 HTTP_CURL_OPT(WRITEFUNCTION
, http_curl_write_callback
);
238 HTTP_CURL_OPT(HEADERFUNCTION
, http_curl_write_callback
);
241 HTTP_CURL_OPT(WRITEDATA
, response
);
242 HTTP_CURL_OPT(WRITEHEADER
, response
);
245 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
246 HTTP_CURL_OPT(NOSIGNAL
, 1);
248 #if LIBCURL_VERSION_NUM < 0x070c00
249 HTTP_CURL_OPT(ERRORBUFFER
, HTTP_G(request
).curl
.error
);
252 /* progress callback */
253 if (zoption
= http_curl_getopt(options
, "onprogress", 0)) {
254 HTTP_CURL_OPT(NOPROGRESS
, 0);
255 HTTP_CURL_OPT(PROGRESSFUNCTION
, http_curl_progress_callback
);
256 HTTP_CURL_OPT(PROGRESSDATA
, zoption
);
258 HTTP_CURL_OPT(NOPROGRESS
, 1);
262 if (zoption
= http_curl_getopt(options
, "ondebug", 0)) {
263 HTTP_CURL_OPT(VERBOSE
, 1);
264 HTTP_CURL_OPT(DEBUGFUNCTION
, http_curl_debug_callback
);
265 HTTP_CURL_OPT(DEBUGDATA
, zoption
);
267 HTTP_CURL_OPT(VERBOSE
, 0);
271 if (zoption
= http_curl_getopt(options
, "proxyhost", IS_STRING
)) {
272 HTTP_CURL_OPT(PROXY
, http_curl_copystr(Z_STRVAL_P(zoption
)));
274 if (zoption
= http_curl_getopt(options
, "proxyport", IS_LONG
)) {
275 HTTP_CURL_OPT(PROXYPORT
, Z_LVAL_P(zoption
));
278 if (zoption
= http_curl_getopt(options
, "proxyauth", IS_STRING
)) {
279 HTTP_CURL_OPT(PROXYUSERPWD
, http_curl_copystr(Z_STRVAL_P(zoption
)));
281 #if LIBCURL_VERSION_NUM >= 0x070a07
283 if (zoption
= http_curl_getopt(options
, "proxyauthtype", IS_LONG
)) {
284 HTTP_CURL_OPT(PROXYAUTH
, Z_LVAL_P(zoption
));
289 /* outgoing interface */
290 if (zoption
= http_curl_getopt(options
, "interface", IS_STRING
)) {
291 HTTP_CURL_OPT(INTERFACE
, http_curl_copystr(Z_STRVAL_P(zoption
)));
295 if (zoption
= http_curl_getopt(options
, "port", IS_LONG
)) {
296 HTTP_CURL_OPT(PORT
, Z_LVAL_P(zoption
));
300 if (zoption
= http_curl_getopt(options
, "httpauth", IS_STRING
)) {
301 HTTP_CURL_OPT(USERPWD
, http_curl_copystr(Z_STRVAL_P(zoption
)));
303 #if LIBCURL_VERSION_NUM >= 0x070a06
304 if (zoption
= http_curl_getopt(options
, "httpauthtype", IS_LONG
)) {
305 HTTP_CURL_OPT(HTTPAUTH
, Z_LVAL_P(zoption
));
309 /* compress, empty string enables deflate and gzip */
310 if (zoption
= http_curl_getopt(options
, "compress", IS_BOOL
)) {
311 if (Z_LVAL_P(zoption
)) {
312 HTTP_CURL_OPT(ENCODING
, http_curl_copystr(""));
316 /* redirects, defaults to 0 */
317 if (zoption
= http_curl_getopt(options
, "redirect", IS_LONG
)) {
318 HTTP_CURL_OPT(FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
319 HTTP_CURL_OPT(MAXREDIRS
, Z_LVAL_P(zoption
));
320 if (zoption
= http_curl_getopt(options
, "unrestrictedauth", IS_BOOL
)) {
321 HTTP_CURL_OPT(UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
324 HTTP_CURL_OPT(FOLLOWLOCATION
, 0);
328 if (zoption
= http_curl_getopt(options
, "referer", IS_STRING
)) {
329 HTTP_CURL_OPT(REFERER
, http_curl_copystr(Z_STRVAL_P(zoption
)));
332 /* useragent, default "PECL::HTTP/version (PHP/version)" */
333 if (zoption
= http_curl_getopt(options
, "useragent", IS_STRING
)) {
334 HTTP_CURL_OPT(USERAGENT
, http_curl_copystr(Z_STRVAL_P(zoption
)));
336 HTTP_CURL_OPT(USERAGENT
, "PECL::HTTP/" HTTP_PEXT_VERSION
" (PHP/" PHP_VERSION
")");
339 /* additional headers, array('name' => 'value') */
340 if (zoption
= http_curl_getopt(options
, "headers", IS_ARRAY
)) {
343 struct curl_slist
*headers
= NULL
;
345 FOREACH_KEY(zoption
, header_key
, header_idx
) {
348 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &header_val
)) {
349 char header
[1024] = {0};
350 snprintf(header
, 1023, "%s: %s", header_key
, Z_STRVAL_PP(header_val
));
351 headers
= curl_slist_append(headers
, http_curl_copystr(header
));
360 HTTP_CURL_OPT(HTTPHEADER
, headers
);
364 /* cookies, array('name' => 'value') */
365 if (zoption
= http_curl_getopt(options
, "cookies", IS_ARRAY
)) {
366 char *cookie_key
= NULL
;
367 ulong cookie_idx
= 0;
368 phpstr
*qstr
= phpstr_new();
370 FOREACH_KEY(zoption
, cookie_key
, cookie_idx
) {
373 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &cookie_val
)) {
374 phpstr_appendf(qstr
, "%s=%s; ", cookie_key
, Z_STRVAL_PP(cookie_val
));
384 HTTP_CURL_OPT(COOKIE
, http_curl_copystr(qstr
->data
));
390 if (zoption
= http_curl_getopt(options
, "cookiestore", IS_STRING
)) {
391 HTTP_CURL_OPT(COOKIEFILE
, http_curl_copystr(Z_STRVAL_P(zoption
)));
392 HTTP_CURL_OPT(COOKIEJAR
, http_curl_copystr(Z_STRVAL_P(zoption
)));
396 if (zoption
= http_curl_getopt(options
, "resume", IS_LONG
)) {
398 HTTP_CURL_OPT(RESUME_FROM
, Z_LVAL_P(zoption
));
402 if (zoption
= http_curl_getopt(options
, "maxfilesize", IS_LONG
)) {
403 HTTP_CURL_OPT(MAXFILESIZE
, Z_LVAL_P(zoption
));
407 if (zoption
= http_curl_getopt(options
, "lastmodified", IS_LONG
)) {
408 HTTP_CURL_OPT(TIMECONDITION
, range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
);
409 HTTP_CURL_OPT(TIMEVALUE
, Z_LVAL_P(zoption
));
413 if (zoption
= http_curl_getopt(options
, "timeout", IS_LONG
)) {
414 HTTP_CURL_OPT(TIMEOUT
, Z_LVAL_P(zoption
));
417 /* connecttimeout, defaults to 1 */
418 if (zoption
= http_curl_getopt(options
, "connecttimeout", IS_LONG
)) {
419 HTTP_CURL_OPT(CONNECTTIMEOUT
, Z_LVAL_P(zoption
));
421 HTTP_CURL_OPT(CONNECTTIMEOUT
, 1);
425 if (zoption
= http_curl_getopt(options
, "ssl", IS_ARRAY
)) {
430 FOREACH_KEYVAL(zoption
, key
, idx
, param
) {
432 HTTP_CURL_OPT_SSL_STRING(CERT
);
433 #if LIBCURL_VERSION_NUM >= 0x070903
434 HTTP_CURL_OPT_SSL_STRING(CERTTYPE
);
436 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD
);
438 HTTP_CURL_OPT_SSL_STRING(KEY
);
439 HTTP_CURL_OPT_SSL_STRING(KEYTYPE
);
440 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD
);
442 HTTP_CURL_OPT_SSL_STRING(ENGINE
);
443 HTTP_CURL_OPT_SSL_LONG(VERSION
);
445 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER
);
446 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST
);
447 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST
);
450 HTTP_CURL_OPT_STRING(CAINFO
);
451 #if LIBCURL_VERSION_NUM >= 0x070908
452 HTTP_CURL_OPT_STRING(CAPATH
);
454 HTTP_CURL_OPT_STRING(RANDOM_FILE
);
455 HTTP_CURL_OPT_STRING(EGDSOCKET
);
462 /* disable SSL verification by default */
463 HTTP_CURL_OPT(SSL_VERIFYPEER
, 0);
464 HTTP_CURL_OPT(SSL_VERIFYHOST
, 0);
471 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1);
475 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
479 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
483 curl_easy_setopt(ch
, CURLOPT_UPLOAD
, 1);
487 if (http_request_method_exists(0, meth
, NULL
)) {
488 curl_easy_setopt(ch
, CURLOPT_CUSTOMREQUEST
, http_request_method_name(meth
));
490 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Unsupported request method: %d", meth
);
492 goto http_request_end
;
497 /* attach request body */
498 if (body
&& (meth
!= HTTP_GET
) && (meth
!= HTTP_HEAD
)) {
501 case HTTP_REQUEST_BODY_CSTRING
:
502 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, (char *) body
->data
);
503 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, body
->size
);
506 case HTTP_REQUEST_BODY_CURLPOST
:
507 curl_easy_setopt(ch
, CURLOPT_HTTPPOST
, (struct curl_httppost
*) body
->data
);
510 case HTTP_REQUEST_BODY_UPLOADFILE
:
511 case HTTP_REQUEST_BODY_UPLOADDATA
:
512 curl_easy_setopt(ch
, CURLOPT_READDATA
, body
);
513 curl_easy_setopt(ch
, CURLOPT_INFILESIZE
, body
->size
);
517 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Unkown request body type: %d", body
->type
);
519 goto http_request_end
;
524 /* perform request */
525 if (CURLE_OK
== (result
= curl_easy_perform(ch
))) {
529 Z_ARRVAL(array
) = info
;
531 HTTP_CURL_INFO(EFFECTIVE_URL
);
532 #if LIBCURL_VERSION_NUM >= 0x070a07
533 HTTP_CURL_INFO(RESPONSE_CODE
);
535 HTTP_CURL_INFO_EX(HTTP_CODE
, RESPONSE_CODE
);
537 HTTP_CURL_INFO(HTTP_CONNECTCODE
);
538 #if LIBCURL_VERSION_NUM >= 0x070500
539 HTTP_CURL_INFO(FILETIME
);
541 HTTP_CURL_INFO(TOTAL_TIME
);
542 HTTP_CURL_INFO(NAMELOOKUP_TIME
);
543 HTTP_CURL_INFO(CONNECT_TIME
);
544 HTTP_CURL_INFO(PRETRANSFER_TIME
);
545 HTTP_CURL_INFO(STARTTRANSFER_TIME
);
546 #if LIBCURL_VERSION_NUM >= 0x070907
547 HTTP_CURL_INFO(REDIRECT_TIME
);
548 HTTP_CURL_INFO(REDIRECT_COUNT
);
550 HTTP_CURL_INFO(SIZE_UPLOAD
);
551 HTTP_CURL_INFO(SIZE_DOWNLOAD
);
552 HTTP_CURL_INFO(SPEED_DOWNLOAD
);
553 HTTP_CURL_INFO(SPEED_UPLOAD
);
554 HTTP_CURL_INFO(HEADER_SIZE
);
555 HTTP_CURL_INFO(REQUEST_SIZE
);
556 HTTP_CURL_INFO(SSL_VERIFYRESULT
);
557 #if LIBCURL_VERSION_NUM >= 0x070c03
558 /*HTTP_CURL_INFO(SSL_ENGINES); todo: CURLINFO_SLIST */
560 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD
);
561 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD
);
562 HTTP_CURL_INFO(CONTENT_TYPE
);
563 #if LIBCURL_VERSION_NUM >= 0x070a03
564 /*HTTP_CURL_INFO(PRIVATE);*/
566 #if LIBCURL_VERSION_NUM >= 0x070a08
567 HTTP_CURL_INFO(HTTPAUTH_AVAIL
);
568 HTTP_CURL_INFO(PROXYAUTH_AVAIL
);
570 #if LIBCURL_VERSION_NUM >= 0x070c02
571 /*HTTP_CURL_INFO(OS_ERRNO);*/
573 #if LIBCURL_VERSION_NUM >= 0x070c03
574 HTTP_CURL_INFO(NUM_CONNECTS
);
578 http_error_ex(E_WARNING
, HTTP_E_CURL
, "Could not perform request: %s", curl_easy_strerror(result
));
583 /* free strings copied with http_curl_copystr() */
584 zend_llist_clean(&HTTP_G(request
).curl
.copies
);
586 /* clean curl handle if acquired */
588 curl_easy_cleanup(ch
);
592 /* finalize response */
594 phpstr_fix(PHPSTR(response
));
601 /* {{{ char *http_request_method_name(http_request_method) */
602 PHP_HTTP_API
const char *_http_request_method_name(http_request_method m TSRMLS_DC
)
606 if (HTTP_STD_REQUEST_METHOD(m
)) {
607 return http_request_methods
[m
];
610 if (SUCCESS
== zend_hash_index_find(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(m
), (void **) &meth
)) {
611 return Z_STRVAL_PP(meth
);
614 return http_request_methods
[0];
618 /* {{{ unsigned long http_request_method_exists(zend_bool, unsigned long, char *) */
619 PHP_HTTP_API
unsigned long _http_request_method_exists(zend_bool by_name
, unsigned long id
, const char *name TSRMLS_DC
)
624 for (i
= HTTP_NO_REQUEST_METHOD
+ 1; i
< HTTP_MAX_REQUEST_METHOD
; ++i
) {
625 if (!strcmp(name
, http_request_methods
[i
])) {
634 FOREACH_HASH_KEYVAL(&HTTP_G(request
).methods
.custom
, key
, idx
, data
) {
635 if (!strcmp(name
, Z_STRVAL_PP(data
))) {
636 return idx
+ HTTP_MAX_REQUEST_METHOD
;
642 return HTTP_STD_REQUEST_METHOD(id
) || zend_hash_index_exists(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(id
)) ? id
: 0;
647 /* {{{ unsigned long http_request_method_register(char *) */
648 PHP_HTTP_API
unsigned long _http_request_method_register(const char *method TSRMLS_DC
)
652 unsigned long meth_num
= HTTP_G(request
).methods
.custom
.nNextFreeElement
+ HTTP_MAX_REQUEST_METHOD
;
654 Z_ARRVAL(array
) = &HTTP_G(request
).methods
.custom
;
655 add_next_index_string(&array
, estrdup(method
), 0);
657 spprintf(&http_method
, 0, "HTTP_%s", method
);
658 zend_register_long_constant(http_method
, strlen(http_method
) + 1, meth_num
, CONST_CS
, http_module_number TSRMLS_CC
);
665 /* {{{ STATUS http_request_method_unregister(usngigned long) */
666 PHP_HTTP_API STATUS
_http_request_method_unregister(unsigned long method TSRMLS_DC
)
671 if (SUCCESS
!= zend_hash_index_find(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(method
), (void **) &zmethod
)) {
672 http_error_ex(E_NOTICE
, HTTP_E_PARAM
, "Request method with id %lu does not exist", method
);
676 spprintf(&http_method
, 0, "HTTP_%s", Z_STRVAL_PP(zmethod
));
678 if ( (SUCCESS
!= zend_hash_index_del(&HTTP_G(request
).methods
.custom
, HTTP_CUSTOM_REQUEST_METHOD(method
)))
679 || (SUCCESS
!= zend_hash_del(EG(zend_constants
), http_method
, strlen(http_method
) + 1))) {
680 http_error_ex(E_NOTICE
, 0, "Could not unregister request method: %s", http_method
);
690 /* {{{ char *http_request_methods[] */
691 static const char *const http_request_methods
[] = {
702 /* WebDAV - RFC 2518 */
710 /* WebDAV Versioning - RFC 3253 */
722 /* WebDAV Access Control - RFC 3744 */
728 /* {{{ static inline char *http_curl_copystr(char *) */
729 static inline char *_http_curl_copystr(const char *str TSRMLS_DC
)
731 char *new_str
= estrdup(str
);
732 zend_llist_add_element(&HTTP_G(request
).curl
.copies
, &new_str
);
737 /* {{{ static size_t http_curl_write_callback(char *, size_t, size_t, void *) */
738 static size_t http_curl_write_callback(char *buf
, size_t len
, size_t n
, void *s
)
740 return s
? phpstr_append(PHPSTR(s
), buf
, len
* n
) : len
* n
;
744 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
745 static size_t http_curl_read_callback(void *data
, size_t len
, size_t n
, void *s
)
747 static char *offset
= NULL
, *original
= NULL
;
748 http_request_body
*body
= (http_request_body
*) s
;
752 case HTTP_REQUEST_BODY_UPLOADFILE
:
755 return php_stream_read((php_stream
*) body
->data
, data
, len
* n
);
759 case HTTP_REQUEST_BODY_UPLOADDATA
:
763 original
= offset
= s
;
765 if ((avail
= body
->size
- (offset
- original
)) < 1) {
768 if (avail
< (len
* n
)) {
769 memcpy(data
, offset
, avail
);
773 memcpy(data
, offset
, len
* n
);
787 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
788 static int http_curl_progress_callback(void *data
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
790 zval
*params_pass
[4], params_local
[4], retval
, *func
= (zval
*) data
;
793 params_pass
[0] = ¶ms_local
[0];
794 params_pass
[1] = ¶ms_local
[1];
795 params_pass
[2] = ¶ms_local
[2];
796 params_pass
[3] = ¶ms_local
[3];
798 ZVAL_DOUBLE(params_pass
[0], dltotal
);
799 ZVAL_DOUBLE(params_pass
[1], dlnow
);
800 ZVAL_DOUBLE(params_pass
[2], ultotal
);
801 ZVAL_DOUBLE(params_pass
[3], ulnow
);
803 return call_user_function(EG(function_table
), NULL
, func
, &retval
, 4, params_pass TSRMLS_CC
);
807 static int http_curl_debug_callback(CURL
*ch
, curl_infotype type
, char *string
, size_t length
, void *data
)
809 zval
*params_pass
[2], params_local
[2], retval
, *func
= (zval
*) data
;
812 params_pass
[0] = ¶ms_local
[0];
813 params_pass
[1] = ¶ms_local
[1];
815 ZVAL_LONG(params_pass
[0], type
);
816 ZVAL_STRINGL(params_pass
[1], string
, length
, 1);
818 call_user_function(EG(function_table
), NULL
, func
, &retval
, 2, params_pass TSRMLS_CC
);
822 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
823 static inline zval
*_http_curl_getopt_ex(HashTable
*options
, char *key
, size_t keylen
, int type TSRMLS_DC
)
827 if (!options
|| (SUCCESS
!= zend_hash_find(options
, key
, keylen
, (void **) &zoption
))) {
831 if (Z_TYPE_PP(zoption
) != type
) {
834 case IS_BOOL
: convert_to_boolean_ex(zoption
); break;
835 case IS_LONG
: convert_to_long_ex(zoption
); break;
836 case IS_DOUBLE
: convert_to_double_ex(zoption
); break;
837 case IS_STRING
: convert_to_string_ex(zoption
); break;
838 case IS_ARRAY
: convert_to_array_ex(zoption
); break;
839 case IS_OBJECT
: convert_to_object_ex(zoption
); break;
854 * vim600: noet sw=4 ts=4 fdm=marker
855 * vim<600: noet sw=4 ts=4