summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--include/opkele/util.h10
-rw-r--r--lib/util.cc28
2 files changed, 37 insertions, 1 deletions
diff --git a/include/opkele/util.h b/include/opkele/util.h
index fd974a1..0130bff 100644
--- a/include/opkele/util.h
+++ b/include/opkele/util.h
@@ -1,89 +1,97 @@
1#ifndef __OPKELE_UTIL_H 1#ifndef __OPKELE_UTIL_H
2#define __OPKELE_UTIL_H 2#define __OPKELE_UTIL_H
3 3
4#include <time.h> 4#include <time.h>
5#include <string> 5#include <string>
6#include <vector> 6#include <vector>
7#include <opkele/types.h> 7#include <opkele/types.h>
8 8
9namespace opkele { 9namespace opkele {
10 using std::string; 10 using std::string;
11 using std::vector; 11 using std::vector;
12 12
13 /** 13 /**
14 * @brief opkele utils namespace 14 * @brief opkele utils namespace
15 */ 15 */
16 namespace util { 16 namespace util {
17 17
18 /** 18 /**
19 * Convert internal time representation to w3c format 19 * Convert internal time representation to w3c format
20 * @param t internal representation 20 * @param t internal representation
21 * @return w3c time 21 * @return w3c time
22 * @throw failed_conversion in case of error 22 * @throw failed_conversion in case of error
23 */ 23 */
24 string time_to_w3c(time_t t); 24 string time_to_w3c(time_t t);
25 /** 25 /**
26 * Convert W3C time representation to internal time_t 26 * Convert W3C time representation to internal time_t
27 * @param w w3c representation 27 * @param w w3c representation
28 * @return converted time 28 * @return converted time
29 * @throw failed_conversion in case of error 29 * @throw failed_conversion in case of error
30 */ 30 */
31 time_t w3c_to_time(const string& w); 31 time_t w3c_to_time(const string& w);
32 32
33 /** 33 /**
34 * Encode string to the representation suitable for using in URL. 34 * Encode string to the representation suitable for using in URL
35 * @param str string to encode 35 * @param str string to encode
36 * @return encoded string 36 * @return encoded string
37 * @throw failed_conversion in case of failure 37 * @throw failed_conversion in case of failure
38 */ 38 */
39 string url_encode(const string& str); 39 string url_encode(const string& str);
40 40
41 /** 41 /**
42 * Decode url-encoded string back to normal
43 * @param str url-encoded string
44 * @return decoded string
45 * @throw failed_conversion in case of failure
46 */
47 string url_decode(const string& str);
48
49 /**
42 * Make string suitable for using as x(ht)ml attribute. 50 * Make string suitable for using as x(ht)ml attribute.
43 * @param str string to escape 51 * @param str string to escape
44 * @return escaped string 52 * @return escaped string
45 */ 53 */
46 string attr_escape(const string& str); 54 string attr_escape(const string& str);
47 55
48 /** 56 /**
49 * Convert number to string 57 * Convert number to string
50 * @param l number 58 * @param l number
51 * @return string representation 59 * @return string representation
52 * @throw failed_conversion in case of failure 60 * @throw failed_conversion in case of failure
53 */ 61 */
54 string long_to_string(long l); 62 string long_to_string(long l);
55 /** 63 /**
56 * Convert string to number 64 * Convert string to number
57 * @param s string, containing the number 65 * @param s string, containing the number
58 * @return the number 66 * @return the number
59 * @throw failed_conversion in case of failure 67 * @throw failed_conversion in case of failure
60 */ 68 */
61 long string_to_long(const string& s); 69 long string_to_long(const string& s);
62 70
63 /** 71 /**
64 * Encode binary data using base64. 72 * Encode binary data using base64.
65 * @param data pointer to binary data 73 * @param data pointer to binary data
66 * @param length length of data 74 * @param length length of data
67 * @return encoded data 75 * @return encoded data
68 */ 76 */
69 string encode_base64(const void *data,size_t length); 77 string encode_base64(const void *data,size_t length);
70 /** 78 /**
71 * Decode binary data from base64 representation. 79 * Decode binary data from base64 representation.
72 * @param data base64-encoded data 80 * @param data base64-encoded data
73 * @param rv container for decoded binary 81 * @param rv container for decoded binary
74 */ 82 */
75 void decode_base64(const string& data,vector<unsigned char>& rv); 83 void decode_base64(const string& data,vector<unsigned char>& rv);
76 84
77 /** 85 /**
78 * Normalize http(s) URI according to RFC3986, section 6. URI is 86 * Normalize http(s) URI according to RFC3986, section 6. URI is
79 * expected to have scheme: in front of it. 87 * expected to have scheme: in front of it.
80 * @param uri URI 88 * @param uri URI
81 * @return normalized URI 89 * @return normalized URI
82 * @throw not_implemented in case of non-httpi(s) URI 90 * @throw not_implemented in case of non-httpi(s) URI
83 * @throw bad_input in case of malformed URI 91 * @throw bad_input in case of malformed URI
84 */ 92 */
85 string rfc_3986_normalize_uri(const string& uri); 93 string rfc_3986_normalize_uri(const string& uri);
86 94
87 string normalize_identifier(const string& usi,bool strip_fragment); 95 string normalize_identifier(const string& usi,bool strip_fragment);
88 96
89 /** 97 /**
diff --git a/lib/util.cc b/lib/util.cc
index a6e08e2..3e7f3aa 100644
--- a/lib/util.cc
+++ b/lib/util.cc
@@ -147,96 +147,124 @@ namespace opkele {
147 tm_t.tm_year-=1900; 147 tm_t.tm_year-=1900;
148 time_t rv = mktime(&tm_t); 148 time_t rv = mktime(&tm_t);
149 if(rv==(time_t)-1) 149 if(rv==(time_t)-1)
150 throw failed_conversion(OPKELE_CP_ "failed to mktime()"); 150 throw failed_conversion(OPKELE_CP_ "failed to mktime()");
151 return rv-timezone; 151 return rv-timezone;
152 } 152 }
153 153
154 /* 154 /*
155 * 155 *
156 */ 156 */
157 157
158 static inline bool isrfc3986unreserved(int c) { 158 static inline bool isrfc3986unreserved(int c) {
159 if(c<'-') return false; 159 if(c<'-') return false;
160 if(c<='.') return true; 160 if(c<='.') return true;
161 if(c<'0') return false; if(c<='9') return true; 161 if(c<'0') return false; if(c<='9') return true;
162 if(c<'A') return false; if(c<='Z') return true; 162 if(c<'A') return false; if(c<='Z') return true;
163 if(c<'_') return false; 163 if(c<'_') return false;
164 if(c=='_') return true; 164 if(c=='_') return true;
165 if(c<'a') return false; if(c<='z') return true; 165 if(c<'a') return false; if(c<='z') return true;
166 if(c=='~') return true; 166 if(c=='~') return true;
167 return false; 167 return false;
168 } 168 }
169 169
170 struct __url_encoder : public unary_function<char,void> { 170 struct __url_encoder : public unary_function<char,void> {
171 public: 171 public:
172 string& rv; 172 string& rv;
173 173
174 __url_encoder(string& r) : rv(r) { } 174 __url_encoder(string& r) : rv(r) { }
175 175
176 result_type operator()(argument_type c) { 176 result_type operator()(argument_type c) {
177 if(isrfc3986unreserved(c)) 177 if(isrfc3986unreserved(c))
178 rv += c; 178 rv += c;
179 else{ 179 else{
180 char tmp[4]; 180 char tmp[4];
181 snprintf(tmp,sizeof(tmp),"%%%02X", 181 snprintf(tmp,sizeof(tmp),"%%%02X",
182 (c&0xff)); 182 (c&0xff));
183 rv += tmp; 183 rv += tmp;
184 } 184 }
185 } 185 }
186 }; 186 };
187 187
188 string url_encode(const string& str) { 188 string url_encode(const string& str) {
189 string rv; 189 string rv;
190 for_each(str.begin(),str.end(), 190 for_each(str.begin(),str.end(),
191 __url_encoder(rv)); 191 __url_encoder(rv));
192 return rv; 192 return rv;
193 } 193 }
194 194
195 string url_decode(const string& str) {
196 string rv;
197 back_insert_iterator<string> ii(rv);
198 for(string::const_iterator i=str.begin(),ie=str.end();
199 i!=ie;++i) {
200 switch(*i) {
201 case '+':
202 *(ii++) = ' '; break;
203 case '%':
204 ++i;
205 static char tmp[3] = {0,0,0};
206 if(i==ie)
207 throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string");
208 tmp[0] == *(i++);
209 if(i==ie)
210 throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string");
211 tmp[1] == *i;
212 if(!(isxdigit(tmp[0]) && isxdigit(tmp[1])))
213 throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string");
214 *(ii++) = strtol(tmp,0,16);
215 break;
216 default:
217 *(ii++) = *i; break;
218 }
219 }
220 return rv;
221 }
222
195 string attr_escape(const string& str) { 223 string attr_escape(const string& str) {
196 static const char *unsafechars = "<>&\n\"'"; 224 static const char *unsafechars = "<>&\n\"'";
197 string rv; 225 string rv;
198 string::size_type p=0; 226 string::size_type p=0;
199 while(true) { 227 while(true) {
200 string::size_type us = str.find_first_of(unsafechars,p); 228 string::size_type us = str.find_first_of(unsafechars,p);
201 if(us==string::npos) { 229 if(us==string::npos) {
202 if(p!=str.length()) 230 if(p!=str.length())
203 rv.append(str,p,str.length()-p); 231 rv.append(str,p,str.length()-p);
204 return rv; 232 return rv;
205 } 233 }
206 rv.append(str,p,us-p); 234 rv.append(str,p,us-p);
207 rv += "&#"; 235 rv += "&#";
208 rv += long_to_string((long)str[us]); 236 rv += long_to_string((long)str[us]);
209 rv += ';'; 237 rv += ';';
210 p = us+1; 238 p = us+1;
211 } 239 }
212 } 240 }
213 241
214 string long_to_string(long l) { 242 string long_to_string(long l) {
215 char rv[32]; 243 char rv[32];
216 int r=snprintf(rv,sizeof(rv),"%ld",l); 244 int r=snprintf(rv,sizeof(rv),"%ld",l);
217 if(r<0 || r>=(int)sizeof(rv)) 245 if(r<0 || r>=(int)sizeof(rv))
218 throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); 246 throw failed_conversion(OPKELE_CP_ "failed to snprintf()");
219 return rv; 247 return rv;
220 } 248 }
221 249
222 long string_to_long(const string& s) { 250 long string_to_long(const string& s) {
223 char *endptr = 0; 251 char *endptr = 0;
224 long rv = strtol(s.c_str(),&endptr,10); 252 long rv = strtol(s.c_str(),&endptr,10);
225 if((!endptr) || endptr==s.c_str()) 253 if((!endptr) || endptr==s.c_str())
226 throw failed_conversion(OPKELE_CP_ "failed to strtol()"); 254 throw failed_conversion(OPKELE_CP_ "failed to strtol()");
227 return rv; 255 return rv;
228 } 256 }
229 257
230 /* 258 /*
231 * Normalize URL according to the rules, described in rfc 3986, section 6 259 * Normalize URL according to the rules, described in rfc 3986, section 6
232 * 260 *
233 * - uppercase hex triplets (e.g. %ab -> %AB) 261 * - uppercase hex triplets (e.g. %ab -> %AB)
234 * - lowercase scheme and host 262 * - lowercase scheme and host
235 * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, 263 * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3,
236 * that is - [:alpha:][:digit:]._~- 264 * that is - [:alpha:][:digit:]._~-
237 * - remove dot segments 265 * - remove dot segments
238 * - remove empty and default ports 266 * - remove empty and default ports
239 * - if there's no path component, add '/' 267 * - if there's no path component, add '/'
240 */ 268 */
241 string rfc_3986_normalize_uri(const string& uri) { 269 string rfc_3986_normalize_uri(const string& uri) {
242 string rv; 270 string rv;