summaryrefslogtreecommitdiffabout
path: root/lib/util.cc
Unidiff
Diffstat (limited to 'lib/util.cc') (more/less context) (show whitespace changes)
-rw-r--r--lib/util.cc2
1 files changed, 1 insertions, 1 deletions
diff --git a/lib/util.cc b/lib/util.cc
index a46ba2a..249eeed 100644
--- a/lib/util.cc
+++ b/lib/util.cc
@@ -117,200 +117,200 @@ namespace opkele {
117 if(!gmtime_r(&t,&tm_t)) 117 if(!gmtime_r(&t,&tm_t))
118 throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); 118 throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()");
119 char rv[25]; 119 char rv[25];
120 if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) 120 if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t))
121 throw failed_conversion(OPKELE_CP_ "failed to strftime()"); 121 throw failed_conversion(OPKELE_CP_ "failed to strftime()");
122 return rv; 122 return rv;
123 } 123 }
124 124
125#ifndef HAVE_TIMEGM 125#ifndef HAVE_TIMEGM
126 static time_t timegm(struct tm *t) { 126 static time_t timegm(struct tm *t) {
127 char *tz = getenv("TZ"); 127 char *tz = getenv("TZ");
128 setenv("TZ","",1); tzset(); 128 setenv("TZ","",1); tzset();
129 time_t rv = mktime(t); 129 time_t rv = mktime(t);
130 if(tz) 130 if(tz)
131 setenv("TZ",tz,1); 131 setenv("TZ",tz,1);
132 else 132 else
133 unsetenv("TZ"); 133 unsetenv("TZ");
134 tzset(); 134 tzset();
135 return rv; 135 return rv;
136 } 136 }
137 #define timegm opkele::util::timegm 137 #define timegm opkele::util::timegm
138#endif /* HAVE_TIMEGM */ 138#endif /* HAVE_TIMEGM */
139 139
140 time_t w3c_to_time(const string& w) { 140 time_t w3c_to_time(const string& w) {
141 int fraction; 141 int fraction;
142 struct tm tm_t; 142 struct tm tm_t;
143 memset(&tm_t,0,sizeof(tm_t)); 143 memset(&tm_t,0,sizeof(tm_t));
144 if( ( 144 if( (
145 sscanf( 145 sscanf(
146 w.c_str(), 146 w.c_str(),
147 "%04d-%02d-%02dT%02d:%02d:%02dZ", 147 "%04d-%02d-%02dT%02d:%02d:%02dZ",
148 &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, 148 &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday,
149 &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec 149 &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec
150 ) != 6 150 ) != 6
151 ) && ( 151 ) && (
152 sscanf( 152 sscanf(
153 w.c_str(), 153 w.c_str(),
154 "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", 154 "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
155 &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, 155 &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday,
156 &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, 156 &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec,
157 &fraction 157 &fraction
158 ) != 7 158 ) != 7
159 ) ) 159 ) )
160 throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); 160 throw failed_conversion(OPKELE_CP_ "failed to sscanf()");
161 tm_t.tm_mon--; 161 tm_t.tm_mon--;
162 tm_t.tm_year-=1900; 162 tm_t.tm_year-=1900;
163 time_t rv = timegm(&tm_t); 163 time_t rv = timegm(&tm_t);
164 if(rv==(time_t)-1) 164 if(rv==(time_t)-1)
165 throw failed_conversion(OPKELE_CP_ "failed to gmtime()"); 165 throw failed_conversion(OPKELE_CP_ "failed to gmtime()");
166 return rv; 166 return rv;
167 } 167 }
168 168
169 /* 169 /*
170 * 170 *
171 */ 171 */
172 172
173 static inline bool isrfc3986unreserved(int c) { 173 static inline bool isrfc3986unreserved(int c) {
174 if(c<'-') return false; 174 if(c<'-') return false;
175 if(c<='.') return true; 175 if(c<='.') return true;
176 if(c<'0') return false; if(c<='9') return true; 176 if(c<'0') return false; if(c<='9') return true;
177 if(c<'A') return false; if(c<='Z') return true; 177 if(c<'A') return false; if(c<='Z') return true;
178 if(c<'_') return false; 178 if(c<'_') return false;
179 if(c=='_') return true; 179 if(c=='_') return true;
180 if(c<'a') return false; if(c<='z') return true; 180 if(c<'a') return false; if(c<='z') return true;
181 if(c=='~') return true; 181 if(c=='~') return true;
182 return false; 182 return false;
183 } 183 }
184 184
185 struct __url_encoder : public unary_function<char,void> { 185 struct __url_encoder : public unary_function<char,void> {
186 public: 186 public:
187 string& rv; 187 string& rv;
188 188
189 __url_encoder(string& r) : rv(r) { } 189 __url_encoder(string& r) : rv(r) { }
190 190
191 result_type operator()(argument_type c) { 191 result_type operator()(argument_type c) {
192 if(isrfc3986unreserved(c)) 192 if(isrfc3986unreserved(c))
193 rv += c; 193 rv += c;
194 else{ 194 else{
195 char tmp[4]; 195 char tmp[4];
196 snprintf(tmp,sizeof(tmp),"%%%02X", 196 snprintf(tmp,sizeof(tmp),"%%%02X",
197 (c&0xff)); 197 (c&0xff));
198 rv += tmp; 198 rv += tmp;
199 } 199 }
200 } 200 }
201 }; 201 };
202 202
203 string url_encode(const string& str) { 203 string url_encode(const string& str) {
204 string rv; 204 string rv;
205 for_each(str.begin(),str.end(), 205 for_each(str.begin(),str.end(),
206 __url_encoder(rv)); 206 __url_encoder(rv));
207 return rv; 207 return rv;
208 } 208 }
209 209
210 string url_decode(const string& str) { 210 string url_decode(const string& str) {
211 string rv; 211 string rv;
212 back_insert_iterator<string> ii(rv); 212 back_insert_iterator<string> ii(rv);
213 char tmp[3]; tmp[2] = 0;
213 for(string::const_iterator i=str.begin(),ie=str.end(); 214 for(string::const_iterator i=str.begin(),ie=str.end();
214 i!=ie;++i) { 215 i!=ie;++i) {
215 switch(*i) { 216 switch(*i) {
216 case '+': 217 case '+':
217 *(ii++) = ' '; break; 218 *(ii++) = ' '; break;
218 case '%': 219 case '%':
219 ++i; 220 ++i;
220 static char tmp[3] = {0,0,0};
221 if(i==ie) 221 if(i==ie)
222 throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string"); 222 throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string");
223 tmp[0] = *(i++); 223 tmp[0] = *(i++);
224 if(i==ie) 224 if(i==ie)
225 throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string"); 225 throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string");
226 tmp[1] = *i; 226 tmp[1] = *i;
227 if(!(isxdigit(tmp[0]) && isxdigit(tmp[1]))) 227 if(!(isxdigit(tmp[0]) && isxdigit(tmp[1])))
228 throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string"); 228 throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string");
229 *(ii++) = (char)strtol(tmp,0,16); 229 *(ii++) = (char)strtol(tmp,0,16);
230 break; 230 break;
231 default: 231 default:
232 *(ii++) = *i; break; 232 *(ii++) = *i; break;
233 } 233 }
234 } 234 }
235 return rv; 235 return rv;
236 } 236 }
237 237
238 string attr_escape(const string& str) { 238 string attr_escape(const string& str) {
239 static const char *unsafechars = "<>&\n\"'"; 239 static const char *unsafechars = "<>&\n\"'";
240 string rv; 240 string rv;
241 string::size_type p=0; 241 string::size_type p=0;
242 while(true) { 242 while(true) {
243 string::size_type us = str.find_first_of(unsafechars,p); 243 string::size_type us = str.find_first_of(unsafechars,p);
244 if(us==string::npos) { 244 if(us==string::npos) {
245 if(p!=str.length()) 245 if(p!=str.length())
246 rv.append(str,p,str.length()-p); 246 rv.append(str,p,str.length()-p);
247 return rv; 247 return rv;
248 } 248 }
249 rv.append(str,p,us-p); 249 rv.append(str,p,us-p);
250 rv += "&#"; 250 rv += "&#";
251 rv += long_to_string((long)str[us]); 251 rv += long_to_string((long)str[us]);
252 rv += ';'; 252 rv += ';';
253 p = us+1; 253 p = us+1;
254 } 254 }
255 } 255 }
256 256
257 string long_to_string(long l) { 257 string long_to_string(long l) {
258 char rv[32]; 258 char rv[32];
259 int r=snprintf(rv,sizeof(rv),"%ld",l); 259 int r=snprintf(rv,sizeof(rv),"%ld",l);
260 if(r<0 || r>=(int)sizeof(rv)) 260 if(r<0 || r>=(int)sizeof(rv))
261 throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); 261 throw failed_conversion(OPKELE_CP_ "failed to snprintf()");
262 return rv; 262 return rv;
263 } 263 }
264 264
265 long string_to_long(const string& s) { 265 long string_to_long(const string& s) {
266 char *endptr = 0; 266 char *endptr = 0;
267 long rv = strtol(s.c_str(),&endptr,10); 267 long rv = strtol(s.c_str(),&endptr,10);
268 if((!endptr) || endptr==s.c_str()) 268 if((!endptr) || endptr==s.c_str())
269 throw failed_conversion(OPKELE_CP_ "failed to strtol()"); 269 throw failed_conversion(OPKELE_CP_ "failed to strtol()");
270 return rv; 270 return rv;
271 } 271 }
272 272
273 /* 273 /*
274 * Normalize URL according to the rules, described in rfc 3986, section 6 274 * Normalize URL according to the rules, described in rfc 3986, section 6
275 * 275 *
276 * - uppercase hex triplets (e.g. %ab -> %AB) 276 * - uppercase hex triplets (e.g. %ab -> %AB)
277 * - lowercase scheme and host 277 * - lowercase scheme and host
278 * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, 278 * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3,
279 * that is - [:alpha:][:digit:]._~- 279 * that is - [:alpha:][:digit:]._~-
280 * - remove dot segments 280 * - remove dot segments
281 * - remove empty and default ports 281 * - remove empty and default ports
282 * - if there's no path component, add '/' 282 * - if there's no path component, add '/'
283 */ 283 */
284 string rfc_3986_normalize_uri(const string& uri) { 284 string rfc_3986_normalize_uri(const string& uri) {
285 string rv; 285 string rv;
286 string::size_type ns = uri.find_first_not_of(data::_whitespace_chars); 286 string::size_type ns = uri.find_first_not_of(data::_whitespace_chars);
287 if(ns==string::npos) 287 if(ns==string::npos)
288 throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); 288 throw bad_input(OPKELE_CP_ "Can't normalize empty URI");
289 string::size_type colon = uri.find(':',ns); 289 string::size_type colon = uri.find(':',ns);
290 if(colon==string::npos) 290 if(colon==string::npos)
291 throw bad_input(OPKELE_CP_ "No scheme specified in URI"); 291 throw bad_input(OPKELE_CP_ "No scheme specified in URI");
292 transform( 292 transform(
293 uri.begin()+ns, uri.begin()+colon+1, 293 uri.begin()+ns, uri.begin()+colon+1,
294 back_inserter(rv), ::tolower ); 294 back_inserter(rv), ::tolower );
295 bool s; 295 bool s;
296 string::size_type ul = uri.find_last_not_of(data::_whitespace_chars)+1; 296 string::size_type ul = uri.find_last_not_of(data::_whitespace_chars)+1;
297 if(ul <= (colon+3)) 297 if(ul <= (colon+3))
298 throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); 298 throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered");
299 if(uri[colon+1]!='/' || uri[colon+2]!='/') 299 if(uri[colon+1]!='/' || uri[colon+2]!='/')
300 throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); 300 throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component");
301 if(rv=="http:") 301 if(rv=="http:")
302 s = false; 302 s = false;
303 else if(rv=="https:") 303 else if(rv=="https:")
304 s = true; 304 s = true;
305 else{ 305 else{
306 /* TODO: support more schemes. e.g. xri. How do we normalize 306 /* TODO: support more schemes. e.g. xri. How do we normalize
307 * xri? 307 * xri?
308 */ 308 */
309 rv.append(uri,colon+1,ul-colon-1); 309 rv.append(uri,colon+1,ul-colon-1);
310 return rv; 310 return rv;
311 } 311 }
312 rv += "//"; 312 rv += "//";
313 string::size_type interesting = uri.find_first_of(":/#?",colon+3); 313 string::size_type interesting = uri.find_first_of(":/#?",colon+3);
314 if(interesting==string::npos) { 314 if(interesting==string::npos) {
315 transform( 315 transform(
316 uri.begin()+colon+3,uri.begin()+ul, 316 uri.begin()+colon+3,uri.begin()+ul,