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