Thread: CGI Programming: Reading from STDIN for POST method

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Godthåb, Grønland
    Posts
    9

    CGI Programming: Reading from STDIN for POST method

    Having trouble reading from STDIN using fread(). The environment variable CONTENT_LENGTH is used to tell the program the length of the input in bytes. I can use atoi(getenv("CONTENT_LENGTH")) just fine. Where I'm having trouble is with fread()...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(){
    	char *s=getenv("CONTENT_LENGTH");
    	int i=atoi(getenv("CONTENT_LENGTH"));
    	printf("Content-type: text/html\n\n");
    	printf("%s\n<br />",s); //Shows you CONTENT_LENGTH works
    	printf("%d\n<br />",i); //Shows you it was converted to int
    	char *tmp;
    	fread(tmp,i,1,stdin); //read from stdin something of i bytes to tmp
    	printf("%s\n<br />",tmp);
    	return 0;
    }
    Obviously this won't run on the command line. But for some reason it won't work on the server, either. I can only guess, but I think it's always a segmentation fault because the script ends "prematurely" according to the error log:

    [Thu Sep 15 07:18:26 2011] [error] [client 127.0.0.1] Premature end of script headers: a.out, referer: http://localhost/home.html

    What's actually happening?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You did not allocate any memory for tmp to point to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2011
    Location
    Godthåb, Grønland
    Posts
    9
    Thanks for the speedy answer. It works!

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 809AreaCode View Post
    Thanks for the speedy answer. It works!
    Excellent... did you free() the memory when you were done with it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading from stdin
    By carlorfeo in forum C++ Programming
    Replies: 18
    Last Post: 02-06-2008, 08:50 AM
  2. help reading from stdin
    By asdffa in forum C Programming
    Replies: 8
    Last Post: 11-15-2006, 03:53 PM
  3. Open default browser and send an HTTP request (POST Method)
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 09-03-2005, 01:31 AM
  4. Trying to capture POST method from Web Page
    By nice_guy_mel in forum Networking/Device Communication
    Replies: 8
    Last Post: 04-01-2005, 06:57 AM