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