Like Tree1Likes
  • 1 Post By CommonTater

CGI Programming: Reading from STDIN for POST method

This is a discussion on CGI Programming: Reading from STDIN for POST method within the C Programming forums, part of the General Programming Boards category; Having trouble reading from STDIN using fread(). The environment variable CONTENT_LENGTH is used to tell the program the length of ...

  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
    22,259
    You did not allocate any memory for tmp to point to.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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?
    laserlight likes this.

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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21