PDA
Partners

View Full Version : CGI script problem written in C


whiteknight
February 11th, 2004, 05:45 PM
yeah, i know this is the perl forum, but it is as close to a C forum as I could get here, plus, i am doing CGI work, so it almost fits...

anyway, I have a script that i am writting that simply wont display on my browser. I POST data to it from a very simple form, and i always get ERROR 500 "premature end of script headers"

I assume I am reading the post data wrong, because when i dont read any data, i dont get any error.

IIRC, the code for reading this post data would be

char content_data[512];
scanf("%s", content_data);

and then i can manipulate the string data from there.

of course, this might not be the problem at all.

I am printing functional MIME headers...

ggx
February 14th, 2004, 02:32 AM
Sorry pal, I have no idea. But, you surely printed a blank line after the HTTP header, didn't you?

whiteknight
February 14th, 2004, 10:52 AM
yeah, i figured out my problem. i did print the correct MIME header, but i didnt get the input data correctly. getting the data is a lengthy and complicated process, so if anybody is interested in how to do it, let me know.

Matt
February 14th, 2004, 11:04 AM
Originally posted by whiteknight
yeah, i figured out my problem. i did print the correct MIME header, but i didnt get the input data correctly. getting the data is a lengthy and complicated process, so if anybody is interested in how to do it, let me know. Feel free to post your solution for further references. :)

whiteknight
February 14th, 2004, 02:47 PM
oh, good call matt. I'm sure it might be usefull eventually, if only on accident...


#include <stdlib>
#include <stdio>

...

char *query_string;
int content_length;

content_length = atoi(getenv("CONTENT_LENGTH");
query_string = (char *) malloc(content_length);
fread(query_string, content_length, 1, stdin);
query_string[content_length] = "\0";

...


that little bit of code will take all your post data from a form, and set it all up in a char string. your string of course, will be in the form "field1=value1&field2=value2&.....etc"

which you would then have to parse and separate in a different function later.

in general, I've learned that it is much better to use perl.

ggx
February 15th, 2004, 01:34 AM
Thanks for the info whiteknight! I always wanted to play around C, and I got something good to do now :)

And yes, Perl is definately the best language to use for CGI programs ;)

Matrix28
February 15th, 2004, 03:04 PM
Are you printing out headers? I didn't know you could use C for CGI!

whiteknight
February 16th, 2004, 08:44 PM
you can use any language really. I did a little ditty program in bash script, and my buddy did one in basic.

you have to print out the headers every time though, so the server knows whats going on. I just omitted the code for it

on my site, I actually have 2 scripts now written in C. granted i could have done them both in perl much faster and easier, but i love the challenge.