port querystring tests
[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-2014, 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 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 const char *found;
65 char *haystack = estrdup(haystack_str), *needle = estrdup(needle_str);
66
67 if (flags & PHP_HTTP_MATCH_CASE) {
68 found = zend_memnstr(haystack, needle, strlen(needle), haystack+strlen(haystack));
69 } else {
70 found = php_stristr(haystack, needle, strlen(haystack), strlen(needle));
71 }
72
73 if (found) {
74 if (!(flags & PHP_HTTP_MATCH_WORD)
75 || ( (found == haystack || !PHP_HTTP_IS_CTYPE(alnum, *(found - 1)))
76 && (!*(found + strlen(needle)) || !PHP_HTTP_IS_CTYPE(alnum, *(found + strlen(needle))))
77 )
78 ) {
79 result = 1;
80 }
81 }
82
83 STR_FREE(haystack);
84 STR_FREE(needle);
85 }
86
87 return result;
88 }
89
90 char *php_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen)
91 {
92 size_t i;
93 int wasalpha;
94
95 if (key && key_len) {
96 if ((wasalpha = PHP_HTTP_IS_CTYPE(alpha, key[0]))) {
97 key[0] = (char) (uctitle ? PHP_HTTP_TO_CTYPE(upper, key[0]) : PHP_HTTP_TO_CTYPE(lower, key[0]));
98 }
99 for (i = 1; i < key_len; i++) {
100 if (PHP_HTTP_IS_CTYPE(alpha, key[i])) {
101 key[i] = (char) (((!wasalpha) && uctitle) ? PHP_HTTP_TO_CTYPE(upper, key[i]) : PHP_HTTP_TO_CTYPE(lower, key[i]));
102 wasalpha = 1;
103 } else {
104 if (xhyphen && (key[i] == '_')) {
105 key[i] = '-';
106 }
107 wasalpha = 0;
108 }
109 }
110 }
111 return key;
112 }
113
114
115 size_t php_http_boundary(char *buf, size_t buf_len TSRMLS_DC)
116 {
117 return snprintf(buf, buf_len, "%15.15F", PHP_HTTP_G->env.request.time * php_combined_lcg(TSRMLS_C));
118 }
119
120 int php_http_select_str(const char *cmp, int argc, ...)
121 {
122 va_list argv;
123 int match = -1;
124
125 if (cmp && argc > 0) {
126 int i;
127
128 va_start(argv, argc);
129 for (i = 0; i < argc; ++i) {
130 const char *test = va_arg(argv, const char *);
131
132 if (!strcasecmp(cmp, test)) {
133 match = i;
134 break;
135 }
136 }
137 va_end(argv);
138 }
139
140 return match;
141 }
142
143
144 /* ARRAYS */
145
146 unsigned php_http_array_list(HashTable *ht TSRMLS_DC, unsigned argc, ...)
147 {
148 HashPosition pos;
149 unsigned argl = 0;
150 va_list argv;
151
152 va_start(argv, argc);
153 for ( zend_hash_internal_pointer_reset_ex(ht, &pos);
154 SUCCESS == zend_hash_has_more_elements_ex(ht, &pos) && (argl < argc);
155 zend_hash_move_forward_ex(ht, &pos))
156 {
157 zval **data, ***argp = (zval ***) va_arg(argv, zval ***);
158
159 if (SUCCESS == zend_hash_get_current_data_ex(ht, (void *) &data, &pos)) {
160 *argp = data;
161 ++argl;
162 }
163 }
164 va_end(argv);
165
166 return argl;
167 }
168
169 int php_http_array_apply_append_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
170 {
171 int flags;
172 char *key = NULL;
173 HashTable *dst;
174 zval **data = NULL, **value = (zval **) pDest;
175
176 dst = va_arg(args, HashTable *);
177 flags = va_arg(args, int);
178
179 if ((!(flags & ARRAY_JOIN_STRONLY)) || hash_key->nKeyLength) {
180 if ((flags & ARRAY_JOIN_PRETTIFY) && hash_key->nKeyLength) {
181 key = php_http_pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 1);
182 zend_hash_find(dst, key, hash_key->nKeyLength, (void *) &data);
183 } else {
184 zend_hash_quick_find(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) &data);
185 }
186
187 Z_ADDREF_P(*value);
188 if (data) {
189 if (Z_TYPE_PP(data) != IS_ARRAY) {
190 convert_to_array(*data);
191 }
192 add_next_index_zval(*data, *value);
193 } else if (key) {
194 zend_symtable_update(dst, key, hash_key->nKeyLength, value, sizeof(zval *), NULL);
195 } else {
196 zend_hash_quick_add(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, value, sizeof(zval *), NULL);
197 }
198
199 if (key) {
200 efree(key);
201 }
202 }
203
204 return ZEND_HASH_APPLY_KEEP;
205 }
206
207 int php_http_array_apply_merge_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
208 {
209 int flags;
210 char *key = NULL;
211 HashTable *dst;
212 zval **value = (zval **) pDest;
213
214 dst = va_arg(args, HashTable *);
215 flags = va_arg(args, int);
216
217 if ((!(flags & ARRAY_JOIN_STRONLY)) || hash_key->nKeyLength) {
218 Z_ADDREF_P(*value);
219 if ((flags & ARRAY_JOIN_PRETTIFY) && hash_key->nKeyLength) {
220 key = php_http_pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 1);
221 zend_hash_update(dst, key, hash_key->nKeyLength, (void *) value, sizeof(zval *), NULL);
222 efree(key);
223 } else {
224 zend_hash_quick_update(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) value, sizeof(zval *), NULL);
225 }
226 }
227
228 return ZEND_HASH_APPLY_KEEP;
229 }
230
231 /* PASS CALLBACK */
232
233 size_t php_http_pass_fcall_callback(void *cb_arg, const char *str, size_t len)
234 {
235 php_http_pass_fcall_arg_t *fcd = cb_arg;
236 zval *zdata;
237 TSRMLS_FETCH_FROM_CTX(fcd->ts);
238
239 MAKE_STD_ZVAL(zdata);
240 ZVAL_STRINGL(zdata, str, len, 1);
241 if (SUCCESS == zend_fcall_info_argn(&fcd->fci TSRMLS_CC, 2, &fcd->fcz, &zdata)) {
242 zend_fcall_info_call(&fcd->fci, &fcd->fcc, NULL, NULL TSRMLS_CC);
243 zend_fcall_info_args_clear(&fcd->fci, 0);
244 }
245 zval_ptr_dtor(&zdata);
246 return len;
247 }
248
249
250 /* ZEND */
251
252 /*
253 * Local variables:
254 * tab-width: 4
255 * c-basic-offset: 4
256 * End:
257 * vim600: noet sw=4 ts=4 fdm=marker
258 * vim<600: noet sw=4 ts=4
259 */