Last active
November 10, 2018 14:32
-
-
Save maldevel/280b6c3326a7805fa8ab6a162dadb2a1 to your computer and use it in GitHub Desktop.
Dexter URL encoding code snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://github.com/twelvesec/dexter | |
//GNU General Public License v3.0 | |
//@maldevel | |
//... | |
std::string libencode::url_encode(std::string uri) { | |
std::string encoded; | |
DWORD len = (DWORD)uri.length(); | |
char *tmp; | |
HRESULT result; | |
if ((tmp = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1)) == NULL) { | |
return ""; | |
} | |
if ((result = UrlEscapeA(uri.c_str(), tmp, &len, URL_ESCAPE_SEGMENT_ONLY | URL_ESCAPE_PERCENT)) != S_OK && result != E_POINTER) { | |
int error = GetLastError(); | |
HeapFree(GetProcessHeap(), 0, tmp); | |
tmp = NULL; | |
return ""; | |
} | |
HeapFree(GetProcessHeap(), 0, tmp); | |
tmp = NULL; | |
if ((tmp = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len)) == NULL) { | |
return ""; | |
} | |
if ((result = UrlEscapeA(uri.c_str(), tmp, &len, URL_ESCAPE_SEGMENT_ONLY | URL_ESCAPE_PERCENT)) != S_OK && result != E_POINTER) { | |
HeapFree(GetProcessHeap(), 0, tmp); | |
tmp = NULL; | |
return ""; | |
} | |
tmp[len] = 0; | |
encoded = std::string(tmp); | |
HeapFree(GetProcessHeap(), 0, tmp); | |
tmp = NULL; | |
std::string tosearch = "+"; | |
std::string replace = "%2b"; | |
size_t pos = encoded.find(tosearch); | |
while (pos != std::string::npos) | |
{ | |
encoded.replace(pos, tosearch.size(), replace); | |
pos = encoded.find(tosearch, pos + tosearch.size()); | |
} | |
return encoded; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment