author | Michael Krelin <hacker@klever.net> | 2008-03-02 23:52:14 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-03-02 23:52:14 (UTC) |
commit | 5fd5ecad8c2bd1e8846c11fa9b281f0f4ab8a4a7 (patch) (unidiff) | |
tree | 76ba24c589d0ce7de6cab113787cf7a70572f994 | |
parent | f6482fb9003e4953f838ba4ef2c110190355622f (diff) | |
download | libopkele-5fd5ecad8c2bd1e8846c11fa9b281f0f4ab8a4a7.zip libopkele-5fd5ecad8c2bd1e8846c11fa9b281f0f4ab8a4a7.tar.gz libopkele-5fd5ecad8c2bd1e8846c11fa9b281f0f4ab8a4a7.tar.bz2 |
aded util::url_decode()
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | include/opkele/util.h | 10 | ||||
-rw-r--r-- | lib/util.cc | 28 |
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,116 +1,124 @@ | |||
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 | ||
9 | namespace opkele { | 9 | namespace 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 | /** |
90 | * Match URI against realm | 98 | * Match URI against realm |
91 | * @param uri URI to match | 99 | * @param uri URI to match |
92 | * @param realm realm to match against | 100 | * @param realm realm to match against |
93 | * @return true if URI matches realm | 101 | * @return true if URI matches realm |
94 | */ | 102 | */ |
95 | bool uri_matches_realm(const string& uri,const string& realm); | 103 | bool uri_matches_realm(const string& uri,const string& realm); |
96 | 104 | ||
97 | /** | 105 | /** |
98 | * Strip fragment part from URI | 106 | * Strip fragment part from URI |
99 | * @param uri input/output parameter containing the URI | 107 | * @param uri input/output parameter containing the URI |
100 | * @return reference to uri | 108 | * @return reference to uri |
101 | */ | 109 | */ |
102 | string& strip_uri_fragment_part(string& uri); | 110 | string& strip_uri_fragment_part(string& uri); |
103 | 111 | ||
104 | /** | 112 | /** |
105 | * Calculate signature and encode it using base64 | 113 | * Calculate signature and encode it using base64 |
106 | * @param assoc association being used for signing | 114 | * @param assoc association being used for signing |
107 | * @param om openid message | 115 | * @param om openid message |
108 | * @return base64 representation of the signature | 116 | * @return base64 representation of the signature |
109 | */ | 117 | */ |
110 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om); | 118 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om); |
111 | 119 | ||
112 | } | 120 | } |
113 | 121 | ||
114 | } | 122 | } |
115 | 123 | ||
116 | #endif /* __OPKELE_UTIL_H */ | 124 | #endif /* __OPKELE_UTIL_H */ |
diff --git a/lib/util.cc b/lib/util.cc index a6e08e2..3e7f3aa 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -1,497 +1,525 @@ | |||
1 | #include <errno.h> | 1 | #include <errno.h> |
2 | #include <cassert> | 2 | #include <cassert> |
3 | #include <cctype> | 3 | #include <cctype> |
4 | #include <cstring> | 4 | #include <cstring> |
5 | #include <vector> | 5 | #include <vector> |
6 | #include <string> | 6 | #include <string> |
7 | #include <stack> | 7 | #include <stack> |
8 | #include <algorithm> | 8 | #include <algorithm> |
9 | #include <openssl/bio.h> | 9 | #include <openssl/bio.h> |
10 | #include <openssl/evp.h> | 10 | #include <openssl/evp.h> |
11 | #include <openssl/sha.h> | 11 | #include <openssl/sha.h> |
12 | #include <openssl/hmac.h> | 12 | #include <openssl/hmac.h> |
13 | #include <opkele/util.h> | 13 | #include <opkele/util.h> |
14 | #include <opkele/exception.h> | 14 | #include <opkele/exception.h> |
15 | #include <opkele/data.h> | 15 | #include <opkele/data.h> |
16 | #include <opkele/debug.h> | 16 | #include <opkele/debug.h> |
17 | 17 | ||
18 | #include <config.h> | 18 | #include <config.h> |
19 | #ifdef HAVE_DEMANGLE | 19 | #ifdef HAVE_DEMANGLE |
20 | # include <cxxabi.h> | 20 | # include <cxxabi.h> |
21 | #endif | 21 | #endif |
22 | 22 | ||
23 | namespace opkele { | 23 | namespace opkele { |
24 | using namespace std; | 24 | using namespace std; |
25 | 25 | ||
26 | namespace util { | 26 | namespace util { |
27 | 27 | ||
28 | /* | 28 | /* |
29 | * base64 | 29 | * base64 |
30 | */ | 30 | */ |
31 | string encode_base64(const void *data,size_t length) { | 31 | string encode_base64(const void *data,size_t length) { |
32 | BIO *b64 = 0, *bmem = 0; | 32 | BIO *b64 = 0, *bmem = 0; |
33 | try { | 33 | try { |
34 | b64 = BIO_new(BIO_f_base64()); | 34 | b64 = BIO_new(BIO_f_base64()); |
35 | if(!b64) | 35 | if(!b64) |
36 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); | 36 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); |
37 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 37 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
38 | bmem = BIO_new(BIO_s_mem()); | 38 | bmem = BIO_new(BIO_s_mem()); |
39 | BIO_set_flags(b64,BIO_CLOSE); | 39 | BIO_set_flags(b64,BIO_CLOSE); |
40 | if(!bmem) | 40 | if(!bmem) |
41 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); | 41 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); |
42 | BIO_push(b64,bmem); | 42 | BIO_push(b64,bmem); |
43 | if(((size_t)BIO_write(b64,data,length))!=length) | 43 | if(((size_t)BIO_write(b64,data,length))!=length) |
44 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); | 44 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); |
45 | if(BIO_flush(b64)!=1) | 45 | if(BIO_flush(b64)!=1) |
46 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); | 46 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); |
47 | char *rvd; | 47 | char *rvd; |
48 | long rvl = BIO_get_mem_data(bmem,&rvd); | 48 | long rvl = BIO_get_mem_data(bmem,&rvd); |
49 | string rv(rvd,rvl); | 49 | string rv(rvd,rvl); |
50 | BIO_free_all(b64); | 50 | BIO_free_all(b64); |
51 | return rv; | 51 | return rv; |
52 | }catch(...) { | 52 | }catch(...) { |
53 | if(b64) BIO_free_all(b64); | 53 | if(b64) BIO_free_all(b64); |
54 | throw; | 54 | throw; |
55 | } | 55 | } |
56 | } | 56 | } |
57 | 57 | ||
58 | void decode_base64(const string& data,vector<unsigned char>& rv) { | 58 | void decode_base64(const string& data,vector<unsigned char>& rv) { |
59 | BIO *b64 = 0, *bmem = 0; | 59 | BIO *b64 = 0, *bmem = 0; |
60 | rv.clear(); | 60 | rv.clear(); |
61 | try { | 61 | try { |
62 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); | 62 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); |
63 | if(!bmem) | 63 | if(!bmem) |
64 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); | 64 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); |
65 | b64 = BIO_new(BIO_f_base64()); | 65 | b64 = BIO_new(BIO_f_base64()); |
66 | if(!b64) | 66 | if(!b64) |
67 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); | 67 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); |
68 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 68 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
69 | BIO_push(b64,bmem); | 69 | BIO_push(b64,bmem); |
70 | unsigned char tmp[512]; | 70 | unsigned char tmp[512]; |
71 | size_t rb = 0; | 71 | size_t rb = 0; |
72 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) | 72 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) |
73 | rv.insert(rv.end(),tmp,&tmp[rb]); | 73 | rv.insert(rv.end(),tmp,&tmp[rb]); |
74 | BIO_free_all(b64); | 74 | BIO_free_all(b64); |
75 | }catch(...) { | 75 | }catch(...) { |
76 | if(b64) BIO_free_all(b64); | 76 | if(b64) BIO_free_all(b64); |
77 | throw; | 77 | throw; |
78 | } | 78 | } |
79 | } | 79 | } |
80 | 80 | ||
81 | /* | 81 | /* |
82 | * big numerics | 82 | * big numerics |
83 | */ | 83 | */ |
84 | 84 | ||
85 | BIGNUM *base64_to_bignum(const string& b64) { | 85 | BIGNUM *base64_to_bignum(const string& b64) { |
86 | vector<unsigned char> bin; | 86 | vector<unsigned char> bin; |
87 | decode_base64(b64,bin); | 87 | decode_base64(b64,bin); |
88 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); | 88 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); |
89 | if(!rv) | 89 | if(!rv) |
90 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); | 90 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); |
91 | return rv; | 91 | return rv; |
92 | } | 92 | } |
93 | 93 | ||
94 | BIGNUM *dec_to_bignum(const string& dec) { | 94 | BIGNUM *dec_to_bignum(const string& dec) { |
95 | BIGNUM *rv = 0; | 95 | BIGNUM *rv = 0; |
96 | if(!BN_dec2bn(&rv,dec.c_str())) | 96 | if(!BN_dec2bn(&rv,dec.c_str())) |
97 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 97 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
98 | return rv; | 98 | return rv; |
99 | } | 99 | } |
100 | 100 | ||
101 | string bignum_to_base64(const BIGNUM *bn) { | 101 | string bignum_to_base64(const BIGNUM *bn) { |
102 | vector<unsigned char> bin(BN_num_bytes(bn)+1); | 102 | vector<unsigned char> bin(BN_num_bytes(bn)+1); |
103 | unsigned char *binptr = &(bin.front())+1; | 103 | unsigned char *binptr = &(bin.front())+1; |
104 | int l = BN_bn2bin(bn,binptr); | 104 | int l = BN_bn2bin(bn,binptr); |
105 | if(l && (*binptr)&0x80){ | 105 | if(l && (*binptr)&0x80){ |
106 | (*(--binptr)) = 0; ++l; | 106 | (*(--binptr)) = 0; ++l; |
107 | } | 107 | } |
108 | return encode_base64(binptr,l); | 108 | return encode_base64(binptr,l); |
109 | } | 109 | } |
110 | 110 | ||
111 | /* | 111 | /* |
112 | * w3c times | 112 | * w3c times |
113 | */ | 113 | */ |
114 | 114 | ||
115 | string time_to_w3c(time_t t) { | 115 | string time_to_w3c(time_t t) { |
116 | struct tm tm_t; | 116 | struct tm tm_t; |
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 | time_t w3c_to_time(const string& w) { | 125 | time_t w3c_to_time(const string& w) { |
126 | int fraction; | 126 | int fraction; |
127 | struct tm tm_t; | 127 | struct tm tm_t; |
128 | memset(&tm_t,0,sizeof(tm_t)); | 128 | memset(&tm_t,0,sizeof(tm_t)); |
129 | if( ( | 129 | if( ( |
130 | sscanf( | 130 | sscanf( |
131 | w.c_str(), | 131 | w.c_str(), |
132 | "%04d-%02d-%02dT%02d:%02d:%02dZ", | 132 | "%04d-%02d-%02dT%02d:%02d:%02dZ", |
133 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 133 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
134 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec | 134 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec |
135 | ) != 6 | 135 | ) != 6 |
136 | ) && ( | 136 | ) && ( |
137 | sscanf( | 137 | sscanf( |
138 | w.c_str(), | 138 | w.c_str(), |
139 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", | 139 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", |
140 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 140 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
141 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, | 141 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, |
142 | &fraction | 142 | &fraction |
143 | ) != 7 | 143 | ) != 7 |
144 | ) ) | 144 | ) ) |
145 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); | 145 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); |
146 | tm_t.tm_mon--; | 146 | tm_t.tm_mon--; |
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; |
243 | string::size_type ns = uri.find_first_not_of(data::_whitespace_chars); | 271 | string::size_type ns = uri.find_first_not_of(data::_whitespace_chars); |
244 | if(ns==string::npos) | 272 | if(ns==string::npos) |
245 | throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); | 273 | throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); |
246 | string::size_type colon = uri.find(':',ns); | 274 | string::size_type colon = uri.find(':',ns); |
247 | if(colon==string::npos) | 275 | if(colon==string::npos) |
248 | throw bad_input(OPKELE_CP_ "No scheme specified in URI"); | 276 | throw bad_input(OPKELE_CP_ "No scheme specified in URI"); |
249 | transform( | 277 | transform( |
250 | uri.begin()+ns, uri.begin()+colon+1, | 278 | uri.begin()+ns, uri.begin()+colon+1, |
251 | back_inserter(rv), ::tolower ); | 279 | back_inserter(rv), ::tolower ); |
252 | bool s; | 280 | bool s; |
253 | string::size_type ul = uri.find_last_not_of(data::_whitespace_chars)+1; | 281 | string::size_type ul = uri.find_last_not_of(data::_whitespace_chars)+1; |
254 | if(ul <= (colon+3)) | 282 | if(ul <= (colon+3)) |
255 | throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); | 283 | throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); |
256 | if(uri[colon+1]!='/' || uri[colon+2]!='/') | 284 | if(uri[colon+1]!='/' || uri[colon+2]!='/') |
257 | throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); | 285 | throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); |
258 | if(rv=="http:") | 286 | if(rv=="http:") |
259 | s = false; | 287 | s = false; |
260 | else if(rv=="https:") | 288 | else if(rv=="https:") |
261 | s = true; | 289 | s = true; |
262 | else{ | 290 | else{ |
263 | /* TODO: support more schemes. e.g. xri. How do we normalize | 291 | /* TODO: support more schemes. e.g. xri. How do we normalize |
264 | * xri? | 292 | * xri? |
265 | */ | 293 | */ |
266 | rv.append(uri,colon+1,ul-colon-1); | 294 | rv.append(uri,colon+1,ul-colon-1); |
267 | return rv; | 295 | return rv; |
268 | } | 296 | } |
269 | rv += "//"; | 297 | rv += "//"; |
270 | string::size_type interesting = uri.find_first_of(":/#?",colon+3); | 298 | string::size_type interesting = uri.find_first_of(":/#?",colon+3); |
271 | if(interesting==string::npos) { | 299 | if(interesting==string::npos) { |
272 | transform( | 300 | transform( |
273 | uri.begin()+colon+3,uri.begin()+ul, | 301 | uri.begin()+colon+3,uri.begin()+ul, |
274 | back_inserter(rv), ::tolower ); | 302 | back_inserter(rv), ::tolower ); |
275 | rv += '/'; return rv; | 303 | rv += '/'; return rv; |
276 | } | 304 | } |
277 | transform( | 305 | transform( |
278 | uri.begin()+colon+3,uri.begin()+interesting, | 306 | uri.begin()+colon+3,uri.begin()+interesting, |
279 | back_inserter(rv), ::tolower ); | 307 | back_inserter(rv), ::tolower ); |
280 | bool qf = false; | 308 | bool qf = false; |
281 | char ic = uri[interesting]; | 309 | char ic = uri[interesting]; |
282 | if(ic==':') { | 310 | if(ic==':') { |
283 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); | 311 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); |
284 | const char *nptr = uri.data()+interesting+1; | 312 | const char *nptr = uri.data()+interesting+1; |
285 | char *eptr = 0; | 313 | char *eptr = 0; |
286 | long port = strtol(nptr,&eptr,10); | 314 | long port = strtol(nptr,&eptr,10); |
287 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { | 315 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { |
288 | char tmp[8]; | 316 | char tmp[8]; |
289 | snprintf(tmp,sizeof(tmp),":%ld",port); | 317 | snprintf(tmp,sizeof(tmp),":%ld",port); |
290 | rv += tmp; | 318 | rv += tmp; |
291 | } | 319 | } |
292 | if(ni==string::npos) { | 320 | if(ni==string::npos) { |
293 | rv += '/'; return rv; | 321 | rv += '/'; return rv; |
294 | } | 322 | } |
295 | interesting = ni; | 323 | interesting = ni; |
296 | }else if(ic!='/') { | 324 | }else if(ic!='/') { |
297 | rv += '/'; rv += ic; | 325 | rv += '/'; rv += ic; |
298 | qf = true; | 326 | qf = true; |
299 | ++interesting; | 327 | ++interesting; |
300 | } | 328 | } |
301 | string::size_type n = interesting; | 329 | string::size_type n = interesting; |
302 | char tmp[3] = { 0,0,0 }; | 330 | char tmp[3] = { 0,0,0 }; |
303 | stack<string::size_type> psegs; psegs.push(rv.length()); | 331 | stack<string::size_type> psegs; psegs.push(rv.length()); |
304 | string pseg; | 332 | string pseg; |
305 | for(;n<ul;) { | 333 | for(;n<ul;) { |
306 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); | 334 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); |
307 | if(unsafe==string::npos) { | 335 | if(unsafe==string::npos) { |
308 | pseg.append(uri,n,ul-n-1); n = ul-1; | 336 | pseg.append(uri,n,ul-n-1); n = ul-1; |
309 | }else{ | 337 | }else{ |
310 | pseg.append(uri,n,unsafe-n); | 338 | pseg.append(uri,n,unsafe-n); |
311 | n = unsafe; | 339 | n = unsafe; |
312 | } | 340 | } |
313 | char c = uri[n++]; | 341 | char c = uri[n++]; |
314 | if(c=='%') { | 342 | if(c=='%') { |
315 | if((n+1)>=ul) | 343 | if((n+1)>=ul) |
316 | throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); | 344 | throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); |
317 | tmp[0] = uri[n++]; | 345 | tmp[0] = uri[n++]; |
318 | tmp[1] = uri[n++]; | 346 | tmp[1] = uri[n++]; |
319 | if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) | 347 | if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) |
320 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); | 348 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); |
321 | int cc = strtol(tmp,0,16); | 349 | int cc = strtol(tmp,0,16); |
322 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) | 350 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) |
323 | pseg += cc; | 351 | pseg += cc; |
324 | else{ | 352 | else{ |
325 | pseg += '%'; | 353 | pseg += '%'; |
326 | pseg += toupper(tmp[0]); pseg += toupper(tmp[1]); | 354 | pseg += toupper(tmp[0]); pseg += toupper(tmp[1]); |
327 | } | 355 | } |
328 | }else if(qf) { | 356 | }else if(qf) { |
329 | rv += pseg; rv += c; | 357 | rv += pseg; rv += c; |
330 | pseg.clear(); | 358 | pseg.clear(); |
331 | }else if(n>=ul || strchr("?/#",c)) { | 359 | }else if(n>=ul || strchr("?/#",c)) { |
332 | if(pseg.empty() || pseg==".") { | 360 | if(pseg.empty() || pseg==".") { |
333 | }else if(pseg=="..") { | 361 | }else if(pseg=="..") { |
334 | if(psegs.size()>1) { | 362 | if(psegs.size()>1) { |
335 | rv.resize(psegs.top()); psegs.pop(); | 363 | rv.resize(psegs.top()); psegs.pop(); |
336 | } | 364 | } |
337 | }else{ | 365 | }else{ |
338 | psegs.push(rv.length()); | 366 | psegs.push(rv.length()); |
339 | if(c!='/') { | 367 | if(c!='/') { |
340 | pseg += c; | 368 | pseg += c; |
341 | qf = true; | 369 | qf = true; |
342 | } | 370 | } |
343 | rv += '/'; rv += pseg; | 371 | rv += '/'; rv += pseg; |
344 | } | 372 | } |
345 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { | 373 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { |
346 | rv += '/'; | 374 | rv += '/'; |
347 | if(n<ul) | 375 | if(n<ul) |
348 | qf = true; | 376 | qf = true; |
349 | }else if(strchr("?#",c)) { | 377 | }else if(strchr("?#",c)) { |
350 | if(psegs.size()==1 && psegs.top()==rv.length()) | 378 | if(psegs.size()==1 && psegs.top()==rv.length()) |
351 | rv += '/'; | 379 | rv += '/'; |
352 | if(pseg.empty()) | 380 | if(pseg.empty()) |
353 | rv += c; | 381 | rv += c; |
354 | qf = true; | 382 | qf = true; |
355 | } | 383 | } |
356 | pseg.clear(); | 384 | pseg.clear(); |
357 | }else{ | 385 | }else{ |
358 | pseg += c; | 386 | pseg += c; |
359 | } | 387 | } |
360 | } | 388 | } |
361 | if(!pseg.empty()) { | 389 | if(!pseg.empty()) { |
362 | if(!qf) rv += '/'; | 390 | if(!qf) rv += '/'; |
363 | rv += pseg; | 391 | rv += pseg; |
364 | } | 392 | } |
365 | return rv; | 393 | return rv; |
366 | } | 394 | } |
367 | 395 | ||
368 | string& strip_uri_fragment_part(string& u) { | 396 | string& strip_uri_fragment_part(string& u) { |
369 | string::size_type q = u.find('?'), f = u.find('#'); | 397 | string::size_type q = u.find('?'), f = u.find('#'); |
370 | if(q==string::npos) { | 398 | if(q==string::npos) { |
371 | if(f!=string::npos) | 399 | if(f!=string::npos) |
372 | u.erase(f); | 400 | u.erase(f); |
373 | }else{ | 401 | }else{ |
374 | if(f!=string::npos) { | 402 | if(f!=string::npos) { |
375 | if(f<q) | 403 | if(f<q) |
376 | u.erase(f,q-f); | 404 | u.erase(f,q-f); |
377 | else | 405 | else |
378 | u.erase(f); | 406 | u.erase(f); |
379 | } | 407 | } |
380 | } | 408 | } |
381 | return u; | 409 | return u; |
382 | } | 410 | } |
383 | 411 | ||
384 | bool uri_matches_realm(const string& uri,const string& realm) { | 412 | bool uri_matches_realm(const string& uri,const string& realm) { |
385 | string nrealm = opkele::util::rfc_3986_normalize_uri(realm); | 413 | string nrealm = opkele::util::rfc_3986_normalize_uri(realm); |
386 | string nu = opkele::util::rfc_3986_normalize_uri(uri); | 414 | string nu = opkele::util::rfc_3986_normalize_uri(uri); |
387 | string::size_type pr = nrealm.find("://"); | 415 | string::size_type pr = nrealm.find("://"); |
388 | string::size_type pu = nu.find("://"); | 416 | string::size_type pu = nu.find("://"); |
389 | assert(!(pr==string::npos || pu==string::npos)); | 417 | assert(!(pr==string::npos || pu==string::npos)); |
390 | pr += sizeof("://")-1; | 418 | pr += sizeof("://")-1; |
391 | pu += sizeof("://")-1; | 419 | pu += sizeof("://")-1; |
392 | if(!strncmp(nrealm.c_str()+pr,"*.",2)) { | 420 | if(!strncmp(nrealm.c_str()+pr,"*.",2)) { |
393 | pr = nrealm.find('.',pr); | 421 | pr = nrealm.find('.',pr); |
394 | pu = nu.find('.',pu); | 422 | pu = nu.find('.',pu); |
395 | assert(pr!=string::npos); | 423 | assert(pr!=string::npos); |
396 | if(pu==string::npos) | 424 | if(pu==string::npos) |
397 | return false; | 425 | return false; |
398 | // TODO: check for overgeneralized realm | 426 | // TODO: check for overgeneralized realm |
399 | } | 427 | } |
400 | string::size_type lr = nrealm.length(); | 428 | string::size_type lr = nrealm.length(); |
401 | string::size_type lu = nu.length(); | 429 | string::size_type lu = nu.length(); |
402 | if( (lu-pu) < (lr-pr) ) | 430 | if( (lu-pu) < (lr-pr) ) |
403 | return false; | 431 | return false; |
404 | pair<const char*,const char*> mp = mismatch( | 432 | pair<const char*,const char*> mp = mismatch( |
405 | nrealm.c_str()+pr,nrealm.c_str()+lr, | 433 | nrealm.c_str()+pr,nrealm.c_str()+lr, |
406 | nu.c_str()+pu); | 434 | nu.c_str()+pu); |
407 | if( (*(mp.first-1))!='/' | 435 | if( (*(mp.first-1))!='/' |
408 | && !strchr("/?#",*mp.second) ) | 436 | && !strchr("/?#",*mp.second) ) |
409 | return false; | 437 | return false; |
410 | return true; | 438 | return true; |
411 | } | 439 | } |
412 | 440 | ||
413 | string abi_demangle(const char *mn) { | 441 | string abi_demangle(const char *mn) { |
414 | #ifndef HAVE_DEMANGLE | 442 | #ifndef HAVE_DEMANGLE |
415 | return mn; | 443 | return mn; |
416 | #else /* !HAVE_DEMANGLE */ | 444 | #else /* !HAVE_DEMANGLE */ |
417 | int dstat; | 445 | int dstat; |
418 | char *demangled = abi::__cxa_demangle(mn,0,0,&dstat); | 446 | char *demangled = abi::__cxa_demangle(mn,0,0,&dstat); |
419 | if(dstat) | 447 | if(dstat) |
420 | return mn; | 448 | return mn; |
421 | string rv = demangled; | 449 | string rv = demangled; |
422 | free(demangled); | 450 | free(demangled); |
423 | return rv; | 451 | return rv; |
424 | #endif /* !HAVE_DEMANGLE */ | 452 | #endif /* !HAVE_DEMANGLE */ |
425 | } | 453 | } |
426 | 454 | ||
427 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om) { | 455 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om) { |
428 | const string& slist = om.get_field("signed"); | 456 | const string& slist = om.get_field("signed"); |
429 | string kv; | 457 | string kv; |
430 | string::size_type p=0; | 458 | string::size_type p=0; |
431 | while(true) { | 459 | while(true) { |
432 | string::size_type co = slist.find(',',p); | 460 | string::size_type co = slist.find(',',p); |
433 | string f = (co==string::npos) | 461 | string f = (co==string::npos) |
434 | ?slist.substr(p):slist.substr(p,co-p); | 462 | ?slist.substr(p):slist.substr(p,co-p); |
435 | kv += f; | 463 | kv += f; |
436 | kv += ':'; | 464 | kv += ':'; |
437 | kv += om.get_field(f); | 465 | kv += om.get_field(f); |
438 | kv += '\n'; | 466 | kv += '\n'; |
439 | if(co==string::npos) break; | 467 | if(co==string::npos) break; |
440 | p = co+1; | 468 | p = co+1; |
441 | } | 469 | } |
442 | const secret_t& secret = assoc->secret(); | 470 | const secret_t& secret = assoc->secret(); |
443 | const EVP_MD *evpmd; | 471 | const EVP_MD *evpmd; |
444 | const string& at = assoc->assoc_type(); | 472 | const string& at = assoc->assoc_type(); |
445 | if(at=="HMAC-SHA256") | 473 | if(at=="HMAC-SHA256") |
446 | evpmd = EVP_sha256(); | 474 | evpmd = EVP_sha256(); |
447 | else if(at=="HMAC-SHA1") | 475 | else if(at=="HMAC-SHA1") |
448 | evpmd = EVP_sha1(); | 476 | evpmd = EVP_sha1(); |
449 | else | 477 | else |
450 | throw unsupported(OPKELE_CP_ "unknown association type"); | 478 | throw unsupported(OPKELE_CP_ "unknown association type"); |
451 | unsigned int md_len = 0; | 479 | unsigned int md_len = 0; |
452 | unsigned char md[SHA256_DIGEST_LENGTH]; | 480 | unsigned char md[SHA256_DIGEST_LENGTH]; |
453 | HMAC(evpmd, | 481 | HMAC(evpmd, |
454 | &(secret.front()),secret.size(), | 482 | &(secret.front()),secret.size(), |
455 | (const unsigned char*)kv.data(),kv.length(), | 483 | (const unsigned char*)kv.data(),kv.length(), |
456 | md,&md_len); | 484 | md,&md_len); |
457 | return encode_base64(md,md_len); | 485 | return encode_base64(md,md_len); |
458 | } | 486 | } |
459 | 487 | ||
460 | string normalize_identifier(const string& usi,bool strip_fragment) { | 488 | string normalize_identifier(const string& usi,bool strip_fragment) { |
461 | if(usi.empty()) | 489 | if(usi.empty()) |
462 | return usi; | 490 | return usi; |
463 | string rv; | 491 | string rv; |
464 | string::size_type fsc = usi.find_first_not_of(data::_whitespace_chars); | 492 | string::size_type fsc = usi.find_first_not_of(data::_whitespace_chars); |
465 | if(fsc==string::npos) | 493 | if(fsc==string::npos) |
466 | return rv; | 494 | return rv; |
467 | string::size_type lsc = usi.find_last_not_of(data::_whitespace_chars); | 495 | string::size_type lsc = usi.find_last_not_of(data::_whitespace_chars); |
468 | assert(lsc!=string::npos); | 496 | assert(lsc!=string::npos); |
469 | if(!strncasecmp(usi.c_str()+fsc,"xri://",sizeof("xri://")-1)) | 497 | if(!strncasecmp(usi.c_str()+fsc,"xri://",sizeof("xri://")-1)) |
470 | fsc += sizeof("xri://")-1; | 498 | fsc += sizeof("xri://")-1; |
471 | if( (fsc+1) >= lsc ) | 499 | if( (fsc+1) >= lsc ) |
472 | return rv; | 500 | return rv; |
473 | rv.assign(usi,fsc,lsc-fsc+1); | 501 | rv.assign(usi,fsc,lsc-fsc+1); |
474 | if(strchr(data::_iname_leaders,rv[0])) { | 502 | if(strchr(data::_iname_leaders,rv[0])) { |
475 | /* TODO: further normalize xri identity, fold case or | 503 | /* TODO: further normalize xri identity, fold case or |
476 | * whatever... */ | 504 | * whatever... */ |
477 | }else{ | 505 | }else{ |
478 | if(rv.find("://")==string::npos) | 506 | if(rv.find("://")==string::npos) |
479 | rv.insert(0,"http://"); | 507 | rv.insert(0,"http://"); |
480 | if(strip_fragment) { | 508 | if(strip_fragment) { |
481 | string::size_type fp = rv.find('#'); | 509 | string::size_type fp = rv.find('#'); |
482 | if(fp!=string::npos) { | 510 | if(fp!=string::npos) { |
483 | string::size_type qp = rv.find('?'); | 511 | string::size_type qp = rv.find('?'); |
484 | if(qp==string::npos || qp<fp) | 512 | if(qp==string::npos || qp<fp) |
485 | rv.erase(fp); | 513 | rv.erase(fp); |
486 | else if(qp>fp) | 514 | else if(qp>fp) |
487 | rv.erase(fp,qp-fp); | 515 | rv.erase(fp,qp-fp); |
488 | } | 516 | } |
489 | } | 517 | } |
490 | rv = rfc_3986_normalize_uri(rv); | 518 | rv = rfc_3986_normalize_uri(rv); |
491 | } | 519 | } |
492 | return rv; | 520 | return rv; |
493 | } | 521 | } |
494 | 522 | ||
495 | } | 523 | } |
496 | 524 | ||
497 | } | 525 | } |