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