Implementing CGI

This is a discussion on Implementing CGI within the Networking/Device Communication forums, part of the General Programming Boards category; OK, I'm currently writing a wevserver, which is almost done. My current implementation just executes the program/script and redirects the ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    23

    Question Implementing CGI

    OK, I'm currently writing a wevserver, which is almost done. My current implementation just executes the program/script and redirects the output into a file which is then transfered through the socket. This is admitedly not so hot.

    My question is: are there any better ways to do this? Can I "bind" or "pipe" the webserver's child's output directly through my socket?

    I'm on FreeBSD, but my program should run on Mac OS X as well. Thanks in advance.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    So you're creating a child process to do the work, then reading the file it creates?

    system("runScript > file.txt");
    // now transfer file.txt to the socket?

    If you are, then look up these system calls
    fork() - create a child process
    execl() - run another program
    close() - close file descriptor
    dup() - make one file descriptor like another
    Basically, you make the socket file descriptor the stdout file descriptor of the child process
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    23
    OK, I know those functions, but I do not understand the underlying theory here. I'm not sure if I get you, but are you saying that I call dup in the child process and then continue with execl?
    CGI lets CGI programs get their input from stdin/env variables and send their output right to stdout, without calling any other functions first. Could you explain a little more thoroughly?
    Thanks for taking the time.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Paraphrased code
    Code:
    int sockfd;   // the socket which you write to to send data back to the client
    if ( fork() == 0 ) {
      // now in a child process
      close( 1 );  // the existing stdout
      dup ( sockfd );  // this will make sockfd also stdout
      execl( /* run the script here */ );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    23
    Thank you VERY much, Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CGI program help please
    By Lince in forum C Programming
    Replies: 3
    Last Post: 08-01-2007, 01:31 AM
  2. cgi - c++ returns image
    By terracota in forum C++ Programming
    Replies: 1
    Last Post: 08-04-2004, 09:41 PM
  3. testing a cgi script on my computer
    By Bigbio2002 in forum C Programming
    Replies: 1
    Last Post: 12-14-2003, 06:26 PM
  4. CGI Mailing
    By sean in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-14-2003, 09:56 PM
  5. Problem with cgi script - can't rename files
    By bjdea1 in forum C Programming
    Replies: 2
    Last Post: 12-12-2001, 03:09 PM

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