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