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