C - URL Encoding

Is there an easy way to encode URLs in C? I am using libcurl but have not found a method. In particular, I need to fulfill the percent shoots.

+6
source share
3 answers

curl_escape

which apparently was replaced by

curl_easy_escape

+7
source

In C, based on wikipedia, without the need to allocate and release. Ensure that the output buffer has at least 3 input URLs. Usually you only need to encode to 4K, as URLs tend to be short, so just do it on the stack.

 char rfc3986[256] = {0}; char html5[256] = {0}; void url_encoder_rfc_tables_init(){ int i; for (i = 0; i < 256; i++){ rfc3986[i] = isalnum( i) || i == '~' || i == '-' || i == '.' || i == '_' ? i : 0; html5[i] = isalnum( i) || i == '*' || i == '-' || i == '.' || i == '_' ? i : (i == ' ') ? '+' : 0; } } char *url_encode( char *table, unsigned char *s, char *enc){ for (; *s; s++){ if (table[*s]) sprintf( enc, "%c", table[*s]); else sprintf( enc, "%%%02X", *s); while (*++enc); } return( enc); } 

Use it like this:

 url_encoder_rfc_tables_init(); url_encode( html5, url, url_encoded); 
+9
source

I wrote this to also take care of the encoding of the request string of the space character

Usage: UrlEncode (" http://www.example.com/index.html?Hello=World ", ": /", buffer, buf_size)

url The url string to encode. May be a string literal or a string array

encode : a character string with a null character to encode. This is a good reason why you can determine at runtime how many URLs to encode.

buffer : buffer for storing a new line

size : buffer size

return Returns the size of the new line if the buffer is large enough or returns the required buffer size if the buffer is not large enough. You can double-tap this function if you want to select the desired size.

  int UrlEncode(char* url, char* encode, char* buffer, unsigned int size) { char chars[127] = {0}; unsigned int length = 0; if(!url || !encode || !buffer) return 0; //Create an array to hold ascii chars, loop through encode string //and assign to place in array. I used this construct instead of a large if statement for speed. while(*encode) chars[*encode++] = *encode; //Loop through url, if we find an encode char, replace with % and add hex //as ascii chars. Move buffer up by 2 and track the length needed. //If we reach the query string (?), move to query string encoding URLENCODE_BASE_URL: while(size && (*buffer = *url)) { if(*url == '?') goto URLENCODE_QUERY_STRING; if(chars[*url] && size > 2) { *buffer++ = '%'; itoa(*url, buffer, 16); buffer++; size-=2; length+=2; } url++, buffer++, size--; length++; } goto URLENCODE_RETURN; //Same as above but on spaces (' '), replace with plus ('+') and convert //to hex ascii. I moved this out into a separate loop for speed. URLENCODE_QUERY_STRING: while(size && (*buffer = *url)) { if(chars[*url] && size > 2) { *buffer++ = '%'; if(*url == ' ') itoa('+', buffer, 16); else itoa(*url, buffer, 16); buffer++; size-=2; length+=2; } url++, buffer++, size--; length++; } //Terminate the end of the buffer, and if the buffer wasn't large enough //calc the rest of the url length and return URLENCODE_RETURN: *buffer = '\0'; if(*url) while(*url) { if(chars[*url]) length+=2; url++; length++; } return length; } 

This function pretty much handles most (if not all) of the URL encoders you need. Best of all - it's really fast!

0
source

Source: https://habr.com/ru/post/887013/


All Articles