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