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