summaryrefslogtreecommitdiffabout
path: root/lib
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) (side-by-side diff)
tree7d929285bc296777c63d4f482c7bb07f8541bce2 /lib
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') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/data.cc3
-rw-r--r--lib/discovery.cc17
-rw-r--r--lib/util.cc41
3 files changed, 49 insertions, 12 deletions
diff --git a/lib/data.cc b/lib/data.cc
index c040430..f71788f 100644
--- a/lib/data.cc
+++ b/lib/data.cc
@@ -9,2 +9,5 @@ namespace opkele {
+ const char *_iname_leaders = "=@+$!(";
+ const char *_whitespace_chars = " \t\r\n";
+
}
diff --git a/lib/discovery.cc b/lib/discovery.cc
index b7f2db6..5913ad4 100644
--- a/lib/discovery.cc
+++ b/lib/discovery.cc
@@ -8,2 +8,3 @@
#include <opkele/tidy.h>
+#include <opkele/data.h>
#include <opkele/debug.h>
@@ -23,4 +24,2 @@ namespace opkele {
- static const char *whitespace = " \t\r\n";
- static const char *i_leaders = "=@+$!(";
static const size_t max_html = 16384;
@@ -130,6 +129,6 @@ namespace opkele {
idiscovery_t idis;
- string::size_type fsc = identity.find_first_not_of(whitespace);
+ string::size_type fsc = identity.find_first_not_of(data::_whitespace_chars);
if(fsc==string::npos)
throw bad_input(OPKELE_CP_ "whitespace-only identity");
- string::size_type lsc = identity.find_last_not_of(whitespace);
+ string::size_type lsc = identity.find_last_not_of(data::_whitespace_chars);
assert(lsc!=string::npos);
@@ -141,3 +140,3 @@ namespace opkele {
idis.clear();
- if(strchr(i_leaders,id[0])) {
+ if(strchr(data::_iname_leaders,id[0])) {
/* TODO: further normalize xri identity? Like folding case
@@ -497,3 +496,3 @@ namespace opkele {
href.assign(ns);
- string::size_type lns=href.find_last_not_of(whitespace);
+ string::size_type lns=href.find_last_not_of(data::_whitespace_chars);
href.erase(lns+1);
@@ -501,5 +500,5 @@ namespace opkele {
}
- for(string::size_type ns=rels.find_first_not_of(whitespace);
- ns!=string::npos; ns=rels.find_first_not_of(whitespace,ns)) {
- string::size_type s = rels.find_first_of(whitespace,ns);
+ for(string::size_type ns=rels.find_first_not_of(data::_whitespace_chars);
+ ns!=string::npos; ns=rels.find_first_not_of(data::_whitespace_chars,ns)) {
+ string::size_type s = rels.find_first_of(data::_whitespace_chars,ns);
string rel;
diff --git a/lib/util.cc b/lib/util.cc
index bb8a2e8..29e6738 100644
--- a/lib/util.cc
+++ b/lib/util.cc
@@ -15,2 +15,3 @@
#include <opkele/exception.h>
+#include <opkele/data.h>
#include <opkele/debug.h>
@@ -213,5 +214,4 @@ namespace opkele {
string rfc_3986_normalize_uri(const string& uri) {
- static const char *whitespace = " \t\r\n";
string rv;
- string::size_type ns = uri.find_first_not_of(whitespace);
+ string::size_type ns = uri.find_first_not_of(data::_whitespace_chars);
if(ns==string::npos)
@@ -225,3 +225,3 @@ namespace opkele {
bool s;
- string::size_type ul = uri.find_last_not_of(whitespace)+1;
+ string::size_type ul = uri.find_last_not_of(data::_whitespace_chars)+1;
if(ul <= (colon+3))
@@ -432,2 +432,37 @@ namespace opkele {
+ string normalize_identifier(const string& usi,bool strip_fragment) {
+ if(usi.empty())
+ return usi;
+ string rv;
+ string::size_type fsc = usi.find_first_not_of(data::_whitespace_chars);
+ if(fsc==string::npos)
+ return rv;
+ string::size_type lsc = usi.find_last_not_of(data::_whitespace_chars);
+ assert(lsc!=string::npos);
+ if(!strncasecmp(usi.c_str()+fsc,"xri://",sizeof("xri://")-1))
+ fsc += sizeof("xri://")-1;
+ if( (fsc+1) >= lsc )
+ return rv;
+ rv.assign(usi,fsc,lsc-fsc+1);
+ if(strchr(data::_iname_leaders,rv[0])) {
+ /* TODO: further normalize xri identity, fold case or
+ * whatever... */
+ }else{
+ if(rv.find("://")==string::npos)
+ rv.insert(0,"http://");
+ if(strip_fragment) {
+ string::size_type fp = rv.find('#');
+ if(fp!=string::npos) {
+ string::size_type qp = rv.find('?');
+ if(qp==string::npos || qp<fp)
+ rv.erase(fp);
+ else if(qp>fp)
+ rv.erase(fp,qp-fp);
+ }
+ }
+ rv = rfc_3986_normalize_uri(rv);
+ }
+ return rv;
+ }
+
}