Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > int urldecode(char *src, char *last, char *dest)

Reply
Thread Tools

int urldecode(char *src, char *last, char *dest)

 
 
gert
Guest
Posts: n/a
 
      02-12-2007
This based on a example i found at http://www.cs.tut.fi/~jkorpela/
forms/cgic.html

#include <fcgi_stdio.h>
#include <stdlib.h>

int urldecode(char *src, char *last, char *dest){
int code;
for (; src != last; src++, dest++){
if (*src == '+') *dest = ' ';
else if(*src == '%') {
if(sscanf(src+1, "%2x", &code) != 1) code = '?';
*dest = code;
src +=2;
}
else *dest = *src;
}
*dest = '\n';
*++dest = '\0';
return 0;
}

int post(char *output){
char *lenstr=getenv("CONTENT_LENGTH");
char *input;
long len;
if (lenstr != NULL && sscanf(lenstr, "%ld", &len) == 1) {
fgets(input, len+1, stdin);
urldecode(input, input+len, output);
}
return 0;
}

int main (void){
char *output;
while (FCGI_Accept() >= 0) {
post(output);
printf("Content-Type: text/xml; charset=utf-8"
"\r\n"
"\r\n"
"%s"
"\n"
,output);
}
return 0;
}

The only thing i did is simplified it a bit so i could understand it a
bit better. The goal is to read the post from a xmlhttprequest and
return its contents back.

So far i receive the post but the urldecoding seems not to play along
and scrambles the output, any suggestions ?

 
Reply With Quote
 
 
 
 
gert
Guest
Posts: n/a
 
      02-12-2007
On Feb 13, 12:31 am, "gert" <(E-Mail Removed)> wrote:
> This based on a example i found athttp://www.cs.tut.fi/~jkorpela/
> forms/cgic.html
>
> #include <fcgi_stdio.h>
> #include <stdlib.h>
>
> int urldecode(char *src, char *last, char *dest){
> int code;
> for (; src != last; src++, dest++){
> if (*src == '+') *dest = ' ';
> else if(*src == '%') {
> if(sscanf(src+1, "%2x", &code) != 1) code = '?';
> *dest = code;
> src +=2;
> }
> else *dest = *src;
> }
> *dest = '\n';
> *++dest = '\0';
> return 0;
>
> }
>
> int post(char *output){
> char *lenstr=getenv("CONTENT_LENGTH");
> char *input;
> long len;
> if (lenstr != NULL && sscanf(lenstr, "%ld", &len) == 1) {
> fgets(input, len+1, stdin);
> urldecode(input, input+len, output);
> }
> return 0;
>
> }
>
> int main (void){
> char *output;
> while (FCGI_Accept() >= 0) {
> post(output);
> printf("Content-Type: text/xml; charset=utf-8"
> "\r\n"
> "\r\n"
> "%s"
> "\n"
> ,output);
> }
> return 0;
>
> }
>
> The only thing i did is simplified it a bit so i could understand it a
> bit better. The goal is to read the post from a xmlhttprequest and
> return its contents back.
>
> So far i receive the post but the urldecoding seems not to play along
> and scrambles the output, any suggestions ?


better do it like this i think

if (lenstr != NULL ) {
sscanf(lenstr, "%ld", &len)
fgets(input, len+1, stdin);
urldecode(input, input+len, output);
}

still scrambely doh ?

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-13-2007
gert wrote:
> This based on a example i found at http://www.cs.tut.fi/~jkorpela/
> forms/cgic.html
>

<snip code>
>
> The only thing i did is simplified it a bit so i could understand it a
> bit better. The goal is to read the post from a xmlhttprequest and
> return its contents back.
>
> So far i receive the post but the urldecoding seems not to play along
> and scrambles the output, any suggestions ?
>

Write some tests in order to understand how the code works.

--
Ian Collins.
 
Reply With Quote
 
Christopher Layne
Guest
Posts: n/a
 
      02-13-2007
gert wrote:

> This based on a example i found at http://www.cs.tut.fi/~jkorpela/
> forms/cgic.html
>
> #include <fcgi_stdio.h>
> #include <stdlib.h>
>
> int urldecode(char *src, char *last, char *dest){
> int code;
> for (; src != last; src++, dest++){
> if (*src == '+') *dest = ' ';
> else if(*src == '%') {
> if(sscanf(src+1, "%2x", &code) != 1) code = '?';
> *dest = code;
> src +=2;
> }
> else *dest = *src;
> }
> *dest = '\n';
> *++dest = '\0';
> return 0;
> }


I had written something similar for a toy cgi form util I did a while back...
It's not super well checked for errors, but I do remember it atleast working.

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

struct cgi_var__ {
char *key;
char *val;
size_t key_l;
size_t val_l;
};

