|
From: Goran <go...@de...> - 2004-01-04 23:13:14
|
I need to send some data with HTTP POST query to web server, but i am havin=
g=20
problems with urlencode algorithm.
I am using following code:
(g_strconcat is Glib function that does calloc and than strcat to new point=
er)
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
#define hex(ch) ( (char) ( ((ch) >=3D 10) ? ((ch) - 10) + 'A' : ((ch) + '0'=
) ) )
#ifndef is_alnum
#define is_alnum(ch) ( ((ch) >=3D '0' && (ch) <=3D '9') || \
((ch) >=3D 'A' && (ch) <=3D 'Z') || \
((ch) >=3D 'a' && (ch) <=3D 'z') )
#endif
char *=20
urlencode(char *input) {
char *dest;
int len,i;
int j=3D0;
len =3D strlen(input);
dest =3D calloc(len+1,sizeof(char));
for(i =3D 0; i < len; i++ )
{
if( is_alnum(input[i]) || input[i] =3D=3D '_' || input[i] =3D=3D '-')=
=20
{
dest[j] =3D input[i]; j++; =20
}
else if( input[i] =3D=3D ' ')=20
{=20
dest[j] =3D '+'; j++; =20
}
else
{
dest =3D realloc(dest, strlen(dest)+2);
dest[j] =3D '%';
dest[j+1] =3D hex(input[i] >> 4);
dest[j+2] =3D hex(input[i] & 0xF);
j=3Dj+3;
}
}
printf("Len: %i\nString: %s\n", strlen(dest), dest);
input =3D g_strconcat(dest,NULL);
g_free(dest);
return input;
}
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Code is based on function that I found in DsURL library on the web.
But I am having troubles with Latin1 (=C3=9F, =C3=A0,...) and Latin2 (=C4=
=8D, =C4=87, =C5=BE,...) =20
chars. I have UTF8 representation of that chars, and for example web server=
=20
translate "=C3=9F" as "%DF" and my function doesnt - I am getting "%,3%)F".=
It is=20
becouse my string is in UTF8 and this function expects ASCII string. But I=
=20
donot know how to do this for multibyte.
Thanx,
Goran
|