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