1: #include <iostream>
2: #include <fstream>
3: #include <cstdlib>
4: #include <list>
5: #include <pair.h>
6: #include <string>
7:
8: // ------------------------------------------------------
9: // Datenstrukturen
10: // ------------------------------------------------------
11: typedef list<pair<string,string> > Liste;
12: typedef list<pair<string,string> >::iterator ListenIterator;
13:
14: // ------------------------------------------------------
15: // Funktionsprototypen
16: // ------------------------------------------------------
17: bool analysiereAnfrage(Liste& _liste);
18: void antworte(Liste& _liste);
19: void antwortFehler();
20: void konvertiereLeerzeichen(string& _s);
21:
22: // ------------------------------------------------------
23: // Hauptfunktion
24: // ------------------------------------------------------
25: int main()
26: {
27: Liste liste;
28:
29: if (analysiereAnfrage(liste) == false)
30: antwortFehler();
31: else
32: antworte(liste);
33:
34: return 0;
35: }
36:
37: // ------------------------------------------------------
38: // Auswertung und Speicherung der Anfrage
39: // ------------------------------------------------------
40: bool analysiereAnfrage(Liste& _liste)
41: {
42: // Puffer fuer uebergebene Daten
43: char* buffer = 0;
44: unsigned int len;
45:
46: // Bestimme die Anforderungsart
47: string request_method = getenv("REQUEST_METHOD");
48:
49: // Behandle eine POST-Anforderung
50: if (request_method == "POST")
51: {
52: len = atoi(getenv("CONTENT_LENGTH"));
53: buffer = new char[len+1];
54:
55: for(unsigned int i=0; i<len; i++)
56: cin.get(buffer[i]);
57: }
58:
59: // Behandle eine GET-Anforderung
60: if (request_method == "GET")
61: {
62: len = strlen(getenv("QUERY_STRING"));
63: buffer = new char[len+1];
64: strcpy(buffer,getenv("QUERY_STRING"));
65: }
66:
67: // Null-Zeichen zur Terminierung
68: buffer[len] = 0;
69:
70: // Kopiere Puffer in String
71: string eingabe = buffer;
72: delete[] buffer;
73:
74: // Lokale Variablen zur Teilstringsuche
75: size_t pos = 0;
76: size_t old_pos = 0;
77:
78: // Lies Schluessel/Wert-Paare
79: while(pos < len)
80: {
81: pos = eingabe.find("&", old_pos);
82: // einziges oder letztes Paar
83: if (pos == string::npos)
84: pos = eingabe.length();
85:
86: // zerlege das Paar
87: string paar = eingabe.substr(old_pos, pos-old_pos);
88: size_t eq_pos = paar.find("=");
89:
90: string schluessel = paar.substr(0, eq_pos);
91: konvertiereLeerzeichen(schluessel);
92:
93: string wert = paar.substr(eq_pos+1);
94: konvertiereLeerzeichen(wert);
95:
96: // fuege Paar in Liste ein
97: _liste.push_back(pair<string,string>(schluessel,wert));
98: old_pos = pos+1;
99: }
100:
101: return true;
102: }
103:
104: // ------------------------------------------------------
105: // Formulierung der Antwort
106: // ------------------------------------------------------
107: void antworte(Liste& _liste)
108: {
109: cout << "Content-type: text/html" << endl << endl;
110: cout << "<HTML>" << endl;
111: cout << "<HEAD><TITLE>Eingabe verstanden";
112: cout << "</TITLE></HEAD><BODY>" << endl;
113: cout << "<H2>Sie hatten folgende Angaben gemacht: </H2>"<<endl;
114: cout << "<UL>" << endl;
115:
116: ListenIterator li;
117: for(li = _liste.begin(); li != _liste.end(); li++)
118: {
119: cout << "<LI>" << li->first << ": ";
120: cout << "<EM>" << li->second << "</EM></LI>" << endl;
121: }
122:
123: cout << "</UL>" << endl;
124: cout << "</BODY></HTML>" << endl;
125: }
126:
127: // ------------------------------------------------------
128: // Antwort bei einem Fehler
129: // ------------------------------------------------------
130: void antwortFehler()
131: {
132: cout << "Content-type: text/html" << endl;
133: cout << "<HTML>" << endl;
134: cout << "<HEAD><TITLE>Eingabe nicht verstanden";
135: cout << "</TITLE></HEAD><BODY>" << endl;
136: cout << "<H2>Ihre Angaben konnten nicht verarbeitet werden.</H2>"<<endl;
137: cout << "</BODY></HTML>" << endl;
138: }
139:
140: void konvertiereLeerzeichen(string& _s)
141: {
142: for(unsigned int i=0; i<_s.length(); i++)
143: if (_s[i] == '+')
144: _s[i] = ' ';
145: }
146:
147:
148: