Thread: Http cgi server

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    134

    Http cgi server

    I want to make a server HTTP CGI ready and also implement the POST & GET method. If a client makes a GET or POST request in the cgi-bin directory, then the server is expected to execute the file.

    I would like to know the step by step implementation, thanks in advance.
    I am unable to figure out where from I should start? Any pointer to good resources will be appreciated.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Here, let me google that for you.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    134
    Thanx 86,
    I did this but I was expecting some results from some experienced people. BTW Thanks for making me feel I should work on my own
    but most of them are programming CGI on built server, but here I want to built the server too!

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Then you should be able to pretty much just set up a standard TCP server that complies with HTTP (RFC for v1.1; I love RFCs.)
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    134
    I just want to know how to run a cgi script from a c program which is actually a server program, and send the result onto the browser/client.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by kapil1089thekin View Post
    I just want to know how to run a cgi script from a c program which is actually a server program, and send the result onto the browser/client.
    You're going to have to do something like popen() the executable, and then write what you read from the popen() handler to the client.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by kapil1089thekin View Post
    I just want to know how to run a cgi script from a c program which is actually a server program, and send the result onto the browser/client.
    HTTP is very sensitive to excessive/deficient CRLF's, whitespace, etc. In short, RTFM - it'll show you where went wrong, verily.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    134
    suppose html file is requested by a browser/client.

    from my server I have opened the html file and sent the content of the file to the client. that html file is actually a form. when I am trying to get the html file the browser is shwoing the html code instead of the form, i.e. not the html randered page but the code. wht could be the reason and what are the things I should keep in mind to get the html file from the server as I am expecting!

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by kapil1089thekin View Post
    suppose html file is requested by a browser/client.

    from my server I have opened the html file and sent the content of the file to the client. that html file is actually a form. when I am trying to get the html file the browser is shwoing the html code instead of the form, i.e. not the html randered page but the code. wht could be the reason and what are the things I should keep in mind to get the html file from the server as I am expecting!
    You're confused. You're not supposed to send flat binary files; the appropriate HTTP headers and such have to be sent to the web browser (ie: the HTTP client).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The short answer is that you aren't adhering to the HTTP protocol. As for why that's happening, who knows? You haven't provided any code. You probably haven't read the RFC yet. Can you capture some packets and see what exactly you're sending back? Then you can compare that to a capture from a working web server and see where you're not following the protocol.

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    134

    I am done

    I have solved those issues, you are right I was not following the protocol strictly.
    Now I am stuck with another issue.

    I have 3 cgi files, one in bash and another in python, is there anyway to run each of them using same command, I will actually run those cgi file and get the output and then send it back to browser. As I am suggested I'll use popen, but is it possible to use same command for each bash and python?
    I would like to know also how popen can be used and how I can pass parameters to the python cgi.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You basically pass it the command with any parameters you need as a single string. Check the man page for details. If you're using Linux or the like, you have two options for running your various scripts. The first is to call the interpreter and pass it the name of the script file, like "bash script.sh". That string is an example of what you could pass to popen. The second optionis to put the #!/path/to/interpreter as the first line of your script and give it executable permissions. Then, you just call the script itself as though it were a compiled binary, e.g. "/path/to/python_script here are some parameters".

  13. #13
    Registered User
    Join Date
    May 2008
    Posts
    134
    by bash env.cgi, I am able to run that, as its a bash script, but I need to run a python script file called seg.cgi,
    isn't it possible to run each of them using same command? and I am not able to make myself clear how to use popen, any example will help me more than the man pages.

    this is the python script:

    #!/usr/bin/env python2.6

    import cgi

    def main():
    print "Content-type: text/plain\n\n"

    form = cgi.FieldStorage()
    print "This is a SCAM\n"
    print "Your username is", form["name"].value
    print "Your password is", form["pass"].value

    print "\nNow I know them both! HE HE"
    print "This proves your IQ is low"

    if __name__ == "__main__":
    main()

    How can I pass the arguments from my c program using a command? Probably I would like to use popen to send the output back to the client.
    Last edited by kapil1089thekin; 02-03-2011 at 07:42 PM.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kapil1089thekin View Post
    by bash env.cgi, I am able to run that, as its a bash script, but I need to run a python script file called seg.cgi,
    WOW... given the content of that script I should hope nobody answers your question!

  15. #15
    Registered User
    Join Date
    May 2008
    Posts
    134
    This was a test file given by my TA for a cgi server assignment to write a cgi server in C, I was unable to run it on my linux, I just wanted to post the content, I am a college student and I am not in that kind of business, its too obvious that if I had something in my mind why would I post it here? I am just trying to learn and complete my assignment. probably this was made just without any thinking and for testing purpose.
    thanks but It took me to understand few minutes what I wrote here that.. no one will answer ..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. http server
    By kapil1089thekin in forum C Programming
    Replies: 14
    Last Post: 01-18-2011, 02:39 PM
  2. Help with Parsing HTTP response from server
    By NuNn in forum C Programming
    Replies: 4
    Last Post: 02-19-2009, 03:02 PM
  3. HTTP Server?
    By Nebbuchadnezzar in forum Tech Board
    Replies: 9
    Last Post: 07-30-2005, 03:46 PM
  4. CGI Server
    By hwliu in forum Networking/Device Communication
    Replies: 1
    Last Post: 07-14-2004, 04:46 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM