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