static int form_decode(cgi_var *cv, size_t off)
{
char c, tmp[4];
int r;

if (cv->val[off] != '%') {
fprintf(stdout, "form_decode: bad value\n");
return -1;
}

if ((r = (cv->val + cv->val_l) - (cv->val + off)) < 3) {
fprintf(stdout, "form_decode: length too short\n");
return -1;
}

memcpy(tmp, cv->val + off, MIN(3, r)); tmp[3] = '\0';
c = (unsigned char)strtol(tmp + 1, NULL, 16);

/*
* data == text%7Cj%25%5C%25
* 01234567890123456
*
* c == |, off == 4, r == 13
* c == %, off == 6, r == 9
* c == \, off == 7, r == 6
* c == %, off == 8, r == 3
*/

memcpy(cv->val + off, &c, sizeof c);
memmove(cv->val + off + 1, cv->val + off + 3, r - 3);

cv->val_l -= 2;
r = (cv->val + cv->val_l) - (cv->val + off);

return 0;
}

static int cv_filter(cgi_var *cv)
{
size_t i;

/* filter out rug-rats or decode %xx */
for (i = 0; i < cv->val_l; i++) {
if (cv->val[i] == '%')
form_decode(cv, i);
switch (cv->val[i]) {
case '+': case '\'': case '`': case '"':
cv->val[i] = ' '; break;
default: break;
}
}

return 0;
}

 
Reply With Quote
 
gert
Guest
Posts: n/a
 
      02-14-2007
On Feb 13, 12:31 am, "gert" <(E-Mail Removed)> wrote:
> This based on a example i found athttp://www.cs.tut.fi/~jkorpela/
> forms/cgic.html
>
> #include <fcgi_stdio.h>
> #include <stdlib.h>
>
> int urldecode(char *src, char *last, char *dest){
> int code;
> for (; src != last; src++, dest++){
> if (*src == '+') *dest = ' ';
> else if(*src == '%') {
> if(sscanf(src+1, "%2x", &code) != 1) code = '?';
> *dest = code;
> src +=2;
> }
> else *dest = *src;
> }
> *dest = '\n';
> *++dest = '\0';
> return 0;
>
> }
>
> int post(char *output){
> char *lenstr=getenv("CONTENT_LENGTH");
> char *input;
> long len;
> if (lenstr != NULL && sscanf(lenstr, "%ld", &len) == 1) {
> fgets(input, len+1, stdin);
> urldecode(input, input+len, output);
> }
> return 0;
>
> }
>
> int main (void){
> char *output;
> while (FCGI_Accept() >= 0) {
> post(output);
> printf("Content-Type: text/xml; charset=utf-8"
> "\r\n"
> "\r\n"
> "%s"
> "\n"
> ,output);
> }
> return 0;
>
> }
>


My test tool doesnt work segmant fault, something to do with
stdin ???

#! /bin/sh
gcc -I /usr/local/include/ -L /usr/local/lib/ -l fcgi -o /home/gert/
Desktop/svn/db/bin/c /home/gert/Desktop/svn/db/bin/c.c
env CONTENT_LENGTH=3 /home/gert/Desktop/svn/db/bin/c << "test"


 
Reply With Quote
 
Christopher Layne
Guest
Posts: n/a
 
      02-14-2007
gert wrote:

