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