- fix HttpRequest::getResponseCookies()
[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 #define HTTP_WANT_SAPI
16 #define HTTP_WANT_CURL
17 #include "php_http.h"
18
19 #ifdef HTTP_HAVE_CURL
20
21 #include "php_http_api.h"
22 #include "php_http_request_api.h"
23 #include "php_http_url_api.h"
24
25 #ifdef ZEND_ENGINE_2
26 # include "php_http_request_object.h"
27 #endif
28
29 /* {{{ cruft for thread safe SSL crypto locks */
30 #if defined(ZTS) && defined(HTTP_HAVE_SSL)
31 # ifdef PHP_WIN32
32 # define HTTP_NEED_OPENSSL_TSL
33 # include <openssl/crypto.h>
34 # else /* !PHP_WIN32 */
35 # if defined(HTTP_HAVE_OPENSSL)
36 # if defined(HAVE_OPENSSL_CRYPTO_H)
37 # define HTTP_NEED_OPENSSL_TSL
38 # include <openssl/crypto.h>
39 # else
40 # warning \
41 "libcurl was compiled with OpenSSL support, but configure could not find " \
42 "openssl/crypto.h; thus no SSL crypto locking callbacks will be set, which may " \
43 "cause random crashes on SSL requests"
44 # endif
45 # elif defined(HTTP_HAVE_GNUTLS)
46 # if defined(HAVE_GCRYPT_H)
47 # define HTTP_NEED_GNUTLS_TSL
48 # include <gcrypt.h>
49 # else
50 # warning \
51 "libcurl was compiled with GnuTLS support, but configure could not find " \
52 "gcrypt.h; thus no SSL crypto locking callbacks will be set, which may " \
53 "cause random crashes on SSL requests"
54 # endif
55 # else
56 # warning \
57 "libcurl was compiled with SSL support, but configure could not determine which" \
58 "library was used; thus no SSL crypto locking callbacks will be set, which may " \
59 "cause random crashes on SSL requests"
60 # endif /* HTTP_HAVE_OPENSSL || HTTP_HAVE_GNUTLS */
61 # endif /* PHP_WIN32 */
62 #endif /* ZTS && HTTP_HAVE_SSL */
63
64 #ifdef HTTP_NEED_OPENSSL_TSL
65 static MUTEX_T *http_openssl_tsl = NULL;
66
67 static void http_openssl_thread_lock(int mode, int n, const char * file, int line)
68 {
69 if (mode & CRYPTO_LOCK) {
70 tsrm_mutex_lock(http_openssl_tsl[n]);
71 } else {
72 tsrm_mutex_unlock(http_openssl_tsl[n]);
73 }
74 }
75
76 static ulong http_openssl_thread_id(void)
77 {
78 return (ulong) tsrm_thread_id();
79 }
80 #endif
81 #ifdef HTTP_NEED_GNUTLS_TSL
82 static int http_gnutls_mutex_create(void **m)
83 {
84 if (*((MUTEX_T *) m) = tsrm_mutex_alloc()) {
85 return SUCCESS;
86 } else {
87 return FAILURE;
88 }
89 }
90
91 static int http_gnutls_mutex_destroy(void **m)
92 {
93 tsrm_mutex_free(*((MUTEX_T *) m));
94 return SUCCESS;
95 }
96
97 static int http_gnutls_mutex_lock(void **m)
98 {
99 return tsrm_mutex_lock(*((MUTEX_T *) m));
100 }
101
102 static int http_gnutls_mutex_unlock(void **m)
103 {
104 return tsrm_mutex_unlock(*((MUTEX_T *) m));
105 }
106
107 static struct gcry_thread_cbs http_gnutls_tsl = {
108 GCRY_THREAD_OPTION_USER,
109 NULL,
110 http_gnutls_mutex_create,
111 http_gnutls_mutex_destroy,
112 http_gnutls_mutex_lock,
113 http_gnutls_mutex_unlock
114 };
115 #endif
116 /* }}} */
117
118 /* {{{ MINIT */
119 PHP_MINIT_FUNCTION(http_request)
120 {
121 #ifdef HTTP_NEED_OPENSSL_TSL
122 int i, c = CRYPTO_num_locks();
123
124 http_openssl_tsl = malloc(c * sizeof(MUTEX_T));
125
126 for (i = 0; i < c; ++i) {
127 http_openssl_tsl[i] = tsrm_mutex_alloc();
128 }
129
130 CRYPTO_set_id_callback(http_openssl_thread_id);
131 CRYPTO_set_locking_callback(http_openssl_thread_lock);
132 #endif
133 #ifdef HTTP_NED_GNUTLS_TSL
134 gcry_control(GCRYCTL_SET_THREAD_CBS, &http_gnutls_tsl);
135 #endif
136
137 if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
138 return FAILURE;
139 }
140
141 HTTP_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC);
142 HTTP_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST);
143 HTTP_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM);
144 HTTP_LONG_CONSTANT("HTTP_AUTH_ANY", CURLAUTH_ANY);
145
146 HTTP_LONG_CONSTANT("HTTP_VERSION_NONE", CURL_HTTP_VERSION_NONE);
147 HTTP_LONG_CONSTANT("HTTP_VERSION_1_0", CURL_HTTP_VERSION_1_0);
148 HTTP_LONG_CONSTANT("HTTP_VERSION_1_1", CURL_HTTP_VERSION_1_1);
149
150 return SUCCESS;
151 }
152 /* }}} */
153
154 /* {{{ MSHUTDOWN */
155 PHP_MSHUTDOWN_FUNCTION(http_request)
156 {
157 #ifdef HTTP_NEED_OPENSSL_TSL
158 CRYPTO_set_id_callback(http_openssl_thread_id);
159 CRYPTO_set_locking_callback(http_openssl_thread_lock);
160 #endif
161 curl_global_cleanup();
162 #ifdef HTTP_NEED_OPENSSL_TSL
163 if (http_openssl_tsl) {
164 int i, c = CRYPTO_num_locks();
165
166 CRYPTO_set_id_callback(NULL);
167 CRYPTO_set_locking_callback(NULL);
168
169 for (i = 0; i < c; ++i) {
170 tsrm_mutex_free(http_openssl_tsl[i]);
171 }
172
173 free(http_openssl_tsl);
174 http_openssl_tsl = NULL;
175 }
176 #endif
177 return SUCCESS;
178 }
179 /* }}} */
180
181 /* {{{ MACROS */
182 #ifndef HAVE_CURL_EASY_STRERROR
183 # define curl_easy_strerror(dummy) "unknown error"
184 #endif
185
186 #define HTTP_CURL_INFO(I) \
187 { \
188 char *N = #I; \
189 HTTP_CURL_INFO_EX(I, N+lenof("CURLINFO_")); \
190 }
191 #define HTTP_CURL_INFO_EX(I, X) \
192 switch (I & ~CURLINFO_MASK) \
193 { \
194 case CURLINFO_STRING: \
195 { \
196 char *c; \
197 if (CURLE_OK == curl_easy_getinfo(request->ch, I, &c)) { \
198 char *key = estrndup(X, strlen(X)); \
199 add_assoc_string(&array, pretty_key(key, strlen(X), 0, 0), c ? c : "", 1); \
200 efree(key); \
201 } \
202 } \
203 break; \
204 \
205 case CURLINFO_DOUBLE: \
206 { \
207 double d; \
208 if (CURLE_OK == curl_easy_getinfo(request->ch, I, &d)) { \
209 char *key = estrndup(X, strlen(X)); \
210 add_assoc_double(&array, pretty_key(key, strlen(X), 0, 0), d); \
211 efree(key); \
212 } \
213 } \
214 break; \
215 \
216 case CURLINFO_LONG: \
217 { \
218 long l; \
219 if (CURLE_OK == curl_easy_getinfo(request->ch, I, &l)) { \
220 char *key = estrndup(X, strlen(X)); \
221 add_assoc_long(&array, pretty_key(key, strlen(X), 0, 0), l); \
222 efree(key); \
223 } \
224 } \
225 break; \
226 \
227 case CURLINFO_SLIST: \
228 { \
229 struct curl_slist *l, *p; \
230 if (CURLE_OK == curl_easy_getinfo(request->ch, I, &l)) { \
231 zval *subarray; \
232 char *key = estrndup(X, strlen(X)); \
233 MAKE_STD_ZVAL(subarray); \
234 array_init(subarray); \
235 for (p = l; p; p = p->next) { \
236 add_next_index_string(subarray, p->data, 1); \
237 } \
238 add_assoc_zval(&array, pretty_key(key, strlen(X), 0, 0), subarray); \
239 curl_slist_free_all(l); \
240 efree(key); \
241 } \
242 } \
243 }
244
245 #define HTTP_CURL_OPT(OPTION, p) HTTP_CURL_OPT_EX(request->ch, OPTION, (p))
246 #define HTTP_CURL_OPT_EX(ch, OPTION, p) curl_easy_setopt((ch), OPTION, (p))
247
248 #define HTTP_CURL_OPT_STRING(OPTION, ldiff, obdc) \
249 { \
250 char *K = #OPTION; \
251 HTTP_CURL_OPT_STRING_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION, obdc); \
252 }
253 #define HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \
254 if (!strcasecmp(key, keyname)) { \
255 zval *copy = http_request_option_cache(request, keyname, zval_copy(IS_STRING, *param)); \
256 if (obdc) { \
257 HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(copy), return FAILURE); \
258 } \
259 HTTP_CURL_OPT(optname, Z_STRVAL_P(copy)); \
260 key = NULL; \
261 continue; \
262 }
263 #define HTTP_CURL_OPT_LONG(OPTION, ldiff) \
264 { \
265 char *K = #OPTION; \
266 HTTP_CURL_OPT_LONG_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION); \
267 }
268 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
269 if (!strcasecmp(key, keyname)) { \
270 zval *copy = http_request_option_cache(request, keyname, zval_copy(IS_LONG, *param)); \
271 HTTP_CURL_OPT(optname, Z_LVAL_P(copy)); \
272 key = NULL; \
273 continue; \
274 }
275 /* }}} */
276
277 /* {{{ forward declarations */
278 #define http_request_option(r, o, k, t) _http_request_option_ex((r), (o), (k), sizeof(k), (t) TSRMLS_CC)
279 #define http_request_option_ex(r, o, k, l, t) _http_request_option_ex((r), (o), (k), (l), (t) TSRMLS_CC)
280 static inline zval *_http_request_option_ex(http_request *request, HashTable *options, char *key, size_t keylen, int type TSRMLS_DC);
281 #define http_request_option_cache(r, k, z) _http_request_option_cache_ex((r), (k), sizeof(k), 0, (z) TSRMLS_CC)
282 #define http_request_option_cache_ex(r, k, kl, h, z) _http_request_option_cache_ex((r), (k), (kl), (h), (z) TSRMLS_CC)
283 static inline zval *_http_request_option_cache_ex(http_request *r, char *key, size_t keylen, ulong h, zval *opt TSRMLS_DC);
284
285 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
286 static int http_curl_progress_callback(void *, double, double, double, double);
287 static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *);
288 static int http_curl_dummy_callback(char *data, size_t n, size_t l, void *s) { return n*l; }
289 static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *);
290 /* }}} */
291
292 /* {{{ CURL *http_curl_init(http_request *) */
293 PHP_HTTP_API CURL * _http_curl_init_ex(CURL *ch, http_request *request)
294 {
295 if (ch || (ch = curl_easy_init())) {
296 #if defined(ZTS)
297 HTTP_CURL_OPT_EX(ch, CURLOPT_NOSIGNAL, 1);
298 #endif
299 HTTP_CURL_OPT_EX(ch, CURLOPT_HEADER, 0);
300 HTTP_CURL_OPT_EX(ch, CURLOPT_FILETIME, 1);
301 HTTP_CURL_OPT_EX(ch, CURLOPT_AUTOREFERER, 1);
302 HTTP_CURL_OPT_EX(ch, CURLOPT_VERBOSE, 1);
303 HTTP_CURL_OPT_EX(ch, CURLOPT_HEADERFUNCTION, NULL);
304 HTTP_CURL_OPT_EX(ch, CURLOPT_DEBUGFUNCTION, http_curl_raw_callback);
305 HTTP_CURL_OPT_EX(ch, CURLOPT_READFUNCTION, http_curl_read_callback);
306 HTTP_CURL_OPT_EX(ch, CURLOPT_IOCTLFUNCTION, http_curl_ioctl_callback);
307 HTTP_CURL_OPT_EX(ch, CURLOPT_WRITEFUNCTION, http_curl_dummy_callback);
308
309 /* set context */
310 if (request) {
311 HTTP_CURL_OPT_EX(ch, CURLOPT_PRIVATE, request);
312 HTTP_CURL_OPT_EX(ch, CURLOPT_DEBUGDATA, request);
313 HTTP_CURL_OPT_EX(ch, CURLOPT_ERRORBUFFER, request->_error);
314
315 /* attach curl handle */
316 request->ch = ch;
317 /* set defaults (also in http_request_reset()) */
318 http_request_defaults(request);
319 }
320 }
321
322 return ch;
323 }
324 /* }}} */
325
326 /* {{{ void http_curl_free(CURL **) */
327 PHP_HTTP_API void _http_curl_free(CURL **ch)
328 {
329 if (*ch) {
330 /* avoid nasty segfaults with already cleaned up callbacks */
331 HTTP_CURL_OPT_EX(*ch, CURLOPT_NOPROGRESS, 1);
332 HTTP_CURL_OPT_EX(*ch, CURLOPT_PROGRESSFUNCTION, NULL);
333 HTTP_CURL_OPT_EX(*ch, CURLOPT_VERBOSE, 0);
334 HTTP_CURL_OPT_EX(*ch, CURLOPT_DEBUGFUNCTION, NULL);
335 curl_easy_cleanup(*ch);
336 *ch = NULL;
337 }
338 }
339 /* }}} */
340
341 /* {{{ http_request *http_request_init(http_request *) */
342 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)
343 {
344 http_request *r;
345
346 if (request) {
347 r = request;
348 } else {
349 r = emalloc_rel(sizeof(http_request));
350 }
351 memset(r, 0, sizeof(http_request));
352
353 r->ch = ch;
354 r->url = (url) ? http_absolute_url(url) : NULL;
355 r->meth = (meth > 0) ? meth : HTTP_GET;
356
357 phpstr_init(&r->conv.request);
358 phpstr_init_ex(&r->conv.response, HTTP_CURLBUF_SIZE, 0);
359 phpstr_init(&r->_cache.cookies);
360 zend_hash_init(&r->_cache.options, 0, NULL, ZVAL_PTR_DTOR, 0);
361
362 TSRMLS_SET_CTX(r->tsrm_ls);
363
364 return r;
365 }
366 /* }}} */
367
368 /* {{{ void http_request_dtor(http_request *) */
369 PHP_HTTP_API void _http_request_dtor(http_request *request)
370 {
371 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
372
373 http_curl_free(&request->ch);
374 http_request_reset(request);
375
376 phpstr_dtor(&request->_cache.cookies);
377 zend_hash_destroy(&request->_cache.options);
378 if (request->_cache.headers) {
379 curl_slist_free_all(request->_cache.headers);
380 request->_cache.headers = NULL;
381 }
382 if (request->_progress_callback) {
383 zval_ptr_dtor(&request->_progress_callback);
384 request->_progress_callback = NULL;
385 }
386 }
387 /* }}} */
388
389 /* {{{ void http_request_free(http_request **) */
390 PHP_HTTP_API void _http_request_free(http_request **request)
391 {
392 if (*request) {
393 TSRMLS_FETCH_FROM_CTX((*request)->tsrm_ls);
394 http_request_body_free(&(*request)->body);
395 http_request_dtor(*request);
396 efree(*request);
397 *request = NULL;
398 }
399 }
400 /* }}} */
401
402 /* {{{ void http_request_reset(http_request *) */
403 PHP_HTTP_API void _http_request_reset(http_request *request)
404 {
405 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
406 STR_SET(request->url, NULL);
407 request->conv.last_type = 0;
408 phpstr_dtor(&request->conv.request);
409 phpstr_dtor(&request->conv.response);
410 http_request_body_dtor(request->body);
411
412 if (request->ch) {
413 http_request_defaults(request);
414 }
415 }
416 /* }}} */
417
418 /* {{{ void http_request_defaults(http_request *) */
419 PHP_HTTP_API void _http_request_defaults(http_request *request)
420 {
421 if (request->ch) {
422 HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION, NULL);
423 HTTP_CURL_OPT(CURLOPT_URL, NULL);
424 HTTP_CURL_OPT(CURLOPT_NOPROGRESS, 1);
425 HTTP_CURL_OPT(CURLOPT_PROXY, NULL);
426 HTTP_CURL_OPT(CURLOPT_PROXYPORT, 0);
427 HTTP_CURL_OPT(CURLOPT_PROXYUSERPWD, NULL);
428 HTTP_CURL_OPT(CURLOPT_PROXYAUTH, 0);
429 HTTP_CURL_OPT(CURLOPT_INTERFACE, NULL);
430 HTTP_CURL_OPT(CURLOPT_PORT, 0);
431 HTTP_CURL_OPT(CURLOPT_USERPWD, NULL);
432 HTTP_CURL_OPT(CURLOPT_HTTPAUTH, 0);
433 HTTP_CURL_OPT(CURLOPT_ENCODING, NULL);
434 HTTP_CURL_OPT(CURLOPT_FOLLOWLOCATION, 0);
435 HTTP_CURL_OPT(CURLOPT_UNRESTRICTED_AUTH, 0);
436 HTTP_CURL_OPT(CURLOPT_REFERER, NULL);
437 HTTP_CURL_OPT(CURLOPT_USERAGENT, "PECL::HTTP/" PHP_EXT_HTTP_VERSION " (PHP/" PHP_VERSION ")");
438 HTTP_CURL_OPT(CURLOPT_HTTPHEADER, NULL);
439 HTTP_CURL_OPT(CURLOPT_COOKIE, NULL);
440 #if LIBCURL_VERSION_NUM >= 0x070e01
441 HTTP_CURL_OPT(CURLOPT_COOKIELIST, NULL);
442 #endif
443 HTTP_CURL_OPT(CURLOPT_COOKIEFILE, NULL);
444 HTTP_CURL_OPT(CURLOPT_COOKIEJAR, NULL);
445 HTTP_CURL_OPT(CURLOPT_RESUME_FROM, 0);
446 HTTP_CURL_OPT(CURLOPT_MAXFILESIZE, 0);
447 HTTP_CURL_OPT(CURLOPT_TIMECONDITION, 0);
448 HTTP_CURL_OPT(CURLOPT_TIMEVALUE, 0);
449 HTTP_CURL_OPT(CURLOPT_TIMEOUT, 0);
450 HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT, 3);
451 HTTP_CURL_OPT(CURLOPT_SSLCERT, NULL);
452 HTTP_CURL_OPT(CURLOPT_SSLCERTTYPE, NULL);
453 HTTP_CURL_OPT(CURLOPT_SSLCERTPASSWD, NULL);
454 HTTP_CURL_OPT(CURLOPT_SSLKEY, NULL);
455 HTTP_CURL_OPT(CURLOPT_SSLKEYTYPE, NULL);
456 HTTP_CURL_OPT(CURLOPT_SSLKEYPASSWD, NULL);
457 HTTP_CURL_OPT(CURLOPT_SSLENGINE, NULL);
458 HTTP_CURL_OPT(CURLOPT_SSLVERSION, 0);
459 HTTP_CURL_OPT(CURLOPT_SSL_VERIFYPEER, 0);
460 HTTP_CURL_OPT(CURLOPT_SSL_VERIFYHOST, 0);
461 HTTP_CURL_OPT(CURLOPT_SSL_CIPHER_LIST, NULL);
462 HTTP_CURL_OPT(CURLOPT_CAINFO, NULL);
463 HTTP_CURL_OPT(CURLOPT_CAPATH, NULL);
464 HTTP_CURL_OPT(CURLOPT_RANDOM_FILE, NULL);
465 HTTP_CURL_OPT(CURLOPT_EGDSOCKET, NULL);
466 HTTP_CURL_OPT(CURLOPT_POSTFIELDS, NULL);
467 HTTP_CURL_OPT(CURLOPT_POSTFIELDSIZE, 0);
468 HTTP_CURL_OPT(CURLOPT_HTTPPOST, NULL);
469 HTTP_CURL_OPT(CURLOPT_IOCTLDATA, NULL);
470 HTTP_CURL_OPT(CURLOPT_READDATA, NULL);
471 HTTP_CURL_OPT(CURLOPT_INFILESIZE, 0);
472 HTTP_CURL_OPT(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);
473 HTTP_CURL_OPT(CURLOPT_CUSTOMREQUEST, NULL);
474 HTTP_CURL_OPT(CURLOPT_NOBODY, 0);
475 HTTP_CURL_OPT(CURLOPT_POST, 0);
476 HTTP_CURL_OPT(CURLOPT_UPLOAD, 0);
477 HTTP_CURL_OPT(CURLOPT_HTTPGET, 1);
478 }
479 }
480 /* }}} */
481
482 PHP_HTTP_API void _http_request_set_progress_callback(http_request *request, zval *cb)
483 {
484 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
485
486 if (request->_progress_callback) {
487 zval_ptr_dtor(&request->_progress_callback);
488 }
489 if ((request->_progress_callback = cb)) {
490 ZVAL_ADDREF(cb);
491 HTTP_CURL_OPT(CURLOPT_NOPROGRESS, 0);
492 HTTP_CURL_OPT(CURLOPT_PROGRESSDATA, request);
493 HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION, http_curl_progress_callback);
494 } else {
495 HTTP_CURL_OPT(CURLOPT_NOPROGRESS, 1);
496 HTTP_CURL_OPT(CURLOPT_PROGRESSDATA, NULL);
497 HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION, NULL);
498 }
499 }
500
501 /* {{{ STATUS http_request_prepare(http_request *, HashTable *) */
502 PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *options)
503 {
504 zval *zoption;
505 zend_bool range_req = 0;
506
507 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
508
509 HTTP_CHECK_CURL_INIT(request->ch, http_curl_init(request), return FAILURE);
510
511 /* set options */
512 HTTP_CURL_OPT(CURLOPT_URL, request->url);
513
514 /* progress callback */
515 if ((zoption = http_request_option(request, options, "onprogress", -1))) {
516 http_request_set_progress_callback(request, zoption);
517 }
518
519 /* proxy */
520 if ((zoption = http_request_option(request, options, "proxyhost", IS_STRING))) {
521 if (Z_STRLEN_P(zoption)) {
522 HTTP_CURL_OPT(CURLOPT_PROXY, Z_STRVAL_P(zoption));
523 }
524
525 /* port */
526 if ((zoption = http_request_option(request, options, "proxyport", IS_LONG))) {
527 HTTP_CURL_OPT(CURLOPT_PROXYPORT, Z_LVAL_P(zoption));
528 }
529 /* user:pass */
530 if ((zoption = http_request_option(request, options, "proxyauth", IS_STRING)) && Z_STRLEN_P(zoption)) {
531 HTTP_CURL_OPT(CURLOPT_PROXYUSERPWD, Z_STRVAL_P(zoption));
532 }
533 /* auth method */
534 if ((zoption = http_request_option(request, options, "proxyauthtype", IS_LONG))) {
535 HTTP_CURL_OPT(CURLOPT_PROXYAUTH, Z_LVAL_P(zoption));
536 }
537 }
538
539 /* outgoing interface */
540 if ((zoption = http_request_option(request, options, "interface", IS_STRING))) {
541 HTTP_CURL_OPT(CURLOPT_INTERFACE, Z_STRVAL_P(zoption));
542 }
543
544 /* another port */
545 if ((zoption = http_request_option(request, options, "port", IS_LONG))) {
546 HTTP_CURL_OPT(CURLOPT_PORT, Z_LVAL_P(zoption));
547 }
548
549 /* auth */
550 if ((zoption = http_request_option(request, options, "httpauth", IS_STRING)) && Z_STRLEN_P(zoption)) {
551 HTTP_CURL_OPT(CURLOPT_USERPWD, Z_STRVAL_P(zoption));
552 }
553 if ((zoption = http_request_option(request, options, "httpauthtype", IS_LONG))) {
554 HTTP_CURL_OPT(CURLOPT_HTTPAUTH, Z_LVAL_P(zoption));
555 }
556
557 /* redirects, defaults to 0 */
558 if ((zoption = http_request_option(request, options, "redirect", IS_LONG))) {
559 HTTP_CURL_OPT(CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
560 HTTP_CURL_OPT(CURLOPT_MAXREDIRS, Z_LVAL_P(zoption));
561 if ((zoption = http_request_option(request, options, "unrestrictedauth", IS_BOOL))) {
562 HTTP_CURL_OPT(CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
563 }
564 }
565
566 /* referer */
567 if ((zoption = http_request_option(request, options, "referer", IS_STRING)) && Z_STRLEN_P(zoption)) {
568 HTTP_CURL_OPT(CURLOPT_REFERER, Z_STRVAL_P(zoption));
569 }
570
571 /* useragent, default "PECL::HTTP/version (PHP/version)" */
572 if ((zoption = http_request_option(request, options, "useragent", IS_STRING))) {
573 /* allow to send no user agent, not even default one */
574 if (Z_STRLEN_P(zoption)) {
575 HTTP_CURL_OPT(CURLOPT_USERAGENT, Z_STRVAL_P(zoption));
576 } else {
577 HTTP_CURL_OPT(CURLOPT_USERAGENT, NULL);
578 }
579 }
580
581 /* resume */
582 if ((zoption = http_request_option(request, options, "resume", IS_LONG)) && (Z_LVAL_P(zoption) != 0)) {
583 range_req = 1;
584 HTTP_CURL_OPT(CURLOPT_RESUME_FROM, Z_LVAL_P(zoption));
585 }
586
587 /* additional headers, array('name' => 'value') */
588 if (request->_cache.headers) {
589 curl_slist_free_all(request->_cache.headers);
590 request->_cache.headers = NULL;
591 }
592 if ((zoption = http_request_option(request, options, "headers", IS_ARRAY))) {
593 char *header_key;
594 ulong header_idx;
595 HashPosition pos;
596
597 FOREACH_KEY(pos, zoption, header_key, header_idx) {
598 if (header_key) {
599 zval **header_val;
600 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void **) &header_val, &pos)) {
601 char header[1024] = {0};
602
603 ZVAL_ADDREF(*header_val);
604 convert_to_string_ex(header_val);
605 if (!strcasecmp(header_key, "range")) {
606 range_req = 1;
607 }
608 snprintf(header, lenof(header), "%s: %s", header_key, Z_STRVAL_PP(header_val));
609 request->_cache.headers = curl_slist_append(request->_cache.headers, header);
610 zval_ptr_dtor(header_val);
611 }
612
613 /* reset */
614 header_key = NULL;
615 }
616 }
617 }
618 /* etag */
619 if ((zoption = http_request_option(request, options, "etag", IS_STRING)) && Z_STRLEN_P(zoption)) {
620 char match_header[1024] = {0}, *quoted_etag = NULL;
621
622 if ((Z_STRVAL_P(zoption)[0] != '"') || (Z_STRVAL_P(zoption)[Z_STRLEN_P(zoption)-1] != '"')) {
623 spprintf(&quoted_etag, 0, "\"%s\"", Z_STRVAL_P(zoption));
624 }
625 snprintf(match_header, lenof(match_header), "%s: %s", range_req?"If-Match":"If-None-Match", quoted_etag?quoted_etag:Z_STRVAL_P(zoption));
626 request->_cache.headers = curl_slist_append(request->_cache.headers, match_header);
627 STR_FREE(quoted_etag);
628 }
629 /* compression */
630 if ((zoption = http_request_option(request, options, "compress", IS_BOOL)) && Z_LVAL_P(zoption)) {
631 request->_cache.headers = curl_slist_append(request->_cache.headers, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
632 }
633 HTTP_CURL_OPT(CURLOPT_HTTPHEADER, request->_cache.headers);
634
635 /* lastmodified */
636 if ((zoption = http_request_option(request, options, "lastmodified", IS_LONG))) {
637 if (Z_LVAL_P(zoption)) {
638 if (Z_LVAL_P(zoption) > 0) {
639 HTTP_CURL_OPT(CURLOPT_TIMEVALUE, Z_LVAL_P(zoption));
640 } else {
641 HTTP_CURL_OPT(CURLOPT_TIMEVALUE, HTTP_GET_REQUEST_TIME() + Z_LVAL_P(zoption));
642 }
643 HTTP_CURL_OPT(CURLOPT_TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE);
644 } else {
645 HTTP_CURL_OPT(CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
646 }
647 }
648
649 /* cookies, array('name' => 'value') */
650 if ((zoption = http_request_option(request, options, "cookies", IS_ARRAY))) {
651 phpstr_dtor(&request->_cache.cookies);
652 if (zend_hash_num_elements(Z_ARRVAL_P(zoption))) {
653 zval *urlenc_cookies = NULL;
654 /* check whether cookies should not be urlencoded; default is to urlencode them */
655 if ((!(urlenc_cookies = http_request_option(request, options, "encodecookies", IS_BOOL))) || Z_BVAL_P(urlenc_cookies)) {
656 if (SUCCESS == http_urlencode_hash_recursive(HASH_OF(zoption), &request->_cache.cookies, "; ", lenof("; "), NULL, 0)) {
657 phpstr_fix(&request->_cache.cookies);
658 HTTP_CURL_OPT(CURLOPT_COOKIE, request->_cache.cookies.data);
659 }
660 } else {
661 HashPosition pos;
662 zval *cookie_val = NULL;
663 char *cookie_key = NULL;
664 ulong cookie_idx;
665
666 FOREACH_KEY(pos, zoption, cookie_key, cookie_idx) {
667 if (cookie_key) {
668 zval **cookie_val;
669 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void **) &cookie_val, &pos)) {
670 zval *val = zval_copy(IS_STRING, *cookie_val);
671 phpstr_appendf(&request->_cache.cookies, "%s=%s; ", cookie_key, Z_STRVAL_P(val));
672 zval_free(&val);
673 }
674
675 /* reset */
676 cookie_key = NULL;
677 }
678 }
679
680 phpstr_fix(&request->_cache.cookies);
681 if (PHPSTR_LEN(&request->_cache.cookies)) {
682 HTTP_CURL_OPT(CURLOPT_COOKIE, PHPSTR_VAL(&request->_cache.cookies));
683 }
684 }
685 }
686 }
687
688 #if LIBCURL_VERSION_NUM >= 0x070e01
689 /* reset cookies */
690 if ((zoption = http_request_option(request, options, "resetcookies", IS_BOOL)) && Z_LVAL_P(zoption)) {
691 HTTP_CURL_OPT(CURLOPT_COOKIELIST, "ALL");
692 }
693 #endif
694
695 /* session cookies */
696 if ((zoption = http_request_option(request, options, "cookiesession", IS_BOOL))) {
697 if (Z_LVAL_P(zoption)) {
698 /* accept cookies for this session */
699 HTTP_CURL_OPT(CURLOPT_COOKIEFILE, "");
700 } else {
701 /* reset session cookies */
702 HTTP_CURL_OPT(CURLOPT_COOKIESESSION, 1);
703 }
704 }
705
706 /* cookiestore, read initial cookies from that file and store cookies back into that file */
707 if ((zoption = http_request_option(request, options, "cookiestore", IS_STRING)) && Z_STRLEN_P(zoption)) {
708 HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(zoption), return FAILURE);
709 HTTP_CURL_OPT(CURLOPT_COOKIEFILE, Z_STRVAL_P(zoption));
710 HTTP_CURL_OPT(CURLOPT_COOKIEJAR, Z_STRVAL_P(zoption));
711 }
712
713 /* maxfilesize */
714 if ((zoption = http_request_option(request, options, "maxfilesize", IS_LONG))) {
715 HTTP_CURL_OPT(CURLOPT_MAXFILESIZE, Z_LVAL_P(zoption));
716 }
717
718 /* http protocol */
719 if ((zoption = http_request_option(request, options, "protocol", IS_LONG))) {
720 HTTP_CURL_OPT(CURLOPT_HTTP_VERSION, Z_LVAL_P(zoption));
721 }
722
723 /* timeout, defaults to 0 */
724 if ((zoption = http_request_option(request, options, "timeout", IS_LONG))) {
725 HTTP_CURL_OPT(CURLOPT_TIMEOUT, Z_LVAL_P(zoption));
726 }
727
728 /* connecttimeout, defaults to 0 */
729 if ((zoption = http_request_option(request, options, "connecttimeout", IS_LONG))) {
730 HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT, Z_LVAL_P(zoption));
731 }
732
733 /* ssl */
734 if ((zoption = http_request_option(request, options, "ssl", IS_ARRAY))) {
735 ulong idx;
736 char *key = NULL;
737 zval **param;
738 HashPosition pos;
739
740 FOREACH_KEYVAL(pos, zoption, key, idx, param) {
741 if (key) {
742 HTTP_CURL_OPT_STRING(CURLOPT_SSLCERT, 0, 1);
743 HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTTYPE, 0, 0);
744 HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTPASSWD, 0, 0);
745
746 HTTP_CURL_OPT_STRING(CURLOPT_SSLKEY, 0, 0);
747 HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYTYPE, 0, 0);
748 HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYPASSWD, 0, 0);
749
750 HTTP_CURL_OPT_STRING(CURLOPT_SSLENGINE, 0, 0);
751 HTTP_CURL_OPT_LONG(CURLOPT_SSLVERSION, 0);
752
753 HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYPEER, 1);
754 HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYHOST, 1);
755 HTTP_CURL_OPT_STRING(CURLOPT_SSL_CIPHER_LIST, 1, 0);
756
757 HTTP_CURL_OPT_STRING(CURLOPT_CAINFO, -3, 1);
758 HTTP_CURL_OPT_STRING(CURLOPT_CAPATH, -3, 1);
759 HTTP_CURL_OPT_STRING(CURLOPT_RANDOM_FILE, -3, 1);
760 HTTP_CURL_OPT_STRING(CURLOPT_EGDSOCKET, -3, 1);
761
762 /* reset key */
763 key = NULL;
764 }
765 }
766 }
767
768 /* request method */
769 switch (request->meth)
770 {
771 case HTTP_GET:
772 HTTP_CURL_OPT(CURLOPT_HTTPGET, 1);
773 break;
774
775 case HTTP_HEAD:
776 HTTP_CURL_OPT(CURLOPT_NOBODY, 1);
777 break;
778
779 case HTTP_POST:
780 HTTP_CURL_OPT(CURLOPT_POST, 1);
781 break;
782
783 case HTTP_PUT:
784 HTTP_CURL_OPT(CURLOPT_UPLOAD, 1);
785 break;
786
787 default:
788 if (http_request_method_exists(0, request->meth, NULL)) {
789 HTTP_CURL_OPT(CURLOPT_CUSTOMREQUEST, http_request_method_name(request->meth));
790 } else {
791 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unsupported request method: %d (%s)", request->meth, request->url);
792 return FAILURE;
793 }
794 break;
795 }
796
797 /* attach request body */
798 if (request->body && (request->meth != HTTP_GET) && (request->meth != HTTP_HEAD) && (request->meth != HTTP_OPTIONS)) {
799 switch (request->body->type)
800 {
801 case HTTP_REQUEST_BODY_EMPTY:
802 /* nothing */
803 break;
804
805 case HTTP_REQUEST_BODY_CSTRING:
806 HTTP_CURL_OPT(CURLOPT_POSTFIELDS, request->body->data);
807 HTTP_CURL_OPT(CURLOPT_POSTFIELDSIZE, request->body->size);
808 break;
809
810 case HTTP_REQUEST_BODY_CURLPOST:
811 HTTP_CURL_OPT(CURLOPT_HTTPPOST, (struct curl_httppost *) request->body->data);
812 break;
813
814 case HTTP_REQUEST_BODY_UPLOADFILE:
815 HTTP_CURL_OPT(CURLOPT_IOCTLDATA, request);
816 HTTP_CURL_OPT(CURLOPT_READDATA, request);
817 HTTP_CURL_OPT(CURLOPT_INFILESIZE, request->body->size);
818 break;
819
820 default:
821 /* shouldn't ever happen */
822 http_error_ex(HE_ERROR, 0, "Unknown request body type: %d (%s)", request->body->type, request->url);
823 return FAILURE;
824 break;
825 }
826 }
827
828 return SUCCESS;
829 }
830 /* }}} */
831
832 /* {{{ void http_request_exec(http_request *) */
833 PHP_HTTP_API void _http_request_exec(http_request *request)
834 {
835 CURLcode result;
836 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
837
838 if (CURLE_OK != (result = curl_easy_perform(request->ch))) {
839 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(result), request->_error, request->url);
840 }
841 }
842 /* }}} */
843
844 /* {{{ void http_request_info(http_request *, HashTable *) */
845 PHP_HTTP_API void _http_request_info(http_request *request, HashTable *info)
846 {
847 zval array;
848 INIT_ZARR(array, info);
849
850 HTTP_CURL_INFO(CURLINFO_EFFECTIVE_URL);
851 HTTP_CURL_INFO(CURLINFO_RESPONSE_CODE);
852 HTTP_CURL_INFO_EX(CURLINFO_HTTP_CONNECTCODE, "connect_code");
853 HTTP_CURL_INFO(CURLINFO_FILETIME);
854 HTTP_CURL_INFO(CURLINFO_TOTAL_TIME);
855 HTTP_CURL_INFO(CURLINFO_NAMELOOKUP_TIME);
856 HTTP_CURL_INFO(CURLINFO_CONNECT_TIME);
857 HTTP_CURL_INFO(CURLINFO_PRETRANSFER_TIME);
858 HTTP_CURL_INFO(CURLINFO_STARTTRANSFER_TIME);
859 HTTP_CURL_INFO(CURLINFO_REDIRECT_TIME);
860 HTTP_CURL_INFO(CURLINFO_REDIRECT_COUNT);
861 HTTP_CURL_INFO(CURLINFO_SIZE_UPLOAD);
862 HTTP_CURL_INFO(CURLINFO_SIZE_DOWNLOAD);
863 HTTP_CURL_INFO(CURLINFO_SPEED_DOWNLOAD);
864 HTTP_CURL_INFO(CURLINFO_SPEED_UPLOAD);
865 HTTP_CURL_INFO(CURLINFO_HEADER_SIZE);
866 HTTP_CURL_INFO(CURLINFO_REQUEST_SIZE);
867 HTTP_CURL_INFO(CURLINFO_SSL_VERIFYRESULT);
868 HTTP_CURL_INFO(CURLINFO_SSL_ENGINES);
869 HTTP_CURL_INFO(CURLINFO_CONTENT_LENGTH_DOWNLOAD);
870 HTTP_CURL_INFO(CURLINFO_CONTENT_LENGTH_UPLOAD);
871 HTTP_CURL_INFO(CURLINFO_CONTENT_TYPE);
872 HTTP_CURL_INFO(CURLINFO_HTTPAUTH_AVAIL);
873 HTTP_CURL_INFO(CURLINFO_PROXYAUTH_AVAIL);
874 HTTP_CURL_INFO(CURLINFO_OS_ERRNO);
875 HTTP_CURL_INFO(CURLINFO_NUM_CONNECTS);
876 #if LIBCURL_VERSION_NUM >= 0x070e01
877 HTTP_CURL_INFO_EX(CURLINFO_COOKIELIST, "cookies");
878 #endif
879 }
880 /* }}} */
881
882 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
883 static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *ctx)
884 {
885 http_request *request = (http_request *) ctx;
886 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
887
888 if (request->body == NULL || request->body->type != HTTP_REQUEST_BODY_UPLOADFILE) {
889 return 0;
890 }
891 return php_stream_read((php_stream *) request->body->data, data, len * n);
892 }
893 /* }}} */
894
895 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
896 static int http_curl_progress_callback(void *ctx, double dltotal, double dlnow, double ultotal, double ulnow)
897 {
898 zval *param, retval;
899 http_request *request = (http_request *) ctx;
900 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
901
902 INIT_PZVAL(&retval);
903 ZVAL_NULL(&retval);
904
905 MAKE_STD_ZVAL(param);
906 array_init(param);
907 add_assoc_double(param, "dltotal", dltotal);
908 add_assoc_double(param, "dlnow", dlnow);
909 add_assoc_double(param, "ultotal", ultotal);
910 add_assoc_double(param, "ulnow", ulnow);
911
912 call_user_function(EG(function_table), NULL, request->_progress_callback, &retval, 1, &param TSRMLS_CC);
913
914 zval_ptr_dtor(&param);
915 zval_dtor(&retval);
916
917 return 0;
918 }
919 /* }}} */
920
921 /* {{{ static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *) */
922 static curlioerr http_curl_ioctl_callback(CURL *ch, curliocmd cmd, void *ctx)
923 {
924 http_request *request = (http_request *) ctx;
925 TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
926
927 if (cmd != CURLIOCMD_RESTARTREAD) {
928 return CURLIOE_UNKNOWNCMD;
929 }
930 if ( request->body == NULL ||
931 request->body->type != HTTP_REQUEST_BODY_UPLOADFILE ||
932 SUCCESS != php_stream_rewind((php_stream *) request->body->data)) {
933 return CURLIOE_FAILRESTART;
934 }
935 return CURLIOE_OK;
936 }
937 /* }}} */
938
939 /* {{{ static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *) */
940 static int http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size_t length, void *ctx)
941 {
942 http_request *request = (http_request *) ctx;
943
944 switch (type)
945 {
946 case CURLINFO_DATA_IN:
947 if (request->conv.last_type == CURLINFO_HEADER_IN) {
948 phpstr_appends(&request->conv.response, HTTP_CRLF);
949 }
950 case CURLINFO_HEADER_IN:
951 phpstr_append(&request->conv.response, data, length);
952 break;
953 case CURLINFO_DATA_OUT:
954 if (request->conv.last_type == CURLINFO_HEADER_OUT) {
955 phpstr_appends(&request->conv.request, HTTP_CRLF);
956 }
957 case CURLINFO_HEADER_OUT:
958 phpstr_append(&request->conv.request, data, length);
959 break;
960 default:
961 #if 0
962 fprintf(stderr, "## ", type);
963 if (!type) {
964 fprintf(stderr, "%s", data);
965 } else {
966 ulong i;
967 for (i = 1; i <= length; ++i) {
968 fprintf(stderr, "%02X ", data[i-1] & 0xFF);
969 if (!(i % 20)) {
970 fprintf(stderr, "\n## ");
971 }
972 }
973 fprintf(stderr, "\n");
974 }
975 if (data[length-1] != 0xa) {
976 fprintf(stderr, "\n");
977 }
978 #endif
979 break;
980 }
981
982 if (type) {
983 request->conv.last_type = type;
984 }
985 return 0;
986 }
987 /* }}} */
988
989 /* {{{ static inline zval *http_request_option(http_request *, HashTable *, char *, size_t, int) */
990 static inline zval *_http_request_option_ex(http_request *r, HashTable *options, char *key, size_t keylen, int type TSRMLS_DC)
991 {
992 zval **zoption;
993 #ifdef ZEND_ENGINE_2
994 ulong h = zend_get_hash_value(key, keylen);
995 #else
996 ulong h = 0;
997 #endif
998
999 if (!options ||
1000 #ifdef ZEND_ENGINE_2
1001 (SUCCESS != zend_hash_quick_find(options, key, keylen, h, (void **) &zoption))
1002 #else
1003 (SUCCESS != zend_hash_find(options, key, keylen, (void **) &zoption))
1004 #endif
1005 ) {
1006 return NULL;
1007 }
1008
1009 return http_request_option_cache_ex(r, key, keylen, h, zval_copy(type, *zoption));
1010 }
1011 /* }}} */
1012
1013 /* {{{ static inline zval *http_request_option_cache(http_request *, char *key, zval *) */
1014 static inline zval *_http_request_option_cache_ex(http_request *r, char *key, size_t keylen, ulong h, zval *opt TSRMLS_DC)
1015 {
1016 ZVAL_ADDREF(opt);
1017
1018 #ifdef ZEND_ENGINE_2
1019 if (h) {
1020 _zend_hash_quick_add_or_update(&r->_cache.options, key, keylen, h, &opt, sizeof(zval *), NULL,
1021 zend_hash_quick_exists(&r->_cache.options, key, keylen, h)?HASH_UPDATE:HASH_ADD ZEND_FILE_LINE_CC);
1022 }
1023 else
1024 #endif
1025 {
1026 if (zend_hash_exists(&r->_cache.options, key, keylen)) {
1027 zend_hash_update(&r->_cache.options, key, keylen, &opt, sizeof(zval *), NULL);
1028 } else {
1029 zend_hash_add(&r->_cache.options, key, keylen, &opt, sizeof(zval *), NULL);
1030 }
1031 }
1032
1033 return opt;
1034 }
1035 /* }}} */
1036
1037 #endif /* HTTP_HAVE_CURL */
1038
1039 /*
1040 * Local variables:
1041 * tab-width: 4
1042 * c-basic-offset: 4
1043 * End:
1044 * vim600: noet sw=4 ts=4 fdm=marker
1045 * vim<600: noet sw=4 ts=4
1046 */
1047