* use phpstr
[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 "php.h"
29 #include "php_http.h"
30 #include "php_http_api.h"
31 #include "php_http_curl_api.h"
32 #include "php_http_std_defs.h"
33
34 #ifdef ZEND_ENGINE_2
35 # include "ext/standard/php_http.h"
36 #endif
37
38 #include "phpstr/phpstr.h"
39
40 ZEND_DECLARE_MODULE_GLOBALS(http)
41
42 #if LIBCURL_VERSION_NUM >= 0x070c01
43 # define http_curl_reset(ch) curl_easy_reset(ch)
44 #else
45 # define http_curl_reset(ch)
46 #endif
47
48 #if LIBCURL_VERSION_NUM < 0x070c00
49 # define curl_easy_strerror(code) "unkown error"
50 #endif
51
52 #define http_curl_startup(ch, clean_curl, URL, options) \
53 if (!ch) { \
54 if (!(ch = curl_easy_init())) { \
55 php_error_docref(NULL TSRMLS_CC, E_WARNING, "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);
63
64 #define http_curl_perform(ch, clean_curl) \
65 { \
66 CURLcode result; \
67 if (CURLE_OK != (result = curl_easy_perform(ch))) { \
68 http_curl_cleanup(ch, clean_curl); \
69 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request: %s", curl_easy_strerror(result)); \
70 return FAILURE; \
71 } \
72 }
73
74 #define http_curl_cleanup(ch, clean_curl) \
75 phpstr_free(&HTTP_G(curlbuf)); \
76 zend_llist_clean(&HTTP_G(to_free)); \
77 if (clean_curl) { \
78 curl_easy_cleanup(ch); \
79 ch = NULL; \
80 }
81
82 #define http_curl_copybuf(d, l) \
83 phpstr_data(&HTTP_G(curlbuf), d, l)
84
85 #define http_curl_copystr(s) _http_curl_copystr((s) TSRMLS_CC)
86 static inline char *_http_curl_copystr(const char *str TSRMLS_DC);
87
88 #define http_curl_setopts(c, u, o) _http_curl_setopts((c), (u), (o) TSRMLS_CC)
89 static void _http_curl_setopts(CURL *ch, const char *url, HashTable *options TSRMLS_DC);
90
91 #define http_curl_getopt(o, k) _http_curl_getopt((o), (k) TSRMLS_CC, 0)
92 #define http_curl_getopt1(o, k, t1) _http_curl_getopt((o), (k) TSRMLS_CC, 1, (t1))
93 #define http_curl_getopt2(o, k, t1, t2) _http_curl_getopt((o), (k) TSRMLS_CC, 2, (t1), (t2))
94 static inline zval *_http_curl_getopt(HashTable *options, char *key TSRMLS_DC, int checks, ...);
95
96 static size_t http_curl_body_callback(char *, size_t, size_t, void *);
97 static size_t http_curl_hdrs_callback(char *, size_t, size_t, void *);
98
99 #define http_curl_getinfo(c, h) _http_curl_getinfo((c), (h) TSRMLS_CC)
100 static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC);
101
102 /* {{{ static inline char *http_curl_copystr(char *) */
103 static inline char *_http_curl_copystr(const char *str TSRMLS_DC)
104 {
105 char *new_str = estrdup(str);
106 zend_llist_add_element(&HTTP_G(to_free), &new_str);
107 return new_str;
108 }
109 /* }}} */
110
111 /* {{{ static size_t http_curl_body_callback(char *, size_t, size_t, void *) */
112 static size_t http_curl_body_callback(char *buf, size_t len, size_t n, void *s)
113 {
114 TSRMLS_FETCH();
115
116 phpstr_append(&HTTP_G(curlbuf), buf, len *= n);
117 return len;
118 }
119 /* }}} */
120
121 /* {{{ static size_t http_curl_hdrs_callback(char *, size_t, size_t, void *) */
122 static size_t http_curl_hdrs_callback(char *buf, size_t len, size_t n, void *s)
123 {
124 TSRMLS_FETCH();
125
126 /* discard previous headers */
127 if (HTTP_G(curlbuf).used && (!strncmp(buf, "HTTP/1.", sizeof("HTTP/1.") - 1))) {
128 phpstr_free(&HTTP_G(curlbuf));
129 }
130
131 phpstr_append(&HTTP_G(curlbuf), buf, len *= n);
132 return len;
133 }
134 /* }}} */
135
136 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, int, ...) */
137 static inline zval *_http_curl_getopt(HashTable *options, char *key TSRMLS_DC, int checks, ...)
138 {
139 zval **zoption;
140 va_list types;
141 int i;
142
143 if (SUCCESS != zend_hash_find(options, key, strlen(key) + 1, (void **) &zoption)) {
144 return NULL;
145 }
146 if (checks < 1) {
147 return *zoption;
148 }
149
150 va_start(types, checks);
151 for (i = 0; i < checks; ++i) {
152 if ((va_arg(types, int)) == (Z_TYPE_PP(zoption))) {
153 va_end(types);
154 return *zoption;
155 }
156 }
157 va_end(types);
158 return NULL;
159 }
160 /* }}} */
161
162 /* {{{ static void http_curl_setopts(CURL *, char *, HashTable *) */
163 static void _http_curl_setopts(CURL *ch, const char *url, HashTable *options TSRMLS_DC)
164 {
165 zval *zoption;
166 zend_bool range_req = 0;
167
168 /* standard options */
169 curl_easy_setopt(ch, CURLOPT_URL, url);
170 curl_easy_setopt(ch, CURLOPT_HEADER, 0);
171 curl_easy_setopt(ch, CURLOPT_FILETIME, 1);
172 curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1);
173 curl_easy_setopt(ch, CURLOPT_AUTOREFERER, 1);
174 curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, http_curl_body_callback);
175 curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, http_curl_hdrs_callback);
176 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
177 curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1);
178 #endif
179
180 if ((!options) || (1 > zend_hash_num_elements(options))) {
181 return;
182 }
183
184 /* proxy */
185 if (zoption = http_curl_getopt1(options, "proxyhost", IS_STRING)) {
186 curl_easy_setopt(ch, CURLOPT_PROXY, http_curl_copystr(Z_STRVAL_P(zoption)));
187 /* port */
188 if (zoption = http_curl_getopt1(options, "proxyport", IS_LONG)) {
189 curl_easy_setopt(ch, CURLOPT_PROXYPORT, Z_LVAL_P(zoption));
190 }
191 /* user:pass */
192 if (zoption = http_curl_getopt1(options, "proxyauth", IS_STRING)) {
193 curl_easy_setopt(ch, CURLOPT_PROXYUSERPWD, http_curl_copystr(Z_STRVAL_P(zoption)));
194 }
195 #if LIBCURL_VERSION_NUM >= 0x070a07
196 /* auth method */
197 if (zoption = http_curl_getopt1(options, "proxyauthtype", IS_LONG)) {
198 curl_easy_setopt(ch, CURLOPT_PROXYAUTH, Z_LVAL_P(zoption));
199 }
200 #endif
201 }
202
203 /* outgoing interface */
204 if (zoption = http_curl_getopt1(options, "interface", IS_STRING)) {
205 curl_easy_setopt(ch, CURLOPT_INTERFACE, http_curl_copystr(Z_STRVAL_P(zoption)));
206 }
207
208 /* another port */
209 if (zoption = http_curl_getopt1(options, "port", IS_LONG)) {
210 curl_easy_setopt(ch, CURLOPT_PORT, Z_LVAL_P(zoption));
211 }
212
213 /* auth */
214 if (zoption = http_curl_getopt1(options, "httpauth", IS_STRING)) {
215 curl_easy_setopt(ch, CURLOPT_USERPWD, http_curl_copystr(Z_STRVAL_P(zoption)));
216 }
217 #if LIBCURL_VERSION_NUM >= 0x070a06
218 if (zoption = http_curl_getopt1(options, "httpauthtype", IS_LONG)) {
219 curl_easy_setopt(ch, CURLOPT_HTTPAUTH, Z_LVAL_P(zoption));
220 }
221 #endif
222
223 /* compress, empty string enables deflate and gzip */
224 if (zoption = http_curl_getopt2(options, "compress", IS_LONG, IS_BOOL)) {
225 if (Z_LVAL_P(zoption)) {
226 curl_easy_setopt(ch, CURLOPT_ENCODING, "");
227 }
228 }
229
230 /* redirects, defaults to 0 */
231 if (zoption = http_curl_getopt1(options, "redirect", IS_LONG)) {
232 curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
233 curl_easy_setopt(ch, CURLOPT_MAXREDIRS, Z_LVAL_P(zoption));
234 if (zoption = http_curl_getopt2(options, "unrestrictedauth", IS_LONG, IS_BOOL)) {
235 curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
236 }
237 } else {
238 curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 0);
239 }
240
241 /* referer */
242 if (zoption = http_curl_getopt1(options, "referer", IS_STRING)) {
243 curl_easy_setopt(ch, CURLOPT_REFERER, http_curl_copystr(Z_STRVAL_P(zoption)));
244 }
245
246 /* useragent, default "PECL::HTTP/version (PHP/version)" */
247 if (zoption = http_curl_getopt1(options, "useragent", IS_STRING)) {
248 curl_easy_setopt(ch, CURLOPT_USERAGENT, http_curl_copystr(Z_STRVAL_P(zoption)));
249 } else {
250 curl_easy_setopt(ch, CURLOPT_USERAGENT,
251 "PECL::HTTP/" HTTP_PEXT_VERSION " (PHP/" PHP_VERSION ")");
252 }
253
254 /* additional headers, array('name' => 'value') */
255 if (zoption = http_curl_getopt1(options, "headers", IS_ARRAY)) {
256 char *header_key;
257 long header_idx;
258 struct curl_slist *headers = NULL;
259
260 FOREACH_KEY(zoption, header_key, header_idx) {
261 if (header_key) {
262 zval **header_val;
263 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &header_val)) {
264 char header[1024] = {0};
265 snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val));
266 headers = curl_slist_append(headers, http_curl_copystr(header));
267 }
268
269 /* reset */
270 header_key = NULL;
271 }
272 }
273
274 if (headers) {
275 curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headers);
276 }
277 }
278
279 /* cookies, array('name' => 'value') */
280 if (zoption = http_curl_getopt1(options, "cookies", IS_ARRAY)) {
281 char *cookie_key = NULL;
282 long cookie_idx = 0;
283 phpstr *qstr = phpstr_new();
284
285 FOREACH_KEY(zoption, cookie_key, cookie_idx) {
286 if (cookie_key) {
287 zval **cookie_val;
288 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &cookie_val)) {
289 phpstr_appendf(qstr, "%s=%s; ", cookie_key, Z_STRVAL_PP(cookie_val));
290 }
291
292 /* reset */
293 cookie_key = NULL;
294 }
295 }
296
297 if (qstr->used) {
298 phpstr_fix(qstr);
299 curl_easy_setopt(ch, CURLOPT_COOKIE, http_curl_copystr(qstr->data));
300 }
301 phpstr_dtor(qstr);
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 /* resume */
311 if (zoption = http_curl_getopt1(options, "resume", IS_LONG)) {
312 range_req = 1;
313 curl_easy_setopt(ch, CURLOPT_RESUME_FROM, Z_LVAL_P(zoption));
314 }
315
316 /* maxfilesize */
317 if (zoption = http_curl_getopt1(options, "maxfilesize", IS_LONG)) {
318 curl_easy_setopt(ch, CURLOPT_MAXFILESIZE, Z_LVAL_P(zoption));
319 }
320
321 /* lastmodified */
322 if (zoption = http_curl_getopt1(options, "lastmodified", IS_LONG)) {
323 curl_easy_setopt(ch, CURLOPT_TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE);
324 curl_easy_setopt(ch, CURLOPT_TIMEVALUE, Z_LVAL_P(zoption));
325 }
326
327 /* timeout */
328 if (zoption = http_curl_getopt1(options, "timeout", IS_LONG)) {
329 curl_easy_setopt(ch, CURLOPT_TIMEOUT, Z_LVAL_P(zoption));
330 }
331
332 /* connecttimeout */
333 if (zoption = http_curl_getopt1(options, "connecttimeout", IS_LONG)) {
334 curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT, Z_LVAL_P(zoption));
335 }
336 }
337 /* }}} */
338
339 /* {{{ static inline http_curl_getinfo(CURL, HashTable *) */
340 static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC)
341 {
342 zval array;
343 Z_ARRVAL(array) = info;
344
345 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
346 #define HTTP_CURL_INFO_EX(I, X) \
347 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
348 { \
349 case CURLINFO_STRING: \
350 { \
351 char *c; \
352 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
353 add_assoc_string(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
354 } \
355 } \
356 break; \
357 \
358 case CURLINFO_DOUBLE: \
359 { \
360 double d; \
361 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
362 add_assoc_double(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), d); \
363 } \
364 } \
365 break; \
366 \
367 case CURLINFO_LONG: \
368 { \
369 long l; \
370 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
371 add_assoc_long(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), l); \
372 } \
373 } \
374 break; \
375 }
376
377 HTTP_CURL_INFO(EFFECTIVE_URL);
378
379 #if LIBCURL_VERSION_NUM >= 0x070a07
380 HTTP_CURL_INFO(RESPONSE_CODE);
381 #else
382 HTTP_CURL_INFO_EX(HTTP_CODE, RESPONSE_CODE);
383 #endif
384 HTTP_CURL_INFO(HTTP_CONNECTCODE);
385
386 #if LIBCURL_VERSION_NUM >= 0x070500
387 HTTP_CURL_INFO(FILETIME);
388 #endif
389 HTTP_CURL_INFO(TOTAL_TIME);
390 HTTP_CURL_INFO(NAMELOOKUP_TIME);
391 HTTP_CURL_INFO(CONNECT_TIME);
392 HTTP_CURL_INFO(PRETRANSFER_TIME);
393 HTTP_CURL_INFO(STARTTRANSFER_TIME);
394 #if LIBCURL_VERSION_NUM >= 0x070907
395 HTTP_CURL_INFO(REDIRECT_TIME);
396 HTTP_CURL_INFO(REDIRECT_COUNT);
397 #endif
398
399 HTTP_CURL_INFO(SIZE_UPLOAD);
400 HTTP_CURL_INFO(SIZE_DOWNLOAD);
401 HTTP_CURL_INFO(SPEED_DOWNLOAD);
402 HTTP_CURL_INFO(SPEED_UPLOAD);
403
404 HTTP_CURL_INFO(HEADER_SIZE);
405 HTTP_CURL_INFO(REQUEST_SIZE);
406
407 HTTP_CURL_INFO(SSL_VERIFYRESULT);
408 #if LIBCURL_VERSION_NUM >= 0x070c03
409 /*HTTP_CURL_INFO(SSL_ENGINES);
410 todo: CURLINFO_SLIST */
411 #endif
412
413 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD);
414 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD);
415 HTTP_CURL_INFO(CONTENT_TYPE);
416
417 #if LIBCURL_VERSION_NUM >= 0x070a03
418 /*HTTP_CURL_INFO(PRIVATE);*/
419 #endif
420
421 #if LIBCURL_VERSION_NUM >= 0x070a08
422 HTTP_CURL_INFO(HTTPAUTH_AVAIL);
423 HTTP_CURL_INFO(PROXYAUTH_AVAIL);
424 #endif
425
426 #if LIBCURL_VERSION_NUM >= 0x070c02
427 /*HTTP_CURL_INFO(OS_ERRNO);*/
428 #endif
429
430 #if LIBCURL_VERSION_NUM >= 0x070c03
431 HTTP_CURL_INFO(NUM_CONNECTS);
432 #endif
433 }
434 /* }}} */
435
436 /* {{{ STATUS http_get_ex(CURL *, char *, HashTable *, HashTable *, char **, size_t *) */
437 PHP_HTTP_API STATUS _http_get_ex(CURL *ch, const char *URL, HashTable *options,
438 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
439 {
440 zend_bool clean_curl = 0;
441
442 http_curl_startup(ch, clean_curl, URL, options);
443 curl_easy_setopt(ch, CURLOPT_HTTPGET, 1);
444 http_curl_perform(ch, clean_curl);
445
446 if (info) {
447 http_curl_getinfo(ch, info);
448 }
449
450 http_curl_copybuf(data, data_len);
451 http_curl_cleanup(ch, clean_curl);
452
453 return SUCCESS;
454 }
455
456 /* {{{ STATUS http_head_ex(CURL *, char *, HashTable *, HashTable *, char **data, size_t *) */
457 PHP_HTTP_API STATUS _http_head_ex(CURL *ch, const char *URL, HashTable *options,
458 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
459 {
460 zend_bool clean_curl = 0;
461
462 http_curl_startup(ch, clean_curl, URL, options);
463 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
464 http_curl_perform(ch, clean_curl);
465
466 if (info) {
467 http_curl_getinfo(ch, info);
468 }
469
470 http_curl_copybuf(data, data_len);
471 http_curl_cleanup(ch, clean_curl);
472
473 return SUCCESS;
474 }
475
476 /* {{{ STATUS http_post_data_ex(CURL *, char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
477 PHP_HTTP_API STATUS _http_post_data_ex(CURL *ch, const char *URL, char *postdata,
478 size_t postdata_len, HashTable *options, HashTable *info, char **data,
479 size_t *data_len TSRMLS_DC)
480 {
481 zend_bool clean_curl = 0;
482
483 http_curl_startup(ch, clean_curl, URL, options);
484 curl_easy_setopt(ch, CURLOPT_POST, 1);
485 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, postdata);
486 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, postdata_len);
487 http_curl_perform(ch, clean_curl);
488
489 if (info) {
490 http_curl_getinfo(ch, info);
491 }
492
493 http_curl_copybuf(data, data_len);
494 http_curl_cleanup(ch, clean_curl);
495
496 return SUCCESS;
497 }
498 /* }}} */
499
500 /* {{{ STATUS http_post_array_ex(CURL *, char *, HashTable *, HashTable *, HashTable *, char **, size_t *) */
501 PHP_HTTP_API STATUS _http_post_array_ex(CURL *ch, const char *URL, HashTable *postarray,
502 HashTable *options, HashTable *info, char **data, size_t *data_len TSRMLS_DC)
503 {
504 STATUS status;
505 char *encoded;
506 size_t encoded_len;
507
508 if (SUCCESS != http_urlencode_hash_ex(postarray, 1, NULL, 0, &encoded, &encoded_len)) {
509 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not encode post data");
510 return FAILURE;
511 }
512
513 status = http_post_data_ex(ch, URL, encoded, encoded_len, options, info, data, data_len);
514 efree(encoded);
515
516 return status;
517 }
518 /* }}} */
519
520 /* {{{ STATUS http_post_curldata_ex(CURL *, char *, curl_httppost *, HashTable *, HashTable *, char **, size_t *) */
521 PHP_HTTP_API STATUS _http_post_curldata_ex(CURL *ch, const char *URL,
522 struct curl_httppost *curldata, HashTable *options, HashTable *info,
523 char **data, size_t *data_len TSRMLS_DC)
524 {
525 zend_bool clean_curl = 0;
526
527 http_curl_startup(ch, clean_curl, URL, options);
528 curl_easy_setopt(ch, CURLOPT_POST, 1);
529 curl_easy_setopt(ch, CURLOPT_HTTPPOST, curldata);
530 http_curl_perform(ch, clean_curl);
531
532 if (info) {
533 http_curl_getinfo(ch, info);
534 }
535
536 http_curl_copybuf(data, data_len);
537 http_curl_cleanup(ch, clean_curl);
538
539 return SUCCESS;
540 }
541 /* }}} */
542
543 /*
544 * Local variables:
545 * tab-width: 4
546 * c-basic-offset: 4
547 * End:
548 * vim600: noet sw=4 ts=4 fdm=marker
549 * vim<600: noet sw=4 ts=4
550 */
551