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