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