/
main.c
239 lines (184 loc) · 6.68 KB
/
main.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "unistd.h"
#include "pthread.h"
#include "time.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include "config.h"
#define NOT_FOUND "HTTP/1.1 404 Not Found\r\nConnection: close\r\n\r\n"
#define METHOD_NOT_ALLOWED "HTTP/1.1 405 Method not allowed\r\nConnection: close\r\n\r\n"
#define INTERNAL_SERVER_ERROR "HTTP/1.1 500 Internal server error\r\nConnection: close\r\n\r\n"
typedef struct HTTPRequest {
char* method;
char* path;
} HTTPRequest;
typedef struct HTTPResponse {
int status_code;
const char* status_text;
} HTTPResponse;
Config config;
HTTPRequest parse_request(char* buffer) {
HTTPRequest req;
char* first_line = strtok_r(buffer, "\n", &buffer);
req.method = strtok_r(first_line, " ", &first_line);
req.path = strtok_r(first_line, " ", &first_line);
return req;
}
const char* get_content_type(const char* file_name) {
const char* dot = strrchr(file_name, '.');
if (dot == NULL || dot == file_name) {
return "";
}
const char* file_ext = dot + 1;
if (strcmp(file_ext, "html") == 0 || strcmp(file_ext, "htm") == 0) {
return "text/html";
} else if (strcmp(file_ext, "css") == 0) {
return "text/css";
} else if (strcmp(file_ext, "js") == 0) {
return "text/javascript";
} else if (strcmp(file_ext, "jpg") == 0 || strcmp(file_ext, "jpeg") == 0) {
return "image/jpeg";
} else if (strcmp(file_ext, "png") == 0) {
return "image/png";
} else if (strcmp(file_ext, "svg") == 0) {
return "image/svg+xml";
} else {
return "";
}
}
char* get_static_file_path(const char* request_path) {
size_t file_path_len = strlen(request_path);
size_t base_path_len = strlen(config.static_path);
char* file_path = (char*)malloc(file_path_len+base_path_len+1);
if (file_path == NULL) {
return NULL;
}
strcpy(file_path, config.static_path);
strcpy(file_path+base_path_len, request_path);
return file_path;
}
void send_not_found(HTTPResponse* response, int client_socket_fd) {
response->status_code = 404;
response->status_text = "Not Found";
send(client_socket_fd, NOT_FOUND, strlen(NOT_FOUND), 0);
}
void send_method_not_allowed(HTTPResponse* response, int client_socket_fd) {
response->status_code = 405;
response->status_text = "Method not allowed";
send(client_socket_fd, METHOD_NOT_ALLOWED, strlen(METHOD_NOT_ALLOWED), 0);
}
void send_internal_server_error(HTTPResponse* response, int client_socket_fd) {
response->status_code = 500;
response->status_text = "Internal server error";
send(client_socket_fd, INTERNAL_SERVER_ERROR, strlen(INTERNAL_SERVER_ERROR), 0);
}
void send_static_file(HTTPRequest* request, HTTPResponse* response, int client_socket_fd, const char* filename) {
int file_fd = open(filename, O_RDONLY);
if (file_fd == -1) {
send_not_found(response, client_socket_fd);
return;
}
char response_buffer[2048];
memset(response_buffer, 0, sizeof(response_buffer));
struct stat file_stat;
if (fstat(file_fd, &file_stat) != 0) {
send_internal_server_error(response, client_socket_fd);
return;
}
snprintf(response_buffer, sizeof(response_buffer),
"HTTP/1.1 200 OK\r\nServer: MowlCod-C\r\nContent-Length: %ld\r\nContent-Type: %s\r\n\r\n",
(long)file_stat.st_size, get_content_type(filename)
);
send(client_socket_fd, response_buffer, strlen(response_buffer), 0);
char buffer[2048];
int bytes_read;
while ((bytes_read = read(file_fd, buffer, sizeof(buffer))) > 0) {
send(client_socket_fd, buffer, bytes_read, 0);
}
response->status_code = 200;
response->status_text = "OK";
close(file_fd);
}
void* handle_client(void* arg) {
clock_t before = clock();
int client_fd = *((int*)arg);
char* buffer = (char *)malloc(2048 * sizeof(char));
ssize_t bytes_received = recv(client_fd, buffer, 2048, 0);
HTTPRequest request = parse_request(buffer);
HTTPResponse response;
if (strcmp(request.method, "GET") != 0) {
send_method_not_allowed(&response, client_fd);
} else {
if (strrchr(request.path, '.') == NULL) {
char* index_file_path = get_static_file_path("/index.html");
send_static_file(&request, &response, client_fd, index_file_path);
free(index_file_path);
} else {
char* file_path = get_static_file_path(request.path);
send_static_file(&request, &response, client_fd, file_path);
free(file_path);
}
}
close(client_fd);
if (config.request_logging) {
double ms = (double)(clock() - before) * 1000.0 / CLOCKS_PER_SEC;
printf("INFO: %s %s - %d %s (%.2f ms)\n", request.method, request.path, response.status_code, response.status_text, ms);
}
free(buffer);
free(arg);
return NULL;
}
int main(void) {
parse_config(&config);
printf("INFO: Starting web server...\n");
int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd < 0) {
perror("ERROR: Can not create socket file descriptor");
return 1;
}
int reuse = 1;
if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0) {
perror("ERROR: Can not set sock opt SO_REUSEADDR");
return 1;
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(config.port);
if (bind(socket_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("ERROR: Can not bind socket to port");
return 1;
}
if (listen(socket_fd, 10) < 0) {
perror("ERROR: Can not start listening");
return 1;
}
printf("INFO: Web server is started on port %d\n", config.port);
while (1)
{
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int client_fd = accept(socket_fd, (struct sockaddr*)&client_addr, &client_addr_len);
if (client_fd < 0) {
perror("ERROR: Failed to accept connection from client");
continue;
}
int* client_fd_arg = (int*)malloc(sizeof(int));
if (client_fd_arg == NULL) {
perror("ERROR: Failed to allocate memory for thread argument");
close(client_fd);
continue;
}
*client_fd_arg = client_fd;
pthread_t thread_id;
pthread_create(&thread_id, NULL, handle_client, client_fd_arg);
pthread_detach(thread_id);
}
close(socket_fd);
return 0;
}