* strlen("") => sizeof("")-1
[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 } else {
257 curl_easy_setopt(ch, CURLOPT_ENCODING, "");
258 }
259
260 /* another port */
261 if (zoption = http_curl_getopt1(options, "port", IS_LONG)) {
262 curl_easy_setopt(ch, CURLOPT_PORT, Z_LVAL_P(zoption));
263 }
264
265 /* referer */
266 if (zoption = http_curl_getopt1(options, "referer", IS_STRING)) {
267 curl_easy_setopt(ch, CURLOPT_REFERER, http_curl_copystr(Z_STRVAL_P(zoption)));
268 }
269
270 /* useragent, default "PECL::HTTP/version (PHP/version)" */
271 if (zoption = http_curl_getopt1(options, "useragent", IS_STRING)) {
272 curl_easy_setopt(ch, CURLOPT_USERAGENT, http_curl_copystr(Z_STRVAL_P(zoption)));
273 } else {
274 curl_easy_setopt(ch, CURLOPT_USERAGENT,
275 "PECL::HTTP/" PHP_EXT_HTTP_VERSION " (PHP/" PHP_VERSION ")");
276 }
277
278 /* cookies, array('name' => 'value') */
279 if (zoption = http_curl_getopt1(options, "cookies", IS_ARRAY)) {
280 char *cookie_key;
281 zval **cookie_val;
282 int key_type;
283 smart_str qstr = {0};
284
285 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
286 while (HASH_KEY_NON_EXISTANT != (key_type = zend_hash_get_current_key_type(Z_ARRVAL_P(zoption)))) {
287 if (key_type == HASH_KEY_IS_STRING) {
288 zend_hash_get_current_key(Z_ARRVAL_P(zoption), &cookie_key, NULL, 0);
289 zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &cookie_val);
290 smart_str_appends(&qstr, cookie_key);
291 smart_str_appendl(&qstr, "=", 1);
292 smart_str_appendl(&qstr, Z_STRVAL_PP(cookie_val), Z_STRLEN_PP(cookie_val));
293 smart_str_appendl(&qstr, "; ", 2);
294 zend_hash_move_forward(Z_ARRVAL_P(zoption));
295 }
296 }
297 smart_str_0(&qstr);
298
299 if (qstr.c) {
300 curl_easy_setopt(ch, CURLOPT_COOKIE, http_curl_copystr(qstr.c));
301 efree(qstr.c);
302 }
303 }
304
305 /* cookiestore */
306 if (zoption = http_curl_getopt1(options, "cookiestore", IS_STRING)) {
307 curl_easy_setopt(ch, CURLOPT_COOKIEFILE, http_curl_copystr(Z_STRVAL_P(zoption)));
308 curl_easy_setopt(ch, CURLOPT_COOKIEJAR, http_curl_copystr(Z_STRVAL_P(zoption)));
309 }
310
311 /* additional headers, array('name' => 'value') */
312 if (zoption = http_curl_getopt1(options, "headers", IS_ARRAY)) {
313 int key_type;
314 char *header_key, header[1024] = {0};
315 zval **header_val;
316 struct curl_slist *headers = NULL;
317
318 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
319 while (HASH_KEY_NON_EXISTANT != (key_type = zend_hash_get_current_key_type(Z_ARRVAL_P(zoption)))) {
320 if (key_type == HASH_KEY_IS_STRING) {
321 zend_hash_get_current_key(Z_ARRVAL_P(zoption), &header_key, NULL, 0);
322 zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &header_val);
323 snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val));
324 headers = curl_slist_append(headers, http_curl_copystr(header));
325 zend_hash_move_forward(Z_ARRVAL_P(zoption));
326 }
327 }
328 if (headers) {
329 curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headers);
330 }
331 }
332 }
333 /* }}} */
334
335 /* {{{ static inline char *http_curl_getinfoname(CURLINFO) */
336 static inline char *_http_curl_getinfoname(CURLINFO i TSRMLS_DC)
337 {
338 #define CASE(I) case CURLINFO_ ##I : { return pretty_key(http_curl_copystr(#I), sizeof(#I)-1, 0, 0); }
339 switch (i)
340 {
341 /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
342 CASE(EFFECTIVE_URL);
343 /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
344 #if LIBCURL_VERSION_NUM > 0x070a06
345 CASE(RESPONSE_CODE);
346 #else
347 CASE(HTTP_CODE);
348 #endif
349 /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
350 CASE(TOTAL_TIME);
351 /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
352 CASE(NAMELOOKUP_TIME);
353 /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
354 CASE(CONNECT_TIME);
355 /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
356 CASE(PRETRANSFER_TIME);
357 /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
358 CASE(SIZE_UPLOAD);
359 /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
360 CASE(SIZE_DOWNLOAD);
361 /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
362 CASE(SPEED_DOWNLOAD);
363 /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
364 CASE(SPEED_UPLOAD);
365 /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
366 CASE(HEADER_SIZE);
367 /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
368 CASE(REQUEST_SIZE);
369 /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
370 CASE(SSL_VERIFYRESULT);
371 /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
372 CASE(FILETIME);
373 /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
374 CASE(CONTENT_LENGTH_DOWNLOAD);
375 /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
376 CASE(CONTENT_LENGTH_UPLOAD);
377 /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
378 CASE(STARTTRANSFER_TIME);
379 /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
380 CASE(CONTENT_TYPE);
381 /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
382 CASE(REDIRECT_TIME);
383 /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
384 CASE(REDIRECT_COUNT);
385 /* CURLINFO_PRIVATE = CURLINFO_STRING +21, * (mike) /
386 CASE(PRIVATE);
387 /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
388 CASE(HTTP_CONNECTCODE);
389 #if LIBCURL_VERSION_NUM > 0x070a07
390 /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
391 CASE(HTTPAUTH_AVAIL);
392 /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
393 CASE(PROXYAUTH_AVAIL);
394 #endif
395 }
396 #undef CASE
397 return NULL;
398 }
399 /* }}} */
400
401 /* {{{ static inline void http_curl_getinfo_ex(CURL, CURLINFO, zval *) */
402 static inline void _http_curl_getinfo_ex(CURL *ch, CURLINFO i, zval *array TSRMLS_DC)
403 {
404 char *key;
405 if (key = http_curl_getinfoname(i)) {
406 switch (i & ~CURLINFO_MASK)
407 {
408 case CURLINFO_STRING:
409 {
410 char *c;
411 if (CURLE_OK == curl_easy_getinfo(ch, i, &c)) {
412 add_assoc_string(array, key, c ? c : "", 1);
413 }
414 }
415 break;
416
417 case CURLINFO_DOUBLE:
418 {
419 double d;
420 if (CURLE_OK == curl_easy_getinfo(ch, i, &d)) {
421 add_assoc_double(array, key, d);
422 }
423 }
424 break;
425
426 case CURLINFO_LONG:
427 {
428 long l;
429 if (CURLE_OK == curl_easy_getinfo(ch, i, &l)) {
430 add_assoc_long(array, key, l);
431 }
432 }
433 break;
434 }
435 }
436 }
437 /* }}} */
438
439 /* {{{ static inline http_curl_getinfo(CURL, HashTable *) */
440 static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC)
441 {
442 zval array;
443 Z_ARRVAL(array) = info;
444
445 #define INFO(I) http_curl_getinfo_ex(ch, CURLINFO_ ##I , &array)
446 /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
447 INFO(EFFECTIVE_URL);
448 #if LIBCURL_VERSION_NUM > 0x070a06
449 /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
450 INFO(RESPONSE_CODE);
451 #else
452 INFO(HTTP_CODE);
453 #endif
454 /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
455 INFO(TOTAL_TIME);
456 /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
457 INFO(NAMELOOKUP_TIME);
458 /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
459 INFO(CONNECT_TIME);
460 /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
461 INFO(PRETRANSFER_TIME);
462 /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
463 INFO(SIZE_UPLOAD);
464 /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
465 INFO(SIZE_DOWNLOAD);
466 /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
467 INFO(SPEED_DOWNLOAD);
468 /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
469 INFO(SPEED_UPLOAD);
470 /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
471 INFO(HEADER_SIZE);
472 /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
473 INFO(REQUEST_SIZE);
474 /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
475 INFO(SSL_VERIFYRESULT);
476 /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
477 INFO(FILETIME);
478 /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
479 INFO(CONTENT_LENGTH_DOWNLOAD);
480 /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
481 INFO(CONTENT_LENGTH_UPLOAD);
482 /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
483 INFO(STARTTRANSFER_TIME);
484 /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
485 INFO(CONTENT_TYPE);
486 /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
487 INFO(REDIRECT_TIME);
488 /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
489 INFO(REDIRECT_COUNT);
490 /* CURLINFO_PRIVATE = CURLINFO_STRING +21, */
491 INFO(PRIVATE);
492 /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
493 INFO(HTTP_CONNECTCODE);
494 #if LIBCURL_VERSION_NUM > 0x070a07
495 /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
496 INFO(HTTPAUTH_AVAIL);
497 /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
498 INFO(PROXYAUTH_AVAIL);
499 #endif
500 #undef INFO
501 }
502 /* }}} */
503
504 /* {{{ STATUS http_get_ex(CURL *, char *, HashTable *, HashTable *, char **, size_t *) */
505 PHP_HTTP_API STATUS _http_get_ex(CURL *ch, const char *URL, HashTable *options,
506 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
507 {
508 zend_bool clean_curl = 0;
509
510 http_curl_startup(ch, clean_curl, URL, options);
511 curl_easy_setopt(ch, CURLOPT_NOBODY, 0);
512 curl_easy_setopt(ch, CURLOPT_POST, 0);
513
514 if (CURLE_OK != curl_easy_perform(ch)) {
515 http_curl_cleanup(ch, clean_curl);
516 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
517 return FAILURE;
518 }
519
520 if (info) {
521 http_curl_getinfo(ch, info);
522 }
523
524 http_curl_copybuf(data, data_len);
525 http_curl_cleanup(ch, clean_curl);
526
527 return SUCCESS;
528 }
529
530 /* {{{ STATUS http_head_ex(CURL *, char *, HashTable *, HashTable *, char **data, size_t *) */
531 PHP_HTTP_API STATUS _http_head_ex(CURL *ch, const char *URL, HashTable *options,
532 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
533 {
534 zend_bool clean_curl = 0;
535
536 http_curl_startup(ch, clean_curl, URL, options);
537 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
538 curl_easy_setopt(ch, CURLOPT_POST, 0);
539
540 if (CURLE_OK != curl_easy_perform(ch)) {
541 http_curl_cleanup(ch, clean_curl);
542 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
543 return FAILURE;
544 }
545
546 if (info) {
547 http_curl_getinfo(ch, info);
548 }
549
550 http_curl_copybuf(data, data_len);
551 http_curl_cleanup(ch, clean_curl);
552
553 return SUCCESS;
554 }
555
556 /* {{{ STATUS http_post_data_ex(CURL *, char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
557 PHP_HTTP_API STATUS _http_post_data_ex(CURL *ch, const char *URL, char *postdata,
558 size_t postdata_len, HashTable *options, HashTable *info, char **data,
559 size_t *data_len TSRMLS_DC)
560 {
561 zend_bool clean_curl = 0;
562
563 http_curl_startup(ch, clean_curl, URL, options);
564 curl_easy_setopt(ch, CURLOPT_POST, 1);
565 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, postdata);
566 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, postdata_len);
567
568 if (CURLE_OK != curl_easy_perform(ch)) {
569 http_curl_cleanup(ch, clean_curl);
570 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
571 return FAILURE;
572 }
573
574 if (info) {
575 http_curl_getinfo(ch, info);
576 }
577
578 http_curl_copybuf(data, data_len);
579 http_curl_cleanup(ch, clean_curl);
580
581 return SUCCESS;
582 }
583 /* }}} */
584
585 /* {{{ STATUS http_post_array_ex(CURL *, char *, HashTable *, HashTable *, HashTable *, char **, size_t *) */
586 PHP_HTTP_API STATUS _http_post_array_ex(CURL *ch, const char *URL, HashTable *postarray,
587 HashTable *options, HashTable *info, char **data, size_t *data_len TSRMLS_DC)
588 {
589 smart_str qstr = {0};
590 STATUS status;
591
592 HTTP_URL_ARGSEP_OVERRIDE;
593 if (php_url_encode_hash_ex(postarray, &qstr, NULL,0,NULL,0,NULL,0,NULL TSRMLS_CC) != SUCCESS) {
594 if (qstr.c) {
595 efree(qstr.c);
596 }
597 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not encode post data");
598 HTTP_URL_ARGSEP_RESTORE;
599 return FAILURE;
600 }
601 smart_str_0(&qstr);
602 HTTP_URL_ARGSEP_RESTORE;
603
604 status = http_post_data_ex(ch, URL, qstr.c, qstr.len, options, info, data, data_len);
605
606 if (qstr.c) {
607 efree(qstr.c);
608 }
609 return status;
610 }
611 /* }}} */
612
613 /* {{{ STATUS http_post_curldata_ex(CURL *, char *, curl_httppost *, HashTable *, HashTable *, char **, size_t *) */
614 PHP_HTTP_API STATUS _http_post_curldata_ex(CURL *ch, const char *URL,
615 struct curl_httppost *curldata, HashTable *options, HashTable *info,
616 char **data, size_t *data_len TSRMLS_DC)
617 {
618 zend_bool clean_curl = 0;
619
620 http_curl_startup(ch, clean_curl, URL, options);
621 curl_easy_setopt(ch, CURLOPT_POST, 1);
622 curl_easy_setopt(ch, CURLOPT_HTTPPOST, curldata);
623
624 if (CURLE_OK != curl_easy_perform(ch)) {
625 http_curl_cleanup(ch, clean_curl);
626 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
627 return FAILURE;
628 }
629
630 if (info) {
631 http_curl_getinfo(ch, info);
632 }
633
634 http_curl_copybuf(data, data_len);
635 http_curl_cleanup(ch, clean_curl);
636
637 return SUCCESS;
638 }
639 /* }}} */
640
641 /*
642 * Local variables:
643 * tab-width: 4
644 * c-basic-offset: 4
645 * End:
646 * vim600: noet sw=4 ts=4 fdm=marker
647 * vim<600: noet sw=4 ts=4
648 */