-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
221 lines (193 loc) · 5.43 KB
/
main.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* TODO:
* -
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sstream>
#include "web-server.h"
#include "routes/get_entity.h"
#include "routes/get_user_visits.h"
#include "routes/get_location_avg.h"
#include "routes/post_entity.h"
using namespace std;
#define M_BUFFER_MAX_SIZE (1 << 14)
ssize_t read_buf(int sfd, char* buf, size_t size)
{
auto n = recv(sfd, buf, size, 0);
if (n == -1)
{
// If errno == EAGAIN, that means we have read all data. So go back to the main loop.
if (errno != EAGAIN)
{
perror("read");
//abort();
}
return -1;
}
else if (n == 0)
{
#ifdef DEBUG
perror("read() = 0");
#endif
return 0;
}
return n;
}
int handle_request(int socket_fd)
{
ssize_t n;
static thread_local char buffer[M_BUFFER_MAX_SIZE];
n = read_buf(socket_fd, buffer, M_BUFFER_MAX_SIZE);
if (n <= 0)
{
if (n == 0)
{
M_DEBUG_LOG((long long) pthread_self() << "| close sock=" << socket_fd);
close(socket_fd);
}
else
{
M_LOG((long long) pthread_self() << "| miss sock=" << socket_fd);
}
return -1;
}
buffer[n] = 0;
M_DEBUG_LOG((long long) pthread_self() << "| Processing " << state.query_id << " query, sock=" << socket_fd);
bool is_get = buffer[0] == 'G';
char* path = buffer + 4 + !is_get;
char* ptr = path;
ptr++; // skip slash
char *id_ptr = NULL;
EntityType entity_type;
if (*ptr == 'u')
{
entity_type = UserEntity;
ptr += M_STRLEN("users/");
}
else if (*ptr == 'v')
{
entity_type = VisitEntity;
ptr += M_STRLEN("visits/");
}
else
{
entity_type = LocationEntity;
ptr += M_STRLEN("locations/");
}
id_ptr = ptr;
while (*ptr != 0 && *ptr != '/' && *ptr != ' ' && *ptr != '?')
ptr++;
if (ptr - id_ptr > INT_MAX_LENGTH) // too large id, invalid int
{
RouteProcessor(socket_fd).handle_404();
goto exit;
}
if (is_get)
{
char *from_date = NULL;
char *to_date = NULL;
char *country = NULL;
char *to_distance = NULL;
char *from_age = NULL;
char *to_age = NULL;
char *gender = NULL;
bool is_locations_avg = false;
bool is_user_visits = false;
if (*ptr == '/')
{
*ptr = 0;
ptr++; // skip slash
if (*ptr == 'a')
{
is_locations_avg = true;
ptr += M_STRLEN("avg");
}
else
{
is_user_visits = true;
ptr += M_STRLEN("visits");
}
}
if (*ptr == '?')
{
while (*ptr == '?' || *ptr == '&')
{
*ptr = 0;
ptr++;
#define PARSE_GETS_1(prop, str_prop) { ptr += M_STRLEN(str_prop) + 1; (prop) = ptr; while (*ptr != '&' && *ptr != ' ') ptr++; }
if (ptr[0] == 'g') PARSE_GETS_1(gender, "gender")
else if (ptr[0] == 'c') PARSE_GETS_1(country, "country")
else if (ptr[2] == 'A') PARSE_GETS_1(to_age, "toAge")
else if (ptr[4] == 'D') PARSE_GETS_1(from_date, "fromDate")
else if (ptr[4] == 'A') PARSE_GETS_1(from_age, "fromAge")
else if (ptr[3] == 'a') PARSE_GETS_1(to_date, "toDate")
else if (ptr[3] == 'i') PARSE_GETS_1(to_distance, "toDistance")
}
}
*ptr = 0;
if (entity_type == UserEntity)
{
if (is_user_visits)
{
auto processor = GetUserVisitsRouteProcessor(socket_fd);
processor.process(id_ptr, from_date, to_date, country, to_distance);
}
else
{
auto processor = GetEntityRouteProcessor(socket_fd);
processor.process(entity_type, id_ptr);
}
}
else if (entity_type == LocationEntity)
{
if (is_locations_avg)
{
auto processor = GetLocationAvgRouteProcessor(socket_fd);
processor.process(id_ptr, from_date, to_date, from_age, to_age, gender);
}
else
{
auto processor = GetEntityRouteProcessor(socket_fd);
processor.process(LocationEntity, id_ptr);
}
}
else
{
auto processor = GetEntityRouteProcessor(socket_fd);
processor.process(VisitEntity, id_ptr);
}
}
else
{
*ptr = 0;
while (!(ptr[0] == '\n' && ptr[-1] == '\n' || ptr[0] == '\n' && ptr[-1] == '\r' && ptr[-2] == '\n' && ptr[-3] == '\r'))
ptr++;
char* body_ptr = ptr + 1;
auto processor = PostEntityRouteProcessor(socket_fd);
processor.process(entity_type, id_ptr, body_ptr);
}
exit:
if (!is_get)
{
//shutdown(socket_fd, SHUT_RDWR);
close(socket_fd);
}
state.query_id++;
return 0;
}
int main(int argc, const char *argv[])
{
M_LOG("Unpacking data...");
state.fill_from_file("/tmp/data");
M_LOG("Data unpacked");
const int port = 80;
M_LOG("Initializing server...");
WebServer server(port, handle_request);
M_LOG("Starting server...");
server.start();
M_LOG("Server stopped...");
return 0;
}