diff --git a/lib/src/FileUpload.cc b/lib/src/FileUpload.cc index a045dc0c..22152a7f 100755 --- a/lib/src/FileUpload.cc +++ b/lib/src/FileUpload.cc @@ -1,5 +1,5 @@ +#include "Utilities.h" #include -#include "Funcs.h" #include #include #include @@ -137,5 +137,5 @@ const std::string HttpFile::getMd5() const MD5_Init(&c); MD5_Update(&c, fileContent_.c_str(), fileContent_.size()); MD5_Final(md5, &c); - return StringToHex(md5, 16); + return stringToHex(md5, 16); } \ No newline at end of file diff --git a/lib/src/Funcs.cc b/lib/src/Funcs.cc deleted file mode 100755 index 292205b6..00000000 --- a/lib/src/Funcs.cc +++ /dev/null @@ -1,257 +0,0 @@ -#include "Funcs.h" -#include - -static const std::string base64_chars = - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/"; - - -static inline bool is_base64(unsigned char c) { - return (isalnum(c) || (c == '+') || (c == '/')); -} - -bool isInteger(const std::string &str) -{ - for(auto c:str) - { - if(c>'9'||c<'0') - return false; - } - return true; -} -std::string genRandomString(int length) -{ - int i; - char str[length + 1]; - - timespec tp; - clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp); - //LOG_INFO<<"time: "<(tp.tv_nsec)); - std::string char_space = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; - - for (i = 0; i < length; i++) - { - str[i] = char_space[rand() % char_space.length()]; - } - return std::string(str); -} - -std::string StringToHex(unsigned char* ptr, long long length) -{ - std::string idString; - for (long long i = 0; i < length; i++) - { - int value = (ptr[i] & 0xf0)>>4; - if (value < 10) - { - idString.append(1, char(value + 48)); - } else - { - idString.append(1, char(value + 55)); - } - - value = (ptr[i] & 0x0f); - if (value < 10) - { - idString.append(1, char(value + 48)); - } else - { - idString.append(1, char(value + 55)); - } - } - return idString; -} -std::vector splitString(const std::string &str, const std::string &separator) -{ - std::vector ret; - std::string::size_type pos1, pos2; - pos1 = pos2 = 0; - pos1 = str.find(separator); - while (pos1 != std::string::npos) - { - if (pos1 != 0) - { - std::string item = str.substr(pos2, pos1 - pos2); - ret.push_back(item); - } - pos2 = pos1 + separator.length(); - while (pos2 < str.length() && str.substr(pos2, separator.length()) == separator) - pos2 += separator.length(); - pos1 = str.find(separator, pos2); - } - if (pos2 < str.length()) - ret.push_back(str.substr(pos2)); - return ret; -} -std::string getuuid() -{ - uuid_t uu; - uuid_generate(uu); - return StringToHex(uu, 16); -} - -std::string stringPreProcess(const std::string& str) -{ - std::string ret; - for(std::string::size_type i = 0; i < str.length(); i++) - { - if(str[i] == '\'') - { - ret += "\'\'"; - } - else - { - ret += str[i]; - } - } - return ret; -} - -std::string encryptPassword(const std::string& plainPasswd,const std::string& salt) -{ - unsigned char psd[SHA256_DIGEST_LENGTH]; - SHA256(reinterpret_cast(plainPasswd.c_str()), plainPasswd.length(), psd); - std::string encryptPasswd = std::string((char *)psd, SHA256_DIGEST_LENGTH); - encryptPasswd +=salt; - SHA256(reinterpret_cast(encryptPasswd.c_str()), encryptPasswd.length(), psd); - encryptPasswd = StringToHex(psd, SHA256_DIGEST_LENGTH); - return encryptPasswd; -} -std::string encryptHashPassword(const std::string& hashPasswd,const std::string& salt) -{ - unsigned char psd[SHA256_DIGEST_LENGTH]; - std::string encryptPasswd = hashPasswd+salt; - SHA256(reinterpret_cast(encryptPasswd.c_str()), encryptPasswd.length(), psd); - encryptPasswd = StringToHex(psd, SHA256_DIGEST_LENGTH); - return encryptPasswd; -} - -std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) { - std::string ret; - int i = 0; - int j = 0; - unsigned char char_array_3[3]; - unsigned char char_array_4[4]; - - while (in_len--) { - char_array_3[i++] = *(bytes_to_encode++); - if (i == 3) { - char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; - char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); - char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); - char_array_4[3] = char_array_3[2] & 0x3f; - - for(i = 0; (i <4) ; i++) - ret += base64_chars[char_array_4[i]]; - i = 0; - } - } - - if (i) - { - for(j = i; j < 3; j++) - char_array_3[j] = '\0'; - - char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; - char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); - char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); - char_array_4[3] = char_array_3[2] & 0x3f; - - for (j = 0; (j < i + 1); j++) - ret += base64_chars[char_array_4[j]]; - - while((i++ < 3)) - ret += '='; - - } - - return ret; - -} - -std::string base64_decode(std::string const& encoded_string) { - int in_len = encoded_string.size(); - int i = 0; - int j = 0; - int in_ = 0; - unsigned char char_array_4[4], char_array_3[3]; - std::string ret; - - while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { - char_array_4[i++] = encoded_string[in_]; in_++; - if (i ==4) { - for (i = 0; i <4; i++) - char_array_4[i] = base64_chars.find(char_array_4[i]); - - char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); - char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); - char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; - - for (i = 0; (i < 3); i++) - ret += char_array_3[i]; - i = 0; - } - } - - if (i) { - for (j = i; j <4; j++) - char_array_4[j] = 0; - - for (j = 0; j <4; j++) - char_array_4[j] = base64_chars.find(char_array_4[j]); - - char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); - char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); - char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; - - for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; - } - - return ret; -} - -std::string UrlDecode(const std::string& szToDecode) -{ - std::string result; - int hex = 0; - for (size_t i = 0; i < szToDecode.length(); ++i) - { - switch (szToDecode[i]) - { - case '+': - result += ' '; - break; - case '%': - if (isxdigit(szToDecode[i + 1]) && isxdigit(szToDecode[i + 2])) - { - std::string hexStr = szToDecode.substr(i + 1, 2); - hex = strtol(hexStr.c_str(), 0, 16); - //字母和数字[0-9a-zA-Z]、一些特殊符号[$-_.+!*'(),] 、以及某些保留字[$&+,/:;=?@] - //可以不经过编码直接用于URL - if (!((hex >= 48 && hex <= 57) || //0-9 - (hex >=97 && hex <= 122) || //a-z - (hex >=65 && hex <= 90) || //A-Z - //一些特殊符号及保留字[$-_.+!*'(),] [$&+,/:;=?@] - hex == 0x21 || hex == 0x24 || hex == 0x26 || hex == 0x27 || hex == 0x28 || hex == 0x29 - || hex == 0x2a || hex == 0x2b|| hex == 0x2c || hex == 0x2d || hex == 0x2e || hex == 0x2f - || hex == 0x3A || hex == 0x3B|| hex == 0x3D || hex == 0x3f || hex == 0x40 || hex == 0x5f - )) - { - result += char(hex); - i += 2; - } - else result += '%'; - }else { - result += '%'; - } - break; - default: - result += szToDecode[i]; - break; - } - } - return result; -} - diff --git a/lib/src/Funcs.h b/lib/src/Funcs.h deleted file mode 100755 index 17d0764d..00000000 --- a/lib/src/Funcs.h +++ /dev/null @@ -1,65 +0,0 @@ -#ifndef FUNCS_H_ -#define FUNCS_H_ - -#include -#include -#include -#include - -#define USING_PQXX_TO_STRING - -using namespace pqxx; - -bool isInteger(const std::string &str); -std::string genRandomString(int length); -std::string StringToHex(unsigned char* ptr, long long length); -std::vector splitString(const std::string &str,const std::string &separator); -std::string getuuid(); -std::string stringPreProcess(const std::string& str); -std::string encryptPassword(const std::string& plainPasswd,const std::string& salt); -std::string encryptHashPassword(const std::string& hashPasswd,const std::string& salt); -template -std::string toString(const T &obj) -{ - #ifdef USING_PQXX_TO_STRING - return pqxx::to_string(obj); - #else - char str[32]; - if(typeid(obj).name() == typeid(long).name()) - { - sprintf(str, "%ld", obj); - return std::string(str); - } - else if(typeid(obj).name() == typeid(int).name()) - { - sprintf(str, "%d", obj); - return std::string(str); - } - else if(typeid(obj).name() == typeid(std::string).name()) - { - return obj; - } - else if(typeid(obj).name() == typeid(bool).name()) - { - return obj ? "true" : "false"; - } - else - { - LOG_ERROR<<"no way to transfer to string" - } - #endif -} - -template -void fromString(const std::string& str, T &obj) -{ - #ifdef USING_PQXX_TO_STRING - pqxx::from_string(str, obj); - #endif -} - -std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len); -std::string base64_decode(std::string const& encoded_string); -std::string UrlDecode(const std::string& szToDecode); -#endif - diff --git a/lib/src/HttpRequestImpl.cc b/lib/src/HttpRequestImpl.cc index 3be7fac6..531fa350 100755 --- a/lib/src/HttpRequestImpl.cc +++ b/lib/src/HttpRequestImpl.cc @@ -52,12 +52,8 @@ void HttpRequestImpl::parsePremeter() cpos++; key=key.substr(cpos); std::string pvalue = coo.substr(epos + 1); - std::string pdecode=pvalue; - std::string keydecode=key; - int ret=urldecode((char *)key.c_str(),(char *)keydecode.c_str()); - keydecode=keydecode.substr(0,ret); - ret=urldecode((char *)pvalue.c_str(),(char *)pdecode.c_str()); - pdecode=pdecode.substr(0,ret); + std::string pdecode=urlDecode(pvalue); + std::string keydecode=urlDecode(key); premeter_[keydecode] = pdecode; } value=value.substr(pos+1); @@ -73,12 +69,8 @@ void HttpRequestImpl::parsePremeter() cpos++; key=key.substr(cpos); std::string pvalue = coo.substr(epos + 1); - std::string pdecode=pvalue; - std::string keydecode=key; - int ret=urldecode((char *)key.c_str(),(char *)keydecode.c_str()); - keydecode=keydecode.substr(0,ret); - ret=urldecode((char *)pvalue.c_str(),(char *)pdecode.c_str()); - pdecode=pdecode.substr(0,ret); + std::string pdecode=urlDecode(pvalue); + std::string keydecode=urlDecode(key); premeter_[keydecode] = pdecode; } } diff --git a/lib/src/HttpRequestImpl.h b/lib/src/HttpRequestImpl.h index 1db07f40..76977eaf 100755 --- a/lib/src/HttpRequestImpl.h +++ b/lib/src/HttpRequestImpl.h @@ -136,11 +136,7 @@ namespace drogon void setPath(const char* start, const char* end) { - path_.assign(start, end); - - std::string decodePath=path_; - int ret=urldecode((char *)path_.c_str(),(char *)decodePath.c_str()); - path_=decodePath.substr(0,ret); + path_=urlDecode(std::string(start,end)); } // void setPath(const std::string& path) // { diff --git a/lib/src/Utilities.cc b/lib/src/Utilities.cc index a4b5809a..44e8fee1 100755 --- a/lib/src/Utilities.cc +++ b/lib/src/Utilities.cc @@ -1,35 +1,223 @@ #include "Utilities.h" #include -//解url编码实现 -int urldecode(const char* encd,char* decd) +#include + +static const std::string base64_chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + + +static inline bool is_base64(unsigned char c) { + return (isalnum(c) || (c == '+') || (c == '/')); +} + +bool isInteger(const std::string &str) { - int j; - char *cd =(char*) encd; - char p[2]; - - j=0; - - for( size_t i = 0; i < strlen(cd); i++ ) + for(auto c:str) { - memset( p,0,2); - if( cd[i] != '%' ) + if(c>'9'||c<'0') + return false; + } + return true; +} +std::string genRandomString(int length) +{ + int i; + char str[length + 1]; + + timespec tp; + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp); + //LOG_INFO<<"time: "<(tp.tv_nsec)); + std::string char_space = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + for (i = 0; i < length; i++) + { + str[i] = char_space[rand() % char_space.length()]; + } + return std::string(str); +} + +std::string stringToHex(unsigned char* ptr, long long length) +{ + std::string idString; + for (long long i = 0; i < length; i++) + { + int value = (ptr[i] & 0xf0)>>4; + if (value < 10) { - if(cd[i]=='+') - decd[j++]=' '; - else - decd[j++] = cd[i]; - continue; + idString.append(1, char(value + 48)); + } else + { + idString.append(1, char(value + 55)); } - p[0] = cd[++i]; - p[1] = cd[++i]; + value = (ptr[i] & 0x0f); + if (value < 10) + { + idString.append(1, char(value + 48)); + } else + { + idString.append(1, char(value + 55)); + } + } + return idString; +} +std::vector splitString(const std::string &str, const std::string &separator) +{ + std::vector ret; + std::string::size_type pos1, pos2; + pos1 = pos2 = 0; + pos1 = str.find(separator); + while (pos1 != std::string::npos) + { + if (pos1 != 0) + { + std::string item = str.substr(pos2, pos1 - pos2); + ret.push_back(item); + } + pos2 = pos1 + separator.length(); + while (pos2 < str.length() && str.substr(pos2, separator.length()) == separator) + pos2 += separator.length(); + pos1 = str.find(separator, pos2); + } + if (pos2 < str.length()) + ret.push_back(str.substr(pos2)); + return ret; +} +std::string getuuid() +{ + uuid_t uu; + uuid_generate(uu); + return stringToHex(uu, 16); +} - p[0] = p[0] - 48 - ((p[0] >= 'A') ? 7 : 0) - ((p[0] >= 'a') ? 32 : 0); - p[1] = p[1] - 48 - ((p[1] >= 'A') ? 7 : 0) - ((p[1] >= 'a') ? 32 : 0); - decd[j++] = (unsigned char)(p[0] * 16 + p[1]); +std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) { + std::string ret; + int i = 0; + int j = 0; + unsigned char char_array_3[3]; + unsigned char char_array_4[4]; + + while (in_len--) { + char_array_3[i++] = *(bytes_to_encode++); + if (i == 3) { + char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; + char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); + char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); + char_array_4[3] = char_array_3[2] & 0x3f; + + for(i = 0; (i <4) ; i++) + ret += base64_chars[char_array_4[i]]; + i = 0; + } + } + + if (i) + { + for(j = i; j < 3; j++) + char_array_3[j] = '\0'; + + char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; + char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); + char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); + char_array_4[3] = char_array_3[2] & 0x3f; + + for (j = 0; (j < i + 1); j++) + ret += base64_chars[char_array_4[j]]; + + while((i++ < 3)) + ret += '='; } - decd[j] = 0; - return j; -} \ No newline at end of file + return ret; + +} + +std::string base64_decode(std::string const& encoded_string) { + int in_len = encoded_string.size(); + int i = 0; + int j = 0; + int in_ = 0; + unsigned char char_array_4[4], char_array_3[3]; + std::string ret; + + while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { + char_array_4[i++] = encoded_string[in_]; in_++; + if (i ==4) { + for (i = 0; i <4; i++) + char_array_4[i] = base64_chars.find(char_array_4[i]); + + char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); + char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); + char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; + + for (i = 0; (i < 3); i++) + ret += char_array_3[i]; + i = 0; + } + } + + if (i) { + for (j = i; j <4; j++) + char_array_4[j] = 0; + + for (j = 0; j <4; j++) + char_array_4[j] = base64_chars.find(char_array_4[j]); + + char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); + char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); + char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; + + for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; + } + + return ret; +} + +std::string urlDecode(const std::string& szToDecode) +{ + std::string result; + result.reserve(szToDecode.length()); + int hex = 0; + for (size_t i = 0; i < szToDecode.length(); ++i) + { + switch (szToDecode[i]) + { + case '+': + result += ' '; + break; + case '%': + if (isxdigit(szToDecode[i + 1]) && isxdigit(szToDecode[i + 2])) + { + std::string hexStr = szToDecode.substr(i + 1, 2); + hex = strtol(hexStr.c_str(), 0, 16); + //字母和数字[0-9a-zA-Z]、一些特殊符号[$-_.+!*'(),] 、以及某些保留字[$&+,/:;=?@] + //可以不经过编码直接用于URL + if (!((hex >= 48 && hex <= 57) || //0-9 + (hex >=97 && hex <= 122) || //a-z + (hex >=65 && hex <= 90) || //A-Z + //一些特殊符号及保留字[$-_.+!*'(),] [$&+,/:;=?@] + hex == 0x21 || hex == 0x24 || hex == 0x26 || hex == 0x27 || hex == 0x28 || hex == 0x29 + || hex == 0x2a || hex == 0x2b|| hex == 0x2c || hex == 0x2d || hex == 0x2e || hex == 0x2f + || hex == 0x3A || hex == 0x3B|| hex == 0x3D || hex == 0x3f || hex == 0x40 || hex == 0x5f + )) + { + result += char(hex); + i += 2; + } + else result += '%'; + }else { + result += '%'; + } + break; + default: + result += szToDecode[i]; + break; + } + } + return result; +} + diff --git a/lib/src/Utilities.h b/lib/src/Utilities.h index 33242497..bef1710e 100755 --- a/lib/src/Utilities.h +++ b/lib/src/Utilities.h @@ -1 +1,16 @@ -int urldecode(const char* encd,char* decd); \ No newline at end of file +#pragma once + +#include +#include +#include + +bool isInteger(const std::string &str); +std::string genRandomString(int length); +std::string stringToHex(unsigned char* ptr, long long length); +std::vector splitString(const std::string &str,const std::string &separator); +std::string getuuid(); +std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len); +std::string base64_decode(std::string const& encoded_string); +std::string urlDecode(const std::string& szToDecode); + +