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