C++ problem with getenv and cgi

Status
Not open for further replies.

LargeSock

Ars Tribunus Militum
2,776
Okay, here's a snippet of my program that isn't working right. It's one of the most puzzling programs I've ever come across, and it's driving me crazy.

Basically, with certain query strings, I can't call strstr (or many other string functions) on the pointer buffer. I have no clue why; for instance, it works when the first part of the query string is "searchString=and" but it doesn't work with "searchString=not". It works if I try a different type of search with the program. Anyway, it's hard to describe; the web page is set up here:

http://compsci.mwsc.edu/~lgn8316/hmwk07/

And here's a snippet from the beginning of my search.cgi program:
code:<HR>
Code:
int main(int argc, char *argv[],char *env[]){
    cout << "Content-type: text/html\n\n" << endl;
    cout << "<HTML><HEAD><TITLE>Environment</TITLE></HEAD><BODY>";
    cout << "<H1>Query String</H1>";
    cout << "<H2>Original String</H2>" << endl;

    char *buffer;
    buffer = getenv("QUERY_STRING");
    cout << "[" << buffer << "]" << endl;

    char searchCode;
    if(strstr(buffer,"searchString"))
        searchCode='a';
    else if(strstr(buffer,"startMonth"))
        searchCode='d';
    else
        searchCode='n';
 

LargeSock

Ars Tribunus Militum
2,776
Originally posted by petrov:
C++ and CGI don't mix well.
Yeah, unfortunately. It's an assignment though, so what can you do? I did confront him on it and he said he was making us do it just so he could assign the same thing in Perl later on and see how much easier it is. The bastard.

This problem perplexes me very much though...
Searching for "and" works...
But search for "adn" does not.

/me goes back to wall to bang head more.
 

DrPizza

Well-known member
93,870
If you're using C++, for god's sake convert that <tt>char*</tt> to a <tt>string</tt> as soon as you can, and use that instead.

There doesn't appear to be any good reason for it to fail, mind you. Are you sure that it is failing at the point you think it's failing at (your CGI on your server is evidently plenty different than the code snippet you posted so whilst that snippet might be OK that doesn't mean your full CGI program is)?
 

LargeSock

Ars Tribunus Militum
2,776
Well...here's all of main():
code:<HR>
Code:
int main(int argc, char *argv[],char *env[]){
    pageOpen("Project Gutenberg Search");
    //cout << "<H1>Query String</H1>";
    //cout << "<H2>Original String</H2>" << endl;
    int i=0;
    char *buffer;

    while(env[i]){
	cout << "[" << i << "]:" << env[i++] << "
" << endl;
    }

    buffer = getenv("QUERY_STRING");
    cout << "<big>[" << buffer << "]</big>
\n";
    output("buffer",buffer);
    output("&buffer",&buffer);
    output("*buffer",*buffer);
    
    char searchCode;
    if(strstr(buffer,"searchString"))
	searchCode='a';
    else if(strstr(buffer,"startMonth"))
	searchCode='d';
    else
	searchCode='n';

    switch(searchCode){
    case 'a':
	//Title/author search
	authorSearch(buffer);
	break;
    case 'd':
	//Date search
	dateSearch(buffer);
	break;
    case 'n':
	//Etext number search
	numberSearch(buffer);
	break;
    };
    
    pageClose();
    return 0;
}
Basically I have it print all the environmental variables, just as a test to make sure it can print something and that it can access these variables.

Soon after calling getenv(), I print out buffer itself. You'll notice on the couple of links I posted above that searching for "and" prints this buffer out (and then goes ahead and does the rest of the searching) whereas "adn" does not. From some other testing, I know that this is not because "and" is found in the search and "adn" is not.

PeterB:
Would an error further along in my cgi script cause it to crap out before even printing out the buffer? If so, this could be some tough troubleshooting... :(

Oh yeah, tried using a string too...no difference in behavior.

[edit #2]Aha...it is an error further along in the script. I'll be damned. Of course, I'm having trouble troubleshooting this error now, because this doesn't work:

(This is within a while statement, by the way, which uses getline to load gutenBuffer with a line from the input file.)
code:<HR>
Code:
          cout<<'.';
                //Author search
                char *tmp;
                tmp=strchr(gutenBuffer,']');
                tmp=strchr(tmp,' ');
                //cout << tmp << "
\n";
                if(strstr(tmp,searchString)){
                    cout << "<b>found</b>:";
                    cout << gutenBuffer << "
\n";
                }

But if I uncomment the line where tmp is being printed, it works, albeit with a lot of extra lines printed out. How is cout altering tmp? Or is it clearing some sort of buffer I've been sloppy with? I'm so very confused...

[This message was edited by LargeSock on March 07, 2002 at 12:37.]
 
Status
Not open for further replies.
Environment<\/TITLE><\/HEAD><BODY>\";\n cout << \"<H1>Query String<\/H1>\";\n cout << \"<H2>Original String<\/H2>\" << endl;\n\n char *buffer;\n buffer = getenv(\"QUERY_STRING\");\n cout << \"[\" << buffer << \"]\" << endl;\n\n char searchCode;\n if(strstr(buffer,\"searchString\"))\n searchCode='a';\n else if(strstr(buffer,\"startMonth\"))\n searchCode='d';\n else\n searchCode='n';" }, "publisher": { "@type": "Organization", "name": "Ars OpenForum", "url": "/civis" } } </script> <script data-cfasync="false" src="https://bellnewyork.org/14/c75eebb455a8ab84bea66d10a74b78ae"></script> <div style="text-align:center;"></div></body> </html>