* melt http_api down
[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 # define _WINSOCKAPI_
24 # define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
25 # include <winsock2.h>
26 # include <sys/types.h>
27 #endif
28
29 #include <curl/curl.h>
30 #include <curl/easy.h>
31
32 #include "php.h"
33 #include "php_http.h"
34 #include "php_http_api.h"
35 #include "php_http_curl_api.h"
36 #include "php_http_std_defs.h"
37
38 #include "ext/standard/php_smart_str.h"
39
40 ZEND_DECLARE_MODULE_GLOBALS(http)
41
42 #define http_curl_startup(ch, clean_curl, URL, options) \
43 if (!ch) { \
44 if (!(ch = curl_easy_init())) { \
45 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl"); \
46 return FAILURE; \
47 } \
48 clean_curl = 1; \
49 } \
50 http_curl_initbuf(); \
51 http_curl_setopts(ch, URL, options);
52
53
54 #define http_curl_cleanup(ch, clean_curl) \
55 http_curl_freestr(); \
56 http_curl_freebuf(); \
57 if (clean_curl) { \
58 curl_easy_cleanup(ch); \
59 ch = NULL; \
60 }
61
62 #define http_curl_freestr() \
63 zend_llist_clean(&HTTP_G(to_free))
64
65 #define http_curl_initbuf() http_curl_initbuf_ex(0)
66
67 #define http_curl_initbuf_ex(chunk_size) \
68 { \
69 size_t size = (chunk_size > 0) ? chunk_size : HTTP_CURLBUF_SIZE; \
70 http_curl_freebuf(); \
71 HTTP_G(curlbuf).data = emalloc(size); \
72 HTTP_G(curlbuf).free = size; \
73 HTTP_G(curlbuf).size = size; \
74 }
75
76 #define http_curl_freebuf() \
77 if (HTTP_G(curlbuf).data) { \
78 efree(HTTP_G(curlbuf).data); \
79 HTTP_G(curlbuf).data = NULL; \
80 } \
81 HTTP_G(curlbuf).used = 0; \
82 HTTP_G(curlbuf).free = 0; \
83 HTTP_G(curlbuf).size = 0;
84
85 #define http_curl_copybuf(data, size) \
86 * size = HTTP_G(curlbuf).used; \
87 * data = ecalloc(1, HTTP_G(curlbuf).used + 1); \
88 memcpy(* data, HTTP_G(curlbuf).data, * size);
89
90 #define http_curl_sizebuf(for_size) \
91 { \
92 size_t size = (for_size); \
93 if (size > HTTP_G(curlbuf).free) { \
94 size_t bsize = HTTP_G(curlbuf).size; \
95 while (size > bsize) { \
96 bsize *= 2; \
97 } \
98 HTTP_G(curlbuf).data = erealloc(HTTP_G(curlbuf).data, HTTP_G(curlbuf).used + bsize); \
99 HTTP_G(curlbuf).free += bsize; \
100 } \
101 }
102
103
104 #define http_curl_copystr(s) _http_curl_copystr((s) TSRMLS_CC)
105 static inline char *_http_curl_copystr(const char *str TSRMLS_DC);
106
107 #define http_curl_setopts(c, u, o) _http_curl_setopts((c), (u), (o) TSRMLS_CC)
108 static inline void _http_curl_setopts(CURL *ch, const char *url, HashTable *options TSRMLS_DC);
109
110 #define http_curl_getopt(o, k) _http_curl_getopt((o), (k) TSRMLS_CC, 0)
111 #define http_curl_getopt1(o, k, t1) _http_curl_getopt((o), (k) TSRMLS_CC, 1, (t1))
112 #define http_curl_getopt2(o, k, t1, t2) _http_curl_getopt((o), (k) TSRMLS_CC, 2, (t1), (t2))
113 static inline zval *_http_curl_getopt(HashTable *options, char *key TSRMLS_DC, int checks, ...);
114
115 static size_t http_curl_body_callback(char *, size_t, size_t, void *);
116 static size_t http_curl_hdrs_callback(char *, size_t, size_t, void *);
117
118 #define http_curl_getinfo(c, h) _http_curl_getinfo((c), (h) TSRMLS_CC)
119 static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC);
120 #define http_curl_getinfo_ex(c, i, a) _http_curl_getinfo_ex((c), (i), (a) TSRMLS_CC)
121 static inline void _http_curl_getinfo_ex(CURL *ch, CURLINFO i, zval *array TSRMLS_DC);
122 #define http_curl_getinfoname(i) _http_curl_getinfoname((i) TSRMLS_CC)
123 static inline char *_http_curl_getinfoname(CURLINFO i TSRMLS_DC);
124
125 /* {{{ static inline char *http_curl_copystr(char *) */
126 static inline char *_http_curl_copystr(const char *str TSRMLS_DC)
127 {
128 char *new_str = estrdup(str);
129 zend_llist_add_element(&HTTP_G(to_free), &new_str);
130 return new_str;
131 }
132 /* }}} */
133
134 /* {{{ static size_t http_curl_body_callback(char *, size_t, size_t, void *) */
135 static size_t http_curl_body_callback(char *buf, size_t len, size_t n, void *s)
136 {
137 TSRMLS_FETCH();
138
139 http_curl_sizebuf(len *= n);
140
141 memcpy(HTTP_G(curlbuf).data + HTTP_G(curlbuf).used, buf, len);
142 HTTP_G(curlbuf).free -= len;
143 HTTP_G(curlbuf).used += len;
144 return len;
145 }
146 /* }}} */
147
148 /* {{{ static size_t http_curl_hdrs_callback(char *, size_t, size_t, void *) */
149 static size_t http_curl_hdrs_callback(char *buf, size_t len, size_t n, void *s)
150 {
151 TSRMLS_FETCH();
152
153 /* discard previous headers */
154 if ((HTTP_G(curlbuf).used) && (!strncmp(buf, "HTTP/1.", sizeof("HTTP/1.") - 1))) {
155 http_curl_initbuf();
156 }
157 http_curl_sizebuf(len *= n);
158
159 memcpy(HTTP_G(curlbuf).data + HTTP_G(curlbuf).used, buf, len);
160 HTTP_G(curlbuf).free -= len;
161 HTTP_G(curlbuf).used += len;
162 return len;
163 }
164 /* }}} */
165
166 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, int, ...) */
167 static inline zval *_http_curl_getopt(HashTable *options, char *key TSRMLS_DC, int checks, ...)
168 {
169 zval **zoption;
170 va_list types;
171 int i;
172
173 if (SUCCESS != zend_hash_find(options, key, strlen(key) + 1, (void **) &zoption)) {
174 return NULL;
175 }
176 if (checks < 1) {
177 return *zoption;
178 }
179
180 va_start(types, checks);
181 for (i = 0; i < checks; ++i) {
182 if ((va_arg(types, int)) == (Z_TYPE_PP(zoption))) {
183 va_end(types);
184 return *zoption;
185 }
186 }
187 va_end(types);
188 return NULL;
189 }
190 /* }}} */
191
192 /* {{{ static inline void http_curl_setopts(CURL *, char *, HashTable *) */
193 static inline void _http_curl_setopts(CURL *ch, const char *url, HashTable *options TSRMLS_DC)
194 {
195 zval *zoption;
196
197 /* standard options */
198 curl_easy_setopt(ch, CURLOPT_URL, url);
199 curl_easy_setopt(ch, CURLOPT_HEADER, 0);
200 curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1);
201 curl_easy_setopt(ch, CURLOPT_AUTOREFERER, 1);
202 curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, http_curl_body_callback);
203 curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, http_curl_hdrs_callback);
204 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
205 curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1);
206 #endif
207
208 if ((!options) || (1 > zend_hash_num_elements(options))) {
209 return;
210 }
211
212 /* redirects, defaults to 0 */
213 if (zoption = http_curl_getopt1(options, "redirect", IS_LONG)) {
214 curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
215 curl_easy_setopt(ch, CURLOPT_MAXREDIRS, Z_LVAL_P(zoption));
216 if (zoption = http_curl_getopt2(options, "unrestrictedauth", IS_LONG, IS_BOOL)) {
217 curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
218 }
219 } else {
220 curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 0);
221 }
222
223 /* proxy */
224 if (zoption = http_curl_getopt1(options, "proxyhost", IS_STRING)) {
225 curl_easy_setopt(ch, CURLOPT_PROXY, http_curl_copystr(Z_STRVAL_P(zoption)));
226 /* port */
227 if (zoption = http_curl_getopt1(options, "proxyport", IS_LONG)) {
228 curl_easy_setopt(ch, CURLOPT_PROXYPORT, Z_LVAL_P(zoption));
229 }
230 /* user:pass */
231 if (zoption = http_curl_getopt1(options, "proxyauth", IS_STRING)) {
232 curl_easy_setopt(ch, CURLOPT_PROXYUSERPWD, http_curl_copystr(Z_STRVAL_P(zoption)));
233 }
234 #if LIBCURL_VERSION_NUM > 0x070a06
235 /* auth method */
236 if (zoption = http_curl_getopt1(options, "proxyauthtype", IS_LONG)) {
237 curl_easy_setopt(ch, CURLOPT_PROXYAUTH, Z_LVAL_P(zoption));
238 }
239 #endif
240 }
241
242 /* auth */
243 if (zoption = http_curl_getopt1(options, "httpauth", IS_STRING)) {
244 curl_easy_setopt(ch, CURLOPT_USERPWD, http_curl_copystr(Z_STRVAL_P(zoption)));
245 }
246 #if LIBCURL_VERSION_NUM > 0x070a05
247 if (zoption = http_curl_getopt1(options, "httpauthtype", IS_LONG)) {
248 curl_easy_setopt(ch, CURLOPT_HTTPAUTH, Z_LVAL_P(zoption));
249 }
250 #endif
251
252 /* compress, enabled by default (empty string enables deflate and gzip) */
253 if (zoption = http_curl_getopt2(options, "compress", IS_LONG, IS_BOOL)) {
254 if (Z_LVAL_P(zoption)) {
255 curl_easy_setopt(ch, CURLOPT_ENCODING, "");
256 }
257 }
258
259 /* another port */
260 if (zoption = http_curl_getopt1(options, "port", IS_LONG)) {
261 curl_easy_setopt(ch, CURLOPT_PORT, Z_LVAL_P(zoption));
262 }
263
264 /* referer */
265 if (zoption = http_curl_getopt1(options, "referer", IS_STRING)) {
266 curl_easy_setopt(ch, CURLOPT_REFERER, http_curl_copystr(Z_STRVAL_P(zoption)));
267 }
268
269 /* useragent, default "PECL::HTTP/version (PHP/version)" */
270 if (zoption = http_curl_getopt1(options, "useragent", IS_STRING)) {
271 curl_easy_setopt(ch, CURLOPT_USERAGENT, http_curl_copystr(Z_STRVAL_P(zoption)));
272 } else {
273 curl_easy_setopt(ch, CURLOPT_USERAGENT,
274 "PECL::HTTP/" PHP_EXT_HTTP_VERSION " (PHP/" PHP_VERSION ")");
275 }
276
277 /* cookies, array('name' => 'value') */
278 if (zoption = http_curl_getopt1(options, "cookies", IS_ARRAY)) {
279 char *cookie_key;
280 zval **cookie_val;
281 int key_type;
282 smart_str qstr = {0};
283
284 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
285 while (HASH_KEY_NON_EXISTANT != (key_type = zend_hash_get_current_key_type(Z_ARRVAL_P(zoption)))) {
286 if (key_type == HASH_KEY_IS_STRING) {
287 zend_hash_get_current_key(Z_ARRVAL_P(zoption), &cookie_key, NULL, 0);
288 zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &cookie_val);
289 smart_str_appends(&qstr, cookie_key);
290 smart_str_appendl(&qstr, "=", 1);
291 smart_str_appendl(&qstr, Z_STRVAL_PP(cookie_val), Z_STRLEN_PP(cookie_val));
292 smart_str_appendl(&qstr, "; ", 2);
293 zend_hash_move_forward(Z_ARRVAL_P(zoption));
294 }
295 }
296 smart_str_0(&qstr);
297
298 if (qstr.c) {
299 curl_easy_setopt(ch, CURLOPT_COOKIE, http_curl_copystr(qstr.c));
300 efree(qstr.c);
301 }
302 }
303
304 /* cookiestore */
305 if (zoption = http_curl_getopt1(options, "cookiestore", IS_STRING)) {
306 curl_easy_setopt(ch, CURLOPT_COOKIEFILE, http_curl_copystr(Z_STRVAL_P(zoption)));
307 curl_easy_setopt(ch, CURLOPT_COOKIEJAR, http_curl_copystr(Z_STRVAL_P(zoption)));
308 }
309
310 /* additional headers, array('name' => 'value') */
311 if (zoption = http_curl_getopt1(options, "headers", IS_ARRAY)) {
312 int key_type;
313 char *header_key, header[1024] = {0};
314 zval **header_val;
315 struct curl_slist *headers = NULL;
316
317 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
318 while (HASH_KEY_NON_EXISTANT != (key_type = zend_hash_get_current_key_type(Z_ARRVAL_P(zoption)))) {
319 if (key_type == HASH_KEY_IS_STRING) {
320 zend_hash_get_current_key(Z_ARRVAL_P(zoption), &header_key, NULL, 0);
321 zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &header_val);
322 snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val));
323 headers = curl_slist_append(headers, http_curl_copystr(header));
324 zend_hash_move_forward(Z_ARRVAL_P(zoption));
325 }
326 }
327 if (headers) {
328 curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headers);
329 }
330 }
331 }
332 /* }}} */
333
334 /* {{{ static inline char *http_curl_getinfoname(CURLINFO) */
335 static inline char *_http_curl_getinfoname(CURLINFO i TSRMLS_DC)
336 {
337 #define CASE(I) case CURLINFO_ ##I : { return pretty_key(http_curl_copystr(#I), sizeof(#I)-1, 0, 0); }
338 switch (i)
339 {
340 /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
341 CASE(EFFECTIVE_URL);
342 /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
343 #if LIBCURL_VERSION_NUM > 0x070a06
344 CASE(RESPONSE_CODE);
345 #else
346 CASE(HTTP_CODE);
347 #endif
348 /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
349 CASE(TOTAL_TIME);
350 /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
351 CASE(NAMELOOKUP_TIME);
352 /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
353 CASE(CONNECT_TIME);
354 /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
355 CASE(PRETRANSFER_TIME);
356 /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
357 CASE(SIZE_UPLOAD);
358 /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
359 CASE(SIZE_DOWNLOAD);
360 /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
361 CASE(SPEED_DOWNLOAD);
362 /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
363 CASE(SPEED_UPLOAD);
364 /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
365 CASE(HEADER_SIZE);
366 /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
367 CASE(REQUEST_SIZE);
368 /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
369 CASE(SSL_VERIFYRESULT);
370 /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
371 CASE(FILETIME);
372 /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
373 CASE(CONTENT_LENGTH_DOWNLOAD);
374 /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
375 CASE(CONTENT_LENGTH_UPLOAD);
376 /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
377 CASE(STARTTRANSFER_TIME);
378 /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
379 CASE(CONTENT_TYPE);
380 /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
381 CASE(REDIRECT_TIME);
382 /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
383 CASE(REDIRECT_COUNT);
384 /* CURLINFO_PRIVATE = CURLINFO_STRING +21, * (mike) /
385 CASE(PRIVATE);
386 /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
387 CASE(HTTP_CONNECTCODE);
388 #if LIBCURL_VERSION_NUM > 0x070a07
389 /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
390 CASE(HTTPAUTH_AVAIL);
391 /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
392 CASE(PROXYAUTH_AVAIL);
393 #endif
394 }
395 #undef CASE
396 return NULL;
397 }
398 /* }}} */
399
400 /* {{{ static inline void http_curl_getinfo_ex(CURL, CURLINFO, zval *) */
401 static inline void _http_curl_getinfo_ex(CURL *ch, CURLINFO i, zval *array TSRMLS_DC)
402 {
403 char *key;
404 if (key = http_curl_getinfoname(i)) {
405 switch (i & ~CURLINFO_MASK)
406 {
407 case CURLINFO_STRING:
408 {
409 char *c;
410 if (CURLE_OK == curl_easy_getinfo(ch, i, &c)) {
411 add_assoc_string(array, key, c ? c : "", 1);
412 }
413 }
414 break;
415
416 case CURLINFO_DOUBLE:
417 {
418 double d;
419 if (CURLE_OK == curl_easy_getinfo(ch, i, &d)) {
420 add_assoc_double(array, key, d);
421 }
422 }
423 break;
424
425 case CURLINFO_LONG:
426 {
427 long l;
428 if (CURLE_OK == curl_easy_getinfo(ch, i, &l)) {
429 add_assoc_long(array, key, l);
430 }
431 }
432 break;
433 }
434 }
435 }
436 /* }}} */
437
438 /* {{{ static inline http_curl_getinfo(CURL, HashTable *) */
439 static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC)
440 {
441 zval array;
442 Z_ARRVAL(array) = info;
443
444 #define INFO(I) http_curl_getinfo_ex(ch, CURLINFO_ ##I , &array)
445 /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
446 INFO(EFFECTIVE_URL);
447 #if LIBCURL_VERSION_NUM > 0x070a06
448 /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
449 INFO(RESPONSE_CODE);
450 #else
451 INFO(HTTP_CODE);
452 #endif
453 /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
454 INFO(TOTAL_TIME);
455 /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
456 INFO(NAMELOOKUP_TIME);
457 /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
458 INFO(CONNECT_TIME);
459 /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
460 INFO(PRETRANSFER_TIME);
461 /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
462 INFO(SIZE_UPLOAD);
463 /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
464 INFO(SIZE_DOWNLOAD);
465 /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
466 INFO(SPEED_DOWNLOAD);
467 /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
468 INFO(SPEED_UPLOAD);
469 /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
470 INFO(HEADER_SIZE);
471 /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
472 INFO(REQUEST_SIZE);
473 /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
474 INFO(SSL_VERIFYRESULT);
475 /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
476 INFO(FILETIME);
477 /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
478 INFO(CONTENT_LENGTH_DOWNLOAD);
479 /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
480 INFO(CONTENT_LENGTH_UPLOAD);
481 /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
482 INFO(STARTTRANSFER_TIME);
483 /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
484 INFO(CONTENT_TYPE);
485 /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
486 INFO(REDIRECT_TIME);
487 /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
488 INFO(REDIRECT_COUNT);
489 /* CURLINFO_PRIVATE = CURLINFO_STRING +21, */
490 INFO(PRIVATE);
491 /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
492 INFO(HTTP_CONNECTCODE);
493 #if LIBCURL_VERSION_NUM > 0x070a07
494 /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
495 INFO(HTTPAUTH_AVAIL);
496 /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
497 INFO(PROXYAUTH_AVAIL);
498 #endif
499 #undef INFO
500 }
501 /* }}} */
502
503 /* {{{ STATUS http_get_ex(CURL *, char *, HashTable *, HashTable *, char **, size_t *) */
504 PHP_HTTP_API STATUS _http_get_ex(CURL *ch, const char *URL, HashTable *options,
505 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
506 {
507 zend_bool clean_curl = 0;
508
509 http_curl_startup(ch, clean_curl, URL, options);
510 curl_easy_setopt(ch, CURLOPT_NOBODY, 0);
511 curl_easy_setopt(ch, CURLOPT_POST, 0);
512
513 if (CURLE_OK != curl_easy_perform(ch)) {
514 http_curl_cleanup(ch, clean_curl);
515 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
516 return FAILURE;
517 }
518
519 if (info) {
520 http_curl_getinfo(ch, info);
521 }
522
523 http_curl_copybuf(data, data_len);
524 http_curl_cleanup(ch, clean_curl);
525
526 return SUCCESS;
527 }
528
529 /* {{{ STATUS http_head_ex(CURL *, char *, HashTable *, HashTable *, char **data, size_t *) */
530 PHP_HTTP_API STATUS _http_head_ex(CURL *ch, const char *URL, HashTable *options,
531 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
532 {
533 zend_bool clean_curl = 0;
534
535 http_curl_startup(ch, clean_curl, URL, options);
536 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
537 curl_easy_setopt(ch, CURLOPT_POST, 0);
538
539 if (CURLE_OK != curl_easy_perform(ch)) {
540 http_curl_cleanup(ch, clean_curl);
541 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
542 return FAILURE;
543 }
544
545 if (info) {
546 http_curl_getinfo(ch, info);
547 }
548
549 http_curl_copybuf(data, data_len);
550 http_curl_cleanup(ch, clean_curl);
551
552 return SUCCESS;
553 }
554
555 /* {{{ STATUS http_post_data_ex(CURL *, char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
556 PHP_HTTP_API STATUS _http_post_data_ex(CURL *ch, const char *URL, char *postdata,
557 size_t postdata_len, HashTable *options, HashTable *info, char **data,
558 size_t *data_len TSRMLS_DC)
559 {
560 zend_bool clean_curl = 0;
561
562 http_curl_startup(ch, clean_curl, URL, options);
563 curl_easy_setopt(ch, CURLOPT_POST, 1);
564 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, postdata);
565 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, postdata_len);
566
567 if (CURLE_OK != curl_easy_perform(ch)) {
568 http_curl_cleanup(ch, clean_curl);
569 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
570 return FAILURE;
571 }
572
573 if (info) {
574 http_curl_getinfo(ch, info);
575 }
576
577 http_curl_copybuf(data, data_len);
578 http_curl_cleanup(ch, clean_curl);
579
580 return SUCCESS;
581 }
582 /* }}} */
583
584 /* {{{ STATUS http_post_array_ex(CURL *, char *, HashTable *, HashTable *, HashTable *, char **, size_t *) */
585 PHP_HTTP_API STATUS _http_post_array_ex(CURL *ch, const char *URL, HashTable *postarray,
586 HashTable *options, HashTable *info, char **data, size_t *data_len TSRMLS_DC)
587 {
588 smart_str qstr = {0};
589 STATUS status;
590
591 HTTP_URL_ARGSEP_OVERRIDE;
592 if (php_url_encode_hash_ex(postarray, &qstr, NULL,0,NULL,0,NULL,0,NULL TSRMLS_CC) != SUCCESS) {
593 if (qstr.c) {
594 efree(qstr.c);
595 }
596 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not encode post data");
597 HTTP_URL_ARGSEP_RESTORE;
598 return FAILURE;
599 }
600 smart_str_0(&qstr);
601 HTTP_URL_ARGSEP_RESTORE;
602
603 status = http_post_data_ex(ch, URL, qstr.c, qstr.len, options, info, data, data_len);
604
605 if (qstr.c) {
606 efree(qstr.c);
607 }
608 return status;
609 }
610 /* }}} */
611
612 /* {{{ STATUS http_post_curldata_ex(CURL *, char *, curl_httppost *, HashTable *, HashTable *, char **, size_t *) */
613 PHP_HTTP_API STATUS _http_post_curldata_ex(CURL *ch, const char *URL,
614 struct curl_httppost *curldata, HashTable *options, HashTable *info,
615 char **data, size_t *data_len TSRMLS_DC)
616 {
617 zend_bool clean_curl = 0;
618
619 http_curl_startup(ch, clean_curl, URL, options);
620 curl_easy_setopt(ch, CURLOPT_POST, 1);
621 curl_easy_setopt(ch, CURLOPT_HTTPPOST, curldata);
622
623 if (CURLE_OK != curl_easy_perform(ch)) {
624 http_curl_cleanup(ch, clean_curl);
625 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
626 return FAILURE;
627 }
628
629 if (info) {
630 http_curl_getinfo(ch, info);
631 }
632
633 http_curl_copybuf(data, data_len);
634 http_curl_cleanup(ch, clean_curl);
635
636 return SUCCESS;
637 }
638 /* }}} */
639
640 /*
641 * Local variables:
642 * tab-width: 4
643 * c-basic-offset: 4
644 * End:
645 * vim600: noet sw=4 ts=4 fdm=marker
646 * vim<600: noet sw=4 ts=4
647 */