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