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