简体   繁体   中英

Anomaly with a input with C++ CGI script

i'm working on a CGI script which will authorise a person on a server. i am writing this on a C++ using CGI provided by Ubuntu's apache2 server.

However, i have encountered a strange anomaly which i cannot understand.

The case is the following: As you know, when i have a HTML like:

<form method = "post" action = "./cgi-bin/main.cgi?a=auth">
    u: <input type = "text" name = "u" value = "Alfred Bot"> <br>
    p: <input type = "password" name = "p" value = "alfred"> <br>
    <input type = "submit" value = "Accept">
</form>

This will recall a main.cgi program with a "u=Alfred+Bot&p=alfred" on the input. However, in my case this is false.

CASE 1: NO MEMORIZING

string s;
while (!cin.eof()) {
    cin >> s;
    cerr << s << "\n";
}

In this case, log file will contain a needed line "u=Alfred+Bot&p=alfred".

CASE 2: SAVING THE DATA

while (!cin.eof()) {
    cin >> s;
    cerr << s << "\n";
    form_output += string(s);
}
cerr << "\"" << form_output << "\".\n";

In this case, log file will contain an EMPTY LINE either in cycle and beyond.


I have tried various other methods including stringstreams and scanf read, but result stays the same: whenever i try to memorize the input, it is null and void.

Thanks for any suggestions, marqueewinq

The data needs to be read from the standard input, but the program won't receive an EOF when it reaches the end of the data but instead it should stop reading after reading a specified amount of bytes, which is defined in the environment variable “CONTENT_LENGTH“. So you should read getenv("CONTENT_LENGTH") bytes from the standard input to receive the “post” data. So, apache may not terminate a cin with a EOF symbol. Good for him :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM