Encode / Decode URLs in C ++

Does anyone know any good C ++ code that does this?

+72
c ++ percent-encoding urlencode urldecode
Sep 30 '08 at 19:21
source share
21 answers

I ran into half of this problem the other day. Unhappy with the options available, and after reviewing this C code example, I decided to collapse my own C ++ url-encode function:

#include <cctype> #include <iomanip> #include <sstream> #include <string> using namespace std; string url_encode(const string &value) { ostringstream escaped; escaped.fill('0'); escaped << hex; for (string::const_iterator i = value.begin(), n = value.end(); i != n; ++i) { string::value_type c = (*i); // Keep alphanumeric and other accepted characters intact if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') { escaped << c; continue; } // Any other characters are percent-encoded escaped << uppercase; escaped << '%' << setw(2) << int((unsigned char) c); escaped << nouppercase; } return escaped.str(); } 

The implementation of the decoding function is left to the reader as an exercise. :P

+68
Jul 17 '13 at 19:40
source share

Answering my own question ...

libcurl has curl_easy_escape for coding.

For decoding, curl_easy_unescape

+65
Sep 30 '08 at 19:41
source share
 string urlDecode(string &SRC) { string ret; char ch; int i, ii; for (i=0; i<SRC.length(); i++) { if (int(SRC[i])==37) { sscanf(SRC.substr(i+1,2).c_str(), "%x", &ii); ch=static_cast<char>(ii); ret+=ch; i=i+2; } else { ret+=SRC[i]; } } return (ret); } 

not the best, but it works fine; -)

+11
Jan 28 2018-11-11T00:
source share

And the source code ...

http://www.codeguru.com/cpp/cpp/string/conversions/article.php/c12759

Body must be at least 30 characters

+9
Sep 30 '08 at 20:44
source share

cpp-netlib has functions

 namespace boost { namespace network { namespace uri { inline std::string decoded(const std::string &input); inline std::string encoded(const std::string &input); } } } 

they make it very easy to encode and decode URL strings.

+9
Aug 15 '14 at 22:36
source share

Usually adding '%' to an int char value will not work during encoding, the value should be equivalent to the hexadecimal equivalent. for example, '/' '% 2F' is not '% 47'.

I think this is the best and concise solution for encoding and decoding url (not many header dependencies).

 string urlEncode(string str){ string new_str = ""; char c; int ic; const char* chars = str.c_str(); char bufHex[10]; int len = strlen(chars); for(int i=0;i<len;i++){ c = chars[i]; ic = c; // uncomment this if you want to encode spaces with + /*if (c==' ') new_str += '+'; else */if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') new_str += c; else { sprintf(bufHex,"%X",c); if(ic < 16) new_str += "%0"; else new_str += "%"; new_str += bufHex; } } return new_str; } string urlDecode(string str){ string ret; char ch; int i, ii, len = str.length(); for (i=0; i < len; i++){ if(str[i] != '%'){ if(str[i] == '+') ret += ' '; else ret += str[i]; }else{ sscanf(str.substr(i + 1, 2).c_str(), "%x", &ii); ch = static_cast<char>(ii); ret += ch; i = i + 2; } } return ret; } 
+8
Apr 30 '15 at 7:59
source share

CGICC includes url encoding and decoding methods. form_urlencode and form_urldecode

+6
Sep 30 '08 at 19:27
source share

Url encoding / decoding algorithm is not so difficult.

I would start with the specification:

Wikipedia URL Encoding

If you want pre-prepared code, just search the web:

http://www.google.it/search?hl=it&q=Encode+Decode+URLs+in+C%2B%2B&meta=

(yep, this URL is URL encoded)

+6
Sep 30 '08 at 19:30
source share

[Necromancer Mode]
I came across this question when I was looking for a fast, modern, platform-independent and elegant solution. I didn’t like it above, cpp-netlib would be a winner, but it had a terrible memory vulnerability in the “decoded” function. So, I came up with a qi / karma solution to increase the pressure.

 namespace bsq = boost::spirit::qi; namespace bk = boost::spirit::karma; bsq::int_parser<unsigned char, 16, 2, 2> hex_byte; template <typename InputIterator> struct unescaped_string : bsq::grammar<InputIterator, std::string(char const *)> { unescaped_string() : unescaped_string::base_type(unesc_str) { unesc_char.add("+", ' '); unesc_str = *(unesc_char | "%" >> hex_byte | bsq::char_); } bsq::rule<InputIterator, std::string(char const *)> unesc_str; bsq::symbols<char const, char const> unesc_char; }; template <typename OutputIterator> struct escaped_string : bk::grammar<OutputIterator, std::string(char const *)> { escaped_string() : escaped_string::base_type(esc_str) { esc_str = *(bk::char_("a-zA-Z0-9_.~-") | "%" << bk::right_align(2,0)[bk::hex]); } bk::rule<OutputIterator, std::string(char const *)> esc_str; }; 

Use above as below:

 std::string unescape(const std::string &input) { std::string retVal; retVal.reserve(input.size()); typedef std::string::const_iterator iterator_type; char const *start = ""; iterator_type beg = input.begin(); iterator_type end = input.end(); unescaped_string<iterator_type> p; if (!bsq::parse(beg, end, p(start), retVal)) retVal = input; return retVal; } std::string escape(const std::string &input) { typedef std::back_insert_iterator<std::string> sink_type; std::string retVal; retVal.reserve(input.size() * 3); sink_type sink(retVal); char const *start = ""; escaped_string<sink_type> g; if (!bk::generate(sink, g(start), input)) retVal = input; return retVal; } 

[Necromancer is off]

EDIT01: Zero Add-ons Fixed - Special Thanks to Hartmouth Kaiser
EDIT02: Live on CoLiRu

+6
Apr 16 '15 at 12:36
source share

Inspired by xperroni, I wrote a decoder. Thanks for the pointer.

 #include <iostream> #include <sstream> #include <string> using namespace std; char from_hex(char ch) { return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10; } string url_decode(string text) { char h; ostringstream escaped; escaped.fill('0'); for (auto i = text.begin(), n = text.end(); i != n; ++i) { string::value_type c = (*i); if (c == '%') { if (i[1] && i[2]) { h = from_hex(i[1]) << 4 | from_hex(i[2]); escaped << h; i += 2; } } else if (c == '+') { escaped << ' '; } else { escaped << c; } } return escaped.str(); } int main(int argc, char** argv) { string msg = "J%C3%B8rn!"; cout << msg << endl; string decodemsg = url_decode(msg); cout << decodemsg << endl; return 0; } 

edit: Removed unnecessary cctype and iomainip.

+5
Sep 15 '15 at 21:25
source share

Adding a recommendation to Bill's recommendation on using libcurl: big suggestion and update:
after 3 years, the curl_escape function is deprecated, so curl_easy_escape is better for future use.

+4
Jun 28 '11 at 10:11
source share

I ended this question when looking for api to decode url in win32 C ++ application. Since the question does not completely determine the platform, assuming windows is not bad.

InternetCanonicalizeUrl is an API for Windows programs. More here

  LPTSTR lpOutputBuffer = new TCHAR[1]; DWORD dwSize = 1; BOOL fRes = ::InternetCanonicalizeUrl(strUrl, lpOutputBuffer, &dwSize, ICU_DECODE | ICU_NO_ENCODE); DWORD dwError = ::GetLastError(); if (!fRes && dwError == ERROR_INSUFFICIENT_BUFFER) { delete lpOutputBuffer; lpOutputBuffer = new TCHAR[dwSize]; fRes = ::InternetCanonicalizeUrl(strUrl, lpOutputBuffer, &dwSize, ICU_DECODE | ICU_NO_ENCODE); if (fRes) { //lpOutputBuffer has decoded url } else { //failed to decode } if (lpOutputBuffer !=NULL) { delete [] lpOutputBuffer; lpOutputBuffer = NULL; } } else { //some other error OR the input string url is just 1 char and was successfully decoded } 

InternetCrackUrl ( here ) also has flags to indicate whether to decrypt the URL

+4
Jan 04 2018-12-12T00:
source share

I could not find decoding / unescape URI here, which also decodes 2 and 3 byte sequences. Introducing its own high-performance version, which on-the-fly converts input c to wstring:

 #include <string> const char HEX2DEC[55] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1, -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,10,11,12, 13,14,15 }; #define __x2d__(s) HEX2DEC[*(s)-48] #define __x2d2__(s) __x2d__(s) << 4 | __x2d__(s+1) std::wstring decodeURI(const char * s) { unsigned char b; std::wstring ws; while (*s) { if (*s == '%') if ((b = __x2d2__(s + 1)) >= 0x80) { if (b >= 0xE0) { // three byte codepoint ws += ((b & 0b00001111) << 12) | ((__x2d2__(s + 4) & 0b00111111) << 6) | (__x2d2__(s + 7) & 0b00111111); s += 9; } else { // two byte codepoint ws += (__x2d2__(s + 4) & 0b00111111) | (b & 0b00000011) << 6; s += 6; } } else { // one byte codepoints ws += b; s += 3; } else { // no % ws += *s; s++; } } return ws; } 
+3
Jan 02 '17 at 23:16
source share

The Windows API has UrlEscape / UrlUnescape functions exported by shlwapi.dll for this task.

+2
Oct 23 '14 at 0:47
source share

This version is pure C and can optionally normalize the resource path. Using it with C ++ is trivial:

 #include <string> #include <iostream> int main(int argc, char** argv) { const std::string src("/some.url/foo/../bar/%2e/"); std::cout << "src=\"" << src << "\"" << std::endl; // either do it the C++ conformant way: char* dst_buf = new char[src.size() + 1]; urldecode(dst_buf, src.c_str(), 1); std::string dst1(dst_buf); delete[] dst_buf; std::cout << "dst1=\"" << dst1 << "\"" << std::endl; // or in-place with the &[0] trick to skip the new/delete std::string dst2; dst2.resize(src.size() + 1); dst2.resize(urldecode(&dst2[0], src.c_str(), 1)); std::cout << "dst2=\"" << dst2 << "\"" << std::endl; } 

Outputs:

 src="/some.url/foo/../bar/%2e/" dst1="/some.url/bar/" dst2="/some.url/bar/" 

And the actual function:

 #include <stddef.h> #include <ctype.h> /** * decode a percent-encoded C string with optional path normalization * * The buffer pointed to by @dst must be at least strlen(@src) bytes. * Decoding stops at the first character from @src that decodes to null. * Path normalization will remove redundant slashes and slash+dot sequences, * as well as removing path components when slash+dot+dot is found. It will * keep the root slash (if one was present) and will stop normalization * at the first questionmark found (so query parameters won't be normalized). * * @param dst destination buffer * @param src source buffer * @param normalize perform path normalization if nonzero * @return number of valid characters in @dst * @author Johan Lindh <johan@linkdata.se> * @legalese BSD licensed (http://opensource.org/licenses/BSD-2-Clause) */ ptrdiff_t urldecode(char* dst, const char* src, int normalize) { char* org_dst = dst; int slash_dot_dot = 0; char ch, a, b; do { ch = *src++; if (ch == '%' && isxdigit(a = src[0]) && isxdigit(b = src[1])) { if (a < 'A') a -= '0'; else if(a < 'a') a -= 'A' - 10; else a -= 'a' - 10; if (b < 'A') b -= '0'; else if(b < 'a') b -= 'A' - 10; else b -= 'a' - 10; ch = 16 * a + b; src += 2; } if (normalize) { switch (ch) { case '/': if (slash_dot_dot < 3) { /* compress consecutive slashes and remove slash-dot */ dst -= slash_dot_dot; slash_dot_dot = 1; break; } /* fall-through */ case '?': /* at start of query, stop normalizing */ if (ch == '?') normalize = 0; /* fall-through */ case '\0': if (slash_dot_dot > 1) { /* remove trailing slash-dot-(dot) */ dst -= slash_dot_dot; /* remove parent directory if it was two dots */ if (slash_dot_dot == 3) while (dst > org_dst && *--dst != '/') /* empty body */; slash_dot_dot = (ch == '/') ? 1 : 0; /* keep the root slash if any */ if (!slash_dot_dot && dst == org_dst && *dst == '/') ++dst; } break; case '.': if (slash_dot_dot == 1 || slash_dot_dot == 2) { ++slash_dot_dot; break; } /* fall-through */ default: slash_dot_dot = 0; } } *dst++ = ch; } while(ch); return (dst - org_dst) - 1; } 
+1
Nov 09 '13 at 10:44
source share

juicy bits

 #include <ctype.h> // isdigit, tolower from_hex(char ch) { return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10; } char to_hex(char code) { static char hex[] = "0123456789abcdef"; return hex[code & 15]; } 

noting that

 char d = from_hex(hex[0]) << 4 | from_hex(hex[1]); 

how in

 // %7B = '{' char d = from_hex('7') << 4 | from_hex('B'); 
+1
May 28 '15 at 7:03
source share

You can use the g_uri_escape_string () function provided by glib.h. https://developer.gnome.org/glib/stable/glib-URI-Functions.html

 #include <stdio.h> #include <stdlib.h> #include <glib.h> int main() { char *uri = "http://www.example.com?hello world"; char *encoded_uri = NULL; //as per wiki (https://en.wikipedia.org/wiki/Percent-encoding) char *escape_char_str = "!*'();:@&=+$,/?#[]"; encoded_uri = g_uri_escape_string(uri, escape_char_str, TRUE); printf("[%s]\n", encoded_uri); free(encoded_uri); return 0; } 

compile it with

 gcc encoding_URI.c `pkg-config --cflags --libs glib-2.0` 
+1
Mar 11 '16 at 13:39
source share

Another solution is available via the Facebook folly library : folly::uriEscape and folly::uriUnescape .

0
Apr 05 '16 at 16:36
source share

You can just use the AtlEscapeUrl () function from atlutil.h, just check out the documentation for using it.

0
Jan 22 '18 at 10:16
source share

Will have to do this in a project without Boost. So, finished writing my own. I will just put it on GitHub: https://github.com/corporateshark/LUrlParser

 clParseURL URL = clParseURL::ParseURL( "https://name:pwd@github.com:80/path/res" ); if ( URL.IsValid() ) { cout << "Scheme : " << URL.m_Scheme << endl; cout << "Host : " << URL.m_Host << endl; cout << "Port : " << URL.m_Port << endl; cout << "Path : " << URL.m_Path << endl; cout << "Query : " << URL.m_Query << endl; cout << "Fragment : " << URL.m_Fragment << endl; cout << "User name : " << URL.m_UserName << endl; cout << "Password : " << URL.m_Password << endl; } 
-one
Feb 04 '15 at 16:27
source share

I know that the question is asked by the C ++ method, but for those who might need it, I came up with a very short function in simple C to encode a string. It does not create a new line, but changes the existing one, which means that it must be large enough to hold the new line. It’s very easy to keep up.

 void urlEncode(char *string) { char charToEncode; int posToEncode; while (((posToEncode=strspn(string,"1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.~"))!=0) &&(posToEncode<strlen(string))) { charToEncode=string[posToEncode]; memmove(string+posToEncode+3,string+posToEncode+1,strlen(string+posToEncode)); string[posToEncode]='%'; string[posToEncode+1]="0123456789ABCDEF"[charToEncode>>4]; string[posToEncode+2]="0123456789ABCDEF"[charToEncode&0xf]; string+=posToEncode+3; } } 
-one
Nov 10 '15 at 21:04
source share



All Articles