summaryrefslogtreecommitdiffabout
path: root/src/util.cc
authorMichael Krelin <hacker@klever.net>2005-01-29 20:14:37 (UTC)
committer Michael Krelin <hacker@klever.net>2005-01-29 20:14:37 (UTC)
commitff4b919683537625f693eedf53006364d0f8444d (patch) (side-by-side diff)
tree4c19e38c0832b16b4ca98ae5af6542d932373eb1 /src/util.cc
parentf9a64a67c89a7566e63ed66c3a69c359abea4dfd (diff)
downloadkingate-ff4b919683537625f693eedf53006364d0f8444d.zip
kingate-ff4b919683537625f693eedf53006364d0f8444d.tar.gz
kingate-ff4b919683537625f693eedf53006364d0f8444d.tar.bz2
initial commit into repository0.0
Diffstat (limited to 'src/util.cc') (more/less context) (show whitespace changes)
-rw-r--r--src/util.cc53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/util.cc b/src/util.cc
new file mode 100644
index 0000000..2e2d305
--- a/dev/null
+++ b/src/util.cc
@@ -0,0 +1,53 @@
+#include "kingate/util.h"
+#include "kingate/exception.h"
+
+namespace kingate {
+
+ static const char *safeChars =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "0123456789"
+ "_-" ;
+
+ string url_escape(const string& str) {
+ string rv = str;
+ string::size_type screwed = 0;
+ for(;;) {
+ screwed = rv.find_first_not_of(safeChars,screwed);
+ if(screwed == string::npos)
+ break;
+ while(screwed<rv.length() && !strchr(safeChars,rv.at(screwed))) {
+ char danger = rv.at(screwed);
+ if(danger==' ') {
+ rv.replace(screwed++,1,1,'+');
+ }else{
+ static char tmp[4] = {'%',0,0,0};
+ snprintf(&tmp[1],3,"%02X",0xFF&(int)danger);
+ rv.replace(screwed,1,tmp,3);
+ screwed+=3;
+ }
+ }
+ }
+ return rv;
+ }
+ string url_unescape(const string& str) {
+ string rv = str;
+ string::size_type unscrewed = 0;
+ for(;;) {
+ unscrewed = rv.find_first_of("%+",unscrewed);
+ if(unscrewed == string::npos)
+ break;
+ if(rv.at(unscrewed)=='+') {
+ rv.replace(unscrewed++,1,1,' ');
+ }else{
+ if((rv.length()-unscrewed)<3)
+ throw exception(CODEPOINT,"incorrectly escaped string");
+ // XXX: ensure it's hex?
+ int danger = strtol(rv.substr(unscrewed+1,2).c_str(),NULL,16);
+ rv.replace(unscrewed,3,1,danger);
+ unscrewed++;
+ }
+ }
+ return rv;
+ }
+}