8d61558a73a3052799db5850f9ed23c5d3b40afd
[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 add_next_index_string(subarray, p->data, 1); \
150 } \
151 add_assoc_zval(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), subarray); \
152 curl_slist_free_all(l); \
153 } \
154 } \
155 }
156
157 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
158 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
159 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
160 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
161 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
162 if (!strcasecmp(key, #keyname)) { \
163 convert_to_string_ex(param); \
164 HTTP_CURL_OPT(optname, http_request_data_copy(COPY_STRING, Z_STRVAL_PP(param))); \
165 key = NULL; \
166 continue; \
167 }
168 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
169 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
170 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
171 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
172 if (!strcasecmp(key, #keyname)) { \
173 convert_to_long_ex(param); \
174 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
175 key = NULL; \
176 continue; \
177 }
178
179 #define http_curl_getopt(o, k, t) _http_curl_getopt_ex((o), (k), sizeof(k), (t) TSRMLS_CC)
180 #define http_curl_getopt_ex(o, k, l, t) _http_curl_getopt_ex((o), (k), (l), (t) TSRMLS_CC)
181 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC);
182 #define http_curl_defaults(ch) _http_curl_defaults((ch))
183 static inline void _http_curl_defaults(CURL *ch);
184 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
185 static int http_curl_progress_callback(void *, double, double, double, double);
186 static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *);
187 static int http_curl_dummy_callback(char *data, size_t n, size_t l, void *s) { return n*l; }
188 static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *);
189
190 /* {{{ http_request_callback_ctx http_request_callback_data(void *) */
191 http_request_callback_ctx *_http_request_callback_data_ex(void *data, zend_bool cpy TSRMLS_DC)
192 {
193 http_request_callback_ctx *ctx = emalloc(sizeof(http_request_callback_ctx));
194
195 TSRMLS_SET_CTX(ctx->tsrm_ctx);
196 ctx->data = data;
197
198 if (cpy) {
199 return http_request_data_copy(COPY_CONTEXT, ctx);
200 } else {
201 return ctx;
202 }
203 }
204 /* }}} */
205
206 /* {{{ void *http_request_data_copy(int, void *) */
207 void *_http_request_data_copy(int type, void *data TSRMLS_DC)
208 {
209 switch (type)
210 {
211 case COPY_STRING:
212 {
213 char *new_str = estrdup(data);
214 zend_llist_add_element(&HTTP_G(request).copies.strings, &new_str);
215 return new_str;
216 }
217
218 case COPY_SLIST:
219 {
220 zend_llist_add_element(&HTTP_G(request).copies.slists, &data);
221 return data;
222 }
223
224 case COPY_CONTEXT:
225 {
226 zend_llist_add_element(&HTTP_G(request).copies.contexts, &data);
227 return data;
228 }
229
230 case COPY_CONV:
231 {
232 zend_llist_add_element(&HTTP_G(request).copies.convs, &data);
233 return data;
234 }
235
236 default:
237 {
238 return data;
239 }
240 }
241 }
242 /* }}} */
243
244 /* {{{ void http_request_data_free_string(char **) */
245 void _http_request_data_free_string(void *string)
246 {
247 efree(*((char **)string));
248 }
249 /* }}} */
250
251 /* {{{ void http_request_data_free_slist(struct curl_slist **) */
252 void _http_request_data_free_slist(void *list)
253 {
254 curl_slist_free_all(*((struct curl_slist **) list));
255 }
256 /* }}} */
257
258 /* {{{ _http_request_data_free_context(http_request_callback_ctx **) */
259 void _http_request_data_free_context(void *context)
260 {
261 efree(*((http_request_callback_ctx **) context));
262 }
263 /* }}} */
264
265 /* {{{ _http_request_data_free_conv(http_request_conv **) */
266 void _http_request_data_free_conv(void *conv)
267 {
268 efree(*((http_request_conv **) conv));
269 }
270 /* }}} */
271
272 /* {{{ http_request_body *http_request_body_new() */
273 PHP_HTTP_API http_request_body *_http_request_body_new(TSRMLS_D)
274 {
275 http_request_body *body = ecalloc(1, sizeof(http_request_body));
276 return body;
277 }
278 /* }}} */
279
280 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
281 PHP_HTTP_API STATUS _http_request_body_fill(http_request_body *body, HashTable *fields, HashTable *files TSRMLS_DC)
282 {
283 if (files && (zend_hash_num_elements(files) > 0)) {
284 char *key = NULL;
285 ulong idx;
286 zval **data;
287 HashPosition pos;
288 struct curl_httppost *http_post_data[2] = {NULL, NULL};
289
290 /* normal data */
291 FOREACH_HASH_KEYVAL(pos, fields, key, idx, data) {
292 CURLcode err;
293 if (key) {
294 zval *orig = *data;
295
296 convert_to_string_ex(data);
297 err = curl_formadd(&http_post_data[0], &http_post_data[1],
298 CURLFORM_COPYNAME, key,
299 CURLFORM_COPYCONTENTS, Z_STRVAL_PP(data),
300 CURLFORM_CONTENTSLENGTH, (long) Z_STRLEN_PP(data),
301 CURLFORM_END
302 );
303
304 if (orig != *data) {
305 zval_ptr_dtor(data);
306 }
307
308 if (CURLE_OK != err) {
309 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not encode post fields: %s", curl_easy_strerror(err));
310 curl_formfree(http_post_data[0]);
311 return FAILURE;
312 }
313
314 /* reset */
315 key = NULL;
316 }
317 }
318
319 /* file data */
320 FOREACH_HASH_VAL(pos, files, data) {
321 zval **file, **type, **name;
322
323 if (Z_TYPE_PP(data) != IS_ARRAY) {
324 http_error(HE_NOTICE, HTTP_E_INVALID_PARAM, "Unrecognized type of post file array entry");
325 } else if ( SUCCESS != zend_hash_find(Z_ARRVAL_PP(data), "name", sizeof("name"), (void **) &name) ||
326 SUCCESS != zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &type) ||
327 SUCCESS != zend_hash_find(Z_ARRVAL_PP(data), "file", sizeof("file"), (void **) &file)) {
328 http_error(HE_NOTICE, HTTP_E_INVALID_PARAM, "Post file array entry misses either 'name', 'type' or 'file' entry");
329 } else {
330 CURLcode err = curl_formadd(&http_post_data[0], &http_post_data[1],
331 CURLFORM_COPYNAME, Z_STRVAL_PP(name),
332 CURLFORM_FILE, Z_STRVAL_PP(file),
333 CURLFORM_CONTENTTYPE, Z_STRVAL_PP(type),
334 CURLFORM_END
335 );
336 if (CURLE_OK != err) {
337 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not encode post files: %s", curl_easy_strerror(err));
338 curl_formfree(http_post_data[0]);
339 return FAILURE;
340 }
341 }
342 }
343
344 body->type = HTTP_REQUEST_BODY_CURLPOST;
345 body->data = http_post_data[0];
346 body->size = 0;
347
348 } else {
349 char *encoded;
350 size_t encoded_len;
351
352 if (SUCCESS != http_urlencode_hash_ex(fields, 1, NULL, 0, &encoded, &encoded_len)) {
353 http_error(HE_WARNING, HTTP_E_ENCODING, "Could not encode post data");
354 return FAILURE;
355 }
356
357 body->type = HTTP_REQUEST_BODY_CSTRING;
358 body->data = encoded;
359 body->size = encoded_len;
360 }
361
362 return SUCCESS;
363 }
364 /* }}} */
365
366 /* {{{ void http_request_body_dtor(http_request_body *) */
367 PHP_HTTP_API void _http_request_body_dtor(http_request_body *body TSRMLS_DC)
368 {
369 if (body) {
370 switch (body->type)
371 {
372 case HTTP_REQUEST_BODY_CSTRING:
373 if (body->data) {
374 efree(body->data);
375 }
376 break;
377
378 case HTTP_REQUEST_BODY_CURLPOST:
379 curl_formfree(body->data);
380 break;
381
382 case HTTP_REQUEST_BODY_UPLOADFILE:
383 php_stream_close(body->data);
384 break;
385 }
386 }
387 }
388 /* }}} */
389
390 /* {{{ void http_request_body_free(http_request_body *) */
391 PHP_HTTP_API void _http_request_body_free(http_request_body **body TSRMLS_DC)
392 {
393 if (*body) {
394 http_request_body_dtor(*body);
395 efree(*body);
396 *body = NULL;
397 }
398 }
399 /* }}} */
400
401 /* {{{ STATUS http_request_init(CURL *, http_request_method, char *, http_request_body *, HashTable *) */
402 PHP_HTTP_API STATUS _http_request_init(CURL *ch, http_request_method meth, char *url, http_request_body *body, HashTable *options TSRMLS_DC)
403 {
404 zval *zoption;
405 zend_bool range_req = 0;
406
407 /* reset CURL handle */
408 #ifdef HAVE_CURL_EASY_RESET
409 curl_easy_reset(ch);
410 #endif
411 http_curl_defaults(ch);
412
413 /* set options */
414 if (url) {
415 HTTP_CURL_OPT(URL, http_request_data_copy(COPY_STRING, url));
416 }
417
418 HTTP_CURL_OPT(HEADER, 0);
419 HTTP_CURL_OPT(FILETIME, 1);
420 HTTP_CURL_OPT(AUTOREFERER, 1);
421 HTTP_CURL_OPT(READFUNCTION, http_curl_read_callback);
422 HTTP_CURL_OPT(IOCTLFUNCTION, http_curl_ioctl_callback);
423 /* we'll get all data through the debug function */
424 HTTP_CURL_OPT(WRITEFUNCTION, http_curl_dummy_callback);
425 HTTP_CURL_OPT(HEADERFUNCTION, NULL);
426
427 HTTP_CURL_OPT(VERBOSE, 1);
428 HTTP_CURL_OPT(DEBUGFUNCTION, http_curl_raw_callback);
429
430 #if defined(ZTS)
431 HTTP_CURL_OPT(NOSIGNAL, 1);
432 #endif
433
434 /* progress callback */
435 if ((zoption = http_curl_getopt(options, "onprogress", 0))) {
436 HTTP_CURL_OPT(NOPROGRESS, 0);
437 HTTP_CURL_OPT(PROGRESSFUNCTION, http_curl_progress_callback);
438 HTTP_CURL_OPT(PROGRESSDATA, http_request_callback_data(zoption));
439 }
440
441 /* proxy */
442 if ((zoption = http_curl_getopt(options, "proxyhost", IS_STRING))) {
443 HTTP_CURL_OPT(PROXY, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
444 /* port */
445 if ((zoption = http_curl_getopt(options, "proxyport", IS_LONG))) {
446 HTTP_CURL_OPT(PROXYPORT, Z_LVAL_P(zoption));
447 }
448 /* user:pass */
449 if ((zoption = http_curl_getopt(options, "proxyauth", IS_STRING))) {
450 HTTP_CURL_OPT(PROXYUSERPWD, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
451 }
452 /* auth method */
453 if ((zoption = http_curl_getopt(options, "proxyauthtype", IS_LONG))) {
454 HTTP_CURL_OPT(PROXYAUTH, Z_LVAL_P(zoption));
455 }
456 }
457
458 /* outgoing interface */
459 if ((zoption = http_curl_getopt(options, "interface", IS_STRING))) {
460 HTTP_CURL_OPT(INTERFACE, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
461 }
462
463 /* another port */
464 if ((zoption = http_curl_getopt(options, "port", IS_LONG))) {
465 HTTP_CURL_OPT(PORT, Z_LVAL_P(zoption));
466 }
467
468 /* auth */
469 if ((zoption = http_curl_getopt(options, "httpauth", IS_STRING))) {
470 HTTP_CURL_OPT(USERPWD, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
471 }
472 if ((zoption = http_curl_getopt(options, "httpauthtype", IS_LONG))) {
473 HTTP_CURL_OPT(HTTPAUTH, Z_LVAL_P(zoption));
474 }
475
476 /* compress, empty string enables all supported if libcurl was build with zlib support */
477 if ((zoption = http_curl_getopt(options, "compress", IS_BOOL)) && Z_LVAL_P(zoption)) {
478 #ifdef HTTP_HAVE_CURL_ZLIB
479 HTTP_CURL_OPT(ENCODING, "");
480 #else
481 HTTP_CURL_OPT(ENCODING, "gzip;q=1.0, deflate;q=0.5, *;q=0.1");
482 #endif
483 }
484
485 /* redirects, defaults to 0 */
486 if ((zoption = http_curl_getopt(options, "redirect", IS_LONG))) {
487 HTTP_CURL_OPT(FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
488 HTTP_CURL_OPT(MAXREDIRS, Z_LVAL_P(zoption));
489 if ((zoption = http_curl_getopt(options, "unrestrictedauth", IS_BOOL))) {
490 HTTP_CURL_OPT(UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
491 }
492 }
493
494 /* referer */
495 if ((zoption = http_curl_getopt(options, "referer", IS_STRING))) {
496 HTTP_CURL_OPT(REFERER, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
497 }
498
499 /* useragent, default "PECL::HTTP/version (PHP/version)" */
500 if ((zoption = http_curl_getopt(options, "useragent", IS_STRING))) {
501 HTTP_CURL_OPT(USERAGENT, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
502 }
503
504 /* additional headers, array('name' => 'value') */
505 if ((zoption = http_curl_getopt(options, "headers", IS_ARRAY))) {
506 char *header_key;
507 ulong header_idx;
508 HashPosition pos;
509 struct curl_slist *headers = NULL;
510
511 FOREACH_KEY(pos, zoption, header_key, header_idx) {
512 if (header_key) {
513 zval **header_val;
514 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void **) &header_val, &pos)) {
515 char header[1024] = {0};
516 snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val));
517 headers = curl_slist_append(headers, http_request_data_copy(COPY_STRING, header));
518 }
519
520 /* reset */
521 header_key = NULL;
522 }
523 }
524
525 if (headers) {
526 HTTP_CURL_OPT(HTTPHEADER, http_request_data_copy(COPY_SLIST, headers));
527 }
528 }
529
530 /* cookies, array('name' => 'value') */
531 if ((zoption = http_curl_getopt(options, "cookies", IS_ARRAY))) {
532 char *cookie_key = NULL;
533 ulong cookie_idx = 0;
534 HashPosition pos;
535 phpstr *qstr = phpstr_new();
536
537 FOREACH_KEY(pos, zoption, cookie_key, cookie_idx) {
538 if (cookie_key) {
539 zval **cookie_val;
540 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void **) &cookie_val, &pos)) {
541 phpstr_appendf(qstr, "%s=%s; ", cookie_key, Z_STRVAL_PP(cookie_val));
542 }
543
544 /* reset */
545 cookie_key = NULL;
546 }
547 }
548
549 if (qstr->used) {
550 phpstr_fix(qstr);
551 HTTP_CURL_OPT(COOKIE, http_request_data_copy(COPY_STRING, qstr->data));
552 }
553 phpstr_free(&qstr);
554 }
555
556 /* session cookies */
557 if ((zoption = http_curl_getopt(options, "cookiesession", IS_BOOL))) {
558 if (Z_LVAL_P(zoption)) {
559 /* accept cookies for this session */
560 HTTP_CURL_OPT(COOKIEFILE, "");
561 } else {
562 /* reset session cookies */
563 HTTP_CURL_OPT(COOKIESESSION, 1);
564 }
565 }
566
567 /* cookiestore, read initial cookies from that file and store cookies back into that file */
568 if ((zoption = http_curl_getopt(options, "cookiestore", IS_STRING)) && Z_STRLEN_P(zoption)) {
569 HTTP_CURL_OPT(COOKIEFILE, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
570 HTTP_CURL_OPT(COOKIEJAR, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
571 }
572
573 /* resume */
574 if ((zoption = http_curl_getopt(options, "resume", IS_LONG)) && (Z_LVAL_P(zoption) != 0)) {
575 range_req = 1;
576 HTTP_CURL_OPT(RESUME_FROM, Z_LVAL_P(zoption));
577 }
578
579 /* maxfilesize */
580 if ((zoption = http_curl_getopt(options, "maxfilesize", IS_LONG))) {
581 HTTP_CURL_OPT(MAXFILESIZE, Z_LVAL_P(zoption));
582 }
583
584 /* lastmodified */
585 if ((zoption = http_curl_getopt(options, "lastmodified", IS_LONG))) {
586 if (Z_LVAL_P(zoption)) {
587 if (Z_LVAL_P(zoption) > 0) {
588 HTTP_CURL_OPT(TIMEVALUE, Z_LVAL_P(zoption));
589 } else {
590 HTTP_CURL_OPT(TIMEVALUE, time(NULL) + Z_LVAL_P(zoption));
591 }
592 HTTP_CURL_OPT(TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE);
593 } else {
594 HTTP_CURL_OPT(TIMECONDITION, CURL_TIMECOND_NONE);
595 }
596 }
597
598 /* timeout, defaults to 0 */
599 if ((zoption = http_curl_getopt(options, "timeout", IS_LONG))) {
600 HTTP_CURL_OPT(TIMEOUT, Z_LVAL_P(zoption));
601 }
602
603 /* connecttimeout, defaults to 3 */
604 if ((zoption = http_curl_getopt(options, "connecttimeout", IS_LONG))) {
605 HTTP_CURL_OPT(CONNECTTIMEOUT, Z_LVAL_P(zoption));
606 }
607
608 /* ssl */
609 if ((zoption = http_curl_getopt(options, "ssl", IS_ARRAY))) {
610 ulong idx;
611 char *key = NULL;
612 zval **param;
613 HashPosition pos;
614
615 FOREACH_KEYVAL(pos, zoption, key, idx, param) {
616 if (key) {
617 HTTP_CURL_OPT_SSL_STRING(CERT);
618 HTTP_CURL_OPT_SSL_STRING(CERTTYPE);
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 HTTP_CURL_OPT_STRING(CAINFO);
633 HTTP_CURL_OPT_STRING(CAPATH);
634 HTTP_CURL_OPT_STRING(RANDOM_FILE);
635 HTTP_CURL_OPT_STRING(EGDSOCKET);
636
637 /* reset key */
638 key = NULL;
639 }
640 }
641 }
642
643 /* request method */
644 switch (meth)
645 {
646 case HTTP_GET:
647 curl_easy_setopt(ch, CURLOPT_HTTPGET, 1);
648 break;
649
650 case HTTP_HEAD:
651 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
652 break;
653
654 case HTTP_POST:
655 curl_easy_setopt(ch, CURLOPT_POST, 1);
656 break;
657
658 case HTTP_PUT:
659 curl_easy_setopt(ch, CURLOPT_UPLOAD, 1);
660 break;
661
662 default:
663 if (http_request_method_exists(0, meth, NULL)) {
664 curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, http_request_method_name(meth));
665 } else {
666 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unsupported request method: %d", meth);
667 return FAILURE;
668 }
669 break;
670 }
671
672 /* attach request body */
673 if (body && (meth != HTTP_GET) && (meth != HTTP_HEAD)) {
674 switch (body->type)
675 {
676 case HTTP_REQUEST_BODY_CSTRING:
677 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, body->data);
678 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, body->size);
679 break;
680
681 case HTTP_REQUEST_BODY_CURLPOST:
682 curl_easy_setopt(ch, CURLOPT_HTTPPOST, (struct curl_httppost *) body->data);
683 break;
684
685 case HTTP_REQUEST_BODY_UPLOADFILE:
686 curl_easy_setopt(ch, CURLOPT_IOCTLDATA, http_request_callback_data(body));
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 curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *) */
828 static curlioerr http_curl_ioctl_callback(CURL *ch, curliocmd cmd, void *ctx)
829 {
830 HTTP_REQUEST_CALLBACK_DATA(ctx, http_request_body *, body);
831
832 if (cmd != CURLIOCMD_RESTARTREAD) {
833 return CURLIOE_UNKNOWNCMD;
834 }
835 if (body->type != HTTP_REQUEST_BODY_UPLOADFILE) {
836 return CURLIOE_FAILRESTART;
837 }
838 if (SUCCESS != php_stream_rewind((php_stream *) body->data)) {
839 return CURLIOE_FAILRESTART;
840 }
841 return CURLIOE_OK;
842 }
843 /* }}} */
844
845 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
846 static int http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size_t length, void *ctx)
847 {
848 HTTP_REQUEST_CALLBACK_DATA(ctx, http_request_conv *, conv);
849
850 switch (type)
851 {
852 case CURLINFO_DATA_IN:
853 if (conv->response && conv->last_info == CURLINFO_HEADER_IN) {
854 phpstr_appends(conv->response, HTTP_CRLF);
855 }
856 case CURLINFO_HEADER_IN:
857 if (conv->response) {
858 phpstr_append(conv->response, data, length);
859 }
860 break;
861 case CURLINFO_DATA_OUT:
862 if (conv->request && conv->last_info == CURLINFO_HEADER_OUT) {
863 phpstr_appends(conv->request, HTTP_CRLF);
864 }
865 case CURLINFO_HEADER_OUT:
866 if (conv->request) {
867 phpstr_append(conv->request, data, length);
868 }
869 break;
870 default:
871 #if 0
872 fprintf(stderr, "## ", type);
873 if (!type) {
874 fprintf(stderr, "%s", data);
875 } else {
876 ulong i;
877 for (i = 1; i <= length; ++i) {
878 fprintf(stderr, "%02X ", data[i-1] & 0xFF);
879 if (!(i % 20)) {
880 fprintf(stderr, "\n## ");
881 }
882 }
883 fprintf(stderr, "\n");
884 }
885 if (data[length-1] != 0xa) {
886 fprintf(stderr, "\n");
887 }
888 #endif
889 break;
890 }
891
892 if (type) {
893 conv->last_info = type;
894 }
895 return 0;
896 }
897 /* }}} */
898
899 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
900 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC)
901 {
902 zval **zoption;
903
904 if (!options || (SUCCESS != zend_hash_find(options, key, keylen, (void **) &zoption))) {
905 return NULL;
906 }
907
908 if (Z_TYPE_PP(zoption) != type) {
909 switch (type)
910 {
911 case IS_BOOL: convert_to_boolean_ex(zoption); break;
912 case IS_LONG: convert_to_long_ex(zoption); break;
913 case IS_DOUBLE: convert_to_double_ex(zoption); break;
914 case IS_STRING: convert_to_string_ex(zoption); break;
915 case IS_ARRAY: convert_to_array_ex(zoption); break;
916 case IS_OBJECT: convert_to_object_ex(zoption); break;
917 default:
918 break;
919 }
920 }
921
922 return *zoption;
923 }
924 /* }}} */
925
926 #ifdef HTTP_NEED_OPENSSL_TSL
927 /* {{{ */
928 static MUTEX_T *http_openssl_tsl = NULL;
929
930 static void http_ssl_lock(int mode, int n, const char * file, int line)
931 {
932 if (mode & CRYPTO_LOCK) {
933 tsrm_mutex_lock(http_openssl_tsl[n]);
934 } else {
935 tsrm_mutex_unlock(http_openssl_tsl[n]);
936 }
937 }
938
939 static ulong http_ssl_id(void)
940 {
941 return (ulong) tsrm_thread_id();
942 }
943
944 static inline void http_ssl_init(void)
945 {
946 int i, c = CRYPTO_num_locks();
947
948 http_openssl_tsl = malloc(c * sizeof(MUTEX_T));
949
950 for (i = 0; i < c; ++i) {
951 http_openssl_tsl[i] = tsrm_mutex_alloc();
952 }
953
954 CRYPTO_set_id_callback(http_ssl_id);
955 CRYPTO_set_locking_callback(http_ssl_lock);
956 }
957
958 static inline void http_ssl_cleanup(void)
959 {
960 if (http_openssl_tsl) {
961 int i, c = CRYPTO_num_locks();
962
963 CRYPTO_set_id_callback(NULL);
964 CRYPTO_set_locking_callback(NULL);
965
966 for (i = 0; i < c; ++i) {
967 tsrm_mutex_free(http_openssl_tsl[i]);
968 }
969
970 free(http_openssl_tsl);
971 http_openssl_tsl = NULL;
972 }
973 }
974 #endif /* HTTP_NEED_OPENSSL_TSL */
975 /* }}} */
976
977 #ifdef HTTP_NEED_GNUTLS_TSL
978 /* {{{ */
979 static int http_ssl_mutex_create(void **m)
980 {
981 if (*((MUTEX_T *) m) = tsrm_mutex_alloc()) {
982 return SUCCESS;
983 } else {
984 return FAILURE;
985 }
986 }
987
988 static int http_ssl_mutex_destroy(void **m)
989 {
990 tsrm_mutex_free(*((MUTEX_T *) m));
991 return SUCCESS;
992 }
993
994 static int http_ssl_mutex_lock(void **m)
995 {
996 return tsrm_mutex_lock(*((MUTEX_T *) m));
997 }
998
999 static int http_ssl_mutex_unlock(void **m)
1000 {
1001 return tsrm_mutex_unlock(*((MUTEX_T *) m));
1002 }
1003
1004 static struct gcry_thread_cbs http_gnutls_tsl = {
1005 GCRY_THREAD_OPTIONS_USER,
1006 NULL,
1007 http_ssl_mutex_create,
1008 http_ssl_mutex_destroy,
1009 http_ssl_mutex_lock,
1010 http_ssl_mutex_unlock
1011 };
1012
1013 static inline void http_ssl_init(void)
1014 {
1015 gcry_control(GCRYCTL_SET_THREAD_CBS, &http_gnutls_tsl);
1016 }
1017
1018 static inline void http_ssl_cleanup(void)
1019 {
1020 return;
1021 }
1022 #endif /* HTTP_NEED_GNUTLS_TSL */
1023 /* }}} */
1024
1025 /* {{{ http_curl_defaults(CURL *) */
1026 static inline void _http_curl_defaults(CURL *ch)
1027 {
1028 HTTP_CURL_OPT(URL, NULL);
1029 HTTP_CURL_OPT(NOPROGRESS, 1);
1030 HTTP_CURL_OPT(PROXY, NULL);
1031 HTTP_CURL_OPT(PROXYPORT, 0);
1032 HTTP_CURL_OPT(PROXYUSERPWD, NULL);
1033 HTTP_CURL_OPT(PROXYAUTH, 0);
1034 HTTP_CURL_OPT(INTERFACE, NULL);
1035 HTTP_CURL_OPT(PORT, 0);
1036 HTTP_CURL_OPT(USERPWD, NULL);
1037 HTTP_CURL_OPT(HTTPAUTH, 0);
1038 HTTP_CURL_OPT(ENCODING, 0);
1039 HTTP_CURL_OPT(FOLLOWLOCATION, 0);
1040 HTTP_CURL_OPT(UNRESTRICTED_AUTH, 0);
1041 HTTP_CURL_OPT(REFERER, NULL);
1042 HTTP_CURL_OPT(USERAGENT, "PECL::HTTP/" PHP_EXT_HTTP_VERSION " (PHP/" PHP_VERSION ")");
1043 HTTP_CURL_OPT(HTTPHEADER, NULL);
1044 HTTP_CURL_OPT(COOKIE, NULL);
1045 HTTP_CURL_OPT(COOKIEFILE, NULL);
1046 HTTP_CURL_OPT(COOKIEJAR, NULL);
1047 HTTP_CURL_OPT(RESUME_FROM, 0);
1048 HTTP_CURL_OPT(MAXFILESIZE, 0);
1049 HTTP_CURL_OPT(TIMECONDITION, 0);
1050 HTTP_CURL_OPT(TIMEVALUE, 0);
1051 HTTP_CURL_OPT(TIMEOUT, 0);
1052 HTTP_CURL_OPT(CONNECTTIMEOUT, 3);
1053 HTTP_CURL_OPT(SSLCERT, NULL);
1054 HTTP_CURL_OPT(SSLCERTTYPE, NULL);
1055 HTTP_CURL_OPT(SSLCERTPASSWD, NULL);
1056 HTTP_CURL_OPT(SSLKEY, NULL);
1057 HTTP_CURL_OPT(SSLKEYTYPE, NULL);
1058 HTTP_CURL_OPT(SSLKEYPASSWD, NULL);
1059 HTTP_CURL_OPT(SSLENGINE, NULL);
1060 HTTP_CURL_OPT(SSLVERSION, 0);
1061 HTTP_CURL_OPT(SSL_VERIFYPEER, 0);
1062 HTTP_CURL_OPT(SSL_VERIFYHOST, 0);
1063 HTTP_CURL_OPT(SSL_CIPHER_LIST, NULL);
1064 HTTP_CURL_OPT(CAINFO, NULL);
1065 HTTP_CURL_OPT(CAPATH, NULL);
1066 HTTP_CURL_OPT(RANDOM_FILE, NULL);
1067 HTTP_CURL_OPT(EGDSOCKET, NULL);
1068 HTTP_CURL_OPT(POSTFIELDS, NULL);
1069 HTTP_CURL_OPT(POSTFIELDSIZE, 0);
1070 HTTP_CURL_OPT(HTTPPOST, NULL);
1071 HTTP_CURL_OPT(IOCTLDATA, NULL);
1072 HTTP_CURL_OPT(READDATA, NULL);
1073 HTTP_CURL_OPT(INFILESIZE, 0);
1074 }
1075 /* }}} */
1076
1077 #endif /* HTTP_HAVE_CURL */
1078
1079 /*
1080 * Local variables:
1081 * tab-width: 4
1082 * c-basic-offset: 4
1083 * End:
1084 * vim600: noet sw=4 ts=4 fdm=marker
1085 * vim<600: noet sw=4 ts=4
1086 */
1087