- add debug callback
[m6w6/ext-http] / http_curl_api.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
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 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #ifdef PHP_WIN32
23 # include <winsock2.h>
24 #endif
25
26 #include <curl/curl.h>
27
28 #include "phpstr/phpstr.h"
29
30 #include "php.h"
31 #include "php_http.h"
32 #include "php_http_std_defs.h"
33 #include "php_http_api.h"
34 #include "php_http_curl_api.h"
35 #include "php_http_url_api.h"
36
37 ZEND_EXTERN_MODULE_GLOBALS(http)
38
39 #if LIBCURL_VERSION_NUM >= 0x070c01
40 # define http_curl_reset(ch) curl_easy_reset(ch)
41 #else
42 # define http_curl_reset(ch)
43 #endif
44
45 #if LIBCURL_VERSION_NUM < 0x070c00
46 # define http_curl_error(dummy) HTTP_G(curlerr)
47 # define curl_easy_strerror(code) "unkown error"
48 #else
49 # define http_curl_error(code) curl_easy_strerror(code)
50 #endif
51
52 #define http_curl_startup(ch, clean_curl, URL, options, response) \
53 if (!ch) { \
54 if (!(ch = curl_easy_init())) { \
55 http_error(E_WARNING, HTTP_E_CURL, "Could not initialize curl"); \
56 return FAILURE; \
57 } \
58 clean_curl = 1; \
59 } else { \
60 http_curl_reset(ch); \
61 } \
62 http_curl_setopts(ch, URL, options, response);
63
64 #define http_curl_perform(ch, clean_curl, response) \
65 { \
66 CURLcode result; \
67 if (CURLE_OK != (result = curl_easy_perform(ch))) { \
68 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not perform request: %s", curl_easy_strerror(result)); \
69 http_curl_cleanup(ch, clean_curl, response); \
70 return FAILURE; \
71 } \
72 }
73
74 #define http_curl_cleanup(ch, clean_curl, response) \
75 zend_llist_clean(&HTTP_G(to_free)); \
76 if (clean_curl) { \
77 curl_easy_cleanup(ch); \
78 ch = NULL; \
79 } \
80 phpstr_fix(PHPSTR(response))
81
82 #define http_curl_copystr(s) _http_curl_copystr((s) TSRMLS_CC)
83 static inline char *_http_curl_copystr(const char *str TSRMLS_DC);
84
85 #define http_curl_setopts(c, u, o, r) _http_curl_setopts((c), (u), (o), (r) TSRMLS_CC)
86 static void _http_curl_setopts(CURL *ch, const char *url, HashTable *options, phpstr *response TSRMLS_DC);
87
88 #define http_curl_getopt(o, k, t) _http_curl_getopt_ex((o), (k), sizeof(k), (t) TSRMLS_CC)
89 #define http_curl_getopt_ex(o, k, l, t) _http_curl_getopt_ex((o), (k), (l), (t) TSRMLS_CC)
90 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC);
91
92 static size_t http_curl_write_callback(char *, size_t, size_t, void *);
93 static int http_curl_progress_callback(void *, double, double, double, double);
94 static int http_curl_debug_callback(CURL *, curl_infotype, char *, size_t, void *);
95
96 #define http_curl_getinfo(c, h) _http_curl_getinfo((c), (h) TSRMLS_CC)
97 static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC);
98
99 /* {{{ static inline char *http_curl_copystr(char *) */
100 static inline char *_http_curl_copystr(const char *str TSRMLS_DC)
101 {
102 char *new_str = estrdup(str);
103 zend_llist_add_element(&HTTP_G(to_free), &new_str);
104 return new_str;
105 }
106 /* }}} */
107
108 /* {{{ static size_t http_curl_write_callback(char *, size_t, size_t, void *) */
109 static size_t http_curl_write_callback(char *buf, size_t len, size_t n, void *s)
110 {
111 return s ? phpstr_append(PHPSTR(s), buf, len * n) : len * n;
112 }
113 /* }}} */
114
115 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
116 static int http_curl_progress_callback(void *data, double dltotal, double dlnow, double ultotal, double ulnow)
117 {
118 int i;
119 zval *params_pass[4], params_local[4], retval, *func = (zval *) data;
120 TSRMLS_FETCH();
121
122 for (i = 0; i < 5; ++i) {
123 params_pass[i] = &params_local[i];
124 }
125
126 ZVAL_DOUBLE(params_pass[0], dltotal);
127 ZVAL_DOUBLE(params_pass[1], dlnow);
128 ZVAL_DOUBLE(params_pass[2], ultotal);
129 ZVAL_DOUBLE(params_pass[3], ulnow);
130
131 return call_user_function(EG(function_table), NULL, func, &retval, 4, params_pass TSRMLS_CC);
132 }
133 /* }}} */
134
135 static int http_curl_debug_callback(CURL *ch, curl_infotype type, char *string, size_t length, void *data)
136 {
137 int i;
138 zval *params_pass[2], params_local[2], retval, *func = (zval *) data;
139 TSRMLS_FETCH();
140
141 params_pass[0] = &params_local[0];
142 params_pass[1] = &params_local[1];
143
144 ZVAL_LONG(params_pass[0], type);
145 ZVAL_STRINGL(params_pass[1], string, length, 1);
146
147 call_user_function(EG(function_table), NULL, func, &retval, 2, params_pass TSRMLS_CC);
148
149 return 0;
150 }
151 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
152 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC)
153 {
154 zval **zoption;
155
156 if (!options || (SUCCESS != zend_hash_find(options, key, keylen, (void **) &zoption))) {
157 return NULL;
158 }
159
160 if (Z_TYPE_PP(zoption) != type) {
161 switch (type)
162 {
163 case IS_BOOL: convert_to_boolean_ex(zoption); break;
164 case IS_LONG: convert_to_long_ex(zoption); break;
165 case IS_DOUBLE: convert_to_double_ex(zoption); break;
166 case IS_STRING: convert_to_string_ex(zoption); break;
167 case IS_ARRAY: convert_to_array_ex(zoption); break;
168 case IS_OBJECT: convert_to_object_ex(zoption); break;
169 default:
170 break;
171 }
172 }
173
174 return *zoption;
175 }
176 /* }}} */
177
178 /* {{{ static void http_curl_setopts(CURL *, char *, HashTable *, phpstr *) */
179 static void _http_curl_setopts(CURL *ch, const char *url, HashTable *options, phpstr *response TSRMLS_DC)
180 {
181 zval *zoption;
182 zend_bool range_req = 0;
183
184 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
185
186 /* standard options */
187 if (url) {
188 HTTP_CURL_OPT(URL, url);
189 }
190
191 HTTP_CURL_OPT(HEADER, 0);
192 HTTP_CURL_OPT(FILETIME, 1);
193 HTTP_CURL_OPT(NOPROGRESS, 1);
194 HTTP_CURL_OPT(AUTOREFERER, 1);
195 HTTP_CURL_OPT(WRITEFUNCTION, http_curl_write_callback);
196 HTTP_CURL_OPT(HEADERFUNCTION, http_curl_write_callback);
197
198 if (response) {
199 HTTP_CURL_OPT(WRITEDATA, response);
200 HTTP_CURL_OPT(WRITEHEADER, response);
201 }
202
203 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
204 HTTP_CURL_OPT(NOSIGNAL, 1);
205 #endif
206 #if LIBCURL_VERSION_NUM < 0x070c00
207 HTTP_CURL_OPT(ERRORBUFFER, HTTP_G(curlerr));
208 #endif
209
210 /* progress callback */
211 if (zoption = http_curl_getopt(options, "onprogress", 0)) {
212 HTTP_CURL_OPT(NOPROGRESS, 0);
213 HTTP_CURL_OPT(PROGRESSFUNCTION, http_curl_progress_callback);
214 HTTP_CURL_OPT(PROGRESSDATA, zoption);
215 } else {
216 HTTP_CURL_OPT(NOPROGRESS, 1);
217 }
218
219 /* debug callback */
220 if (zoption = http_curl_getopt(options, "ondebug", 0)) {
221 HTTP_CURL_OPT(VERBOSE, 1);
222 HTTP_CURL_OPT(DEBUGFUNCTION, http_curl_debug_callback);
223 HTTP_CURL_OPT(DEBUGDATA, zoption);
224 } else {
225 HTTP_CURL_OPT(VERBOSE, 0);
226 }
227
228 /* proxy */
229 if (zoption = http_curl_getopt(options, "proxyhost", IS_STRING)) {
230 HTTP_CURL_OPT(PROXY, http_curl_copystr(Z_STRVAL_P(zoption)));
231 /* port */
232 if (zoption = http_curl_getopt(options, "proxyport", IS_LONG)) {
233 HTTP_CURL_OPT(PROXYPORT, Z_LVAL_P(zoption));
234 }
235 /* user:pass */
236 if (zoption = http_curl_getopt(options, "proxyauth", IS_STRING)) {
237 HTTP_CURL_OPT(PROXYUSERPWD, http_curl_copystr(Z_STRVAL_P(zoption)));
238 }
239 #if LIBCURL_VERSION_NUM >= 0x070a07
240 /* auth method */
241 if (zoption = http_curl_getopt(options, "proxyauthtype", IS_LONG)) {
242 HTTP_CURL_OPT(PROXYAUTH, Z_LVAL_P(zoption));
243 }
244 #endif
245 }
246
247 /* outgoing interface */
248 if (zoption = http_curl_getopt(options, "interface", IS_STRING)) {
249 HTTP_CURL_OPT(INTERFACE, http_curl_copystr(Z_STRVAL_P(zoption)));
250 }
251
252 /* another port */
253 if (zoption = http_curl_getopt(options, "port", IS_LONG)) {
254 HTTP_CURL_OPT(PORT, Z_LVAL_P(zoption));
255 }
256
257 /* auth */
258 if (zoption = http_curl_getopt(options, "httpauth", IS_STRING)) {
259 HTTP_CURL_OPT(USERPWD, http_curl_copystr(Z_STRVAL_P(zoption)));
260 }
261 #if LIBCURL_VERSION_NUM >= 0x070a06
262 if (zoption = http_curl_getopt(options, "httpauthtype", IS_LONG)) {
263 HTTP_CURL_OPT(HTTPAUTH, Z_LVAL_P(zoption));
264 }
265 #endif
266
267 /* compress, empty string enables deflate and gzip */
268 if (zoption = http_curl_getopt(options, "compress", IS_BOOL)) {
269 if (Z_LVAL_P(zoption)) {
270 HTTP_CURL_OPT(ENCODING, http_curl_copystr(""));
271 }
272 }
273
274 /* redirects, defaults to 0 */
275 if (zoption = http_curl_getopt(options, "redirect", IS_LONG)) {
276 HTTP_CURL_OPT(FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
277 HTTP_CURL_OPT(MAXREDIRS, Z_LVAL_P(zoption));
278 if (zoption = http_curl_getopt(options, "unrestrictedauth", IS_BOOL)) {
279 HTTP_CURL_OPT(UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
280 }
281 } else {
282 HTTP_CURL_OPT(FOLLOWLOCATION, 0);
283 }
284
285 /* referer */
286 if (zoption = http_curl_getopt(options, "referer", IS_STRING)) {
287 HTTP_CURL_OPT(REFERER, http_curl_copystr(Z_STRVAL_P(zoption)));
288 }
289
290 /* useragent, default "PECL::HTTP/version (PHP/version)" */
291 if (zoption = http_curl_getopt(options, "useragent", IS_STRING)) {
292 HTTP_CURL_OPT(USERAGENT, http_curl_copystr(Z_STRVAL_P(zoption)));
293 } else {
294 HTTP_CURL_OPT(USERAGENT,
295 "PECL::HTTP/" HTTP_PEXT_VERSION " (PHP/" PHP_VERSION ")");
296 }
297
298 /* additional headers, array('name' => 'value') */
299 if (zoption = http_curl_getopt(options, "headers", IS_ARRAY)) {
300 char *header_key;
301 long header_idx;
302 struct curl_slist *headers = NULL;
303
304 FOREACH_KEY(zoption, header_key, header_idx) {
305 if (header_key) {
306 zval **header_val;
307 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &header_val)) {
308 char header[1024] = {0};
309 snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val));
310 headers = curl_slist_append(headers, http_curl_copystr(header));
311 }
312
313 /* reset */
314 header_key = NULL;
315 }
316 }
317
318 if (headers) {
319 HTTP_CURL_OPT(HTTPHEADER, headers);
320 }
321 }
322
323 /* cookies, array('name' => 'value') */
324 if (zoption = http_curl_getopt(options, "cookies", IS_ARRAY)) {
325 char *cookie_key = NULL;
326 long cookie_idx = 0;
327 phpstr *qstr = phpstr_new();
328
329 FOREACH_KEY(zoption, cookie_key, cookie_idx) {
330 if (cookie_key) {
331 zval **cookie_val;
332 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &cookie_val)) {
333 phpstr_appendf(qstr, "%s=%s; ", cookie_key, Z_STRVAL_PP(cookie_val));
334 }
335
336 /* reset */
337 cookie_key = NULL;
338 }
339 }
340
341 if (qstr->used) {
342 phpstr_fix(qstr);
343 HTTP_CURL_OPT(COOKIE, http_curl_copystr(qstr->data));
344 }
345 phpstr_free(qstr);
346 }
347
348 /* cookiestore */
349 if (zoption = http_curl_getopt(options, "cookiestore", IS_STRING)) {
350 HTTP_CURL_OPT(COOKIEFILE, http_curl_copystr(Z_STRVAL_P(zoption)));
351 HTTP_CURL_OPT(COOKIEJAR, http_curl_copystr(Z_STRVAL_P(zoption)));
352 }
353
354 /* resume */
355 if (zoption = http_curl_getopt(options, "resume", IS_LONG)) {
356 range_req = 1;
357 HTTP_CURL_OPT(RESUME_FROM, Z_LVAL_P(zoption));
358 }
359
360 /* maxfilesize */
361 if (zoption = http_curl_getopt(options, "maxfilesize", IS_LONG)) {
362 HTTP_CURL_OPT(MAXFILESIZE, Z_LVAL_P(zoption));
363 }
364
365 /* lastmodified */
366 if (zoption = http_curl_getopt(options, "lastmodified", IS_LONG)) {
367 HTTP_CURL_OPT(TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE);
368 HTTP_CURL_OPT(TIMEVALUE, Z_LVAL_P(zoption));
369 }
370
371 /* timeout */
372 if (zoption = http_curl_getopt(options, "timeout", IS_LONG)) {
373 HTTP_CURL_OPT(TIMEOUT, Z_LVAL_P(zoption));
374 }
375
376 /* connecttimeout, defaults to 1 */
377 if (zoption = http_curl_getopt(options, "connecttimeout", IS_LONG)) {
378 HTTP_CURL_OPT(CONNECTTIMEOUT, Z_LVAL_P(zoption));
379 } else {
380 HTTP_CURL_OPT(CONNECTTIMEOUT, 1);
381 }
382
383 /* ssl */
384 if (zoption = http_curl_getopt(options, "ssl", IS_ARRAY)) {
385 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
386 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
387 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
388 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
389 if (!strcasecmp(key, #keyname)) { \
390 convert_to_string_ex(param); \
391 HTTP_CURL_OPT(optname, http_curl_copystr(Z_STRVAL_PP(param))); \
392 key = NULL; \
393 continue; \
394 }
395 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
396 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
397 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
398 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
399 if (!strcasecmp(key, #keyname)) { \
400 convert_to_long_ex(param); \
401 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
402 key = NULL; \
403 continue; \
404 }
405
406 long idx;
407 char *key = NULL;
408 zval **param;
409
410 FOREACH_KEYVAL(zoption, key, idx, param) {
411 if (key) {fprintf(stderr, "%s\n", key);
412 HTTP_CURL_OPT_SSL_STRING(CERT);
413 #if LIBCURL_VERSION_NUM >= 0x070903
414 HTTP_CURL_OPT_SSL_STRING(CERTTYPE);
415 #endif
416 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD);
417
418 HTTP_CURL_OPT_SSL_STRING(KEY);
419 HTTP_CURL_OPT_SSL_STRING(KEYTYPE);
420 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD);
421
422 HTTP_CURL_OPT_SSL_STRING(ENGINE);
423 HTTP_CURL_OPT_SSL_LONG(VERSION);
424
425 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER);
426 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST);
427 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST);
428
429
430 HTTP_CURL_OPT_STRING(CAINFO);
431 #if LIBCURL_VERSION_NUM >= 0x070908
432 HTTP_CURL_OPT_STRING(CAPATH);
433 #endif
434 HTTP_CURL_OPT_STRING(RANDOM_FILE);
435 HTTP_CURL_OPT_STRING(EGDSOCKET);
436
437 /* reset key */
438 key = NULL;
439 }
440 }
441 } else {
442 /* disable SSL verification by default */
443 HTTP_CURL_OPT(SSL_VERIFYPEER, 0);
444 HTTP_CURL_OPT(SSL_VERIFYHOST, 0);
445 }
446 }
447 /* }}} */
448
449 /* {{{ static inline http_curl_getinfo(CURL, HashTable *) */
450 static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC)
451 {
452 zval array;
453 Z_ARRVAL(array) = info;
454
455 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
456 #define HTTP_CURL_INFO_EX(I, X) \
457 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
458 { \
459 case CURLINFO_STRING: \
460 { \
461 char *c; \
462 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
463 add_assoc_string(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
464 } \
465 } \
466 break; \
467 \
468 case CURLINFO_DOUBLE: \
469 { \
470 double d; \
471 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
472 add_assoc_double(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), d); \
473 } \
474 } \
475 break; \
476 \
477 case CURLINFO_LONG: \
478 { \
479 long l; \
480 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
481 add_assoc_long(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), l); \
482 } \
483 } \
484 break; \
485 }
486
487 HTTP_CURL_INFO(EFFECTIVE_URL);
488
489 #if LIBCURL_VERSION_NUM >= 0x070a07
490 HTTP_CURL_INFO(RESPONSE_CODE);
491 #else
492 HTTP_CURL_INFO_EX(HTTP_CODE, RESPONSE_CODE);
493 #endif
494 HTTP_CURL_INFO(HTTP_CONNECTCODE);
495
496 #if LIBCURL_VERSION_NUM >= 0x070500
497 HTTP_CURL_INFO(FILETIME);
498 #endif
499 HTTP_CURL_INFO(TOTAL_TIME);
500 HTTP_CURL_INFO(NAMELOOKUP_TIME);
501 HTTP_CURL_INFO(CONNECT_TIME);
502 HTTP_CURL_INFO(PRETRANSFER_TIME);
503 HTTP_CURL_INFO(STARTTRANSFER_TIME);
504 #if LIBCURL_VERSION_NUM >= 0x070907
505 HTTP_CURL_INFO(REDIRECT_TIME);
506 HTTP_CURL_INFO(REDIRECT_COUNT);
507 #endif
508
509 HTTP_CURL_INFO(SIZE_UPLOAD);
510 HTTP_CURL_INFO(SIZE_DOWNLOAD);
511 HTTP_CURL_INFO(SPEED_DOWNLOAD);
512 HTTP_CURL_INFO(SPEED_UPLOAD);
513
514 HTTP_CURL_INFO(HEADER_SIZE);
515 HTTP_CURL_INFO(REQUEST_SIZE);
516
517 HTTP_CURL_INFO(SSL_VERIFYRESULT);
518 #if LIBCURL_VERSION_NUM >= 0x070c03
519 /*HTTP_CURL_INFO(SSL_ENGINES);
520 todo: CURLINFO_SLIST */
521 #endif
522
523 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD);
524 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD);
525 HTTP_CURL_INFO(CONTENT_TYPE);
526
527 #if LIBCURL_VERSION_NUM >= 0x070a03
528 /*HTTP_CURL_INFO(PRIVATE);*/
529 #endif
530
531 #if LIBCURL_VERSION_NUM >= 0x070a08
532 HTTP_CURL_INFO(HTTPAUTH_AVAIL);
533 HTTP_CURL_INFO(PROXYAUTH_AVAIL);
534 #endif
535
536 #if LIBCURL_VERSION_NUM >= 0x070c02
537 /*HTTP_CURL_INFO(OS_ERRNO);*/
538 #endif
539
540 #if LIBCURL_VERSION_NUM >= 0x070c03
541 HTTP_CURL_INFO(NUM_CONNECTS);
542 #endif
543 }
544 /* }}} */
545
546 /* {{{ STATUS http_get_ex(CURL *, char *, HashTable *, HashTable *, phpstr *) */
547 PHP_HTTP_API STATUS _http_get_ex(CURL *ch, const char *URL, HashTable *options, HashTable *info, phpstr *response TSRMLS_DC)
548 {
549 zend_bool clean_curl = 0;
550
551 http_curl_startup(ch, clean_curl, URL, options, response);
552 curl_easy_setopt(ch, CURLOPT_HTTPGET, 1);
553 http_curl_perform(ch, clean_curl, response);
554
555 if (info) {
556 http_curl_getinfo(ch, info);
557 }
558
559 http_curl_cleanup(ch, clean_curl, response);
560
561 return SUCCESS;
562 }
563
564 /* {{{ STATUS http_head_ex(CURL *, char *, HashTable *, HashTable *, phpstr *) */
565 PHP_HTTP_API STATUS _http_head_ex(CURL *ch, const char *URL, HashTable *options,HashTable *info, phpstr *response TSRMLS_DC)
566 {
567 zend_bool clean_curl = 0;
568
569 http_curl_startup(ch, clean_curl, URL, options, response);
570 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
571 http_curl_perform(ch, clean_curl, response);
572
573 if (info) {
574 http_curl_getinfo(ch, info);
575 }
576
577 http_curl_cleanup(ch, clean_curl, response);
578
579 return SUCCESS;
580 }
581
582 /* {{{ STATUS http_post_data_ex(CURL *, char *, char *, size_t, HashTable *, HashTable *, phpstr *) */
583 PHP_HTTP_API STATUS _http_post_data_ex(CURL *ch, const char *URL, char *postdata,
584 size_t postdata_len, HashTable *options, HashTable *info, phpstr *response TSRMLS_DC)
585 {
586 zend_bool clean_curl = 0;
587
588 http_curl_startup(ch, clean_curl, URL, options, response);
589 curl_easy_setopt(ch, CURLOPT_POST, 1);
590 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, postdata);
591 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, postdata_len);
592 http_curl_perform(ch, clean_curl, response);
593
594 if (info) {
595 http_curl_getinfo(ch, info);
596 }
597
598 http_curl_cleanup(ch, clean_curl, response);
599
600 return SUCCESS;
601 }
602 /* }}} */
603
604 /* {{{ STATUS http_post_array_ex(CURL *, char *, HashTable *, HashTable *, HashTable *, phpstr *) */
605 PHP_HTTP_API STATUS _http_post_array_ex(CURL *ch, const char *URL, HashTable *postarray,
606 HashTable *options, HashTable *info, phpstr *response TSRMLS_DC)
607 {
608 STATUS status;
609 char *encoded;
610 size_t encoded_len;
611
612 if (SUCCESS != http_urlencode_hash_ex(postarray, 1, NULL, 0, &encoded, &encoded_len)) {
613 http_error(E_WARNING, HTTP_E_ENCODE, "Could not encode post data");
614 return FAILURE;
615 }
616
617 status = http_post_data_ex(ch, URL, encoded, encoded_len, options, info, response);
618 efree(encoded);
619
620 return status;
621 }
622 /* }}} */
623
624 /* {{{ STATUS http_post_curldata_ex(CURL *, char *, curl_httppost *, HashTable *, HashTable *, phpstr *) */
625 PHP_HTTP_API STATUS _http_post_curldata_ex(CURL *ch, const char *URL, struct curl_httppost *curldata,
626 HashTable *options, HashTable *info, phpstr *response TSRMLS_DC)
627 {
628 zend_bool clean_curl = 0;
629
630 http_curl_startup(ch, clean_curl, URL, options, response);
631 curl_easy_setopt(ch, CURLOPT_POST, 1);
632 curl_easy_setopt(ch, CURLOPT_HTTPPOST, curldata);
633 http_curl_perform(ch, clean_curl, response);
634
635 if (info) {
636 http_curl_getinfo(ch, info);
637 }
638
639 http_curl_cleanup(ch, clean_curl, response);
640
641 return SUCCESS;
642 }
643 /* }}} */
644
645 /*
646 * Local variables:
647 * tab-width: 4
648 * c-basic-offset: 4
649 * End:
650 * vim600: noet sw=4 ts=4 fdm=marker
651 * vim<600: noet sw=4 ts=4
652 */