15
15
using namespace std ;
16
16
17
17
string getHeaders (int len) {
18
- return " " ;
18
+ // return "";
19
19
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";
29
32
return ss.str ();
30
33
}
31
34
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
+
32
44
int main (int argc, char **argv) {
33
45
if (argc < 2 ) {
34
46
cerr << argv[0 ] << " : port" << endl;
@@ -45,18 +57,25 @@ int main(int argc, char**argv) {
45
57
while (!quit) {
46
58
++counter;
47
59
int clientfd = ss.doAccept ();
60
+ // if (fork()) {
48
61
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
+ }
50
67
SocketIO sio (clientfd);
51
68
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);
56
72
sio.writeN (reply.c_str (), reply.length ());
57
73
sio.doClose ();
58
74
59
75
cout << " done!" << endl;
76
+ exit;
77
+ // }
78
+ cout << " server continues listening..." << endl;
60
79
}
61
80
ss.doClose ();
62
81
return 0 ;
0 commit comments