Thread: CGI with C/C++

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Boston
    Posts
    2
    Rep Power
    0

    CGI with C/C++


    I'd like to take a program I have written in C and port it to the web. Assume that the input for the script will be a list of 4 integers (call the variables whatever you want -- it doesn't matter).

    My idea is to create a form whereby the user can input the integers (heck, even with one textbox they can do 2,3,4,5 etc), and then press the submit button to execute the CGI script (a c program). The C program will then perform the calculations, and generate a new html page with the answers.

    Anybody have any ideas where I could find the info to start this process?

    Would I have script.cgi, and then have the complied c program "numbers.c"? If so, does the executable C file have to point to the program executable (e.g. /usr/bin like in perl)?

    Also, because it's just integers and some really funky calculations, I have no problem using a GET method. I think it'd be easier to read the variables into the C program this ways as opposed to decoding the POST method. It is REALLY that bad to use a GET method -- especially considering that the data here is not sensitive in any way?

    Thanks in advance,
    David W
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Bavaria, Germany
    Posts
    140
    Rep Power
    12
    If your apache (assuming you use apache) has user defined cgi-bin's turned on, you just need to copy your bin into your ~/wwwroot/cgi-bin/ or ~/public_html/cgi-bin/ (or if you're root to /usr/local/apache/cgi-bin/) directory and change it to a+rx. That's it :-).

    All files which are in cgi-bin are executed and it's stdout output will be sent to the client. You can use .sh files, .tcl, .pl, .whatever if it's just executable.

    You can get all request variables thorugh the enviroment of your script/executable/whatever, just do a sample request and print out all env variables ;-)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    13

    CGI with C


    David,

    Writing CGI applications with C is pretty easy. I have source for a moderately complex CGI at My Website

    I also discuss the topic at Lazarus Notes
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Boston
    Posts
    2
    Rep Power
    0
    What do you think about the GET vs POST thing above (e.g. using GET for really simple, not security-minded things)? I don't feel like writing a script to decode the POST method.

    Thanks,
    DavidW
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    13
    David,

    Read this article: Who's Afraid of the Big Bad C

    Among other things, it mentions a nice interface library for the CGI standard. The source code I mentioned in a previous post also provides examples.

    Wingman's advice is correct, but it's the hard way around.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Bavaria, Germany
    Posts
    140
    Rep Power
    12
    What do you think about the GET vs POST thing above (e.g. using GET for really simple, not security-minded things)? I don't feel like writing a script to decode the POST method.
    There isn't much difference between GET and POST, beside size limitation and that you can't read it in the URL (and you can't transfer file types via GET).

    if you use <form method="get" /> then *all* your input variables are in your GET field. If you use post, then your input variables are in the body of your http request.

    Example:
    Code:
    <form method="" action="http://localhost:81/test">
      <input type="hidden" name="key1" value="value1" />
      <input type="hidden" name="key2" value="value2" />
      <input type="submit" />
    </form>
    If you click "submit" with "get", your client will send the following request to your server:

    Code:
    GET /test?key1=value1&key2=value2 HTTP/1.1
    Host: localhost:81
    ...and with post...

    Code:
    POST /test HTTP/1.1
    Host: localhost:81
    Content-Length: 23
    
    key1=value1&key2=value2
    The decoding routine is in both cases the same, split into & pairs and then into key, value pairs with delimiter = and last the decoding.

    If you do file uploading, then your post body will be somehow complicated since it's diverted into several bodies.

    See? There isn't really a difference between both :-)

    But ClayDowling is right, those decoding routines and nicer accessing of HTTP variables has written a lot times before, so just search for a CGI request/response wrapper for c/c++ :-).
    Last edited by Wingman; December 27th, 2002 at 12:45 PM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    68
    Rep Power
    13
    ClayDowling, I can't find the cgi you're talking about on your site. Could you please point me to the exact page?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    13
    The CGI in question is on the Newsreel Page.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    61
    Rep Power
    12
    CGI programs in C are as easy to write as in PERL or any other language.

    The basic idea of CGI is that input is taken from envirement variables and standard input, while output is printed to standard output.

    Simple example:

    #include <stdio.h>

    int main(void)
    {
    int i;
    puts("Content-Type: text/html\n");
    for (i=1; i<=5; i++)
    printf("<h%d>Hello!</h%d>\n", i, i);
    return 0;
    }

IMN logo majestic logo threadwatch logo seochat tools logo