-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHttpSocket.cpp
67 lines (61 loc) · 1.25 KB
/
HttpSocket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "HttpSocket.h"
USING_CW
HttpSocket::HttpSocket()
{
create(AF_INET,SOCK_STREAM);
}
void HttpSocket::start(unsigned short port)
{
bind(port);
listen(1024);
#ifdef linux
Socket cli;
for(;;)
{
/* here may exist a close error,the server
* do not close the cli.m_socket!,so I change
* the Socket::close() function.Not perfect yet*/
accept(cli);
pid_t childpid;
if((childpid=fork())==0)
{
close();
handleRequest(cli);
cli.close();
exit(0);
}
cli.close();
}
#else
//throw Error("Win32 HttpSocket start under construction...");
Socket cli;
for(;;)
{
accept(cli);
handleRequest(cli);
cli.close();
}
#endif
}
void HttpSocket::handleRequest(Socket& req)
{
char buf[MAXLINE];//,method[MAXLINE],uri[MAXLINE],version[MAXLINE];
SocketStream ss=req.getSocketStream();
ss.readlineb(buf,MAXLINE);
fputs(buf,stdout);
char ret[MAXLINE];
char body[MAXLINE];
//send the response
sprintf(body,"<html>\
<title>test Web Server</title>\
<body>\
Hello World!Just a Web Server test:)\
</body>\
</html>\r\n");
sprintf(ret,"HTTP/1.0 200 OK\r\n\
Server: test Web Server\r\n\
Content-type: text/html\r\n\
Content-length: %d\r\n\r\n",strlen(body));
ss.writen(ret,strlen(ret));
ss.writen(body,strlen(body));
}