Thread: QUERY_STRING in CGI with a C/C++ Program

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    5

    QUERY_STRING in CGI with a C/C++ Program

    I am writing a CGI program in C++. When my web server passes environment variables on to the program it send them seperated, eg: If the query string (following the '?') said paul+rules, my program is passed the variables 'paul' and 'rules'. I have no way of knowing if it was a plus or a = sign that was joing the variables.

    Also, if I write a perl script in CGI I am able to access variables like PATH_INFO, and one that carries the users IP Address. I seem unable to access those.

    If anyone has any idea's on this subject or know's a place with a lot of people who do, could you please reply?

    Thanks for listening to me ramble, Paul.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, I thought that the entire string was sent to the program to parse itself, thus preserving the '+' or '=', etc. So are you saying that if the query was "prog.exe?param1=23", then your argv would look like:
    argv[0]:"prog.exe"]
    argv[1]:"param1"
    argv[2]:"23"

    Is that right?

    If so, then I guess your out of luck, still my intuition tells me it would be:

    argv[0]:"prog.exe"]
    argv[1]:"param1=23"

    //...parse argv[1]...
    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;
    }

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    One more thought.

    The CGI program probably parses the expression for you, yeilding:

    argv[0]:"prog.exe"
    argv[1]:"param1"
    argv[2]:"="
    argv[3]:"23"
    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;
    }

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    5
    Nah Like you I expected to get the whole thing, and I wanted to as well, but all I get is

    paulscript.ps?username=freddy

    Code:
    argv[0]=program.exe
    argv[1]=paulscript.ps
    argv[2]=username
    argv[3]=freddy

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Yes but then why are they present? Certainly not for the CGI program?!
    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;
    }

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    Ahhh CGI. I wrote a C/C++ library to use CGI sometime last year. Was going to incorporate ODBC tools into it, but I never go around to it.

    Two things, you have POST and GET methods to worry about.

    When you POST (fill out a from and click submit) you are using the standard input stream...you guessed it "cin". My suggestion (and it's what I did) is to write a loop to loop until you stop getting data from cin.

    When you GET (click on a link...you can also GET forms) you need to get your query string. The method getenv("QUERY_STRING") will get it for you. It is not passed as a parameter to the main program...at least not from what I've seen.

    Here's a list of enviromental variables you can get:
    getenv ("SERVER_SOFTWARE");
    getenv ("SERVER_NAME");
    getenv ("GATEWAY_INTERFACE");
    getenv ("SERVER_PROTOCOL");
    getenv ("SERVER_PORT");
    getenv ("REQUEST_METHOD");
    getenv ("PATH_INFO");
    getenv ("PATH_TRANSLATED");
    getenv ("SCRIPT_NAME");
    getenv ("QUERY_STRING");
    getenv ("REMOTE_HOST");
    getenv ("REMOTE_ADDR");
    getenv ("AUTH_TYPE");
    getenv ("REMOTE_USER");
    getenv ("REMOTE_IDENT");
    getenv ("CONTENT_TYPE");
    getenv ("HTTP_ACCEPT");
    getenv ("HTTP_USER_AGENT");
    getenv ("HTTP_REFERER");

    Now with the query string, you WILL have to parse it. What you will have to parse out are, special characters, convert hex values to characters (such as &, =, +, etc...), all your variable names and appropriate values. If the variable name doesn't exist in the query string, there was no data assigned to it.

    If you'd like, I have no problem sending you my code to browse through it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. redirection program help needed??
    By Unregistered in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2002, 05:50 AM
  5. Replies: 1
    Last Post: 11-23-2001, 10:01 AM