- prepare release
[m6w6/ext-http] / http_request_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 #include "php.h"
22
23 #ifdef HTTP_HAVE_CURL
24
25 #include "php_http.h"
26 #include "php_http_std_defs.h"
27 #include "php_http_api.h"
28 #include "php_http_request_api.h"
29 #include "php_http_request_method_api.h"
30 #include "php_http_url_api.h"
31 #ifdef ZEND_ENGINE_2
32 # include "php_http_request_object.h"
33 #endif
34
35 #include "phpstr/phpstr.h"
36
37 #ifdef PHP_WIN32
38 # include <winsock2.h>
39 #endif
40
41 #include <curl/curl.h>
42
43 ZEND_EXTERN_MODULE_GLOBALS(http);
44
45 #ifndef HAVE_CURL_EASY_STRERROR
46 # define curl_easy_strerror(code) HTTP_G(request).error
47 #endif
48
49 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
50 #define HTTP_CURL_INFO_EX(I, X) \
51 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
52 { \
53 case CURLINFO_STRING: \
54 { \
55 char *c; \
56 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
57 add_assoc_string(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
58 } \
59 } \
60 break; \
61 \
62 case CURLINFO_DOUBLE: \
63 { \
64 double d; \
65 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
66 add_assoc_double(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), d); \
67 } \
68 } \
69 break; \
70 \
71 case CURLINFO_LONG: \
72 { \
73 long l; \
74 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
75 add_assoc_long(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), l); \
76 } \
77 } \
78 break; \
79 }
80
81 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
82 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
83 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
84 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
85 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
86 if (!strcasecmp(key, #keyname)) { \
87 convert_to_string_ex(param); \
88 HTTP_CURL_OPT(optname, http_request_data_copy(COPY_STRING, Z_STRVAL_PP(param))); \
89 key = NULL; \
90 continue; \
91 }
92 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
93 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
94 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
95 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
96 if (!strcasecmp(key, #keyname)) { \
97 convert_to_long_ex(param); \
98 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
99 key = NULL; \
100 continue; \
101 }
102
103 #define http_curl_getopt(o, k, t) _http_curl_getopt_ex((o), (k), sizeof(k), (t) TSRMLS_CC)
104 #define http_curl_getopt_ex(o, k, l, t) _http_curl_getopt_ex((o), (k), (l), (t) TSRMLS_CC)
105 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC);
106 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
107 static int http_curl_progress_callback(void *, double, double, double, double);
108 static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *);
109 static int http_curl_dummy_callback(char *data, size_t n, size_t l, void *s) { return n*l; }
110
111
112 /* {{{ http_request_callback_ctx http_request_callback_data(void *) */
113 http_request_callback_ctx *_http_request_callback_data_ex(void *data, zend_bool cpy TSRMLS_DC)
114 {
115 http_request_callback_ctx *ctx = emalloc(sizeof(http_request_callback_ctx));
116
117 TSRMLS_SET_CTX(ctx->tsrm_ctx);
118 ctx->data = data;
119
120 if (cpy) {
121 return http_request_data_copy(COPY_CONTEXT, ctx);
122 } else {
123 return ctx;
124 }
125 }
126 /* }}} */
127
128 /* {{{ void *http_request_data_copy(int, void *) */
129 void *_http_request_data_copy(int type, void *data TSRMLS_DC)
130 {
131 switch (type)
132 {
133 case COPY_STRING:
134 {
135 char *new_str = estrdup(data);
136 zend_llist_add_element(&HTTP_G(request).copies.strings, &new_str);
137 return new_str;
138 }
139
140 case COPY_SLIST:
141 {
142 zend_llist_add_element(&HTTP_G(request).copies.slists, &data);
143 return data;
144 }
145
146 case COPY_CONTEXT:
147 {
148 zend_llist_add_element(&HTTP_G(request).copies.contexts, &data);
149 return data;
150 }
151
152 case COPY_CONV:
153 {
154 zend_llist_add_element(&HTTP_G(request).copies.convs, &data);
155 return data;
156 }
157
158 default:
159 {
160 return data;
161 }
162 }
163 }
164 /* }}} */
165
166 /* {{{ void http_request_data_free_string(char **) */
167 void _http_request_data_free_string(void *string)
168 {
169 efree(*((char **)string));
170 }
171 /* }}} */
172
173 /* {{{ void http_request_data_free_slist(struct curl_slist **) */
174 void _http_request_data_free_slist(void *list)
175 {
176 curl_slist_free_all(*((struct curl_slist **) list));
177 }
178 /* }}} */
179
180 /* {{{ _http_request_data_free_context(http_request_callback_ctx **) */
181 void _http_request_data_free_context(void *context)
182 {
183 efree(*((http_request_callback_ctx **) context));
184 }
185 /* }}} */
186
187 /* {{{ _http_request_data_free_conv(http_request_conv **) */
188 void _http_request_data_free_conv(void *conv)
189 {
190 efree(*((http_request_conv **) conv));
191 }
192 /* }}} */
193
194 /* {{{ http_request_body *http_request_body_new() */
195 PHP_HTTP_API http_request_body *_http_request_body_new(TSRMLS_D)
196 {
197 http_request_body *body = ecalloc(1, sizeof(http_request_body));
198 return body;
199 }
200 /* }}} */
201
202 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
203 PHP_HTTP_API STATUS _http_request_body_fill(http_request_body *body, HashTable *fields, HashTable *files TSRMLS_DC)
204 {
205 if (files && (zend_hash_num_elements(files) > 0)) {
206 char *key = NULL;
207 ulong idx;
208 zval **data;
209 struct curl_httppost *http_post_data[2] = {NULL, NULL};
210
211 /* normal data */
212 FOREACH_HASH_KEYVAL(fields, key, idx, data) {
213 CURLcode err;
214 if (key) {
215 convert_to_string_ex(data);
216 err = curl_formadd(&http_post_data[0], &http_post_data[1],
217 CURLFORM_COPYNAME, key,
218 CURLFORM_COPYCONTENTS, Z_STRVAL_PP(data),
219 CURLFORM_CONTENTSLENGTH, (long) Z_STRLEN_PP(data),
220 CURLFORM_END
221 );
222 if (CURLE_OK != err) {
223 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not encode post fields: %s", curl_easy_strerror(err));
224 curl_formfree(http_post_data[0]);
225 return FAILURE;
226 }
227
228 /* reset */
229 key = NULL;
230 }
231 }
232
233 /* file data */
234 FOREACH_HASH_VAL(files, data) {
235 CURLcode err;
236 zval **file, **type, **name;
237 if ( SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "name", sizeof("name"), (void **) &name) &&
238 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &type) &&
239 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "file", sizeof("file"), (void **) &file)) {
240 err = curl_formadd(&http_post_data[0], &http_post_data[1],
241 CURLFORM_COPYNAME, Z_STRVAL_PP(name),
242 CURLFORM_FILE, Z_STRVAL_PP(file),
243 CURLFORM_CONTENTTYPE, Z_STRVAL_PP(type),
244 CURLFORM_END
245 );
246 if (CURLE_OK != err) {
247 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not encode post files: %s", curl_easy_strerror(err));
248 curl_formfree(http_post_data[0]);
249 return FAILURE;
250 }
251 } else {
252 http_error(HE_NOTICE, HTTP_E_INVALID_PARAM, "Post file array entry misses either 'name', 'type' or 'file' entry");
253 }
254 }
255
256 body->type = HTTP_REQUEST_BODY_CURLPOST;
257 body->data = http_post_data[0];
258 body->size = 0;
259
260 } else {
261 char *encoded;
262 size_t encoded_len;
263
264 if (SUCCESS != http_urlencode_hash_ex(fields, 1, NULL, 0, &encoded, &encoded_len)) {
265 http_error(HE_WARNING, HTTP_E_ENCODING, "Could not encode post data");
266 return FAILURE;
267 }
268
269 body->type = HTTP_REQUEST_BODY_CSTRING;
270 body->data = encoded;
271 body->size = encoded_len;
272 }
273
274 return SUCCESS;
275 }
276 /* }}} */
277
278 /* {{{ void http_request_body_dtor(http_request_body *) */
279 PHP_HTTP_API void _http_request_body_dtor(http_request_body *body TSRMLS_DC)
280 {
281 if (body) {
282 switch (body->type)
283 {
284 case HTTP_REQUEST_BODY_CSTRING:
285 if (body->data) {
286 efree(body->data);
287 }
288 break;
289
290 case HTTP_REQUEST_BODY_CURLPOST:
291 curl_formfree(body->data);
292 break;
293
294 case HTTP_REQUEST_BODY_UPLOADFILE:
295 php_stream_close(body->data);
296 break;
297 }
298 }
299 }
300 /* }}} */
301
302 /* {{{ void http_request_body_free(http_request_body *) */
303 PHP_HTTP_API void _http_request_body_free(http_request_body *body TSRMLS_DC)
304 {
305 if (body) {
306 http_request_body_dtor(body);
307 efree(body);
308 }
309 }
310 /* }}} */
311
312 /* {{{ STATUS http_request_init(CURL *, http_request_method, char *, http_request_body *, HashTable *) */
313 PHP_HTTP_API STATUS _http_request_init(CURL *ch, http_request_method meth, char *url, http_request_body *body, HashTable *options TSRMLS_DC)
314 {
315 zval *zoption;
316 zend_bool range_req = 0;
317
318 /* reset CURL handle */
319 #if LIBCURL_VERSION_NUM >= 0x070c01
320 curl_easy_reset(ch);
321 #endif
322
323 /* set options */
324 if (url) {
325 HTTP_CURL_OPT(URL, http_request_data_copy(COPY_STRING, url));
326 }
327
328 HTTP_CURL_OPT(HEADER, 0);
329 HTTP_CURL_OPT(FILETIME, 1);
330 HTTP_CURL_OPT(AUTOREFERER, 1);
331 HTTP_CURL_OPT(READFUNCTION, http_curl_read_callback);
332 /* we'll get all data through the debug function */
333 HTTP_CURL_OPT(WRITEFUNCTION, http_curl_dummy_callback);
334 HTTP_CURL_OPT(HEADERFUNCTION, NULL);
335
336 HTTP_CURL_OPT(VERBOSE, 1);
337 HTTP_CURL_OPT(DEBUGFUNCTION, http_curl_raw_callback);
338
339 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
340 HTTP_CURL_OPT(NOSIGNAL, 1);
341 #endif
342 #if LIBCURL_VERSION_NUM < 0x070c00
343 HTTP_CURL_OPT(ERRORBUFFER, HTTP_G(request).error);
344 #endif
345
346 /* progress callback */
347 if (zoption = http_curl_getopt(options, "onprogress", 0)) {
348 HTTP_CURL_OPT(NOPROGRESS, 0);
349 HTTP_CURL_OPT(PROGRESSFUNCTION, http_curl_progress_callback);
350 HTTP_CURL_OPT(PROGRESSDATA, http_request_callback_data(zoption));
351 } else {
352 HTTP_CURL_OPT(NOPROGRESS, 1);
353 }
354
355 /* proxy */
356 if (zoption = http_curl_getopt(options, "proxyhost", IS_STRING)) {
357 HTTP_CURL_OPT(PROXY, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
358 /* port */
359 if (zoption = http_curl_getopt(options, "proxyport", IS_LONG)) {
360 HTTP_CURL_OPT(PROXYPORT, Z_LVAL_P(zoption));
361 }
362 /* user:pass */
363 if (zoption = http_curl_getopt(options, "proxyauth", IS_STRING)) {
364 HTTP_CURL_OPT(PROXYUSERPWD, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
365 }
366 #if LIBCURL_VERSION_NUM >= 0x070a07
367 /* auth method */
368 if (zoption = http_curl_getopt(options, "proxyauthtype", IS_LONG)) {
369 HTTP_CURL_OPT(PROXYAUTH, Z_LVAL_P(zoption));
370 }
371 #endif
372 }
373
374 /* outgoing interface */
375 if (zoption = http_curl_getopt(options, "interface", IS_STRING)) {
376 HTTP_CURL_OPT(INTERFACE, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
377 }
378
379 /* another port */
380 if (zoption = http_curl_getopt(options, "port", IS_LONG)) {
381 HTTP_CURL_OPT(PORT, Z_LVAL_P(zoption));
382 }
383
384 /* auth */
385 if (zoption = http_curl_getopt(options, "httpauth", IS_STRING)) {
386 HTTP_CURL_OPT(USERPWD, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
387 }
388 #if LIBCURL_VERSION_NUM >= 0x070a06
389 if (zoption = http_curl_getopt(options, "httpauthtype", IS_LONG)) {
390 HTTP_CURL_OPT(HTTPAUTH, Z_LVAL_P(zoption));
391 }
392 #endif
393
394 /* compress, empty string enables deflate and gzip */
395 if ((zoption = http_curl_getopt(options, "compress", IS_BOOL)) && Z_LVAL_P(zoption)) {
396 HTTP_CURL_OPT(ENCODING, "");
397 } else {
398 HTTP_CURL_OPT(ENCODING, 0);
399 }
400
401 /* redirects, defaults to 0 */
402 if (zoption = http_curl_getopt(options, "redirect", IS_LONG)) {
403 HTTP_CURL_OPT(FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
404 HTTP_CURL_OPT(MAXREDIRS, Z_LVAL_P(zoption));
405 if (zoption = http_curl_getopt(options, "unrestrictedauth", IS_BOOL)) {
406 HTTP_CURL_OPT(UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
407 }
408 } else {
409 HTTP_CURL_OPT(FOLLOWLOCATION, 0);
410 }
411
412 /* referer */
413 if (zoption = http_curl_getopt(options, "referer", IS_STRING)) {
414 HTTP_CURL_OPT(REFERER, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
415 } else {
416 HTTP_CURL_OPT(REFERER, NULL);
417 }
418
419 /* useragent, default "PECL::HTTP/version (PHP/version)" */
420 if (zoption = http_curl_getopt(options, "useragent", IS_STRING)) {
421 HTTP_CURL_OPT(USERAGENT, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
422 } else {
423 HTTP_CURL_OPT(USERAGENT, "PECL::HTTP/" HTTP_PEXT_VERSION " (PHP/" PHP_VERSION ")");
424 }
425
426 /* additional headers, array('name' => 'value') */
427 if (zoption = http_curl_getopt(options, "headers", IS_ARRAY)) {
428 char *header_key;
429 ulong header_idx;
430 struct curl_slist *headers = NULL;
431
432 FOREACH_KEY(zoption, header_key, header_idx) {
433 if (header_key) {
434 zval **header_val;
435 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &header_val)) {
436 char header[1024] = {0};
437 snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val));
438 headers = curl_slist_append(headers, http_request_data_copy(COPY_STRING, header));
439 }
440
441 /* reset */
442 header_key = NULL;
443 }
444 }
445
446 if (headers) {
447 HTTP_CURL_OPT(HTTPHEADER, http_request_data_copy(COPY_SLIST, headers));
448 }
449 } else {
450 HTTP_CURL_OPT(HTTPHEADER, NULL);
451 }
452
453 /* cookies, array('name' => 'value') */
454 if (zoption = http_curl_getopt(options, "cookies", IS_ARRAY)) {
455 char *cookie_key = NULL;
456 ulong cookie_idx = 0;
457 phpstr *qstr = phpstr_new();
458
459 FOREACH_KEY(zoption, cookie_key, cookie_idx) {
460 if (cookie_key) {
461 zval **cookie_val;
462 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &cookie_val)) {
463 phpstr_appendf(qstr, "%s=%s; ", cookie_key, Z_STRVAL_PP(cookie_val));
464 }
465
466 /* reset */
467 cookie_key = NULL;
468 }
469 }
470
471 if (qstr->used) {
472 phpstr_fix(qstr);
473 HTTP_CURL_OPT(COOKIE, http_request_data_copy(COPY_STRING, qstr->data));
474 }
475 phpstr_free(&qstr);
476 } else {
477 HTTP_CURL_OPT(COOKIE, NULL);
478 }
479
480 /* session cookies */
481 if (zoption = http_curl_getopt(options, "cookiesession", IS_BOOL)) {
482 if (Z_LVAL_P(zoption)) {
483 /* accept cookies for this session */
484 HTTP_CURL_OPT(COOKIEFILE, "");
485 } else {
486 /* reset session cookies */
487 HTTP_CURL_OPT(COOKIESESSION, 1);
488 }
489 } else {
490 HTTP_CURL_OPT(COOKIEFILE, NULL);
491 }
492
493 /* cookiestore, read initial cookies from that file and store cookies back into that file */
494 if ((zoption = http_curl_getopt(options, "cookiestore", IS_STRING)) && Z_STRLEN_P(zoption)) {
495 HTTP_CURL_OPT(COOKIEFILE, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
496 HTTP_CURL_OPT(COOKIEJAR, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
497 } else {
498 HTTP_CURL_OPT(COOKIEFILE, NULL);
499 HTTP_CURL_OPT(COOKIEJAR, NULL);
500 }
501
502 /* resume */
503 if ((zoption = http_curl_getopt(options, "resume", IS_LONG)) && (Z_LVAL_P(zoption) != 0)) {
504 range_req = 1;
505 HTTP_CURL_OPT(RESUME_FROM, Z_LVAL_P(zoption));
506 } else {
507 HTTP_CURL_OPT(RESUME_FROM, 0);
508 }
509
510 /* maxfilesize */
511 if (zoption = http_curl_getopt(options, "maxfilesize", IS_LONG)) {
512 HTTP_CURL_OPT(MAXFILESIZE, Z_LVAL_P(zoption));
513 } else {
514 HTTP_CURL_OPT(MAXFILESIZE, 0);
515 }
516
517 /* lastmodified */
518 if (zoption = http_curl_getopt(options, "lastmodified", IS_LONG)) {
519 HTTP_CURL_OPT(TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE);
520 HTTP_CURL_OPT(TIMEVALUE, Z_LVAL_P(zoption));
521 } else {
522 HTTP_CURL_OPT(TIMEVALUE, 0);
523 }
524
525 /* timeout, defaults to 0 */
526 if (zoption = http_curl_getopt(options, "timeout", IS_LONG)) {
527 HTTP_CURL_OPT(TIMEOUT, Z_LVAL_P(zoption));
528 } else {
529 HTTP_CURL_OPT(TIMEOUT, 0);
530 }
531
532 /* connecttimeout, defaults to 3 */
533 if (zoption = http_curl_getopt(options, "connecttimeout", IS_LONG)) {
534 HTTP_CURL_OPT(CONNECTTIMEOUT, Z_LVAL_P(zoption));
535 } else {
536 HTTP_CURL_OPT(CONNECTTIMEOUT, 3);
537 }
538
539 /* ssl */
540 if (zoption = http_curl_getopt(options, "ssl", IS_ARRAY)) {
541 ulong idx;
542 char *key = NULL;
543 zval **param;
544
545 FOREACH_KEYVAL(zoption, key, idx, param) {
546 if (key) {
547 HTTP_CURL_OPT_SSL_STRING(CERT);
548 #if LIBCURL_VERSION_NUM >= 0x070903
549 HTTP_CURL_OPT_SSL_STRING(CERTTYPE);
550 #endif
551 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD);
552
553 HTTP_CURL_OPT_SSL_STRING(KEY);
554 HTTP_CURL_OPT_SSL_STRING(KEYTYPE);
555 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD);
556
557 HTTP_CURL_OPT_SSL_STRING(ENGINE);
558 HTTP_CURL_OPT_SSL_LONG(VERSION);
559
560 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER);
561 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST);
562 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST);
563
564
565 HTTP_CURL_OPT_STRING(CAINFO);
566 #if LIBCURL_VERSION_NUM >= 0x070908
567 HTTP_CURL_OPT_STRING(CAPATH);
568 #endif
569 HTTP_CURL_OPT_STRING(RANDOM_FILE);
570 HTTP_CURL_OPT_STRING(EGDSOCKET);
571
572 /* reset key */
573 key = NULL;
574 }
575 }
576 } else {
577 /* disable SSL verification by default */
578 HTTP_CURL_OPT(SSL_VERIFYPEER, 0);
579 HTTP_CURL_OPT(SSL_VERIFYHOST, 0);
580 }
581
582 /* request method */
583 switch (meth)
584 {
585 case HTTP_GET:
586 curl_easy_setopt(ch, CURLOPT_HTTPGET, 1);
587 break;
588
589 case HTTP_HEAD:
590 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
591 break;
592
593 case HTTP_POST:
594 curl_easy_setopt(ch, CURLOPT_POST, 1);
595 break;
596
597 case HTTP_PUT:
598 curl_easy_setopt(ch, CURLOPT_UPLOAD, 1);
599 break;
600
601 default:
602 if (http_request_method_exists(0, meth, NULL)) {
603 curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, http_request_method_name(meth));
604 } else {
605 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unsupported request method: %d", meth);
606 return FAILURE;
607 }
608 break;
609 }
610
611 /* attach request body */
612 if (body && (meth != HTTP_GET) && (meth != HTTP_HEAD)) {
613 switch (body->type)
614 {
615 case HTTP_REQUEST_BODY_CSTRING:
616 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, body->data);
617 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, body->size);
618 break;
619
620 case HTTP_REQUEST_BODY_CURLPOST:
621 curl_easy_setopt(ch, CURLOPT_HTTPPOST, (struct curl_httppost *) body->data);
622 break;
623
624 case HTTP_REQUEST_BODY_UPLOADFILE:
625 curl_easy_setopt(ch, CURLOPT_READDATA, http_request_callback_data(body));
626 curl_easy_setopt(ch, CURLOPT_INFILESIZE, body->size);
627 break;
628
629 default:
630 /* shouldn't ever happen */
631 http_error_ex(HE_ERROR, 0, "Unknown request body type: %d", body->type);
632 return FAILURE;
633 break;
634 }
635 }
636
637 return SUCCESS;
638 }
639 /* }}} */
640
641 /* {{{ void http_request_conv(CURL *, phpstr *, phpstr *) */
642 void _http_request_conv(CURL *ch, phpstr* response, phpstr *request TSRMLS_DC)
643 {
644 http_request_conv *conv = emalloc(sizeof(http_request_conv));
645 conv->response = response;
646 conv->request = request;
647 conv->last_info = -1;
648 HTTP_CURL_OPT(DEBUGDATA, http_request_callback_data(http_request_data_copy(COPY_CONV, conv)));
649 }
650 /* }}} */
651
652 /* {{{ STATUS http_request_exec(CURL *, HashTable *) */
653 PHP_HTTP_API STATUS _http_request_exec(CURL *ch, HashTable *info, phpstr *response, phpstr *request TSRMLS_DC)
654 {
655 CURLcode result;
656
657 http_request_conv(ch, response, request);
658
659 /* perform request */
660 if (CURLE_OK != (result = curl_easy_perform(ch))) {
661 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "Could not perform request: %s", curl_easy_strerror(result));
662 return FAILURE;
663 } else {
664 /* get curl info */
665 if (info) {
666 http_request_info(ch, info);
667 }
668 return SUCCESS;
669 }
670 }
671 /* }}} */
672
673 /* {{{ void http_request_info(CURL *, HashTable *) */
674 PHP_HTTP_API void _http_request_info(CURL *ch, HashTable *info TSRMLS_DC)
675 {
676 zval array;
677 Z_ARRVAL(array) = info;
678
679 HTTP_CURL_INFO(EFFECTIVE_URL);
680 #if LIBCURL_VERSION_NUM >= 0x070a07
681 HTTP_CURL_INFO(RESPONSE_CODE);
682 #else
683 HTTP_CURL_INFO_EX(HTTP_CODE, RESPONSE_CODE);
684 #endif
685 HTTP_CURL_INFO(HTTP_CONNECTCODE);
686 #if LIBCURL_VERSION_NUM >= 0x070500
687 HTTP_CURL_INFO(FILETIME);
688 #endif
689 HTTP_CURL_INFO(TOTAL_TIME);
690 HTTP_CURL_INFO(NAMELOOKUP_TIME);
691 HTTP_CURL_INFO(CONNECT_TIME);
692 HTTP_CURL_INFO(PRETRANSFER_TIME);
693 HTTP_CURL_INFO(STARTTRANSFER_TIME);
694 #if LIBCURL_VERSION_NUM >= 0x070907
695 HTTP_CURL_INFO(REDIRECT_TIME);
696 HTTP_CURL_INFO(REDIRECT_COUNT);
697 #endif
698 HTTP_CURL_INFO(SIZE_UPLOAD);
699 HTTP_CURL_INFO(SIZE_DOWNLOAD);
700 HTTP_CURL_INFO(SPEED_DOWNLOAD);
701 HTTP_CURL_INFO(SPEED_UPLOAD);
702 HTTP_CURL_INFO(HEADER_SIZE);
703 HTTP_CURL_INFO(REQUEST_SIZE);
704 HTTP_CURL_INFO(SSL_VERIFYRESULT);
705 #if LIBCURL_VERSION_NUM >= 0x070c03
706 /*HTTP_CURL_INFO(SSL_ENGINES); todo: CURLINFO_SLIST */
707 #endif
708 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD);
709 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD);
710 HTTP_CURL_INFO(CONTENT_TYPE);
711 #if LIBCURL_VERSION_NUM >= 0x070a03
712 /*HTTP_CURL_INFO(PRIVATE);*/
713 #endif
714 #if LIBCURL_VERSION_NUM >= 0x070a08
715 HTTP_CURL_INFO(HTTPAUTH_AVAIL);
716 HTTP_CURL_INFO(PROXYAUTH_AVAIL);
717 #endif
718 #if LIBCURL_VERSION_NUM >= 0x070c02
719 /*HTTP_CURL_INFO(OS_ERRNO);*/
720 #endif
721 #if LIBCURL_VERSION_NUM >= 0x070c03
722 HTTP_CURL_INFO(NUM_CONNECTS);
723 #endif
724 }
725 /* }}} */
726
727 /* {{{ STATUS http_request_ex(CURL *, http_request_method, char *, http_request_body, HashTable, HashTable, phpstr *) */
728 PHP_HTTP_API STATUS _http_request_ex(CURL *ch, http_request_method meth, char *url, http_request_body *body, HashTable *options, HashTable *info, phpstr *response TSRMLS_DC)
729 {
730 STATUS status;
731 zend_bool clean_curl;
732
733 if ((clean_curl = (!ch))) {
734 if (!(ch = curl_easy_init())) {
735 http_error(HE_WARNING, HTTP_E_REQUEST, "Could not initialize curl.");
736 return FAILURE;
737 }
738 }
739
740 status = ((SUCCESS == http_request_init(ch, meth, url, body, options)) &&
741 (SUCCESS == http_request_exec(ch, info, response, NULL))) ? SUCCESS : FAILURE;
742
743 if (clean_curl) {
744 curl_easy_cleanup(ch);
745 }
746 return status;
747 }
748 /* }}} */
749
750 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
751 static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *s)
752 {
753 HTTP_REQUEST_CALLBACK_DATA(s, http_request_body *, body);
754
755 if (body->type != HTTP_REQUEST_BODY_UPLOADFILE) {
756 return 0;
757 }
758 return php_stream_read((php_stream *) body->data, data, len * n);
759 }
760 /* }}} */
761
762 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
763 static int http_curl_progress_callback(void *data, double dltotal, double dlnow, double ultotal, double ulnow)
764 {
765 zval *params_pass[4], params_local[4], retval;
766 HTTP_REQUEST_CALLBACK_DATA(data, zval *, func);
767
768 params_pass[0] = &params_local[0];
769 params_pass[1] = &params_local[1];
770 params_pass[2] = &params_local[2];
771 params_pass[3] = &params_local[3];
772
773 INIT_PZVAL(params_pass[0]);
774 INIT_PZVAL(params_pass[1]);
775 INIT_PZVAL(params_pass[2]);
776 INIT_PZVAL(params_pass[3]);
777 ZVAL_DOUBLE(params_pass[0], dltotal);
778 ZVAL_DOUBLE(params_pass[1], dlnow);
779 ZVAL_DOUBLE(params_pass[2], ultotal);
780 ZVAL_DOUBLE(params_pass[3], ulnow);
781
782 return call_user_function(EG(function_table), NULL, func, &retval, 4, params_pass TSRMLS_CC);
783 }
784 /* }}} */
785
786 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
787 static int http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size_t length, void *ctx)
788 {
789 HTTP_REQUEST_CALLBACK_DATA(ctx, http_request_conv *, conv);
790
791 #if 0
792 fprintf(stderr, "DEBUG: %s\n", data);
793 #endif
794
795 switch (type)
796 {
797 case CURLINFO_DATA_IN:
798 if (conv->response && conv->last_info == CURLINFO_HEADER_IN) {
799 phpstr_appends(conv->response, HTTP_CRLF);
800 }
801 case CURLINFO_HEADER_IN:
802 if (conv->response) {
803 phpstr_append(conv->response, data, length);
804 }
805 break;
806 case CURLINFO_DATA_OUT:
807 if (conv->request && conv->last_info == CURLINFO_HEADER_OUT) {
808 phpstr_appends(conv->request, HTTP_CRLF);
809 }
810 case CURLINFO_HEADER_OUT:
811 if (conv->request) {
812 phpstr_append(conv->request, data, length);
813 }
814 break;
815 }
816
817 if (type) {
818 conv->last_info = type;
819 }
820 return 0;
821 }
822 /* }}} */
823
824 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
825 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC)
826 {
827 zval **zoption;
828
829 if (!options || (SUCCESS != zend_hash_find(options, key, keylen, (void **) &zoption))) {
830 return NULL;
831 }
832
833 if (Z_TYPE_PP(zoption) != type) {
834 switch (type)
835 {
836 case IS_BOOL: convert_to_boolean_ex(zoption); break;
837 case IS_LONG: convert_to_long_ex(zoption); break;
838 case IS_DOUBLE: convert_to_double_ex(zoption); break;
839 case IS_STRING: convert_to_string_ex(zoption); break;
840 case IS_ARRAY: convert_to_array_ex(zoption); break;
841 case IS_OBJECT: convert_to_object_ex(zoption); break;
842 default:
843 break;
844 }
845 }
846
847 return *zoption;
848 }
849 /* }}} */
850
851 #endif
852
853 /*
854 * Local variables:
855 * tab-width: 4
856 * c-basic-offset: 4
857 * End:
858 * vim600: noet sw=4 ts=4 fdm=marker
859 * vim<600: noet sw=4 ts=4
860 */
861