3ad9988955f279ff8e366d398ec41d5d41c2036e
[m6w6/ext-http] / php_http_misc.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-2013, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 #include <ext/standard/php_lcg.h>
16 #include <zend_exceptions.h>
17
18 /* SLEEP */
19
20 PHP_HTTP_API void php_http_sleep(double s)
21 {
22 #if defined(PHP_WIN32)
23 Sleep((DWORD) PHP_HTTP_MSEC(s));
24 #elif defined(HAVE_USLEEP)
25 usleep(PHP_HTTP_USEC(s));
26 #elif defined(HAVE_NANOSLEEP)
27 struct timespec req, rem;
28
29 req.tv_sec = (time_t) s;
30 req.tv_nsec = PHP_HTTP_NSEC(s) % PHP_HTTP_NANOSEC;
31
32 while (nanosleep(&req, &rem) && (errno == EINTR) && (PHP_HTTP_NSEC(rem.tv_sec) + rem.tv_nsec) > PHP_HTTP_NSEC(PHP_HTTP_DIFFSEC))) {
33 req.tv_sec = rem.tv_sec;
34 req.tv_nsec = rem.tv_nsec;
35 }
36 #else
37 struct timeval timeout;
38
39 timeout.tv_sec = (time_t) s;
40 timeout.tv_usec = PHP_HTTP_USEC(s) % PHP_HTTP_MCROSEC;
41
42 select(0, NULL, NULL, NULL, &timeout);
43 #endif
44 }
45
46
47 /* STRING UTILITIES */
48
49 int php_http_match(const char *haystack_str, const char *needle_str, int flags)
50 {
51 int result = 0;
52
53 if (!haystack_str || !needle_str) {
54 return result;
55 }
56
57 if (flags & PHP_HTTP_MATCH_FULL) {
58 if (flags & PHP_HTTP_MATCH_CASE) {
59 result = !strcmp(haystack_str, needle_str);
60 } else {
61 result = !strcasecmp(haystack_str, needle_str);
62 }
63 } else {
64 char *found, *haystack = estrdup(haystack_str), *needle = estrdup(needle_str);
65
66 if (flags & PHP_HTTP_MATCH_CASE) {
67 found = zend_memnstr(haystack, needle, strlen(needle), haystack+strlen(haystack));
68 } else {
69 found = php_stristr(haystack, needle, strlen(haystack), strlen(needle));
70 }
71
72 if (found) {
73 if (!(flags & PHP_HTTP_MATCH_WORD)
74 || ( (found == haystack || !PHP_HTTP_IS_CTYPE(alnum, *(found - 1)))
75 && (!*(found + strlen(needle)) || !PHP_HTTP_IS_CTYPE(alnum, *(found + strlen(needle))))
76 )
77 ) {
78 result = 1;
79 }
80 }
81
82 STR_FREE(haystack);
83 STR_FREE(needle);
84 }
85
86 return result;
87 }
88
89 char *php_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen)
90 {
91 size_t i;
92 int wasalpha;
93
94 if (key && key_len) {
95 if ((wasalpha = PHP_HTTP_IS_CTYPE(alpha, key[0]))) {
96 key[0] = (char) (uctitle ? PHP_HTTP_TO_CTYPE(upper, key[0]) : PHP_HTTP_TO_CTYPE(lower, key[0]));
97 }
98 for (i = 1; i < key_len; i++) {
99 if (PHP_HTTP_IS_CTYPE(alpha, key[i])) {
100 key[i] = (char) (((!wasalpha) && uctitle) ? PHP_HTTP_TO_CTYPE(upper, key[i]) : PHP_HTTP_TO_CTYPE(lower, key[i]));
101 wasalpha = 1;
102 } else {
103 if (xhyphen && (key[i] == '_')) {
104 key[i] = '-';
105 }
106 wasalpha = 0;
107 }
108 }
109 }
110 return key;
111 }
112
113
114 size_t php_http_boundary(char *buf, size_t buf_len TSRMLS_DC)
115 {
116 return snprintf(buf, buf_len, "%15.15F", PHP_HTTP_G->env.request.time * php_combined_lcg(TSRMLS_C));
117 }
118
119 int php_http_select_str(const char *cmp, int argc, ...)
120 {
121 va_list argv;
122 int match = -1;
123
124 if (cmp && argc > 0) {
125 int i;
126
127 va_start(argv, argc);
128 for (i = 0; i < argc; ++i) {
129 const char *test = va_arg(argv, const char *);
130
131 if (!strcasecmp(cmp, test)) {
132 match = i;
133 break;
134 }
135 }
136 va_end(argv);
137 }
138
139 return match;
140 }
141
142
143 /* ARRAYS */
144
145 PHP_HTTP_API unsigned php_http_array_list(HashTable *ht TSRMLS_DC, unsigned argc, ...)
146 {
147 HashPosition pos;
148 unsigned argl = 0;
149 va_list argv;
150
151 va_start(argv, argc);
152 for ( zend_hash_internal_pointer_reset_ex(ht, &pos);
153 SUCCESS == zend_hash_has_more_elements_ex(ht, &pos) && (argl < argc);
154 zend_hash_move_forward_ex(ht, &pos))
155 {
156 zval **data, ***argp = (zval ***) va_arg(argv, zval ***);
157
158 if (SUCCESS == zend_hash_get_current_data_ex(ht, (void *) &data, &pos)) {
159 *argp = data;
160 ++argl;
161 }
162 }
163 va_end(argv);
164
165 return argl;
166 }
167
168 int php_http_array_apply_append_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
169 {
170 int flags;
171 char *key = NULL;
172 HashTable *dst;
173 zval **data = NULL, **value = (zval **) pDest;
174
175 dst = va_arg(args, HashTable *);
176 flags = va_arg(args, int);
177
178 if ((!(flags & ARRAY_JOIN_STRONLY)) || hash_key->nKeyLength) {
179 if ((flags & ARRAY_JOIN_PRETTIFY) && hash_key->nKeyLength) {
180 key = php_http_pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 1);
181 zend_hash_find(dst, key, hash_key->nKeyLength, (void *) &data);
182 } else {
183 zend_hash_quick_find(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) &data);
184 }
185
186 Z_ADDREF_P(*value);
187 if (data) {
188 if (Z_TYPE_PP(data) != IS_ARRAY) {
189 convert_to_array(*data);
190 }
191 add_next_index_zval(*data, *value);
192 } else if (key) {
193 zend_symtable_update(dst, key, hash_key->nKeyLength, value, sizeof(zval *), NULL);
194 } else {
195 zend_hash_quick_add(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, value, sizeof(zval *), NULL);
196 }
197
198 if (key) {
199 efree(key);
200 }
201 }
202
203 return ZEND_HASH_APPLY_KEEP;
204 }
205
206 int php_http_array_apply_merge_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
207 {
208 int flags;
209 char *key = NULL;
210 HashTable *dst;
211 zval **value = (zval **) pDest;
212
213 dst = va_arg(args, HashTable *);
214 flags = va_arg(args, int);
215
216 if ((!(flags & ARRAY_JOIN_STRONLY)) || hash_key->nKeyLength) {
217 Z_ADDREF_P(*value);
218 if ((flags & ARRAY_JOIN_PRETTIFY) && hash_key->nKeyLength) {
219 key = php_http_pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 1);
220 zend_hash_update(dst, key, hash_key->nKeyLength, (void *) value, sizeof(zval *), NULL);
221 efree(key);
222 } else {
223 zend_hash_quick_update(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) value, sizeof(zval *), NULL);
224 }
225 }
226
227 return ZEND_HASH_APPLY_KEEP;
228 }
229
230 /* PASS CALLBACK */
231
232 PHP_HTTP_API size_t php_http_pass_fcall_callback(void *cb_arg, const char *str, size_t len)
233 {
234 php_http_pass_fcall_arg_t *fcd = cb_arg;
235 zval *zdata;
236 TSRMLS_FETCH_FROM_CTX(fcd->ts);
237
238 MAKE_STD_ZVAL(zdata);
239 ZVAL_STRINGL(zdata, str, len, 1);
240 if (SUCCESS == zend_fcall_info_argn(&fcd->fci TSRMLS_CC, 2, &fcd->fcz, &zdata)) {
241 zend_fcall_info_call(&fcd->fci, &fcd->fcc, NULL, NULL TSRMLS_CC);
242 zend_fcall_info_args_clear(&fcd->fci, 0);
243 }
244 zval_ptr_dtor(&zdata);
245 return len;
246 }
247
248 /* ERROR */
249
250 static inline int scope_error_handling(long type TSRMLS_DC)
251 {
252 if ((type == E_THROW) || (EG(error_handling) == EH_THROW)) {
253 return EH_THROW;
254 }
255
256 if (EG(This) && instanceof_function(Z_OBJCE_P(EG(This)), php_http_object_class_entry TSRMLS_CC)) {
257 return php_http_object_get_error_handling(EG(This) TSRMLS_CC);
258 }
259
260 return EH_NORMAL;
261 }
262
263 void php_http_error(long type TSRMLS_DC, long code, const char *format, ...)
264 {
265 va_list args;
266
267 va_start(args, format);
268 switch (scope_error_handling(type TSRMLS_CC)) {
269 case EH_THROW: {
270 char *message;
271 zend_class_entry *ce = php_http_exception_class_entry;
272
273 /* FIXME wat? */
274 if (0&& EG(exception_class) && instanceof_function(EG(exception_class), ce TSRMLS_CC)) {
275 ce = EG(exception_class);
276 }
277
278 vspprintf(&message, 0, format, args);
279 zend_throw_exception(ce, message, code TSRMLS_CC);
280 efree(message);
281 break;
282 }
283 case EH_NORMAL:
284 php_verror(NULL, "", type, format, args TSRMLS_CC);
285 break;
286 case EH_SUPPRESS:
287 break;
288 }
289 va_end(args);
290 }
291
292 /* ZEND */
293
294 STATUS php_http_method_call(zval *object, const char *method_str, size_t method_len, int argc, zval **argv[], zval **retval_ptr TSRMLS_DC)
295 {
296 zend_fcall_info fci;
297 zval zmethod;
298 zval *retval;
299 STATUS rv;
300
301 fci.size = sizeof(fci);
302 fci.object_ptr = object;
303 fci.function_name = &zmethod;
304 fci.retval_ptr_ptr = retval_ptr ? retval_ptr : &retval;
305 fci.param_count = argc;
306 fci.params = argv;
307 fci.no_separation = 1;
308 fci.symbol_table = NULL;
309 fci.function_table = NULL;
310
311 INIT_PZVAL(&zmethod);
312 ZVAL_STRINGL(&zmethod, method_str, method_len, 0);
313 rv = zend_call_function(&fci, NULL TSRMLS_CC);
314
315 if (!retval_ptr && retval) {
316 zval_ptr_dtor(&retval);
317 }
318 return rv;
319 }
320 /*
321 * Local variables:
322 * tab-width: 4
323 * c-basic-offset: 4
324 * End:
325 * vim600: noet sw=4 ts=4 fdm=marker
326 * vim<600: noet sw=4 ts=4
327 */