header cleanups and fix some warnings
[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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 #include <ext/standard/php_lcg.h>
16 #include <ext/standard/php_string.h>
17 #include <zend_exceptions.h>
18
19 /* SLEEP */
20
21 PHP_HTTP_API void php_http_sleep(double s)
22 {
23 #if defined(PHP_WIN32)
24 Sleep((DWORD) PHP_HTTP_MSEC(s));
25 #elif defined(HAVE_USLEEP)
26 usleep(PHP_HTTP_USEC(s));
27 #elif defined(HAVE_NANOSLEEP)
28 struct timespec req, rem;
29
30 req.tv_sec = (time_t) s;
31 req.tv_nsec = PHP_HTTP_NSEC(s) % PHP_HTTP_NANOSEC;
32
33 while (nanosleep(&req, &rem) && (errno == EINTR) && (PHP_HTTP_NSEC(rem.tv_sec) + rem.tv_nsec) > PHP_HTTP_NSEC(PHP_HTTP_DIFFSEC))) {
34 req.tv_sec = rem.tv_sec;
35 req.tv_nsec = rem.tv_nsec;
36 }
37 #else
38 struct timeval timeout;
39
40 timeout.tv_sec = (time_t) s;
41 timeout.tv_usec = PHP_HTTP_USEC(s) % PHP_HTTP_MCROSEC;
42
43 select(0, NULL, NULL, NULL, &timeout);
44 #endif
45 }
46
47
48 /* STRING UTILITIES */
49
50 int php_http_match(const char *haystack_str, const char *needle_str, int flags)
51 {
52 int result = 0;
53
54 if (flags & PHP_HTTP_MATCH_FULL) {
55 if (flags & PHP_HTTP_MATCH_CASE) {
56 result = !strcmp(haystack_str, needle_str);
57 } else {
58 result = !strcasecmp(haystack_str, needle_str);
59 }
60 } else {
61 char *found, *haystack = estrdup(haystack_str), *needle = estrdup(needle_str);
62
63 if (flags & PHP_HTTP_MATCH_CASE) {
64 found = zend_memnstr(haystack, needle, strlen(needle), haystack+strlen(haystack));
65 } else {
66 found = php_stristr(haystack, needle, strlen(haystack), strlen(needle));
67 }
68
69 if (found) {
70 if (!(flags & PHP_HTTP_MATCH_WORD)
71 || ( (found == haystack || !PHP_HTTP_IS_CTYPE(alnum, *(found - 1)))
72 && (!*(found + strlen(needle)) || !PHP_HTTP_IS_CTYPE(alnum, *(found + strlen(needle))))
73 )
74 ) {
75 result = 1;
76 }
77 }
78
79 STR_FREE(haystack);
80 STR_FREE(needle);
81 }
82
83 return result;
84 }
85
86 char *php_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen)
87 {
88 size_t i;
89 int wasalpha;
90
91 if (key && key_len) {
92 if ((wasalpha = PHP_HTTP_IS_CTYPE(alpha, key[0]))) {
93 key[0] = (char) (uctitle ? PHP_HTTP_TO_CTYPE(upper, key[0]) : PHP_HTTP_TO_CTYPE(lower, key[0]));
94 }
95 for (i = 1; i < key_len; i++) {
96 if (PHP_HTTP_IS_CTYPE(alpha, key[i])) {
97 key[i] = (char) (((!wasalpha) && uctitle) ? PHP_HTTP_TO_CTYPE(upper, key[i]) : PHP_HTTP_TO_CTYPE(lower, key[i]));
98 wasalpha = 1;
99 } else {
100 if (xhyphen && (key[i] == '_')) {
101 key[i] = '-';
102 }
103 wasalpha = 0;
104 }
105 }
106 }
107 return key;
108 }
109
110
111 size_t php_http_boundary(char *buf, size_t buf_len TSRMLS_DC)
112 {
113 return snprintf(buf, buf_len, "%15.15F", PHP_HTTP_G->env.request.time * php_combined_lcg(TSRMLS_C));
114 }
115
116 /* ARRAYS */
117
118 int php_http_array_apply_append_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
119 {
120 int flags;
121 char *key = NULL;
122 HashTable *dst;
123 zval **data = NULL, **value = (zval **) pDest;
124
125 dst = va_arg(args, HashTable *);
126 flags = va_arg(args, int);
127
128 if ((!(flags & ARRAY_JOIN_STRONLY)) || hash_key->nKeyLength) {
129 if ((flags & ARRAY_JOIN_PRETTIFY) && hash_key->nKeyLength) {
130 key = php_http_pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 1);
131 zend_hash_find(dst, key, hash_key->nKeyLength, (void *) &data);
132 } else {
133 zend_hash_quick_find(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) &data);
134 }
135
136 Z_ADDREF_P(*value);
137 if (data) {
138 if (Z_TYPE_PP(data) != IS_ARRAY) {
139 convert_to_array(*data);
140 }
141 add_next_index_zval(*data, *value);
142 } else if (key) {
143 zend_symtable_update(dst, key, hash_key->nKeyLength, value, sizeof(zval *), NULL);
144 } else {
145 zend_hash_quick_add(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, value, sizeof(zval *), NULL);
146 }
147
148 if (key) {
149 efree(key);
150 }
151 }
152
153 return ZEND_HASH_APPLY_KEEP;
154 }
155
156 int php_http_array_apply_merge_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
157 {
158 int flags;
159 char *key = NULL;
160 HashTable *dst;
161 zval **value = (zval **) pDest;
162
163 dst = va_arg(args, HashTable *);
164 flags = va_arg(args, int);
165
166 if ((!(flags & ARRAY_JOIN_STRONLY)) || hash_key->nKeyLength) {
167 Z_ADDREF_P(*value);
168 if ((flags & ARRAY_JOIN_PRETTIFY) && hash_key->nKeyLength) {
169 key = php_http_pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 1);
170 zend_hash_update(dst, key, hash_key->nKeyLength, (void *) value, sizeof(zval *), NULL);
171 efree(key);
172 } else {
173 zend_hash_quick_update(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) value, sizeof(zval *), NULL);
174 }
175 }
176
177 return ZEND_HASH_APPLY_KEEP;
178 }
179
180 /* PASS CALLBACK */
181
182 PHP_HTTP_API size_t php_http_pass_wrapper(php_http_pass_callback_arg_t *cb, const char *str, size_t len)
183 {
184 TSRMLS_FETCH();
185 return cb->cb_zts(cb->cb_arg, str, len TSRMLS_CC);
186 }
187
188 /* ERROR */
189
190 static inline int scope_error_handling(long type TSRMLS_DC)
191 {
192 if ((type == E_THROW) || (EG(error_handling) == EH_THROW)) {
193 return EH_THROW;
194 }
195
196 if (EG(This) && instanceof_function(Z_OBJCE_P(EG(This)), php_http_object_class_entry TSRMLS_CC)) {
197 return php_http_object_get_error_handling(EG(This) TSRMLS_CC);
198 }
199
200 return EH_NORMAL;
201 }
202
203 void php_http_error(long type TSRMLS_DC, long code, const char *format, ...)
204 {
205 va_list args;
206
207 va_start(args, format);
208 switch (scope_error_handling(type TSRMLS_CC)) {
209 case EH_THROW: {
210 char *message;
211 zend_class_entry *ce = php_http_exception_class_entry;
212
213 if (0&& EG(exception_class) && instanceof_function(EG(exception_class), php_http_exception_class_entry TSRMLS_CC)) {
214 ce = EG(exception_class);
215 }
216
217 vspprintf(&message, 0, format, args);
218 zend_throw_exception(ce, message, code TSRMLS_CC);
219 efree(message);
220 break;
221 }
222 case EH_NORMAL:
223 php_verror(NULL, "", type, format, args TSRMLS_CC);
224 break;
225 case EH_SUPPRESS:
226 break;
227 }
228 va_end(args);
229 }
230
231 /*
232 * Local variables:
233 * tab-width: 4
234 * c-basic-offset: 4
235 * End:
236 * vim600: noet sw=4 ts=4 fdm=marker
237 * vim<600: noet sw=4 ts=4
238 */