-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
136 lines (119 loc) · 3.85 KB
/
server.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include<iostream>
#include<cstdio>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include "includes/server.h"
server::server(int port, int ip_address){
// Create socket
if ((socket_fd = socket(DEFAULT_FAMILY, SOCK_STREAM, 0)) == -1)
{
cerr<<"Fail to create sockets!";
exit(0);
}
memset(&sockaddr, 0, sizeof(sockaddr));
// Initialize IP address
sockaddr.sin_family = AF_INET;
sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
sockaddr.sin_port = htons(12345);
// Bind socket with address
if (bind(socket_fd, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == -1)
{
cerr<<"Fail to bind socket with address!";
exit(0);
}
config_header[string("Server")] = string("mini_server 0.1");
config_header[string("Content-Type")] = string("text/html; charset=utf-8");
config_header[string("Connection")] = string("keep-alive");
response_header = config_header;
}
server::~server()
{
close(socket_fd);
while(!session_schedule.isEmpty())
{
string filename = SESSION_PATH + session_schedule.dequeue();
remove(filename.c_str());
}
}
void server::Listen()
{
if (listen(socket_fd, MAX_CONNECTIONS) == -1)
{
cerr<<"Fail to listen!";
exit(0);
}
cout<<socket_fd;
cout<<"The server is running, listening on port: "<<ntohs(sockaddr.sin_port)<<endl;
struct sockaddr_in clientaddr;
socklen_t sin_size = sizeof(clientaddr);
while(1)
{
if ((connect_fd = accept(socket_fd, (struct sockaddr*)&clientaddr, &sin_size)) == -1)
{
cerr<<"Fail to accept raw_data!";
continue;
}
memset(buff, 0, sizeof(buff));
int end_position = recv(connect_fd, buff, MAX_BUFFER, 0);
session_manager();
string response = get_response(string(buff)); // TODO multiprocess communication! a difficult part.
if (!fork())
{
string ip_info = inet_ntoa(clientaddr.sin_addr);
string tmp = string(buff);
int first_line = tmp.find("\n");
Log newlog;
newlog.access_log(ip_info + " " + tmp.substr(0, first_line));
if (send(connect_fd, response.c_str(), response.size(),0) == -1)
cerr<<"Fail to send message!";
close(connect_fd);
exit(0);
}
buff[end_position] = '\0';
cout<<"Receive message: \n"<<buff<<endl;
close(connect_fd);
}
}
void server::session_manager()
{
if (session_schedule.isEmpty())return;
int now = time(NULL);
int first = session_schedule.peek();
while(!session_schedule.isEmpty() && now > first)
{
string filename = SESSION_PATH + session_schedule.dequeue();
remove(filename.c_str());
first = session_schedule.peek();
}
}
void server::add_response_header(string key, string value)
{
response_header[key] = value;
}
string server::add_header(string response, string version)
{
string tmp = "HTTP/" + version + " 200 OK\r\n";
for (map<string, string>::iterator itr = response_header.begin(); itr != response_header.end(); itr++)
{
tmp += itr->first + ": " + itr->second + "\r\n";
}
tmp += "Content-Length: " + to_string(response.size()) + "\r\n\r\n" + response;
return tmp;
}
void server::set_cookie(string key, string value)
{
add_response_header(string("Set-Cookie"), key+"="+value+";");
}
string server::get_response(string content)
{
_parser.request_parse(content);
_GET = _parser.get("get"); _POST = _parser.get("post");
_COOKIE = _parser.get("cookie"); _HEADER = _parser.get("header");
_REQUEST = _parser.get("request");
response_header = config_header;
string response;
response = _route.trace(_parser.path, response_header);
return add_header(response, _parser.http_version);
}