author | Michael Krelin <hacker@klever.net> | 2008-09-22 20:08:35 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-09-22 20:08:35 (UTC) |
commit | 4522de61114018633f66492e2e9977cdb3108098 (patch) (unidiff) | |
tree | 2af460f9269163320098476d484dee867c4e8138 | |
parent | 767b9926a3b2a2ab000415cc5d36df84dd90f13f (diff) | |
download | libopkele-4522de61114018633f66492e2e9977cdb3108098.zip libopkele-4522de61114018633f66492e2e9977cdb3108098.tar.gz libopkele-4522de61114018633f66492e2e9977cdb3108098.tar.bz2 |
A couple of bugfixes
- added missing 'return' statement to the forward_iterator_proxy operator=()
- made temporary non-static for thread safety in url_decode()
Thanks to Masato Kataoka of orenosv project
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | include/opkele/iterator.h | 2 | ||||
-rw-r--r-- | lib/util.cc | 2 |
2 files changed, 2 insertions, 2 deletions
diff --git a/include/opkele/iterator.h b/include/opkele/iterator.h index 8f86234..94da7e4 100644 --- a/include/opkele/iterator.h +++ b/include/opkele/iterator.h | |||
@@ -1,216 +1,216 @@ | |||
1 | #ifndef __OPKELE_ITERATOR_H | 1 | #ifndef __OPKELE_ITERATOR_H |
2 | #define __OPKELE_ITERATOR_H | 2 | #define __OPKELE_ITERATOR_H |
3 | 3 | ||
4 | #include <cassert> | 4 | #include <cassert> |
5 | #include <iterator> | 5 | #include <iterator> |
6 | 6 | ||
7 | namespace opkele { | 7 | namespace opkele { |
8 | namespace util { | 8 | namespace util { |
9 | using std::iterator; | 9 | using std::iterator; |
10 | using std::forward_iterator_tag; | 10 | using std::forward_iterator_tag; |
11 | using std::output_iterator_tag; | 11 | using std::output_iterator_tag; |
12 | 12 | ||
13 | template <typename T> | 13 | template <typename T> |
14 | class basic_output_iterator_proxy_impl : public iterator<output_iterator_tag,T,void,T*,T&> { | 14 | class basic_output_iterator_proxy_impl : public iterator<output_iterator_tag,T,void,T*,T&> { |
15 | public: | 15 | public: |
16 | virtual ~basic_output_iterator_proxy_impl() { } | 16 | virtual ~basic_output_iterator_proxy_impl() { } |
17 | 17 | ||
18 | virtual basic_output_iterator_proxy_impl<T>* dup() const = 0; | 18 | virtual basic_output_iterator_proxy_impl<T>* dup() const = 0; |
19 | basic_output_iterator_proxy_impl<T>& operator*() { return *this; }; | 19 | basic_output_iterator_proxy_impl<T>& operator*() { return *this; }; |
20 | virtual basic_output_iterator_proxy_impl<T>& operator=(const T& x) = 0; | 20 | virtual basic_output_iterator_proxy_impl<T>& operator=(const T& x) = 0; |
21 | 21 | ||
22 | }; | 22 | }; |
23 | 23 | ||
24 | template<typename IT,typename T=typename IT::value_type> | 24 | template<typename IT,typename T=typename IT::value_type> |
25 | class output_iterator_proxy_impl : public basic_output_iterator_proxy_impl<T> { | 25 | class output_iterator_proxy_impl : public basic_output_iterator_proxy_impl<T> { |
26 | public: | 26 | public: |
27 | IT i; | 27 | IT i; |
28 | 28 | ||
29 | output_iterator_proxy_impl(const IT& _i) : i(_i) { } | 29 | output_iterator_proxy_impl(const IT& _i) : i(_i) { } |
30 | basic_output_iterator_proxy_impl<T>* dup() const { | 30 | basic_output_iterator_proxy_impl<T>* dup() const { |
31 | return new output_iterator_proxy_impl<IT,T>(i); } | 31 | return new output_iterator_proxy_impl<IT,T>(i); } |
32 | basic_output_iterator_proxy_impl<T>& operator=(const T& x) { | 32 | basic_output_iterator_proxy_impl<T>& operator=(const T& x) { |
33 | (*i) = x; return *this; } | 33 | (*i) = x; return *this; } |
34 | }; | 34 | }; |
35 | 35 | ||
36 | template<typename T> | 36 | template<typename T> |
37 | class output_iterator_proxy : public iterator<output_iterator_tag,T,void,T*,T&> { | 37 | class output_iterator_proxy : public iterator<output_iterator_tag,T,void,T*,T&> { |
38 | public: | 38 | public: |
39 | basic_output_iterator_proxy_impl<T> *I; | 39 | basic_output_iterator_proxy_impl<T> *I; |
40 | 40 | ||
41 | template<typename IT> | 41 | template<typename IT> |
42 | output_iterator_proxy(const IT& i) | 42 | output_iterator_proxy(const IT& i) |
43 | : I(new output_iterator_proxy_impl<IT,T>(i)) { } | 43 | : I(new output_iterator_proxy_impl<IT,T>(i)) { } |
44 | output_iterator_proxy(const output_iterator_proxy<T>& x) | 44 | output_iterator_proxy(const output_iterator_proxy<T>& x) |
45 | : I(x.I->dup()) { } | 45 | : I(x.I->dup()) { } |
46 | ~output_iterator_proxy() { delete I; } | 46 | ~output_iterator_proxy() { delete I; } |
47 | 47 | ||
48 | output_iterator_proxy& operator=(const output_iterator_proxy<T>& x) { | 48 | output_iterator_proxy& operator=(const output_iterator_proxy<T>& x) { |
49 | delete I; I = x.I->dup(); } | 49 | delete I; I = x.I->dup(); } |
50 | 50 | ||
51 | output_iterator_proxy& operator*() { return *this; } | 51 | output_iterator_proxy& operator*() { return *this; } |
52 | output_iterator_proxy& operator=(const T& x) { | 52 | output_iterator_proxy& operator=(const T& x) { |
53 | (**I) = x; return *this; } | 53 | (**I) = x; return *this; } |
54 | 54 | ||
55 | output_iterator_proxy& operator++() { return *this; } | 55 | output_iterator_proxy& operator++() { return *this; } |
56 | output_iterator_proxy& operator++(int) { return *this; } | 56 | output_iterator_proxy& operator++(int) { return *this; } |
57 | }; | 57 | }; |
58 | 58 | ||
59 | template <typename T,typename TR=T&,typename TP=T*> | 59 | template <typename T,typename TR=T&,typename TP=T*> |
60 | class basic_forward_iterator_proxy_impl : public iterator<forward_iterator_tag,T,void,TP,TR> { | 60 | class basic_forward_iterator_proxy_impl : public iterator<forward_iterator_tag,T,void,TP,TR> { |
61 | public: | 61 | public: |
62 | virtual ~basic_forward_iterator_proxy_impl() { } | 62 | virtual ~basic_forward_iterator_proxy_impl() { } |
63 | 63 | ||
64 | virtual basic_forward_iterator_proxy_impl<T,TR,TP>* dup() const = 0; | 64 | virtual basic_forward_iterator_proxy_impl<T,TR,TP>* dup() const = 0; |
65 | 65 | ||
66 | virtual bool operator==(const basic_forward_iterator_proxy_impl<T,TR,TP>& x) const = 0; | 66 | virtual bool operator==(const basic_forward_iterator_proxy_impl<T,TR,TP>& x) const = 0; |
67 | virtual bool operator!=(const basic_forward_iterator_proxy_impl<T,TR,TP>& x) const { | 67 | virtual bool operator!=(const basic_forward_iterator_proxy_impl<T,TR,TP>& x) const { |
68 | return !((*this)==x); } | 68 | return !((*this)==x); } |
69 | virtual TR operator*() const = 0; | 69 | virtual TR operator*() const = 0; |
70 | virtual TP operator->() const = 0; | 70 | virtual TP operator->() const = 0; |
71 | virtual void advance() = 0; | 71 | virtual void advance() = 0; |
72 | }; | 72 | }; |
73 | 73 | ||
74 | template <typename IT> | 74 | template <typename IT> |
75 | class forward_iterator_proxy_impl : public basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer> { | 75 | class forward_iterator_proxy_impl : public basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer> { |
76 | public: | 76 | public: |
77 | IT i; | 77 | IT i; |
78 | 78 | ||
79 | forward_iterator_proxy_impl(const IT& _i) : i(_i) { } | 79 | forward_iterator_proxy_impl(const IT& _i) : i(_i) { } |
80 | 80 | ||
81 | virtual basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer>* dup() const { | 81 | virtual basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer>* dup() const { |
82 | return new forward_iterator_proxy_impl<IT>(i); } | 82 | return new forward_iterator_proxy_impl<IT>(i); } |
83 | 83 | ||
84 | virtual bool operator==(const basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer>& x) const { | 84 | virtual bool operator==(const basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer>& x) const { |
85 | return i==static_cast<const forward_iterator_proxy_impl<IT>*>(&x)->i; } | 85 | return i==static_cast<const forward_iterator_proxy_impl<IT>*>(&x)->i; } |
86 | virtual bool operator!=(const basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer>& x) const { | 86 | virtual bool operator!=(const basic_forward_iterator_proxy_impl<typename IT::value_type,typename IT::reference,typename IT::pointer>& x) const { |
87 | return i!=static_cast<const forward_iterator_proxy_impl<IT>*>(&x)->i; } | 87 | return i!=static_cast<const forward_iterator_proxy_impl<IT>*>(&x)->i; } |
88 | virtual typename IT::reference operator*() const { return *i; } | 88 | virtual typename IT::reference operator*() const { return *i; } |
89 | virtual typename IT::pointer operator->() const { return i.operator->(); } | 89 | virtual typename IT::pointer operator->() const { return i.operator->(); } |
90 | virtual void advance() { ++i; } | 90 | virtual void advance() { ++i; } |
91 | }; | 91 | }; |
92 | 92 | ||
93 | template<typename T,typename TR=T&,typename TP=T*> | 93 | template<typename T,typename TR=T&,typename TP=T*> |
94 | class forward_iterator_proxy : public iterator<forward_iterator_tag,T,void,TP,TR> { | 94 | class forward_iterator_proxy : public iterator<forward_iterator_tag,T,void,TP,TR> { |
95 | public: | 95 | public: |
96 | basic_forward_iterator_proxy_impl<T,TR,TP> *I; | 96 | basic_forward_iterator_proxy_impl<T,TR,TP> *I; |
97 | 97 | ||
98 | template<typename IT> | 98 | template<typename IT> |
99 | forward_iterator_proxy(const IT& i) | 99 | forward_iterator_proxy(const IT& i) |
100 | : I(new forward_iterator_proxy_impl<IT>(i)) { } | 100 | : I(new forward_iterator_proxy_impl<IT>(i)) { } |
101 | forward_iterator_proxy(const forward_iterator_proxy<T,TR,TP>& x) | 101 | forward_iterator_proxy(const forward_iterator_proxy<T,TR,TP>& x) |
102 | : I(x.I->dup()) { } | 102 | : I(x.I->dup()) { } |
103 | ~forward_iterator_proxy() { delete I; } | 103 | ~forward_iterator_proxy() { delete I; } |
104 | 104 | ||
105 | forward_iterator_proxy& operator=(const forward_iterator_proxy<T,TR,TP>& x) { | 105 | forward_iterator_proxy& operator=(const forward_iterator_proxy<T,TR,TP>& x) { |
106 | delete I; I = x.I->dup(); } | 106 | delete I; I = x.I->dup(); return *this; } |
107 | 107 | ||
108 | bool operator==(const forward_iterator_proxy<T,TR,TP>& x) const { | 108 | bool operator==(const forward_iterator_proxy<T,TR,TP>& x) const { |
109 | return (*I)==(*(x.I)); } | 109 | return (*I)==(*(x.I)); } |
110 | bool operator!=(const forward_iterator_proxy<T,TR,TP>& x) const { | 110 | bool operator!=(const forward_iterator_proxy<T,TR,TP>& x) const { |
111 | return (*I)!=(*(x.I)); } | 111 | return (*I)!=(*(x.I)); } |
112 | 112 | ||
113 | TR operator*() const { | 113 | TR operator*() const { |
114 | return **I; } | 114 | return **I; } |
115 | TP operator->() const { | 115 | TP operator->() const { |
116 | return I->operator->(); } | 116 | return I->operator->(); } |
117 | 117 | ||
118 | forward_iterator_proxy<T,TR,TP>& operator++() { | 118 | forward_iterator_proxy<T,TR,TP>& operator++() { |
119 | I->advance(); return *this; } | 119 | I->advance(); return *this; } |
120 | forward_iterator_proxy<T,TR,TP>& operator++(int) { | 120 | forward_iterator_proxy<T,TR,TP>& operator++(int) { |
121 | forward_iterator_proxy<T,TR,TP> rv(*this); | 121 | forward_iterator_proxy<T,TR,TP> rv(*this); |
122 | I->advance(); return rv; } | 122 | I->advance(); return rv; } |
123 | }; | 123 | }; |
124 | 124 | ||
125 | template<typename IT> | 125 | template<typename IT> |
126 | class basic_filterator : public iterator< | 126 | class basic_filterator : public iterator< |
127 | typename IT::iterator_category, | 127 | typename IT::iterator_category, |
128 | typename IT::value_type, | 128 | typename IT::value_type, |
129 | typename IT::difference_type, | 129 | typename IT::difference_type, |
130 | typename IT::pointer, | 130 | typename IT::pointer, |
131 | typename IT::reference> { | 131 | typename IT::reference> { |
132 | public: | 132 | public: |
133 | IT it; | 133 | IT it; |
134 | IT ei; | 134 | IT ei; |
135 | bool empty; | 135 | bool empty; |
136 | 136 | ||
137 | basic_filterator() : empty(true) { } | 137 | basic_filterator() : empty(true) { } |
138 | basic_filterator(const IT& _bi,const IT& _ei) | 138 | basic_filterator(const IT& _bi,const IT& _ei) |
139 | : it(_bi), ei(_ei) { empty = (it==ei); } | 139 | : it(_bi), ei(_ei) { empty = (it==ei); } |
140 | basic_filterator(const basic_filterator<IT>& x) | 140 | basic_filterator(const basic_filterator<IT>& x) |
141 | : it(x.it), ei(x.ei), empty(x.empty) { } | 141 | : it(x.it), ei(x.ei), empty(x.empty) { } |
142 | virtual ~basic_filterator() { } | 142 | virtual ~basic_filterator() { } |
143 | 143 | ||
144 | bool operator==(const basic_filterator<IT>& x) const { | 144 | bool operator==(const basic_filterator<IT>& x) const { |
145 | return empty?x.empty:(it==x.it); } | 145 | return empty?x.empty:(it==x.it); } |
146 | bool operator!=(const basic_filterator<IT>& x) const { | 146 | bool operator!=(const basic_filterator<IT>& x) const { |
147 | return empty!=x.empty || it!=x.it; } | 147 | return empty!=x.empty || it!=x.it; } |
148 | 148 | ||
149 | typename IT::reference operator*() const { | 149 | typename IT::reference operator*() const { |
150 | assert(!empty); | 150 | assert(!empty); |
151 | return *it; } | 151 | return *it; } |
152 | typename IT::pointer operator->() const { | 152 | typename IT::pointer operator->() const { |
153 | assert(!empty); | 153 | assert(!empty); |
154 | return it.operator->(); } | 154 | return it.operator->(); } |
155 | 155 | ||
156 | basic_filterator<IT>& operator++() { | 156 | basic_filterator<IT>& operator++() { |
157 | bool found = false; | 157 | bool found = false; |
158 | for(++it;!(it==ei || (found=is_interesting()));++it) ; | 158 | for(++it;!(it==ei || (found=is_interesting()));++it) ; |
159 | if(!found) empty=true; | 159 | if(!found) empty=true; |
160 | return *this; | 160 | return *this; |
161 | } | 161 | } |
162 | basic_filterator<IT> operator++(int) { | 162 | basic_filterator<IT> operator++(int) { |
163 | basic_filterator<IT> rv(*this); | 163 | basic_filterator<IT> rv(*this); |
164 | ++(*this); | 164 | ++(*this); |
165 | return rv; | 165 | return rv; |
166 | } | 166 | } |
167 | 167 | ||
168 | void prepare() { | 168 | void prepare() { |
169 | bool found = false; | 169 | bool found = false; |
170 | for(;!(it==ei || (found=is_interesting()));++it) ; | 170 | for(;!(it==ei || (found=is_interesting()));++it) ; |
171 | if(!found) empty = true; | 171 | if(!found) empty = true; |
172 | } | 172 | } |
173 | virtual bool is_interesting() const = 0; | 173 | virtual bool is_interesting() const = 0; |
174 | }; | 174 | }; |
175 | 175 | ||
176 | template<typename IT,typename T=typename IT::value_type::first_type,typename TR=T&,typename TP=T*> | 176 | template<typename IT,typename T=typename IT::value_type::first_type,typename TR=T&,typename TP=T*> |
177 | class map_keys_iterator : public iterator< | 177 | class map_keys_iterator : public iterator< |
178 | typename IT::iterator_category, | 178 | typename IT::iterator_category, |
179 | T,void,TP,TR> { | 179 | T,void,TP,TR> { |
180 | public: | 180 | public: |
181 | typedef map_keys_iterator<IT,T,TR,TP> self_type; | 181 | typedef map_keys_iterator<IT,T,TR,TP> self_type; |
182 | IT it; | 182 | IT it; |
183 | IT ei; | 183 | IT ei; |
184 | bool empty; | 184 | bool empty; |
185 | 185 | ||
186 | map_keys_iterator() : empty(true) { } | 186 | map_keys_iterator() : empty(true) { } |
187 | map_keys_iterator(const IT& _bi, | 187 | map_keys_iterator(const IT& _bi, |
188 | const IT& _ei) | 188 | const IT& _ei) |
189 | : it(_bi), ei(_ei) { empty = (it==ei); } | 189 | : it(_bi), ei(_ei) { empty = (it==ei); } |
190 | map_keys_iterator(const self_type& x) | 190 | map_keys_iterator(const self_type& x) |
191 | : it(x.it), ei(x.ei), empty(x.empty) { } | 191 | : it(x.it), ei(x.ei), empty(x.empty) { } |
192 | 192 | ||
193 | bool operator==(const self_type& x) const { | 193 | bool operator==(const self_type& x) const { |
194 | return empty?x.empty:(it==x.it); } | 194 | return empty?x.empty:(it==x.it); } |
195 | bool operator!=(const self_type& x) const { | 195 | bool operator!=(const self_type& x) const { |
196 | return empty!=x.empty || it!=x.it; } | 196 | return empty!=x.empty || it!=x.it; } |
197 | 197 | ||
198 | TR operator*() const { | 198 | TR operator*() const { |
199 | assert(!empty); | 199 | assert(!empty); |
200 | return it->first; } | 200 | return it->first; } |
201 | TP operator->() const { | 201 | TP operator->() const { |
202 | assert(!empty); | 202 | assert(!empty); |
203 | return &(it->first); } | 203 | return &(it->first); } |
204 | 204 | ||
205 | self_type& operator++() { | 205 | self_type& operator++() { |
206 | assert(!empty); | 206 | assert(!empty); |
207 | empty=((++it)==ei); return *this; } | 207 | empty=((++it)==ei); return *this; } |
208 | self_type operator++(int) { | 208 | self_type operator++(int) { |
209 | self_type rv(*this); | 209 | self_type rv(*this); |
210 | ++(*this); return rv; } | 210 | ++(*this); return rv; } |
211 | }; | 211 | }; |
212 | 212 | ||
213 | } | 213 | } |
214 | } | 214 | } |
215 | 215 | ||
216 | #endif /* __OPKELE_ITERATOR_H */ | 216 | #endif /* __OPKELE_ITERATOR_H */ |
diff --git a/lib/util.cc b/lib/util.cc index a46ba2a..249eeed 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -21,392 +21,392 @@ | |||
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 | #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, |
317 | back_inserter(rv), ::tolower ); | 317 | back_inserter(rv), ::tolower ); |
318 | rv += '/'; return rv; | 318 | rv += '/'; return rv; |
319 | } | 319 | } |
320 | transform( | 320 | transform( |
321 | uri.begin()+colon+3,uri.begin()+interesting, | 321 | uri.begin()+colon+3,uri.begin()+interesting, |
322 | back_inserter(rv), ::tolower ); | 322 | back_inserter(rv), ::tolower ); |
323 | bool qf = false; | 323 | bool qf = false; |
324 | char ic = uri[interesting]; | 324 | char ic = uri[interesting]; |
325 | if(ic==':') { | 325 | if(ic==':') { |
326 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); | 326 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); |
327 | const char *nptr = uri.data()+interesting+1; | 327 | const char *nptr = uri.data()+interesting+1; |
328 | char *eptr = 0; | 328 | char *eptr = 0; |
329 | long port = strtol(nptr,&eptr,10); | 329 | long port = strtol(nptr,&eptr,10); |
330 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { | 330 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { |
331 | char tmp[8]; | 331 | char tmp[8]; |
332 | snprintf(tmp,sizeof(tmp),":%ld",port); | 332 | snprintf(tmp,sizeof(tmp),":%ld",port); |
333 | rv += tmp; | 333 | rv += tmp; |
334 | } | 334 | } |
335 | if(ni==string::npos) { | 335 | if(ni==string::npos) { |
336 | rv += '/'; return rv; | 336 | rv += '/'; return rv; |
337 | } | 337 | } |
338 | interesting = ni; | 338 | interesting = ni; |
339 | }else if(ic!='/') { | 339 | }else if(ic!='/') { |
340 | rv += '/'; rv += ic; | 340 | rv += '/'; rv += ic; |
341 | qf = true; | 341 | qf = true; |
342 | ++interesting; | 342 | ++interesting; |
343 | } | 343 | } |
344 | string::size_type n = interesting; | 344 | string::size_type n = interesting; |
345 | char tmp[3] = { 0,0,0 }; | 345 | char tmp[3] = { 0,0,0 }; |
346 | stack<string::size_type> psegs; psegs.push(rv.length()); | 346 | stack<string::size_type> psegs; psegs.push(rv.length()); |
347 | string pseg; | 347 | string pseg; |
348 | for(;n<ul;) { | 348 | for(;n<ul;) { |
349 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); | 349 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); |
350 | if(unsafe==string::npos) { | 350 | if(unsafe==string::npos) { |
351 | pseg.append(uri,n,ul-n-1); n = ul-1; | 351 | pseg.append(uri,n,ul-n-1); n = ul-1; |
352 | }else{ | 352 | }else{ |
353 | pseg.append(uri,n,unsafe-n); | 353 | pseg.append(uri,n,unsafe-n); |
354 | n = unsafe; | 354 | n = unsafe; |
355 | } | 355 | } |
356 | char c = uri[n++]; | 356 | char c = uri[n++]; |
357 | if(c=='%') { | 357 | if(c=='%') { |
358 | if((n+1)>=ul) | 358 | if((n+1)>=ul) |
359 | throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); | 359 | throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); |
360 | tmp[0] = uri[n++]; | 360 | tmp[0] = uri[n++]; |
361 | tmp[1] = uri[n++]; | 361 | tmp[1] = uri[n++]; |
362 | if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) | 362 | if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) |
363 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); | 363 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); |
364 | int cc = strtol(tmp,0,16); | 364 | int cc = strtol(tmp,0,16); |
365 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) | 365 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) |
366 | pseg += (char)cc; | 366 | pseg += (char)cc; |
367 | else{ | 367 | else{ |
368 | pseg += '%'; | 368 | pseg += '%'; |
369 | pseg += (char)toupper(tmp[0]); pseg += (char)toupper(tmp[1]); | 369 | pseg += (char)toupper(tmp[0]); pseg += (char)toupper(tmp[1]); |
370 | } | 370 | } |
371 | }else if(qf) { | 371 | }else if(qf) { |
372 | rv += pseg; rv += c; | 372 | rv += pseg; rv += c; |
373 | pseg.clear(); | 373 | pseg.clear(); |
374 | }else if(n>=ul || strchr("?/#",c)) { | 374 | }else if(n>=ul || strchr("?/#",c)) { |
375 | if(pseg.empty() || pseg==".") { | 375 | if(pseg.empty() || pseg==".") { |
376 | }else if(pseg=="..") { | 376 | }else if(pseg=="..") { |
377 | if(psegs.size()>1) { | 377 | if(psegs.size()>1) { |
378 | rv.resize(psegs.top()); psegs.pop(); | 378 | rv.resize(psegs.top()); psegs.pop(); |
379 | } | 379 | } |
380 | }else{ | 380 | }else{ |
381 | psegs.push(rv.length()); | 381 | psegs.push(rv.length()); |
382 | if(c!='/') { | 382 | if(c!='/') { |
383 | pseg += c; | 383 | pseg += c; |
384 | qf = true; | 384 | qf = true; |
385 | } | 385 | } |
386 | rv += '/'; rv += pseg; | 386 | rv += '/'; rv += pseg; |
387 | } | 387 | } |
388 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { | 388 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { |
389 | rv += '/'; | 389 | rv += '/'; |
390 | if(n<ul) | 390 | if(n<ul) |
391 | qf = true; | 391 | qf = true; |
392 | }else if(strchr("?#",c)) { | 392 | }else if(strchr("?#",c)) { |
393 | if(psegs.size()==1 && psegs.top()==rv.length()) | 393 | if(psegs.size()==1 && psegs.top()==rv.length()) |
394 | rv += '/'; | 394 | rv += '/'; |
395 | if(pseg.empty()) | 395 | if(pseg.empty()) |
396 | rv += c; | 396 | rv += c; |
397 | qf = true; | 397 | qf = true; |
398 | } | 398 | } |
399 | pseg.clear(); | 399 | pseg.clear(); |
400 | }else{ | 400 | }else{ |
401 | pseg += c; | 401 | pseg += c; |
402 | } | 402 | } |
403 | } | 403 | } |
404 | if(!pseg.empty()) { | 404 | if(!pseg.empty()) { |
405 | if(!qf) rv += '/'; | 405 | if(!qf) rv += '/'; |
406 | rv += pseg; | 406 | rv += pseg; |
407 | } | 407 | } |
408 | return rv; | 408 | return rv; |
409 | } | 409 | } |
410 | 410 | ||
411 | string& strip_uri_fragment_part(string& u) { | 411 | string& strip_uri_fragment_part(string& u) { |
412 | string::size_type q = u.find('?'), f = u.find('#'); | 412 | string::size_type q = u.find('?'), f = u.find('#'); |