getting body of HTTP request

This is a discussion on getting body of HTTP request within the C++ Programming forums, part of the General Programming Boards category; How can a CGI program read the entire body of an HTTP request and output it? Iíve tried a number ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    108

    getting body of HTTP request

    How can a CGI program read the entire body of an HTTP request and output it? Iíve tried a number of things and this is what Iíve currently got:

    Code:
        char temp[7];
        char read[1000];
        strcpy(temp ,getenv("CONTENT_LENGTH"));
        int inputLength = atoi(temp);
        cin.get(temp, sizeof(temp));//divide by size of character
        fread( read, inputLength, 1, stdin );
    cout << "Request body: " << read << "</p>\n";
    and some funny stuff gets printed out near end

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Fread() does not add a null terminator and there is not necessarily one in the body.

    At the end, you need to do something like:

    Code:
    read[inputLength] = '\0';
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    This is C, not C++ (except for cout), and also a ticking time bomb. Make up your mind as to whether you want to use C or C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    Well to expand on the "time bomb" bit, this is also unsafe C: strcpy is vulnerable to buffer overflow. Call strncat on an empty string, or call strncpy and '\0' terminate the result yourself.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Not to mention copying into fixed-size buffers without checking to see if there is actually enough room before doing the copying!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    Quote Originally Posted by Elysia
    Not to mention copying into fixed-size buffers without checking to see if there is actually enough room before doing the copying!
    Actually, if you call the functions I mentioned then you shouldn't have to check and if you do check then calls like strcpy can be safe. Since it's fixed size arrays we're using, if some of the data ends up truncated all you can do is increase the fixed size.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HTTP Get request to C Program
    By dcwang3 in forum C Programming
    Replies: 7
    Last Post: 11-16-2010, 05:11 PM
  2. HTTP GET request
    By aosmith in forum C Programming
    Replies: 4
    Last Post: 03-21-2010, 07:00 AM
  3. HTTP GET and POST REQUEST
    By strickey in forum Networking/Device Communication
    Replies: 3
    Last Post: 04-20-2007, 04:23 PM
  4. Getting HTTP Message body
    By karas in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-02-2006, 09:47 AM

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