1515using namespace std ;
1616
1717string getHeaders (int len) {
18- return " " ;
18+ // return "";
1919 stringstream ss;
20- ss << " HTTP/1.1 200 OK\r\n " ;
21- ss << " Date: Mon, 23 May 2005 22:38:34 GMT\r\n " ;
22- ss << " Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)\r\n " ;
23- ss << " Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT\r\n " ;
24- ss << " Etag: \" 3f80f-1b6-3e1cb03b\"\r\n " ;
25- ss << " Accept-Ranges: bytes\r\n " ;
26- ss << " Content-Length: " << len << " \r\n " ;
27- ss << " Connection: close\r\n " ;
28- ss << " Content-Type: text/html; charset=UTF-8\r\n " ;
20+ ss << " HTTP/1.1 200 OK\n " ;
21+ ss << " \n " ;
22+ // ss << "HTTP/1.1 200 OK\r\n";
23+ // ss << "Date: Mon, 23 May 2005 22:38:34 GMT\r\n";
24+ // ss << "Server: LukesServer/0.0.0.1 (Unix) (Windows/NT6.1)\r\n";
25+ // ss << "Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT\r\n";
26+ // ss << "Etag: \"3f80f-1b6-3e1cb03b\"\r\n";
27+ // ss << "Accept-Ranges: bytes\r\n";
28+ // ss << "Content-Length: " << len << "\r\n";
29+ // ss << "Connection: close\r\n";
30+ // ss << "Content-Type: text/html\r\n";
31+ // ss << "Content-Type: text/html; charset=UTF-8\r\n";
2932 return ss.str ();
3033}
3134
35+ string generateResponse (string request, int counter) {
36+ stringstream ss1, ss2;
37+ ss1 << " <html><head></head><body><h1>Dum di dum</h1>This is request #" << counter << " ." <<
38+ " <br/>The request was:" <<
39+ " <pre>" << request << " </pre></body></html>\n " ;
40+ ss2 << getHeaders (ss1.str ().length ()) << ss1.str ();
41+ return ss2.str ();
42+ }
43+
3244int main (int argc, char **argv) {
3345 if (argc < 2 ) {
3446 cerr << argv[0 ] << " : port" << endl;
@@ -45,18 +57,25 @@ int main(int argc, char**argv) {
4557 while (!quit) {
4658 ++counter;
4759 int clientfd = ss.doAccept ();
60+ // if (fork()) {
4861 cout << " Starting request..." ;
49- if (clientfd < 0 ) continue ; // retry
62+ if (clientfd < 0 ) {
63+ cout << " connection failed!" << endl;
64+ exit (0 );
65+ // continue; // retry
66+ }
5067 SocketIO sio (clientfd);
5168
52- stringstream ss1, ss2;
53- ss1 << " <html><head></head><body><h1>Huuuhuuu</h1><pre>yeah yeah, this is request #" << counter << " !</pre></body></html>\r\n " ;
54- ss2 << getHeaders (ss1.str ().length ()) << ss1.str ();
55- string reply = ss2.str ();
69+ string lines_received = sio.readlines ();
70+ cout << lines_received << endl;
71+ string reply = generateResponse (lines_received, counter);
5672 sio.writeN (reply.c_str (), reply.length ());
5773 sio.doClose ();
5874
5975 cout << " done!" << endl;
76+ exit;
77+ // }
78+ cout << " server continues listening..." << endl;
6079 }
6180 ss.doClose ();
6281 return 0 ;
0 commit comments