Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
B.Eckel - Thinking in C++, Vol.2, 2nd edition.pdf
Скачиваний:
50
Добавлен:
08.05.2013
Размер:
2.09 Mб
Скачать

CGImap.h is a tool that makes handling a POST just as easy as handling a GET, which means you can always use POST.

The class Post inherits from a string and only has a constructor. The job of the constructor is to get the query data from the POST into itself (a string). It does this by reading the CONTENT_LENGTH environment variable using the Standard C library function getenv( ). This comes back as a pointer to a C character string. If this pointer is zero, the CONTENT_LENGTH environment variable has not been set, so something is wrong. Otherwise, the character string must be converted to an integer using the Standard C library function atoi( ). The resulting length is used with new to allocate enough storage to hold the query string (plus its null terminator), and then read( ) is called for cin. The read( ) function takes a pointer to the destination buffer and the number of bytes to read. The resulting buffer is inserted into the current string using string::append( ). At this point, the POST data is just a string object and can be easily used without further concern about where it came from.

Testing the CGI parser

Now that the basic tools are defined, they can easily be used in a CGI program like the following which simply dumps the name-value pairs that are parsed from a GET query. Remember that an iterator for a CGImap returns a CGIpair object when it is dereferenced, so you must select the first and second parts of that CGIpair:

//: C10:CGI_GET.cpp

//Tests CGImap by extracting the information

//from a CGI GET submitted by an HTML Web page. #include "CGImap.h"

int main() {

//You MUST print this out, otherwise the

//server will not send the response:

cout << "Content-type: text/plain\n" << endl;

//For a CGI "GET," the server puts the data

//in the environment variable QUERY_STRING: CGImap query(getenv("QUERY_STRING"));

//Test: dump all names and values for(CGImap::iterator it = query.begin();

it != query.end(); it++) { cout << (*it).first << " = "

<<(*it).second << endl;

}

} ///:~

When you use the GET approach (which is controlled by the HTML page with the METHOD tag of the FORM directive), the Web server grabs everything after the ‘?’ and puts in into the operating-system environment variable QUERY_STRING. So to read that information all you have to do is get the QUERY_STRING. You do this with the standard C library function

Appendix B: Programming Guidelines

546

getenv( ), passing it the identifier of the environment variable you wish to fetch. In main( ), notice how simple the act of parsing the QUERY_STRING is: you just hand it to the constructor for the CGImap object called query and all the work is done for you. Although an iterator is used here, you can also pull out the names and values from query using

CGImap::operator[ ].

Now it’s important to understand something about CGI. A CGI program is handed its input in one of two ways: through QUERY_STRING during a GET (as in the above case) or through standard input during a POST. But a CGI program only returns its results through standard output, via cout. Where does this output go? Back to the Web server, which decides what to do with it. The server makes this decision based on the content-type header, which means that if the content-type header isn’t the first thing it sees, it won’t know what to do with the data. Thus it’s essential that you start the output of all CGI programs with the content-type header.

In this case, we want the server to feed all the information directly back to the client program. The information should be unchanged, so the content-type is text/plain. Once the server sees this, it will echo all strings right back to the client as a simple text Web page.

To test this program, you must compile it in the cgi-bin directory of your host Web server. Then you can perform a simple test by writing an HTML page like this:

//:! C10:GETtest.html <HTML><HEAD>

<TITLE>A test of standard HTML GET</TITLE> </HEAD> Test, uses standard html GET

<Form method="GET" ACTION="/cgi-bin/CGI_GET.exe"> <P>Field1: <INPUT TYPE = "text" NAME = "Field1" VALUE = "This is a test" size = "40"></p> <P>Field2: <INPUT TYPE = "text" NAME = "Field2" VALUE = "of the emergency" size = "40"></p> <P>Field3: <INPUT TYPE = "text" NAME = "Field3" VALUE = "broadcast system" size = "40"></p> <P>Field4: <INPUT TYPE = "text" NAME = "Field4" VALUE = "this is only a test" size = "40"></p> <P>Field5: <INPUT TYPE = "text" NAME = "Field5" VALUE = "In a real emergency" size = "40"></p> <P>Field6: <INPUT TYPE = "text" NAME = "Field6" VALUE = "you will be instructed" size = "40"></p> <p><input type = "submit" name = "submit" > </p> </Form></HTML>

///:~

Of course, the CGI_GET.exe program must be compiled on some kind of Web server and placed in the correct subdirectory (typically called “cgi-bin” in order for this web page to work. The dominant Web server is the freely-available Apache (see http://www.Apache.org),

Appendix B: Programming Guidelines

547

Соседние файлы в предмете Численные методы
  • #
    08.05.20133.99 Mб22A.Menezes, P.van Oorschot,S.Vanstone - HANDBOOK OF APPLIED CRYPTOGRAPHY.djvu
  • #
  • #
    08.05.20135.91 Mб24B.Eckel - Thinking in Java, 3rd edition (beta).pdf
  • #
  • #
    08.05.20136.09 Mб17D.MacKay - Information Theory, Inference, and Learning Algorithms.djvu
  • #
    08.05.20133.85 Mб15DIGITAL Visual Fortran ver.5.0 - Programmers Guide to Fortran.djvu
  • #
    08.05.20131.84 Mб12E.A.Lee, P.Varaiya - Structure and Interpretation of Signals and Systems.djvu