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