summaryrefslogtreecommitdiffabout
path: root/lib/util.cc
authorMichael Krelin <hacker@klever.net>2008-02-19 23:48:32 (UTC)
committer Michael Krelin <hacker@klever.net>2008-02-19 23:48:32 (UTC)
commitdaf2d4bcb4a31df6b46d3da7a33ee3f98d85e464 (patch) (unidiff)
tree7d929285bc296777c63d4f482c7bb07f8541bce2 /lib/util.cc
parent42e4fb613d190508b3e8b8993d233044eeea4d20 (diff)
downloadlibopkele-daf2d4bcb4a31df6b46d3da7a33ee3f98d85e464.zip
libopkele-daf2d4bcb4a31df6b46d3da7a33ee3f98d85e464.tar.gz
libopkele-daf2d4bcb4a31df6b46d3da7a33ee3f98d85e464.tar.bz2
added an identifier normalization utility function
* moved iname leader characters and whitespace characters strings to opkele::data namespace * added opkele::util::normalize_identifier() function Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (limited to 'lib/util.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/util.cc41
1 files changed, 38 insertions, 3 deletions
diff --git a/lib/util.cc b/lib/util.cc
index bb8a2e8..29e6738 100644
--- a/lib/util.cc
+++ b/lib/util.cc
@@ -13,6 +13,7 @@
13#include <curl/curl.h> 13#include <curl/curl.h>
14#include <opkele/util.h> 14#include <opkele/util.h>
15#include <opkele/exception.h> 15#include <opkele/exception.h>
16#include <opkele/data.h>
16#include <opkele/debug.h> 17#include <opkele/debug.h>
17 18
18#include <config.h> 19#include <config.h>
@@ -211,9 +212,8 @@ namespace opkele {
211 * - if there's no path component, add '/' 212 * - if there's no path component, add '/'
212 */ 213 */
213 string rfc_3986_normalize_uri(const string& uri) { 214 string rfc_3986_normalize_uri(const string& uri) {
214 static const char *whitespace = " \t\r\n";
215 string rv; 215 string rv;
216 string::size_type ns = uri.find_first_not_of(whitespace); 216 string::size_type ns = uri.find_first_not_of(data::_whitespace_chars);
217 if(ns==string::npos) 217 if(ns==string::npos)
218 throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); 218 throw bad_input(OPKELE_CP_ "Can't normalize empty URI");
219 string::size_type colon = uri.find(':',ns); 219 string::size_type colon = uri.find(':',ns);
@@ -223,7 +223,7 @@ namespace opkele {
223 uri.begin()+ns, uri.begin()+colon+1, 223 uri.begin()+ns, uri.begin()+colon+1,
224 back_inserter(rv), ::tolower ); 224 back_inserter(rv), ::tolower );
225 bool s; 225 bool s;
226 string::size_type ul = uri.find_last_not_of(whitespace)+1; 226 string::size_type ul = uri.find_last_not_of(data::_whitespace_chars)+1;
227 if(ul <= (colon+3)) 227 if(ul <= (colon+3))
228 throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); 228 throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered");
229 if(uri[colon+1]!='/' || uri[colon+2]!='/') 229 if(uri[colon+1]!='/' || uri[colon+2]!='/')
@@ -430,6 +430,41 @@ namespace opkele {
430 return encode_base64(md,md_len); 430 return encode_base64(md,md_len);
431 } 431 }
432 432
433 string normalize_identifier(const string& usi,bool strip_fragment) {
434 if(usi.empty())
435 return usi;
436 string rv;
437 string::size_type fsc = usi.find_first_not_of(data::_whitespace_chars);
438 if(fsc==string::npos)
439 return rv;
440 string::size_type lsc = usi.find_last_not_of(data::_whitespace_chars);
441 assert(lsc!=string::npos);
442 if(!strncasecmp(usi.c_str()+fsc,"xri://",sizeof("xri://")-1))
443 fsc += sizeof("xri://")-1;
444 if( (fsc+1) >= lsc )
445 return rv;
446 rv.assign(usi,fsc,lsc-fsc+1);
447 if(strchr(data::_iname_leaders,rv[0])) {
448 /* TODO: further normalize xri identity, fold case or
449 * whatever... */
450 }else{
451 if(rv.find("://")==string::npos)
452 rv.insert(0,"http://");
453 if(strip_fragment) {
454 string::size_type fp = rv.find('#');
455 if(fp!=string::npos) {
456 string::size_type qp = rv.find('?');
457 if(qp==string::npos || qp<fp)
458 rv.erase(fp);
459 else if(qp>fp)
460 rv.erase(fp,qp-fp);
461 }
462 }
463 rv = rfc_3986_normalize_uri(rv);
464 }
465 return rv;
466 }
467
433 } 468 }
434 469
435} 470}