-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
291 lines (255 loc) · 6.78 KB
/
server.c
File metadata and controls
291 lines (255 loc) · 6.78 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "threadpool.h"
#include <fcntl.h>
#include <memory.h>
#include <netdb.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define PORT "8000"
#define BACKLOG 20
#define BUFFER_SIZE 4096
#define MAX_PATH_LEN 256
struct client_data
{
int fd;
};
void* handle_client(int fd);
int parse_header(const char* header, char* route);
int send_response(int fd, char* response_header, FILE* body);
static pthread_mutex_t print_lock;
int main(int argc, char** argv)
{
pthread_mutex_init(&print_lock, NULL);
// Collect address info for creating a IPv4/IPv6 TCP socket
// for listening on PORT on all interfaces (0.0.0.0:PORT)
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE; // all interfaces (loopback, WiFi, etc.)
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6
hints.ai_socktype = SOCK_STREAM; // TCP socket
// NULL hostname for local machine
struct addrinfo* addrs;
if (getaddrinfo(NULL, PORT, &hints, &addrs) != 0)
{
fprintf(stderr, "No valid address found.\n");
exit(1);
}
struct addrinfo* a;
int socketfd;
for (a = addrs; a != NULL; a = a->ai_next)
{
// Try to initialize socket
if ((socketfd = socket(a->ai_family, a->ai_socktype, a->ai_protocol)) == -1)
continue;
// Try to bind socket to address
if (bind(socketfd, a->ai_addr, a->ai_addrlen) != 0)
{
fprintf(stderr, "Bind failed, trying again...\n");
close(socketfd);
continue;
}
break;
}
if (a == NULL)
{
fprintf(stderr, "Failed to bind.\n");
exit(1);
}
freeaddrinfo(addrs);
// Set socket as listening
if (listen(socketfd, BACKLOG) != 0)
{
fprintf(stderr, "Failed to listen\n");
exit(1);
}
printf("Server listening on %s...\n", PORT);
// Initialize threadpool
threadpool_t threadpool;
threadpool_init(&threadpool, handle_client);
// Handle requests indefinitely
while (1)
{
int clientfd;
struct sockaddr client_addr;
socklen_t size;
if ((clientfd = accept(socketfd, &client_addr, &size)) == -1)
{
fprintf(stderr, "Failure to accept client.\n");
continue;
}
threadpool_enqueue_request(&threadpool, clientfd);
}
threadpool_shutdown(&threadpool);
pthread_mutex_destroy(&print_lock);
close(socketfd);
}
void* handle_client(int fd)
{
// Receive request
char req[BUFFER_SIZE];
int bytes_read = recv(fd, &req, BUFFER_SIZE - 1, 0);
if (bytes_read < 1)
{
close(fd);
return NULL;
}
req[bytes_read] = '\0';
// Parse path
char route[MAX_PATH_LEN];
if ((parse_header(req, route)) != 0)
{
fprintf(stderr, "Error in parsing header: %s\n", req);
close(fd);
return NULL;
}
if (strcmp(route, "/") == 0)
{
// Open HTML file and get size
FILE* f = fopen("www/index.html", "rb");
if (f == NULL)
{
fprintf(stderr, "Could not open index.html.\n");
close(fd);
return NULL;
}
long fsize;
if (fseek(f, 0, SEEK_END) != 0 || (fsize = ftell(f)) == -1 ||
fseek(f, 0, SEEK_SET) != 0)
{
fprintf(stderr, "Failed to get file size.\n");
close(fd);
fclose(f);
return NULL;
}
// Format response header
char response_header[BUFFER_SIZE];
snprintf(response_header, BUFFER_SIZE,
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Content-Length: %ld\r\n"
"\r\n",
fsize);
response_header[BUFFER_SIZE - 1] = '\0';
int bytes_sent;
if ((bytes_sent = send_response(fd, response_header, f)) == -1)
{
fprintf(stderr, "Failed to send response.\n");
close(fd);
fclose(f);
return NULL;
}
if (bytes_sent < strlen(response_header) + fsize)
fprintf(stderr, "Failed to send all data.\n");
pthread_mutex_lock(&print_lock);
printf("Served: %s (%d bytes)\n", route, bytes_sent);
pthread_mutex_unlock(&print_lock);
fclose(f);
}
else if (strcmp(route, "/image") == 0)
{
// Open image file and get size
FILE* f = fopen("www/blah.jpeg", "rb");
if (f == NULL)
{
fprintf(stderr, "Could not open blah.jpeg.\n");
close(fd);
return NULL;
}
long fsize;
if (fseek(f, 0, SEEK_END) != 0 || (fsize = ftell(f)) == -1 ||
fseek(f, 0, SEEK_SET) != 0)
{
fprintf(stderr, "Failed to get file size.\n");
close(fd);
fclose(f);
return NULL;
}
// Format response header
char response_header[BUFFER_SIZE];
snprintf(response_header, BUFFER_SIZE,
"HTTP/1.1 200 OK\r\n"
"Content-Type: image/jpeg\r\n"
"Content-Length: %ld\r\n"
"\r\n",
fsize);
response_header[BUFFER_SIZE - 1] = '\0';
int bytes_sent;
if ((bytes_sent = send_response(fd, response_header, f)) == -1)
{
fprintf(stderr, "Failed to send response.\n");
close(fd);
fclose(f);
return NULL;
}
if (bytes_sent != strlen(response_header) + fsize)
fprintf(stderr, "Failed to send all data.\n");
pthread_mutex_lock(&print_lock);
printf("Served: %s (%d bytes)\n", route, bytes_sent);
pthread_mutex_unlock(&print_lock);
fclose(f);
}
else
{
// 404 message
char response_header[BUFFER_SIZE];
snprintf(response_header, BUFFER_SIZE,
"HTTP/1.1 404 Not Found\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Content-Length: 0\r\n"
"\r\n");
response_header[BUFFER_SIZE - 1] = '\0';
if (send(fd, response_header, strlen(response_header), 0) == -1)
{
fprintf(stderr, "Failed to send response header.\n");
close(fd);
return NULL;
}
pthread_mutex_lock(&print_lock);
printf("404 Not Found: %s\n", route);
pthread_mutex_unlock(&print_lock);
}
close(fd);
return NULL;
}
int parse_header(const char* header, char* route)
{
char method[4], protocol[5];
sscanf(header, "%3s %255s %4s", method, route, protocol);
method[3] = '\0';
route[MAX_PATH_LEN - 1] = '\0';
protocol[4] = '\0';
if (strcmp(method, "GET") != 0)
return -1;
if (strcmp(protocol, "HTTP") != 0)
return -1;
if (route[0] != '/')
return -1;
return 0;
}
int send_response(int fd, char* response_header, FILE* body)
{
int bytes_sent;
int header_size = strlen(response_header);
if ((bytes_sent = send(fd, response_header, header_size, 0)) == -1)
return -1;
char buf[BUFFER_SIZE];
int n;
while ((n = fread(buf, 1, BUFFER_SIZE, body)) > 0)
{
int total_sent = 0;
while (total_sent < n)
{
int sent = send(fd, buf + total_sent, n - total_sent, 0);
if (sent == -1)
return -1;
total_sent += sent;
}
bytes_sent += total_sent;
}
if (ferror(body))
return -1;
return bytes_sent;
}