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