header cleanups; fix IDE 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 <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 (flags & PHP_HTTP_MATCH_FULL) {
54 if (flags & PHP_HTTP_MATCH_CASE) {
55 result = !strcmp(haystack_str, needle_str);
56 } else {
57 result = !strcasecmp(haystack_str, needle_str);
58 }
59 } else {
60 char *found, *haystack = estrdup(haystack_str), *needle = estrdup(needle_str);
61
62 if (flags & PHP_HTTP_MATCH_CASE) {
63 found = zend_memnstr(haystack, needle, strlen(needle), haystack+strlen(haystack));
64 } else {
65 found = php_stristr(haystack, needle, strlen(haystack), strlen(needle));
66 }
67
68 if (found) {
69 if (!(flags & PHP_HTTP_MATCH_WORD)
70 || ( (found == haystack || !PHP_HTTP_IS_CTYPE(alnum, *(found - 1)))
71 && (!*(found + strlen(needle)) || !PHP_HTTP_IS_CTYPE(alnum, *(found + strlen(needle))))
72 )
73 ) {
74 result = 1;
75 }
76 }
77
78 STR_FREE(haystack);
79 STR_FREE(needle);
80 }
81
82 return result;
83 }
84
85 char *php_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen)
86 {
87 size_t i;
88 int wasalpha;
89
90 if (key && key_len) {
91 if ((wasalpha = PHP_HTTP_IS_CTYPE(alpha, key[0]))) {
92 key[0] = (char) (uctitle ? PHP_HTTP_TO_CTYPE(upper, key[0]) : PHP_HTTP_TO_CTYPE(lower, key[0]));
93 }
94 for (i = 1; i < key_len; i++) {
95 if (PHP_HTTP_IS_CTYPE(alpha, key[i])) {
96 key[i] = (char) (((!wasalpha) && uctitle) ? PHP_HTTP_TO_CTYPE(upper, key[i]) : PHP_HTTP_TO_CTYPE(lower, key[i]));
97 wasalpha = 1;
98 } else {
99 if (xhyphen && (key[i] == '_')) {
100 key[i] = '-';
101 }
102 wasalpha = 0;
103 }
104 }
105 }
106 return key;
107 }
108
109
110 size_t php_http_boundary(char *buf, size_t buf_len TSRMLS_DC)
111 {
112 return snprintf(buf, buf_len, "%15.15F", PHP_HTTP_G->env.request.time * php_combined_lcg(TSRMLS_C));
113 }
114
115 /* ARRAYS */
116
117 int php_http_array_apply_append_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
118 {
119 int flags;
120 char *key = NULL;
121 HashTable *dst;
122 zval **data = NULL, **value = (zval **) pDest;
123
124 dst = va_arg(args, HashTable *);
125 flags = va_arg(args, int);
126
127 if ((!(flags & ARRAY_JOIN_STRONLY)) || hash_key->nKeyLength) {
128 if ((flags & ARRAY_JOIN_PRETTIFY) && hash_key->nKeyLength) {
129 key = php_http_pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 1);
130 zend_hash_find(dst, key, hash_key->nKeyLength, (void *) &data);
131 } else {
132 zend_hash_quick_find(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) &data);
133 }
134
135 Z_ADDREF_P(*value);
136 if (data) {
137 if (Z_TYPE_PP(data) != IS_ARRAY) {
138 convert_to_array(*data);
139 }
140 add_next_index_zval(*data, *value);
141 } else if (key) {
142 zend_symtable_update(dst, key, hash_key->nKeyLength, value, sizeof(zval *), NULL);
143 } else {
144 zend_hash_quick_add(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, value, sizeof(zval *), NULL);
145 }
146
147 if (key) {
148 efree(key);
149 }
150 }
151
152 return ZEND_HASH_APPLY_KEEP;
153 }
154
155 int php_http_array_apply_merge_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
156 {
157 int flags;
158 char *key = NULL;
159 HashTable *dst;
160 zval **value = (zval **) pDest;
161
162 dst = va_arg(args, HashTable *);
163 flags = va_arg(args, int);
164
165 if ((!(flags & ARRAY_JOIN_STRONLY)) || hash_key->nKeyLength) {
166 Z_ADDREF_P(*value);
167 if ((flags & ARRAY_JOIN_PRETTIFY) && hash_key->nKeyLength) {
168 key = php_http_pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 1);
169 zend_hash_update(dst, key, hash_key->nKeyLength, (void *) value, sizeof(zval *), NULL);
170 efree(key);
171 } else {
172 zend_hash_quick_update(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) value, sizeof(zval *), NULL);
173 }
174 }
175
176 return ZEND_HASH_APPLY_KEEP;
177 }
178
179 /* PASS CALLBACK */
180
181 PHP_HTTP_API size_t php_http_pass_wrapper(php_http_pass_callback_arg_t *cb, const char *str, size_t len)
182 {
183 TSRMLS_FETCH();
184 return cb->cb_zts(cb->cb_arg, str, len TSRMLS_CC);
185 }
186
187 /* ERROR */
188
189 static inline int scope_error_handling(long type TSRMLS_DC)
190 {
191 if ((type == E_THROW) || (EG(error_handling) == EH_THROW)) {
192 return EH_THROW;
193 }
194
195 if (EG(This) && instanceof_function(Z_OBJCE_P(EG(This)), php_http_object_class_entry TSRMLS_CC)) {
196 return php_http_object_get_error_handling(EG(This) TSRMLS_CC);
197 }
198
199 return EH_NORMAL;
200 }
201
202 void php_http_error(long type TSRMLS_DC, long code, const char *format, ...)
203 {
204 va_list args;
205
206 va_start(args, format);
207 switch (scope_error_handling(type TSRMLS_CC)) {
208 case EH_THROW: {
209 char *message;
210 zend_class_entry *ce = php_http_exception_class_entry;
211
212 if (0&& EG(exception_class) && instanceof_function(EG(exception_class), php_http_exception_class_entry TSRMLS_CC)) {
213 ce = EG(exception_class);
214 }
215
216 vspprintf(&message, 0, format, args);
217 zend_throw_exception(ce, message, code TSRMLS_CC);
218 efree(message);
219 break;
220 }
221 case EH_NORMAL:
222 php_verror(NULL, "", type, format, args TSRMLS_CC);
223 break;
224 case EH_SUPPRESS:
225 break;
226 }
227 va_end(args);
228 }
229
230 /*
231 * Local variables:
232 * tab-width: 4
233 * c-basic-offset: 4
234 * End:
235 * vim600: noet sw=4 ts=4 fdm=marker
236 * vim<600: noet sw=4 ts=4
237 */