G
gert
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 ?
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 ?