- reimplement major parts of the request api
[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_url_api.h"
27
28 #ifdef ZEND_ENGINE_2
29 # include "php_http_request_object.h"
30 #endif
31
32 /* {{{ cruft for thread safe SSL crypto locks */
33 #if defined(ZTS) && defined(HTTP_HAVE_SSL)
34 # ifdef PHP_WIN32
35 # define HTTP_NEED_SSL_TSL
36 # define HTTP_NEED_OPENSSL_TSL
37 # include <openssl/crypto.h>
38 # else /* !PHP_WIN32 */
39 # if defined(HTTP_HAVE_OPENSSL)
40 # if defined(HAVE_OPENSSL_CRYPTO_H)
41 # define HTTP_NEED_SSL_TSL
42 # define HTTP_NEED_OPENSSL_TSL
43 # include <openssl/crypto.h>
44 # else
45 # warning \
46 "libcurl was compiled with OpenSSL support, but configure could not find " \
47 "openssl/crypto.h; thus no SSL crypto locking callbacks will be set, which may " \
48 "cause random crashes on SSL requests"
49 # endif
50 # elif defined(HTTP_HAVE_GNUTLS)
51 # if defined(HAVE_GCRYPT_H)
52 # define HTTP_NEED_SSL_TSL
53 # define HTTP_NEED_GNUTLS_TSL
54 # include <gcrypt.h>
55 # else
56 # warning \
57 "libcurl was compiled with GnuTLS support, but configure could not find " \
58 "gcrypt.h; thus no SSL crypto locking callbacks will be set, which may " \
59 "cause random crashes on SSL requests"
60 # endif
61 # else
62 # warning \
63 "libcurl was compiled with SSL support, but configure could not determine which" \
64 "library was used; thus no SSL crypto locking callbacks will be set, which may " \
65 "cause random crashes on SSL requests"
66 # endif /* HTTP_HAVE_OPENSSL || HTTP_HAVE_GNUTLS */
67 # endif /* PHP_WIN32 */
68 #endif /* ZTS && HTTP_HAVE_SSL */
69 /* }}} */
70
71 ZEND_EXTERN_MODULE_GLOBALS(http);
72
73 #ifdef HTTP_NEED_SSL_TSL
74 static inline void http_ssl_init(void);
75 static inline void http_ssl_cleanup(void);
76 #endif
77
78 PHP_MINIT_FUNCTION(http_request)
79 {
80 #ifdef HTTP_NEED_SSL_TSL
81 http_ssl_init();
82 #endif
83
84 if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
85 return FAILURE;
86 }
87
88 HTTP_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC);
89 HTTP_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST);
90 HTTP_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM);
91 HTTP_LONG_CONSTANT("HTTP_AUTH_ANY", CURLAUTH_ANY);
92
93 return SUCCESS;
94 }
95
96 PHP_MSHUTDOWN_FUNCTION(http_request)
97 {
98 curl_global_cleanup();
99 #ifdef HTTP_NEED_SSL_TSL
100 http_ssl_cleanup();
101 #endif
102 return SUCCESS;
103 }
104
105 #ifndef HAVE_CURL_EASY_STRERROR
106 # define curl_easy_strerror(dummy) "unkown error"
107 #endif
108
109 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
110 #define HTTP_CURL_INFO_EX(I, X) \
111 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
112 { \
113 case CURLINFO_STRING: \
114 { \
115 char *c; \
116 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &c)) { \
117 char *key = estrndup(#X, lenof(#X)); \
118 add_assoc_string(&array, pretty_key(key, lenof(#X), 0, 0), c ? c : "", 1); \
119 efree(key); \
120 } \
121 } \
122 break; \
123 \
124 case CURLINFO_DOUBLE: \
125 { \
126 double d; \
127 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &d)) { \
128 char *key = estrndup(#X, lenof(#X)); \
129 add_assoc_double(&array, pretty_key(key, lenof(#X), 0, 0), d); \
130 efree(key); \
131 } \
132 } \
133 break; \
134 \
135 case CURLINFO_LONG: \
136 { \
137 long l; \
138 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &l)) { \
139 char *key = estrndup(#X, lenof(#X)); \
140 add_assoc_long(&array, pretty_key(key, lenof(#X), 0, 0), l); \
141 efree(key); \
142 } \
143 } \
144 break; \
145 \
146 case CURLINFO_SLIST: \
147 { \
148 struct curl_slist *l, *p; \
149 if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &l)) { \
150 zval *subarray; \
151 char *key = estrndup(#X, lenof(#X)); \
152 MAKE_STD_ZVAL(subarray); \
153 array_init(subarray); \
154 for (p = l; p; p = p->next) { \
155 add_next_index_string(subarray, p->data, 1); \
156 } \
157 add_assoc_zval(&array, pretty_key(key, lenof(#X), 0, 0), subarray); \
158 curl_slist_free_all(l); \
159 efree(key); \
160 } \
161 } \
162 }
163
164 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(request->ch, CURLOPT_##OPTION, (p))
165 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
166 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
167 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
168 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
169 if (!strcasecmp(key, #keyname)) { \
170 convert_to_string(*param); \
171 HTTP_CURL_OPT(optname, Z_STRVAL_PP(param)); \
172 key = NULL; \
173 continue; \
174 }
175 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
176 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
177 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
178 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
179 if (!strcasecmp(key, #keyname)) { \
180 convert_to_long(*param); \
181 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
182 key = NULL; \
183 continue; \
184 }
185
186 #define http_request_option(r, o, k, t) _http_request_option_ex((r), (o), (k), sizeof(k), (t) TSRMLS_CC)
187 #define http_request_option_ex(r, o, k, l, t) _http_request_option_ex((r), (o), (k), (l), (t) TSRMLS_CC)
188 static inline zval *_http_request_option_ex(http_request *request, HashTable *options, char *key, size_t keylen, int type TSRMLS_DC);
189
190 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
191 static int http_curl_progress_callback(void *, double, double, double, double);
192 static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *);
193 static int http_curl_dummy_callback(char *data, size_t n, size_t l, void *s) { return n*l; }
194 static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *);
195
196 PHP_HTTP_API http_request *_http_request_init_ex(http_request *request, CURL *ch, http_request_method meth, const char *url TSRMLS_DC)
197 {
198 http_request *r;
199
200 if (request) {
201 r = request;
202 } else {
203 r = emalloc(sizeof(http_request));
204 }
205 memset(r, 0, sizeof(http_request));
206
207 r->ch = ch;
208 r->url = (url && *url) ? estrdup(url) : NULL;
209 r->meth = (meth > 0) ? meth : HTTP_GET;
210
211 phpstr_init(&r->conv.request);
212 phpstr_init_ex(&r->conv.response, HTTP_CURLBUF_SIZE, 0);
213
214 zend_hash_init(&r->info, 0, NULL, ZVAL_PTR_DTOR, 0);
215
216 phpstr_init(&r->_cache.cookies);
217 zend_hash_init(&r->_cache.options, 0, NULL, ZVAL_PTR_DTOR, 0);
218
219 TSRMLS_SET_CTX(request->tsrm_ls);
220
221 return r;
222 }
223
224 PHP_HTTP_API void _http_request_dtor(http_request *request)
225 {
226 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
227
228 if (request->ch) {
229 /* avoid nasty segfaults with already cleaned up callbacks */
230 curl_easy_setopt(request->ch, CURLOPT_NOPROGRESS, 1);
231 curl_easy_setopt(request->ch, CURLOPT_PROGRESSFUNCTION, NULL);
232 curl_easy_setopt(request->ch, CURLOPT_VERBOSE, 0);
233 curl_easy_setopt(request->ch, CURLOPT_DEBUGFUNCTION, NULL);
234 curl_easy_cleanup(request->ch);
235 request->ch = NULL;
236 }
237
238 STR_SET(request->url, NULL);
239
240 request->conv.last_type = 0;
241 phpstr_dtor(&request->conv.request);
242 phpstr_dtor(&request->conv.response);
243
244 zend_hash_destroy(&request->info);
245
246 phpstr_dtor(&request->_cache.cookies);
247 zend_hash_destroy(&request->_cache.options);
248 if (request->_cache.headers) {
249 curl_slist_free_all(request->_cache.headers);
250 request->_cache.headers = NULL;
251 }
252 }
253
254 PHP_HTTP_API void _http_request_free(http_request **request)
255 {
256 if (*request) {
257 TSRMLS_FETCH_FROM_CTX((*request)->tsrm_ls);
258 http_request_dtor(*request);
259 efree(*request);
260 *request = NULL;
261 }
262 }
263
264 /* {{{ http_request_reset(http_request *) */
265 PHP_HTTP_API void _http_request_reset(http_request *request)
266 {
267 #ifdef HAVE_CURL_EASY_RESET
268 curl_easy_reset(request->ch);
269 #endif
270
271 STR_SET(request->url, NULL);
272 phpstr_dtor(&request->conv.request);
273 phpstr_dtor(&request->conv.response);
274
275 #if defined(ZTS)
276 HTTP_CURL_OPT(NOSIGNAL, 1);
277 #endif
278 HTTP_CURL_OPT(HEADER, 0);
279 HTTP_CURL_OPT(FILETIME, 1);
280 HTTP_CURL_OPT(AUTOREFERER, 1);
281 HTTP_CURL_OPT(VERBOSE, 1);
282 HTTP_CURL_OPT(HEADERFUNCTION, NULL);
283 HTTP_CURL_OPT(DEBUGFUNCTION, http_curl_raw_callback);
284 HTTP_CURL_OPT(READFUNCTION, http_curl_read_callback);
285 HTTP_CURL_OPT(IOCTLFUNCTION, http_curl_ioctl_callback);
286 HTTP_CURL_OPT(WRITEFUNCTION, http_curl_dummy_callback);
287 HTTP_CURL_OPT(URL, NULL);
288 HTTP_CURL_OPT(NOPROGRESS, 1);
289 HTTP_CURL_OPT(PROXY, NULL);
290 HTTP_CURL_OPT(PROXYPORT, 0);
291 HTTP_CURL_OPT(PROXYUSERPWD, NULL);
292 HTTP_CURL_OPT(PROXYAUTH, 0);
293 HTTP_CURL_OPT(INTERFACE, NULL);
294 HTTP_CURL_OPT(PORT, 0);
295 HTTP_CURL_OPT(USERPWD, NULL);
296 HTTP_CURL_OPT(HTTPAUTH, 0);
297 HTTP_CURL_OPT(ENCODING, 0);
298 HTTP_CURL_OPT(FOLLOWLOCATION, 0);
299 HTTP_CURL_OPT(UNRESTRICTED_AUTH, 0);
300 HTTP_CURL_OPT(REFERER, NULL);
301 HTTP_CURL_OPT(USERAGENT, "PECL::HTTP/" PHP_EXT_HTTP_VERSION " (PHP/" PHP_VERSION ")");
302 HTTP_CURL_OPT(HTTPHEADER, NULL);
303 HTTP_CURL_OPT(COOKIE, NULL);
304 HTTP_CURL_OPT(COOKIEFILE, NULL);
305 HTTP_CURL_OPT(COOKIEJAR, NULL);
306 HTTP_CURL_OPT(RESUME_FROM, 0);
307 HTTP_CURL_OPT(MAXFILESIZE, 0);
308 HTTP_CURL_OPT(TIMECONDITION, 0);
309 HTTP_CURL_OPT(TIMEVALUE, 0);
310 HTTP_CURL_OPT(TIMEOUT, 0);
311 HTTP_CURL_OPT(CONNECTTIMEOUT, 3);
312 HTTP_CURL_OPT(SSLCERT, NULL);
313 HTTP_CURL_OPT(SSLCERTTYPE, NULL);
314 HTTP_CURL_OPT(SSLCERTPASSWD, NULL);
315 HTTP_CURL_OPT(SSLKEY, NULL);
316 HTTP_CURL_OPT(SSLKEYTYPE, NULL);
317 HTTP_CURL_OPT(SSLKEYPASSWD, NULL);
318 HTTP_CURL_OPT(SSLENGINE, NULL);
319 HTTP_CURL_OPT(SSLVERSION, 0);
320 HTTP_CURL_OPT(SSL_VERIFYPEER, 0);
321 HTTP_CURL_OPT(SSL_VERIFYHOST, 0);
322 HTTP_CURL_OPT(SSL_CIPHER_LIST, NULL);
323 HTTP_CURL_OPT(CAINFO, NULL);
324 HTTP_CURL_OPT(CAPATH, NULL);
325 HTTP_CURL_OPT(RANDOM_FILE, NULL);
326 HTTP_CURL_OPT(EGDSOCKET, NULL);
327 HTTP_CURL_OPT(POSTFIELDS, NULL);
328 HTTP_CURL_OPT(POSTFIELDSIZE, 0);
329 HTTP_CURL_OPT(HTTPPOST, NULL);
330 HTTP_CURL_OPT(IOCTLDATA, NULL);
331 HTTP_CURL_OPT(READDATA, NULL);
332 HTTP_CURL_OPT(INFILESIZE, 0);
333 }
334 /* }}} */
335
336 PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *options)
337 {
338 zval *zoption;
339 zend_bool range_req = 0;
340
341 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
342
343 HTTP_CHECK_CURL_INIT(request->ch, curl_easy_init(), return FAILURE);
344
345 /* set options */
346 HTTP_CURL_OPT(DEBUGDATA, request);
347 HTTP_CURL_OPT(URL, request->url);
348
349 /* progress callback * /
350 if ((zoption = http_request_option(request, options, "onprogress", 0))) {
351 HTTP_CURL_OPT(NOPROGRESS, 0);
352 HTTP_CURL_OPT(PROGRESSFUNCTION, http_curl_progress_callback);
353 HTTP_CURL_OPT(PROGRESSDATA, http_request_callback_data(zoption));
354 }
355
356 /* proxy */
357 if ((zoption = http_request_option(request, options, "proxyhost", IS_STRING))) {
358 HTTP_CURL_OPT(PROXY, Z_STRVAL_P(zoption));
359 /* port */
360 if ((zoption = http_request_option(request, options, "proxyport", IS_LONG))) {
361 HTTP_CURL_OPT(PROXYPORT, Z_LVAL_P(zoption));
362 }
363 /* user:pass */
364 if ((zoption = http_request_option(request, options, "proxyauth", IS_STRING))) {
365 HTTP_CURL_OPT(PROXYUSERPWD, Z_STRVAL_P(zoption));
366 }
367 /* auth method */
368 if ((zoption = http_request_option(request, options, "proxyauthtype", IS_LONG))) {
369 HTTP_CURL_OPT(PROXYAUTH, Z_LVAL_P(zoption));
370 }
371 }
372
373 /* outgoing interface */
374 if ((zoption = http_request_option(request, options, "interface", IS_STRING))) {
375 HTTP_CURL_OPT(INTERFACE, Z_STRVAL_P(zoption));
376 }
377
378 /* another port */
379 if ((zoption = http_request_option(request, options, "port", IS_LONG))) {
380 HTTP_CURL_OPT(PORT, Z_LVAL_P(zoption));
381 }
382
383 /* auth */
384 if ((zoption = http_request_option(request, options, "httpauth", IS_STRING))) {
385 HTTP_CURL_OPT(USERPWD, Z_STRVAL_P(zoption));
386 }
387 if ((zoption = http_request_option(request, options, "httpauthtype", IS_LONG))) {
388 HTTP_CURL_OPT(HTTPAUTH, Z_LVAL_P(zoption));
389 }
390
391 /* compress, empty string enables all supported if libcurl was build with zlib support */
392 if ((zoption = http_request_option(request, options, "compress", IS_BOOL)) && Z_LVAL_P(zoption)) {
393 #ifdef HTTP_HAVE_CURL_ZLIB
394 HTTP_CURL_OPT(ENCODING, "gzip, deflate");
395 #else
396 HTTP_CURL_OPT(ENCODING, "gzip;q=1.0, deflate;q=0.5, *;q=0.1");
397 #endif
398 }
399
400 /* redirects, defaults to 0 */
401 if ((zoption = http_request_option(request, options, "redirect", IS_LONG))) {
402 HTTP_CURL_OPT(FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
403 HTTP_CURL_OPT(MAXREDIRS, Z_LVAL_P(zoption));
404 if ((zoption = http_request_option(request, options, "unrestrictedauth", IS_BOOL))) {
405 HTTP_CURL_OPT(UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
406 }
407 }
408
409 /* referer */
410 if ((zoption = http_request_option(request, options, "referer", IS_STRING))) {
411 HTTP_CURL_OPT(REFERER, Z_STRVAL_P(zoption));
412 }
413
414 /* useragent, default "PECL::HTTP/version (PHP/version)" */
415 if ((zoption = http_request_option(request, options, "useragent", IS_STRING))) {
416 HTTP_CURL_OPT(USERAGENT, Z_STRVAL_P(zoption));
417 }
418
419 /* additional headers, array('name' => 'value') */
420 if ((zoption = http_request_option(request, options, "headers", IS_ARRAY))) {
421 char *header_key;
422 ulong header_idx;
423 HashPosition pos;
424
425 if (request->_cache.headers) {
426 curl_slist_free_all(request->_cache.headers);
427 request->_cache.headers = NULL;
428 }
429
430 FOREACH_KEY(pos, zoption, header_key, header_idx) {
431 if (header_key) {
432 zval **header_val;
433 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void **) &header_val, &pos)) {
434 char header[1024] = {0};
435 zval *cpy, *val = convert_to_type_ex(IS_STRING, *header_val, &cpy);
436
437 snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_P(val));
438 request->_cache.headers = curl_slist_append(request->_cache.headers, header);
439
440 if (cpy) {
441 zval_ptr_dtor(&cpy);
442 }
443 }
444
445 /* reset */
446 header_key = NULL;
447 }
448 }
449 }
450
451 /* cookies, array('name' => 'value') */
452 if ((zoption = http_request_option(request, options, "cookies", IS_ARRAY))) {
453 char *cookie_key = NULL;
454 ulong cookie_idx = 0;
455 HashPosition pos;
456
457 phpstr_dtor(&request->_cache.cookies);
458
459 FOREACH_KEY(pos, zoption, cookie_key, cookie_idx) {
460 if (cookie_key) {
461 zval **cookie_val;
462 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void **) &cookie_val, &pos)) {
463 zval *cpy, *val = convert_to_type_ex(IS_STRING, *cookie_val, &cpy);
464
465 phpstr_appendf(&request->_cache.cookies, "%s=%s; ", cookie_key, Z_STRVAL_P(val));
466
467 if (cpy) {
468 zval_ptr_dtor(&cpy);
469 }
470 }
471
472 /* reset */
473 cookie_key = NULL;
474 }
475 }
476
477 if (request->_cache.cookies.used) {
478 phpstr_fix(&request->_cache.cookies);
479 HTTP_CURL_OPT(COOKIE, request->_cache.cookies.data);
480 }
481 }
482
483 /* session cookies */
484 if ((zoption = http_request_option(request, options, "cookiesession", IS_BOOL))) {
485 if (Z_LVAL_P(zoption)) {
486 /* accept cookies for this session */
487 HTTP_CURL_OPT(COOKIEFILE, "");
488 } else {
489 /* reset session cookies */
490 HTTP_CURL_OPT(COOKIESESSION, 1);
491 }
492 }
493
494 /* cookiestore, read initial cookies from that file and store cookies back into that file */
495 if ((zoption = http_request_option(request, options, "cookiestore", IS_STRING)) && Z_STRLEN_P(zoption)) {
496 HTTP_CURL_OPT(COOKIEFILE, Z_STRVAL_P(zoption));
497 HTTP_CURL_OPT(COOKIEJAR, Z_STRVAL_P(zoption));
498 }
499
500 /* resume */
501 if ((zoption = http_request_option(request, options, "resume", IS_LONG)) && (Z_LVAL_P(zoption) != 0)) {
502 range_req = 1;
503 HTTP_CURL_OPT(RESUME_FROM, Z_LVAL_P(zoption));
504 }
505
506 /* maxfilesize */
507 if ((zoption = http_request_option(request, options, "maxfilesize", IS_LONG))) {
508 HTTP_CURL_OPT(MAXFILESIZE, Z_LVAL_P(zoption));
509 }
510
511 /* lastmodified */
512 if ((zoption = http_request_option(request, options, "lastmodified", IS_LONG))) {
513 if (Z_LVAL_P(zoption)) {
514 if (Z_LVAL_P(zoption) > 0) {
515 HTTP_CURL_OPT(TIMEVALUE, Z_LVAL_P(zoption));
516 } else {
517 HTTP_CURL_OPT(TIMEVALUE, time(NULL) + Z_LVAL_P(zoption));
518 }
519 HTTP_CURL_OPT(TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE);
520 } else {
521 HTTP_CURL_OPT(TIMECONDITION, CURL_TIMECOND_NONE);
522 }
523 }
524
525 /* timeout, defaults to 0 */
526 if ((zoption = http_request_option(request, options, "timeout", IS_LONG))) {
527 HTTP_CURL_OPT(TIMEOUT, Z_LVAL_P(zoption));
528 }
529
530 /* connecttimeout, defaults to 3 */
531 if ((zoption = http_request_option(request, options, "connecttimeout", IS_LONG))) {
532 HTTP_CURL_OPT(CONNECTTIMEOUT, Z_LVAL_P(zoption));
533 }
534
535 /* ssl */
536 if ((zoption = http_request_option(request, options, "ssl", IS_ARRAY))) {
537 ulong idx;
538 char *key = NULL;
539 zval **param;
540 HashPosition pos;
541
542 FOREACH_KEYVAL(pos, zoption, key, idx, param) {
543 if (key) {
544 HTTP_CURL_OPT_SSL_STRING(CERT);
545 HTTP_CURL_OPT_SSL_STRING(CERTTYPE);
546 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD);
547
548 HTTP_CURL_OPT_SSL_STRING(KEY);
549 HTTP_CURL_OPT_SSL_STRING(KEYTYPE);
550 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD);
551
552 HTTP_CURL_OPT_SSL_STRING(ENGINE);
553 HTTP_CURL_OPT_SSL_LONG(VERSION);
554
555 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER);
556 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST);
557 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST);
558
559 HTTP_CURL_OPT_STRING(CAINFO);
560 HTTP_CURL_OPT_STRING(CAPATH);
561 HTTP_CURL_OPT_STRING(RANDOM_FILE);
562 HTTP_CURL_OPT_STRING(EGDSOCKET);
563
564 /* reset key */
565 key = NULL;
566 }
567 }
568 }
569
570 /* request method */
571 switch (request->meth)
572 {
573 case HTTP_GET:
574 curl_easy_setopt(request->ch, CURLOPT_HTTPGET, 1);
575 break;
576
577 case HTTP_HEAD:
578 curl_easy_setopt(request->ch, CURLOPT_NOBODY, 1);
579 break;
580
581 case HTTP_POST:
582 curl_easy_setopt(request->ch, CURLOPT_POST, 1);
583 break;
584
585 case HTTP_PUT:
586 curl_easy_setopt(request->ch, CURLOPT_UPLOAD, 1);
587 break;
588
589 default:
590 if (http_request_method_exists(0, request->meth, NULL)) {
591 curl_easy_setopt(request->ch, CURLOPT_CUSTOMREQUEST, http_request_method_name(request->meth));
592 } else {
593 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unsupported request method: %d (%s)", request->meth, request->url);
594 return FAILURE;
595 }
596 break;
597 }
598
599 /* attach request body */
600 if (request->body && (request->meth != HTTP_GET) && (request->meth != HTTP_HEAD)) {
601 switch (request->body->type)
602 {
603 case HTTP_REQUEST_BODY_CSTRING:
604 curl_easy_setopt(request->ch, CURLOPT_POSTFIELDS, request->body->data);
605 curl_easy_setopt(request->ch, CURLOPT_POSTFIELDSIZE, request->body->size);
606 break;
607
608 case HTTP_REQUEST_BODY_CURLPOST:
609 curl_easy_setopt(request->ch, CURLOPT_HTTPPOST, (struct curl_httppost *) request->body->data);
610 break;
611
612 case HTTP_REQUEST_BODY_UPLOADFILE:
613 curl_easy_setopt(request->ch, CURLOPT_IOCTLDATA, request);
614 curl_easy_setopt(request->ch, CURLOPT_READDATA, request);
615 curl_easy_setopt(request->ch, CURLOPT_INFILESIZE, request->body->size);
616 break;
617
618 default:
619 /* shouldn't ever happen */
620 http_error_ex(HE_ERROR, 0, "Unknown request body type: %d (%s)", request->body->type, request->url);
621 return FAILURE;
622 break;
623 }
624 }
625
626 return SUCCESS;
627 }
628 /* }}} */
629
630 PHP_HTTP_API void _http_request_exec(http_request *request)
631 {
632 CURLcode result;
633 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
634
635 if (CURLE_OK != (result = curl_easy_perform(request->ch))) {
636 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s (%s)", curl_easy_strerror(result), request->url);
637 }
638 }
639
640 /* {{{ void http_request_info(CURL *, HashTable *) */
641 PHP_HTTP_API void _http_request_info(http_request *request, HashTable *info)
642 {
643 zval array;
644 INIT_ZARR(array, &request->info);
645
646 HTTP_CURL_INFO(EFFECTIVE_URL);
647 HTTP_CURL_INFO(RESPONSE_CODE);
648 HTTP_CURL_INFO_EX(HTTP_CONNECTCODE, connect_code);
649 HTTP_CURL_INFO(FILETIME);
650 HTTP_CURL_INFO(TOTAL_TIME);
651 HTTP_CURL_INFO(NAMELOOKUP_TIME);
652 HTTP_CURL_INFO(CONNECT_TIME);
653 HTTP_CURL_INFO(PRETRANSFER_TIME);
654 HTTP_CURL_INFO(STARTTRANSFER_TIME);
655 HTTP_CURL_INFO(REDIRECT_TIME);
656 HTTP_CURL_INFO(REDIRECT_COUNT);
657 HTTP_CURL_INFO(SIZE_UPLOAD);
658 HTTP_CURL_INFO(SIZE_DOWNLOAD);
659 HTTP_CURL_INFO(SPEED_DOWNLOAD);
660 HTTP_CURL_INFO(SPEED_UPLOAD);
661 HTTP_CURL_INFO(HEADER_SIZE);
662 HTTP_CURL_INFO(REQUEST_SIZE);
663 HTTP_CURL_INFO(SSL_VERIFYRESULT);
664 HTTP_CURL_INFO(SSL_ENGINES);
665 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD);
666 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD);
667 HTTP_CURL_INFO(CONTENT_TYPE);
668 /*HTTP_CURL_INFO(PRIVATE);*/
669 HTTP_CURL_INFO(HTTPAUTH_AVAIL);
670 HTTP_CURL_INFO(PROXYAUTH_AVAIL);
671 /*HTTP_CURL_INFO(OS_ERRNO);*/
672 HTTP_CURL_INFO(NUM_CONNECTS);
673 #if LIBCURL_VERSION_NUM >= 0x070e01
674 HTTP_CURL_INFO_EX(COOKIELIST, cookies);
675 #endif
676
677 if (info) {
678 zend_hash_copy(info, &request->info, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
679 }
680 }
681 /* }}} */
682
683 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
684 static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *ctx)
685 {
686 http_request *request = (http_request *) ctx;
687 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
688
689 if (request->body == NULL || request->body->type != HTTP_REQUEST_BODY_UPLOADFILE) {
690 return 0;
691 }
692 return php_stream_read((php_stream *) request->body->data, data, len * n);
693 }
694 /* }}} */
695
696 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) * /
697 static int http_curl_progress_callback(void *data, double dltotal, double dlnow, double ultotal, double ulnow)
698 {
699 zval *params_pass[4], params_local[4], retval;
700 HTTP_REQUEST_CALLBACK_DATA(data, zval *, func);
701
702 params_pass[0] = &params_local[0];
703 params_pass[1] = &params_local[1];
704 params_pass[2] = &params_local[2];
705 params_pass[3] = &params_local[3];
706
707 INIT_PZVAL(params_pass[0]);
708 INIT_PZVAL(params_pass[1]);
709 INIT_PZVAL(params_pass[2]);
710 INIT_PZVAL(params_pass[3]);
711 ZVAL_DOUBLE(params_pass[0], dltotal);
712 ZVAL_DOUBLE(params_pass[1], dlnow);
713 ZVAL_DOUBLE(params_pass[2], ultotal);
714 ZVAL_DOUBLE(params_pass[3], ulnow);
715
716 return call_user_function(EG(function_table), NULL, func, &retval, 4, params_pass TSRMLS_CC);
717 }
718 /* }}} */
719
720 /* {{{ static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *) */
721 static curlioerr http_curl_ioctl_callback(CURL *ch, curliocmd cmd, void *ctx)
722 {
723 http_request *request = (http_request *) ctx;
724 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
725
726 if (cmd != CURLIOCMD_RESTARTREAD) {
727 return CURLIOE_UNKNOWNCMD;
728 }
729 if ( request->body == NULL ||
730 request->body->type != HTTP_REQUEST_BODY_UPLOADFILE ||
731 SUCCESS != php_stream_rewind((php_stream *) request->body->data)) {
732 return CURLIOE_FAILRESTART;
733 }
734 return CURLIOE_OK;
735 }
736 /* }}} */
737
738 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
739 static int http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size_t length, void *ctx)
740 {
741 http_request *request = (http_request *) ctx;
742
743 switch (type)
744 {
745 case CURLINFO_DATA_IN:
746 if (request->conv.last_type == CURLINFO_HEADER_IN) {
747 phpstr_appends(&request->conv.response, HTTP_CRLF);
748 }
749 case CURLINFO_HEADER_IN:
750 phpstr_append(&request->conv.response, data, length);
751 break;
752 case CURLINFO_DATA_OUT:
753 if (request->conv.last_type == CURLINFO_HEADER_OUT) {
754 phpstr_appends(&request->conv.request, HTTP_CRLF);
755 }
756 case CURLINFO_HEADER_OUT:
757 phpstr_append(&request->conv.request, data, length);
758 break;
759 default:
760 #if 0
761 fprintf(stderr, "## ", type);
762 if (!type) {
763 fprintf(stderr, "%s", data);
764 } else {
765 ulong i;
766 for (i = 1; i <= length; ++i) {
767 fprintf(stderr, "%02X ", data[i-1] & 0xFF);
768 if (!(i % 20)) {
769 fprintf(stderr, "\n## ");
770 }
771 }
772 fprintf(stderr, "\n");
773 }
774 if (data[length-1] != 0xa) {
775 fprintf(stderr, "\n");
776 }
777 #endif
778 break;
779 }
780
781 if (type) {
782 request->conv.last_type = type;
783 }
784 return 0;
785 }
786 /* }}} */
787
788 /* {{{ static inline zval *http_request_option(http_request *, HashTable *, char *, size_t, int) */
789 static inline zval *_http_request_option_ex(http_request *r, HashTable *options, char *key, size_t keylen, int type TSRMLS_DC)
790 {
791 zval **zoption;
792 ulong h = zend_get_hash_value(key, keylen);
793
794 if (!options || (SUCCESS != zend_hash_quick_find(options, key, keylen, h, (void **) &zoption))) {
795 return NULL;
796 }
797
798 if (Z_TYPE_PP(zoption) != type) {
799 switch (type)
800 {
801 case IS_BOOL: convert_to_boolean_ex(zoption); break;
802 case IS_LONG: convert_to_long_ex(zoption); break;
803 case IS_DOUBLE: convert_to_double_ex(zoption); break;
804 case IS_STRING: convert_to_string_ex(zoption); break;
805 case IS_ARRAY: convert_to_array_ex(zoption); break;
806 case IS_OBJECT: convert_to_object_ex(zoption); break;
807 default:
808 break;
809 }
810 }
811
812 ZVAL_ADDREF(*zoption);
813 _zend_hash_quick_add_or_update(&r->_cache.options, key, keylen, h, zoption, sizeof(zval *), NULL,
814 zend_hash_quick_exists(&r->_cache.options, key, keylen, h)?HASH_UPDATE:HASH_ADD ZEND_FILE_LINE_CC);
815
816 return *zoption;
817 }
818 /* }}} */
819
820 #ifdef HTTP_NEED_OPENSSL_TSL
821 /* {{{ */
822 static MUTEX_T *http_openssl_tsl = NULL;
823
824 static void http_ssl_lock(int mode, int n, const char * file, int line)
825 {
826 if (mode & CRYPTO_LOCK) {
827 tsrm_mutex_lock(http_openssl_tsl[n]);
828 } else {
829 tsrm_mutex_unlock(http_openssl_tsl[n]);
830 }
831 }
832
833 static ulong http_ssl_id(void)
834 {
835 return (ulong) tsrm_thread_id();
836 }
837
838 static inline void http_ssl_init(void)
839 {
840 int i, c = CRYPTO_num_locks();
841
842 http_openssl_tsl = malloc(c * sizeof(MUTEX_T));
843
844 for (i = 0; i < c; ++i) {
845 http_openssl_tsl[i] = tsrm_mutex_alloc();
846 }
847
848 CRYPTO_set_id_callback(http_ssl_id);
849 CRYPTO_set_locking_callback(http_ssl_lock);
850 }
851
852 static inline void http_ssl_cleanup(void)
853 {
854 if (http_openssl_tsl) {
855 int i, c = CRYPTO_num_locks();
856
857 CRYPTO_set_id_callback(NULL);
858 CRYPTO_set_locking_callback(NULL);
859
860 for (i = 0; i < c; ++i) {
861 tsrm_mutex_free(http_openssl_tsl[i]);
862 }
863
864 free(http_openssl_tsl);
865 http_openssl_tsl = NULL;
866 }
867 }
868 #endif /* HTTP_NEED_OPENSSL_TSL */
869 /* }}} */
870
871 #ifdef HTTP_NEED_GNUTLS_TSL
872 /* {{{ */
873 static int http_ssl_mutex_create(void **m)
874 {
875 if (*((MUTEX_T *) m) = tsrm_mutex_alloc()) {
876 return SUCCESS;
877 } else {
878 return FAILURE;
879 }
880 }
881
882 static int http_ssl_mutex_destroy(void **m)
883 {
884 tsrm_mutex_free(*((MUTEX_T *) m));
885 return SUCCESS;
886 }
887
888 static int http_ssl_mutex_lock(void **m)
889 {
890 return tsrm_mutex_lock(*((MUTEX_T *) m));
891 }
892
893 static int http_ssl_mutex_unlock(void **m)
894 {
895 return tsrm_mutex_unlock(*((MUTEX_T *) m));
896 }
897
898 static struct gcry_thread_cbs http_gnutls_tsl = {
899 GCRY_THREAD_OPTIONS_USER,
900 NULL,
901 http_ssl_mutex_create,
902 http_ssl_mutex_destroy,
903 http_ssl_mutex_lock,
904 http_ssl_mutex_unlock
905 };
906
907 static inline void http_ssl_init(void)
908 {
909 gcry_control(GCRYCTL_SET_THREAD_CBS, &http_gnutls_tsl);
910 }
911
912 static inline void http_ssl_cleanup(void)
913 {
914 return;
915 }
916 #endif /* HTTP_NEED_GNUTLS_TSL */
917 /* }}} */
918
919 #endif /* HTTP_HAVE_CURL */
920
921 /*
922 * Local variables:
923 * tab-width: 4
924 * c-basic-offset: 4
925 * End:
926 * vim600: noet sw=4 ts=4 fdm=marker
927 * vim<600: noet sw=4 ts=4
928 */
929