-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.c
219 lines (182 loc) · 5.72 KB
/
handlers.c
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
#include "handlers.h"
void handle_home(int new_sockfd, char *buffer)
{
HTTP_METHODS req_method = method(buffer);
if (req_method == GET)
{
char *title = strdup("<h1>Home</h1>");
char* form = malloc(100);
char *resp_msg;
memset(form, 0, 100);
strcpy(form, form_creator("/", "", "name"));
resp_msg = malloc(strlen(title) + strlen(form) + 1);
strcpy(resp_msg, title);
strcat(resp_msg, form);
char *test_resp = response_creator(HTTP_OK, "text/html", resp_msg);
printf("resp = %s", test_resp);
write_response(new_sockfd, test_resp, strlen(test_resp));
FREE_AND_NULL(title);
FREE_AND_NULL(form);
FREE_AND_NULL(resp_msg);
FREE_AND_NULL(test_resp);
}
else if (req_method == POST)
{
// Обработка данных формы
char *body = strstr(buffer, "\r\n\r\n");
if (body)
{
body += 4;
char *name_start = strstr(body, "name=");
if (name_start)
{
name_start += 5;
char *name_end = strchr(name_start, '&');
if (!name_end)
name_end = strchr(name_start, '\0');
if (name_end - name_start > 99)
{
fprintf(stderr, "Name value too long\n");
return;
}
char* name = malloc(100);
strncpy(name, name_start, name_end - name_start);
name[name_end - name_start] = '\0';
char greeting_msg[200];
snprintf(greeting_msg, sizeof(greeting_msg), "<p>Hello %s</p><br><a href='/'>Back to home</a>", name);
printf("%s\n", greeting_msg);
char *response = response_creator(HTTP_OK, "text/html", greeting_msg);
write_response(new_sockfd, response, strlen(response));
FREE_AND_NULL(response);
}
}
}
}
void handle_404(int sockfd)
{
char *message = malloc(strlen("<h1>404</h1>") + strlen(text_creator("Page not found")) + 1);
if (message == NULL)
{
perror("Не удалось выделить память под сообщение 404");
return;
}
strcpy(message, "<h1>404</h1>");
strcat(message, text_creator("Page not found"));
char *response = response_creator(HTTP_NOT_FOUND, "text/html", message);
write_response(sockfd, response, strlen(response));
FREE_AND_NULL(response);
FREE_AND_NULL(message);
}
void handle_about(int sockfd)
{
FILE *aboutfd = fopen("about.html", "r");
if (aboutfd == NULL)
{
fprintf(stderr, "Не удалось найти файл about.html");
}
char *html = read_all_from_html(aboutfd);
if (html == NULL)
{
perror("Не удалось выделить память под сообщение 404");
return;
}
char *response = response_creator(HTTP_OK, "text/html", html);
write_response(sockfd, response, strlen(response));
FREE_AND_NULL(response);
FREE_AND_NULL(html);
fclose(aboutfd);
}
HTTP_METHODS method(char *buffer)
{
HTTP_METHODS result;
if (strncmp(buffer, "POST", 4) == 0)
{
result = POST;
}
else if (strncmp(buffer, "GET", 3) == 0)
{
result = GET;
}
else if (strncmp(buffer, "PUT", 3) == 0)
{
result = PUT;
}
else if (strncmp(buffer, "DELETE", 6) == 0)
{
result = HttpDelete;
}
else
{
fprintf(stderr, "Не удалось распознать метод");
exit(EXIT_FAILURE);
}
return result;
}
void* handle_client_thread(void* arg) {
int new_sockfd = *((int*)arg);
char *buffer = malloc(BUFFER_SIZE);
memset(buffer, 0, BUFFER_SIZE);
ssize_t valRead = read(new_sockfd, buffer, BUFFER_SIZE);
if (valRead < 0) {
perror("Чтение не успешно");
close(new_sockfd);
free(arg);
return NULL;
}
char *url;
url = parse_url(buffer);
printf("url = %s ; len = %lu\n", url, strlen(url));
char *log_msg = malloc(256 * sizeof (char));
snprintf(log_msg, 256, "Кто-то подключился на url %s, номер сокета = %d", url, new_sockfd);
log_write(log_msg);
FREE_AND_NULL(log_msg);
router(url, new_sockfd, buffer);
FREE_AND_NULL(url);
FREE_AND_NULL(buffer);
close(new_sockfd);
// free(arg);
return NULL;
}
void router(char *route, int sockfd, char *buffer)
{
if (strcmp(route, "/") == 0)
{
handle_home(sockfd, buffer);
}
else if (strcmp(route, "/about") == 0)
{
if (method(buffer) == GET)
handle_about(sockfd);
else
{
char *response = response_creator(HTTP_NOT_ALLOWED, "text/plain", "Метод запрещен для данного пути");
write_response(sockfd, response, strlen(response));
FREE_AND_NULL(response);
}
}
else if (strcmp(route, "/favicon.ico") == 0)
{
char *response = response_creator(HTTP_NOT_FOUND, "text/plain", "Favicon not available");
write_response(sockfd, response, strlen(response));
FREE_AND_NULL(response);
}
else if (strncmp(route, "/static/", 8) == 0)
{
if (route != NULL) {
char *filename = malloc(strlen(route) + 1);
if (filename == NULL) {
return;
}
strcpy(filename, ".");
strcat(filename, route);
send_static_file(sockfd, filename);
free(filename);
// FREE_AND_NULL(filename);
}
}
else
{
handle_404(sockfd);
}
// FREE_AND_NULL(buffer);
}