update expectations how history requests look like
[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 PTR_FREE(haystack);
84 PTR_FREE(needle);
85 }
86
87 return result;
88 }
89
90 char *php_http_pretty_key(register char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen)
91 {
92 size_t i = 1;
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 PHP_HTTP_DUFF(key_len,
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 ++i;
110 );
111 }
112 return key;
113 }
114
115
116 size_t php_http_boundary(char *buf, size_t buf_len TSRMLS_DC)
117 {
118 return snprintf(buf, buf_len, "%15.15F", sapi_get_request_time(TSRMLS_C) * php_combined_lcg(TSRMLS_C));
119 }
120
121 int php_http_select_str(const char *cmp, int argc, ...)
122 {
123 va_list argv;
124 int match = -1;
125
126 if (cmp && argc > 0) {
127 int i;
128
129 va_start(argv, argc);
130 for (i = 0; i < argc; ++i) {
131 const char *test = va_arg(argv, const char *);
132
133 if (!strcasecmp(cmp, test)) {
134 match = i;
135 break;
136 }
137 }
138 va_end(argv);
139 }
140
141 return match;
142 }
143
144
145 /* ARRAYS */
146
147 unsigned php_http_array_list(HashTable *ht TSRMLS_DC, unsigned argc, ...)
148 {
149 HashPosition pos;
150 unsigned argl = 0;
151 va_list argv;
152
153 va_start(argv, argc);
154 for ( zend_hash_internal_pointer_reset_ex(ht, &pos);
155 SUCCESS == zend_hash_has_more_elements_ex(ht, &pos) && (argl < argc);
156 zend_hash_move_forward_ex(ht, &pos))
157 {
158 zval **data, ***argp = (zval ***) va_arg(argv, zval ***);
159
160 if (SUCCESS == zend_hash_get_current_data_ex(ht, (void *) &data, &pos)) {
161 *argp = data;
162 ++argl;
163 }
164 }
165 va_end(argv);
166
167 return argl;
168 }
169
170 void php_http_array_copy_strings(void *zpp)
171 {
172 zval **zvpp = ((zval **) zpp);
173
174 *zvpp = php_http_zsep(1, IS_STRING, *zvpp);
175 }
176
177 int php_http_array_apply_append_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
178 {
179 int flags;
180 char *key = NULL;
181 HashTable *dst;
182 zval **data = NULL, *value = *((zval **) pDest);
183
184 dst = va_arg(args, HashTable *);
185 flags = va_arg(args, int);
186
187 if ((!(flags & ARRAY_JOIN_STRONLY)) || hash_key->nKeyLength) {
188 if ((flags & ARRAY_JOIN_PRETTIFY) && hash_key->nKeyLength) {
189 key = php_http_pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 1);
190 zend_hash_find(dst, key, hash_key->nKeyLength, (void *) &data);
191 } else {
192 zend_hash_quick_find(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) &data);
193 }
194
195 if (flags & ARRAY_JOIN_STRINGIFY) {
196 value = php_http_zsep(1, IS_STRING, value);
197 } else {
198 Z_ADDREF_P(value);
199 }
200
201 if (data) {
202 if (Z_TYPE_PP(data) != IS_ARRAY) {
203 convert_to_array(*data);
204 }
205 add_next_index_zval(*data, value);
206 } else if (key) {
207 zend_symtable_update(dst, key, hash_key->nKeyLength, &value, sizeof(zval *), NULL);
208 } else {
209 zend_hash_quick_add(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, &value, sizeof(zval *), NULL);
210 }
211
212 if (key) {
213 efree(key);
214 }
215 }
216
217 return ZEND_HASH_APPLY_KEEP;
218 }
219
220 int php_http_array_apply_merge_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
221 {
222 int flags;
223 char *key = NULL;
224 HashTable *dst;
225 zval *value = *((zval **) pDest);
226
227 dst = va_arg(args, HashTable *);
228 flags = va_arg(args, int);
229
230 if ((!(flags & ARRAY_JOIN_STRONLY)) || hash_key->nKeyLength) {
231 if (flags & ARRAY_JOIN_STRINGIFY) {
232 value = php_http_zsep(1, IS_STRING, value);
233 } else {
234 Z_ADDREF_P(value);
235 }
236
237 if ((flags & ARRAY_JOIN_PRETTIFY) && hash_key->nKeyLength) {
238 key = php_http_pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 1);
239 zend_hash_update(dst, key, hash_key->nKeyLength, (void *) &value, sizeof(zval *), NULL);
240 efree(key);
241 } else {
242 zend_hash_quick_update(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) &value, sizeof(zval *), NULL);
243 }
244 }
245
246 return ZEND_HASH_APPLY_KEEP;
247 }
248
249 /* PASS CALLBACK */
250
251 size_t php_http_pass_fcall_callback(void *cb_arg, const char *str, size_t len)
252 {
253 php_http_pass_fcall_arg_t *fcd = cb_arg;
254 zval *zdata;
255 TSRMLS_FETCH_FROM_CTX(fcd->ts);
256
257 MAKE_STD_ZVAL(zdata);
258 ZVAL_STRINGL(zdata, str, len, 1);
259 if (SUCCESS == zend_fcall_info_argn(&fcd->fci TSRMLS_CC, 2, &fcd->fcz, &zdata)) {
260 zend_fcall_info_call(&fcd->fci, &fcd->fcc, NULL, NULL TSRMLS_CC);
261 zend_fcall_info_args_clear(&fcd->fci, 0);
262 }
263 zval_ptr_dtor(&zdata);
264 return len;
265 }
266
267
268 /* ZEND */
269
270 /*
271 * Local variables:
272 * tab-width: 4
273 * c-basic-offset: 4
274 * End:
275 * vim600: noet sw=4 ts=4 fdm=marker
276 * vim<600: noet sw=4 ts=4
277 */