summaryrefslogtreecommitdiffabout
path: root/lib
authorMichael Krelin <hacker@klever.net>2008-03-02 20:23:40 (UTC)
committer Michael Krelin <hacker@klever.net>2008-03-02 20:23:40 (UTC)
commitda3f84153be2a93da7ffc49af33b29b9725fac38 (patch) (unidiff)
treebcacc4c38a53b70c679fa69fbf2577da2802a197 /lib
parentf47e336b569739bdde8e9add96ff2c46f97257fb (diff)
downloadlibopkele-da3f84153be2a93da7ffc49af33b29b9725fac38.zip
libopkele-da3f84153be2a93da7ffc49af33b29b9725fac38.tar.gz
libopkele-da3f84153be2a93da7ffc49af33b29b9725fac38.tar.bz2
made util::url_encode refrain from encoding unreserved chars
as per rfc 3986 Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (limited to 'lib') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/util.cc39
1 files changed, 33 insertions, 6 deletions
diff --git a/lib/util.cc b/lib/util.cc
index 29e6738..a6e08e2 100644
--- a/lib/util.cc
+++ b/lib/util.cc
@@ -10,7 +10,6 @@
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 <curl/curl.h>
14#include <opkele/util.h> 13#include <opkele/util.h>
15#include <opkele/exception.h> 14#include <opkele/exception.h>
16#include <opkele/data.h> 15#include <opkele/data.h>
@@ -156,12 +155,40 @@ namespace opkele {
156 * 155 *
157 */ 156 */
158 157
158 static inline bool isrfc3986unreserved(int c) {
159 if(c<'-') return false;
160 if(c<='.') return true;
161 if(c<'0') return false; if(c<='9') return true;
162 if(c<'A') return false; if(c<='Z') return true;
163 if(c<'_') return false;
164 if(c=='_') return true;
165 if(c<'a') return false; if(c<='z') return true;
166 if(c=='~') return true;
167 return false;
168 }
169
170 struct __url_encoder : public unary_function<char,void> {
171 public:
172 string& rv;
173
174 __url_encoder(string& r) : rv(r) { }
175
176 result_type operator()(argument_type c) {
177 if(isrfc3986unreserved(c))
178 rv += c;
179 else{
180 char tmp[4];
181 snprintf(tmp,sizeof(tmp),"%%%02X",
182 (c&0xff));
183 rv += tmp;
184 }
185 }
186 };
187
159 string url_encode(const string& str) { 188 string url_encode(const string& str) {
160 char * t = curl_escape(str.c_str(),str.length()); 189 string rv;
161 if(!t) 190 for_each(str.begin(),str.end(),
162 throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); 191 __url_encoder(rv));
163 string rv(t);
164 curl_free(t);
165 return rv; 192 return rv;
166 } 193 }
167 194