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