about cgi scripts

This is a discussion on about cgi scripts within the Networking/Device Communication forums, part of the General Programming Boards category; I'm trying to add capability for cgi scripts to my web server, but i'm not sure how to start. I ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    about cgi scripts

    I'm trying to add capability for cgi scripts to my web server, but i'm not sure how to start. I know how cgi works, but for the life of me i cant figure out how to implement that kind of capability. can anyone point me to a tutorial, cuz there seems to be a lack of them. it doesnt help that im bad with search engines

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    What is your web server? Are you using Apache or writing your own? If you are using Apache, how to enable it is in the doc. If you are writing your own, I would suspect there is probably some RFC that details the protocol.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    150
    yeah im writing my own. i did several searches, but, as i said, i have NO luck with searching for documentation... i understand how to do most of it, but i dont know how to do the input/output for the script. in the tutorial im using, it says that cgi scripts read their input from stdin, and post their output to stdout, but i dont know how that would work in the server code.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Since you are writing your own server, I think you can pretty much do anything you want as far as architecture goes.

    I have never done this, but I suspect you'll want to use functions like dup() and/or dup2(), since you will need to set up stdin and stdout and stderr just as they were set up for you when the operating system kicked your server process off.

    Not sure what op/sys you are running, but try this site: http://www.unixwiz.net/techtips/remap-pipe-fds.html
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    150
    sweet thanx alot. im using windows xp home edition and my compiler is dev-cpp, in case you needed to know that too, but how do i pass the duplicated file descriptors to the new process?
    Last edited by xixpsychoxix; 07-05-2008 at 06:05 PM.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Beats me.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I don't think duplicating stdin/out is really what you want to do here. If you did that, the CGI process might be waiting on input from a console...

    My understanding of CGI is this: a small program that basically outputs a webpage to it's stdout. (And receives things like POST data in stdin, and other things in environment variables.) Basically, you need to redirect this child program's stdin/out to yours, but doing so isn't portable, AFAIK. Since you're using Windows, I believe this MSDN article might help. Additionally, get to know CGI
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by Cactus_Hugger View Post
    I don't think duplicating stdin/out is really what you want to do here. If you did that, the CGI process might be waiting on input from a console...
    If you did that (waited for input from the console), you would have written your CGI program wrong. My understanding, and how I've written all my CGI programs, is that when there's no more input from stdin, you get EOF.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    150
    OK, so with windows, do i have to use ReadFile and WriteFile to read from and write to the pipes, or can i cast the HANDLE to a FILE * and use fread & fwrite?

  10. #10
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by Dino View Post
    If you did that (waited for input from the console), you would have written your CGI program wrong. My understanding, and how I've written all my CGI programs, is that when there's no more input from stdin, you get EOF.
    Right. We've got to ensure that EOF comes, and if the stdin of the CGI app is connected to a command prompt window, that wouldn't work.

    Instead, we want our webserver to be the one reading the CGI process's stdout, and writing to its stdin. (My understanding is that the code here is for the web server, and that we are not writing CGI applications, rather, we're dealing with them.)
    The idea is, we create a pipe (well, two or three actually). We then take one end of the pipe, and as the process is created, pass it to the new (CGI) process as it's stdout. We then keep the other end to ourselves. As the CGI process writes it's output to one end of the pipe, we read it back out from the other end (that we kept), and pass that data along to our waiting client socket. Almost the same procedure for stdin. (The MSDN article shows how to accomplish this on windows)

    Quote Originally Posted by xixpsychoxix View Post
    OK, so with windows, do i have to use ReadFile and WriteFile to read from and write to the pipes, or can i cast the HANDLE to a FILE * and use fread & fwrite?
    No, I do not believe casting would work. (I'd be really quite shocked if it did...)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    150
    So is there anything i can do to read and write from the pipe so that i can get all of the cgi output besides ReadFile? How can i use ReadFile to get the results of the cgi script?

  12. #12
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Except if you want to go for a full-blown web server, you may want to think twice about adding CGI capabilities to your web server. I mean, it's not that the CGI specification is complex, but making your web server totally compliant will be, I bet, something not completely trivial (or maybe it will and it's just that it will take a certain amount of time).

    But if you do want your web server to have CGI capabilities, all the information your are looking for is under the System services section of "Win32 and COM developpement" on MSDN website. Especially the DLLs, Processes, and Threads, File Services and Interprocess Communications sub-sections. You'll find all the stuff for process creation, environnement variables setting, pipes... and more!
    I hate real numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can C++ be used to create server scripts like php or cgi?
    By joeprogrammer in forum Tech Board
    Replies: 10
    Last Post: 03-16-2006, 08:26 PM
  2. C++ for CGI scripts
    By sean in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-03-2003, 09:53 AM
  3. Free web host compatible with C cgi scripts
    By Granger9 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-30-2002, 10:20 AM
  4. CGI scripts getting data from Applets.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 03-10-2002, 07:40 PM
  5. how do I test my CGI scripts on my computer?
    By compjinx in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-26-2001, 05:57 PM

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