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