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

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

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

<Form method="POST" ACTION="/cgi-bin/CGI_POST.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>

///:~

When you press the “submit” button, you’ll get back a simple text page containing the parsed results, so you can see that the CGI program works correctly. The server turns around and feeds the query string to the CGI program via standard input.

Handling mailing lists

Managing an email list is the kind of problem many people need to solve for their Web site. As it is turning out to be the case for everything on the Internet, the simplest approach is always the best. I learned this the hard way, first trying a variety of Java applets (which some firewalls do not allow) and even JavaScript (which isn’t supported uniformly on all browsers). The result of each experiment was a steady stream of email from the folks who couldn’t get it to work. When you set up a Web site, your goal should be to never get email from anyone complaining that it doesn’t work, and the best way to produce this result is to use plain HTML (which, with a little work, can be made to look quite decent).

The second problem was on the server side. Ideally, you’d like all your email addresses to be added and removed from a single master file, but this presents a problem. Most operating systems allow more than one program to open a file. When a client makes a CGI request, the Web server starts up a new invocation of the CGI program, and since a Web server can handle many requests at a time, this means that you can have many instances of your CGI program running at once. If the CGI program opens a specific file, then you can have many programs running at once that open that file. This is a problem if they are each reading and writing to that file.

Appendix B: Programming Guidelines

549

There may be a function for your operating system that “locks” a file, so that other invocations of your program do not access the file at the same time. However, I took a different approach, which was to make a unique file for each client. Making a file unique was quite easy, since the email name itself is a unique character string. The filename for each request is then just the email name, followed by the string “.add” or “.remove”. The contents of the file is also the email address of the client. Then, to produce a list of all the names to add, you simply say something like (in Unix):

cat *.add > addlist

(or the equivalent for your system). For removals, you say:

cat *.remove > removelist

Once the names have been combined into a list you can archive or remove the files.

The HTML code to place on your Web page becomes fairly straightforward. This particular example takes an email address to be added or removed from my C++ mailing list:

<h1 align="center"><font color="#000000"> The C++ Mailing List</font></h1>

<div align="center"><center>

<table border="1" cellpadding="4" cellspacing="1" width="550" bgcolor="#FFFFFF">

<tr>

<td width="30" bgcolor="#FF0000"> </td> <td align="center" width="422" bgcolor="#0"> <form action="/cgi-bin/mlm.exe" method="GET"> <input type="hidden" name="subject-field" value="cplusplus-email-list">

<input type="hidden" name="command-field" value="add"><p>

<input type="text" size="40" name="email-address">

<input type="submit" name="submit" value="Add Address to C++ Mailing List"> </p></form></td>

<td width="30" bgcolor="#FF0000"> </td> </tr>

<tr>

<td width="30" bgcolor="#000000"> </td> <td align="center" width="422" bgcolor="#FF0000">

<form action="/cgi-bin/mlm.exe" method="GET"> <input type="hidden" name="subject-field"

Appendix B: Programming Guidelines

550

value="cplusplus-email-list">

<input type="hidden" name="command-field" value="remove"><p>

<input type="text" size="40" name="email-address">

<input type="submit" name="submit" value="Remove Address From C++ Mailing List"> </p></form></td>

<td width="30" bgcolor="#000000"> </td> </tr>

</table>

</center></div>

Each form contains one data-entry field called email-address, as well as a couple of hidden fields which don’t provide for user input but carry information back to the server nonetheless. The subject-field tells the CGI program the subdirectory where the resulting file should be placed. The command-field tells the CGI program whether the user is requesting that they be added or removed from the list. From the action, you can see that a GET is used with a program called mlm.exe (for “mailing list manager”). Here it is:

//: C10:mlm.cpp

//A GGI program to maintain a mailing list #include "CGImap.h"

#include <fstream> using namespace std;

const string contact("Bruce@EckelObjects.com");

//Paths in this program are for Linux/Unix. You

//must use backslashes (two for each single

//slash) on Win32 servers:

const string rootpath("/home/eckel/");

int main() {

cout << "Content-type: text/html\n"<< endl; CGImap query(getenv("QUERY_STRING")); if(query["test-field"] == "on") {

cout << "map size: " << query.size() << "<br>"; query.dump(cout, "<br>");

}

if(query["subject-field"].size() == 0) { cout << "<h2>Incorrect form. Contact " << contact << endl;

return 0;

}

string email = query["email-address"];

Appendix B: Programming Guidelines

551

if(email.size() == 0) {

cout << "<h2>Please enter your email address" << endl;

return 0;

}

if(email.find_first_of(" \t") != string::npos){ cout << "<h2>You cannot use white space "

"in your email address" << endl; return 0;

}

if(email.find('@') == string::npos) {

cout << "<h2>You must use a proper email"

" address including an '@' sign" << endl; return 0;

}

if(email.find('.') == string::npos) {

cout << "<h2>You must use a proper email" " address including a '.'" << endl;

return 0;

}

string fname = email; if(query["command-field"] == "add")

fname += ".add";

else if(query["command-field"] == "remove") fname += ".remove";

else {

cout << "error: command-field not found. Contact " << contact << endl;

return 0;

}

string path(rootpath + query["subject-field"] + "/" + fname);

ofstream out(path.c_str()); if(!out) {

cout << "cannot open " << path << "; Contact" << contact << endl;

return 0;

}

out << email << endl;

cout << "<br><H2>" << email << " has been "; if(query["command-field"] == "add")

cout << "added";

else if(query["command-field"] == "remove")

Appendix B: Programming Guidelines

552

Соседние файлы в предмете Численные методы
  • #
    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