Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   How Do I Urlencode A String In C? (http://www.programmingforums.org/showthread.php?t=15443)

Sane Mar 19th, 2008 4:04 PM

How Do I Urlencode A String In C?
 
I know the function in other languages is "urlencode". Is there a library, or function I can call in C to urlencode a string? For example, turning spaces into %20?

Seif Mar 19th, 2008 5:23 PM

Re: How Do I Urlencode A String In C?
 
I don't think there is an equivalent in the C standard library. There are a few implementations about online. But you're prob best writing your own, its not too difficult.

Sane Mar 20th, 2008 12:44 AM

Re: How Do I Urlencode A String In C?
 
Could one essentially make a very naive implementation, by replacing every single character with its two-digit hex value? For example, with the string 's', printf("%02X", s[i]) for every 'i' from 1 .. strlen(s)? What are the possible drawbacks from this (besides wasted space)? And are there ways to slightly improve this method?

Jimbo Mar 20th, 2008 2:15 AM

Re: How Do I Urlencode A String In C?
 
Quote:

Originally Posted by Sane (Post 142696)
Could one essentially make a very naive implementation, by replacing every single character with its two-digit hex value? For example, with the string 's', printf("%02X", s[i]) for every 'i' from 1 .. strlen(s)? What are the possible drawbacks from this (besides wasted space)? And are there ways to slightly improve this method?

Don't change them if isalpha(s[i]) || s[i] == '/'. This makes assumptions on the context of the data (e.g. / will be part of the URL and shouldn't be encoded, you're not allowing a protocol handler in the string, and you're not allowing something of the form http://someone@somesite.com, for examples). You'd also have to account for a bunch of other symbols used in URLs, such as '%', '&', '+', and '.' to name a couple.

lectricpharaoh Mar 20th, 2008 4:57 AM

Re: How Do I Urlencode A String In C?
 
What character set/encoding are you using?

You could create a table of strings, and each character value mapped to a string. Then you run each character of the URL through a function that uses it as a lookup into the table, and replaces it with the associated string. You could save space by using NULL for ones that didn't need conversion (alphanumeric characters, forward slashes, etc). The only problem I see with this approach is the table gets really big when you're using a character set besides plain ASCII.

Sane Mar 20th, 2008 6:37 AM

Re: How Do I Urlencode A String In C?
 
Just plain ASCII. And the urlencode is only being processed on the GET arguments of the URL, before plugging them into the URL to be sent (in which case, characters like '&' do need to be encoded).

Ooble Mar 20th, 2008 9:23 AM

Re: How Do I Urlencode A String In C?
 
I think you're right: printf (or, rather, sprintf) is the way to go. For complete safety, I'd recommend doing essentially as Jimbo says and only parsing it if isalnum returns false - that way, you won't have to convert the majority of characters but you'll still catch anything that could even potentially cause a problem.

Sane Mar 20th, 2008 10:38 AM

Re: How Do I Urlencode A String In C?
 
Thanks guys.

:

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int urlencode(char *dest, const char *src) {
  5.     /* urlencode all non-alphanumeric characters in the C-string 'src'
  6.        store result in the C-string 'dest'
  7.        return the length of the url encoded C-string
  8.    */
  9.     char *d;
  10.     int i;
  11.     for(i=0, d=dest; src[i]; i++) {
  12.         if(isalnum(src[i])) *(d++) = src[i];
  13.         else {
  14.             sprintf(d, "%%%02X", src[i]);
  15.             d += 3;
  16.         }
  17.     } 
  18.     *d = 0;
  19.     return d-dest;
  20. }
  21.  
  22. int main() {
  23.     char buff[1024];
  24.     int len;
  25.  
  26.     len = urlencode(buff, "Hello 2 The World! $%^&*()");
  27.     printf("[%d] %s\n", len, buff);
  28.  
  29.     return 0;
  30. }


Sane Mar 20th, 2008 11:35 AM

Re: How Do I Urlencode A String In C?
 
Forgot to add:

:

  1. #include <ctype.h>


Which may or may not be C99. I forget. Will check the reference when I get home if someone doesn't correct me before then.

So I may have to swap isalnum with some other functions.

Ooble Mar 20th, 2008 12:31 PM

Re: How Do I Urlencode A String In C?
 
It is. A point of interest: I was just reading the Wikipedia entry on it to find out the answer to your question, and found this:
Quote:

In all cases the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF. If the argument has any other value, the behavior is undefined.
You have to use an unsigned char, not a char (which can be signed or unsigned, depending on the implementation).


All times are GMT -5. The time now is 4:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC