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