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