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