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