>> char *input;
>> long len;
>> if (lenstr != NULL && sscanf(lenstr, "%ld", &len) == 1) {
>> fgets(input, len+1, stdin);


Uhh, fgets() needs a valid buffer to be writing into. You're just giving it a
pointer to an undefined location. Nothing has been allocated for it. There
are more basic things that need to be solved before you can approach even
getting your code to do what it is you want it to do.
 
Reply With Quote
 
gert
Guest
Posts: n/a
 
      02-14-2007
On Feb 14, 6:24 am, Christopher Layne <(E-Mail Removed)> wrote:
> gert wrote:
> >> char *input;
> >> long len;
> >> if (lenstr != NULL && sscanf(lenstr, "%ld", &len) == 1) {
> >> fgets(input, len+1, stdin);

>
> Uhh, fgets() needs a valid buffer to be writing into. You're just giving it a
> pointer to an undefined location. Nothing has been allocated for it. There
> are more basic things that need to be solved before you can approach even
> getting your code to do what it is you want it to do.


And there goes my plans for the weekend
Better start with valid buffer then, this part came straight from the
example at http://www.cs.tut.fi/~jkorpela/forms/cgic.html

fgets(input, len+1, stdin);

Any other full cgi post examples please ?

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      02-14-2007
gert wrote, On 14/02/07 19:40:
> On Feb 14, 6:24 am, Christopher Layne <(E-Mail Removed)> wrote:
>> gert wrote:
>>>> char *input;
>>>> long len;
>>>> if (lenstr != NULL && sscanf(lenstr, "%ld", &len) == 1) {
>>>> fgets(input, len+1, stdin);

>> Uhh, fgets() needs a valid buffer to be writing into. You're just giving it a
>> pointer to an undefined location. Nothing has been allocated for it. There
>> are more basic things that need to be solved before you can approach even
>> getting your code to do what it is you want it to do.

>
> And there goes my plans for the weekend
> Better start with valid buffer then, this part came straight from the
> example at http://www.cs.tut.fi/~jkorpela/forms/cgic.html
>
> fgets(input, len+1, stdin);


That specific line does, but the definition of input is different. You
have defined it as a pointer to char, the code at that URL defines it as
a char array.

> Any other full cgi post examples please ?


I don't, but I suggest you start looking at the comp.lang.c FAQ (Google
will find it) and buy a copy of K&R2 (the full details are in the
bibliography of the FAQ). Then you can start learning C properly since
you need to know the language in order to write CGIs in it.
--
Flash Gordon
 
Reply With Quote
 
gert
Guest
Posts: n/a
 
      02-14-2007
#include <fcgi_stdio.h>
#include <stdlib.h>

int urldecode(char *src, char *last, char *dest){
int code;
for (; src != last; src++, dest++){
if (*src == '+') *dest = ' ';
else if(*src == '%') {
if(sscanf(src+1, "%2x", &code) != 1) code = '?';
*dest = code;
src +=2;
}
else *dest = *src;
}
*dest = '\n';
*++dest = '\0';
return 0;
}

int post(void){
char input[100];
char *lenstr;
long len;
lenstr=getenv("CONTENT_LENGTH");
if (lenstr != NULL && sscanf(lenstr,"%ld",&len)==1 ) {
fgets(input, len+1, stdin);
//urldecode(input, input+len, input);
printf("Content-Type: text/xml; charset=utf-8"
"\r\n"
"\r\n"
"<xml>%s %d</xml>"
"\n"
,input,len);
}
return 0;
}

int main (void){
char * input;
while (FCGI_Accept() >= 0) {
post();
}
return 0;
}

So far this works thx to flash gorden pointing out the char pointer

Can somebody explain why char * instead of [] only let me post 11
characters ? 12 characters equals segfault ?

my tool still displays hebrew doh but at least it doesnt give me a
segfault ?

#! /bin/sh
gcc -I /usr/local/include/ -L /usr/local/lib/ -l fcgi -o /home/gert/
Desktop/svn/db/bin/c /home/gert/Desktop/svn/db/bin/c.c
sudo /etc/init.d/lighttpd restart
env CONTENT_LENGTH=3 /home/gert/Desktop/svn/db/bin/c << "test" << EOF

gert@gert:~$ Desktop/svn/db/bin/make.sh
* Stopping web server
lighttpd [ ok ]
* Starting web server
lighttpd [ ok ]
Content-Type: text/xml; charset=utf-8

<xml>x+5?++ 3</xml>
gert@gert:~$

 
Reply With Quote
 
gert
Guest
Posts: n/a
 
      02-14-2007
On Feb 15, 12:32 am, "gert" <(E-Mail Removed)> wrote:
> #include <fcgi_stdio.h>
> #include <stdlib.h>
>
> int urldecode(char *src, char *last, char *dest){
> int code;
> for (; src != last; src++, dest++){
> if (*src == '+') *dest = ' ';
> else if(*src == '%') {
> if(sscanf(src+1, "%2x", &code) != 1) code = '?';
> *dest = code;
> src +=2;
> }
> else *dest = *src;
> }
> *dest = '\n';
> *++dest = '\0';
> return 0;
>
> }
>
> int post(void){
> char input[100];
> char *lenstr;
> long len;
> lenstr=getenv("CONTENT_LENGTH");
> if (lenstr != NULL && sscanf(lenstr,"%ld",&len)==1 ) {
> fgets(input, len+1, stdin);
> //urldecode(input, input+len, input);
> printf("Content-Type: text/xml; charset=utf-8"
> "\r\n"
> "\r\n"
> "<xml>%s %d</xml>"
> "\n"
> ,input,len);
> }
> return 0;
>
> }
>
> int main (void){
> char * input;
> while (FCGI_Accept() >= 0) {
> post();
> }
> return 0;
>
> }
>
> So far this works thx to flash gorden pointing out the char pointer
>
> Can somebody explain why char * instead of [] only let me post 11
> characters ? 12 characters equals segfault ?


Never mind i think i know why after reading Christopher Layne and
Flash Gordon answers a few times.

fgets just need some memory, not a pointer. PS can i make the []
dynamic with the sizeof the post without using maloc ?

> my tool still displays hebrew doh but at least it doesnt give me a
> segfault ?
>
> #! /bin/sh
> gcc -I /usr/local/include/ -L /usr/local/lib/ -l fcgi -o /home/gert/
> Desktop/svn/db/bin/c /home/gert/Desktop/svn/db/bin/c.c
> sudo /etc/init.d/lighttpd restart
> env CONTENT_LENGTH=3 /home/gert/Desktop/svn/db/bin/c << "test" << EOF
>
> gert@gert:~$ Desktop/svn/db/bin/make.sh
> * Stopping web server
> lighttpd [ ok ]
> * Starting web server
> lighttpd [ ok ]
> Content-Type: text/xml; charset=utf-8
>
> <xml>x+5?++ 3</xml>
> gert@gert:~$


Still want to know about my tool that speak some foreign language "x+5?
++ 3" what ever that means or sound like

If you people dont mind telling me or point something out ?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
newbie: char* int and char *int trey C Programming 7 09-10-2003 03:24 AM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments