diff --git a/util/socket.cpp b/util/socket.cpp index b0046d02..425d2a70 100644 --- a/util/socket.cpp +++ b/util/socket.cpp @@ -495,3 +495,27 @@ bool Socket::Server::connected(){ /// Returns internal socket number. int Socket::Server::getSocket(){return sock;} + +/// Unescapes URLencoded C-strings to a std::string. +/// This function *will* destroy the incoming data! +std::string Socket::Connection::urlunescape(char *s){ + char *p; + for (p = s; *s != '\0'; ++s){ + if (*s == '%'){ + if (*++s != '\0'){ + *p = unhex(*s) << 4; + } + if (*++s != '\0'){ + *p++ += unhex(*s); + } + } else { + if (*s == '+'){*p++ = ' ';}else{*p++ = *s;} + } + } + *p = '\0'; + return std::string(s); +} + +int Socket::Connection::unhex(char c){ + return( c >= '0' && c <= '9' ? c - '0' : c >= 'A' && c <= 'F' ? c - 'A' + 10 : c - 'a' + 10 ); +} diff --git a/util/socket.h b/util/socket.h index 57aca183..456a162f 100644 --- a/util/socket.h +++ b/util/socket.h @@ -23,6 +23,7 @@ namespace Socket{ private: int sock; ///< Internally saved socket number. std::string remotehost; ///< Stores remote host address. + int unhex(char c); ///< Helper function for urlunescape. public: Connection(); ///< Create a new disconnected base socket. Connection(int sockNo); ///< Create a new base socket. @@ -44,6 +45,7 @@ namespace Socket{ bool swrite(std::string & buffer); ///< Read call that is compatible with std::string. bool iread(std::string & buffer); ///< Incremental write call that is compatible with std::string. bool iwrite(std::string & buffer); ///< Write call that is compatible with std::string. + std::string urlunescape(char *s); ///< Unescapes URLencoded C-strings to a std::string. void close(); ///< Close connection. std::string getHost(); ///< Gets hostname for connection, if available. int getSocket(); ///< Returns internal socket number.