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