-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebapp.c
More file actions
346 lines (298 loc) · 8.49 KB
/
webapp.c
File metadata and controls
346 lines (298 loc) · 8.49 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
An Incomplete Simple Web Server.
Copyright (C) 2017 Sereysethy Touch
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <string.h>
#include "header.h"
/* Global variable */
int server_sockfd;
int running = TRUE;
/*
* Signal handler
*/
void close_socket() {
close(server_sockfd);
running = FALSE;
}
/*
* HTTP Response Status
*
* HTTP/1.1 200 OK
* Date: Wed, 06 Dec 2017 18:22:06 GMT
* Server: Apache/2.4.10 (Debian)
* Last-Modified: Mon, 28 Nov 2016 09:21:54 GMT
* ETag: "2b-5425900a6ef18"
* Accept-Ranges: bytes
* Content-Length: 43
* Content-Type: text/html
*
* @param int payload_length size of payload
* @param int * n store the size of header in return
* @return pointer to new header
*/
char * get_header(int payload_length, int * n) {
char * tmp = NULL;
char * str = NULL;
char * date = NULL;
char * etag = NULL;
int length = 0;
str = (char *) malloc(MAX_BUF);
tmp = str;
strncpy(tmp, "HTTP/1.1 200 OK\n", 16);
tmp = tmp + 16;
date = get_date();
sprintf(tmp, "Date: %s\n", date);
tmp = tmp + 36;
strncpy(tmp, "Server: Citadel (Namur)\n", 24);
tmp = tmp + 24;
sprintf(tmp, "Last-Modified: %s\n", date);
tmp = tmp + 45;
etag = random_str(16);
sprintf(tmp, "ETag: \"%s\"\n", etag);
tmp = tmp + 25;
strncpy(tmp, "Accept-Ranges: bytes\n", 21);
tmp = tmp + 21;
length = sprintf(tmp, "Content-Length: %d\n", payload_length);
tmp = tmp + length;
strncpy(tmp, "Content-Type: text/html; charset=UTF-8\n", 39);
tmp = tmp + 39;
strncpy(tmp, "Connection: close\n", 18);
tmp = tmp + 18;
*tmp = 10; // line feed \r
tmp++;
*tmp = '\0';
*n = strlen(str);
free(date);
free(etag);
return str;
}
/*
* Payload - string
*/
char * get_payload(int *n, int page) {
char * tmp;
char * str;
int length = 0;
str = (char *) malloc(MAX_BUF);
tmp = str;
length = sprintf(tmp, "<html><body><h1>Mon dictionnaire</h1><p>C'est la page %d</p><a href=\"%d\">suivant</a></body></html>", page, page + 1);
tmp = tmp + length;
*tmp = 0; // null terminated string
*n = strlen(str);
return str;
}
/*
* get the value until it meets a space
* @param char * buf
* @return pointer to new string value
*/
char * get_value(char * buf) {
char * tmp = NULL;
char * value = NULL;
int length = 0;
tmp = buf;
while (*tmp != ' ') {
tmp++;
length++;
}
value = (char *) malloc(length + 1);
strncpy(value, buf, length);
value[length] = 0; // null terminated string
return value;
}
/*
* This function will parse the string sent from client (web browser).
* We just only check the GET request, we ignore other request.
* If client send GET /number we will consider that number as page of
* dictionary, otherwise we return 0 (error)
*
* @param char * buf input buffer from client
* @param int length size of buffer
* @return an integer 0 in case of something else rather than number, otherwise
* a number.
*/
int parse(char *buf, int length) {
int step = 0;
int index = 0;
char * value = NULL;
int number = 0;
if (strncmp(buf, HTTP_GET, 3) == 0) {
index = 3;
} else {
return 0;
}
index++; // espace entre GET Resource
if (buf[index++] == '/') {
value = get_value(&buf[index]);
if (is_number(value)) {
number = atoi(value);;
free(value);
}
} else {
number = 0;
}
return number;
}
/*
* Prepare header for error respose
* @param int payload_length size of payload to embed in header
* @param int * n it will store the size of header in return
* @return pointer to header
*/
char * error_header(int payload_length, int * n) {
char * tmp = NULL;
char * str = NULL;
char * date = NULL;
int length = 0;
str = (char *) malloc(MAX_BUF);
tmp = str;
strncpy(tmp, "HTTP/1.1 404 Not Found\n", 23);
tmp = tmp + 23;
strncpy(tmp, "Server: Citadel (Namur)\n", 24);
tmp = tmp + 24;
strncpy(tmp, "Content-Type: text/html; charset=UTF-8\n", 39);
tmp = tmp + 39;
length = sprintf(tmp, "Content-Length: %d\n", payload_length);
tmp = tmp + length;
strncpy(tmp, "Connection: close\n", 18);
tmp = tmp + 18;
date = get_date();
sprintf(tmp, "Date: %s\n", date);
tmp = tmp + 36;
*tmp = 10; // new feed caracter \r
tmp++;
*tmp = '\0';
// free
free(date);
// get length of header
*n = strlen(str);
return str;
}
/*
* HTTP error payload
* @param int * n contain size of payload
* @return a pointer to new string
*/
char * error_payload(int *n) {
char * tmp = NULL;
char * str = NULL;
int length = 0;
str = (char *) malloc(MAX_BUF);
tmp = str;
length = sprintf(tmp, "<html><body>Not Found</body></html>");
tmp = tmp + length;
*tmp = '\0';
*n = strlen(str);
return str;
}
int main(int argc, char * argv[])
{
int client_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
int fd, nread, result, port;
fd_set readfds, testfds;
struct sigaction act;
struct timeval timeout;
char * header = NULL, * payload = NULL;
int header_length, payload_length;
int page = 0;
char buf[MAX_BUF];
// if port is passed as argument
if (argc > 1) {
port = atoi(argv[1]);
} else {
port = DEFAULT_PORT;
}
// signal handler for SIGINT
act.sa_handler = close_socket;
sigaction(SIGINT, &act, NULL);
/* init */
FD_ZERO(&readfds);
/* Create a socket */
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
/* Name the socket */
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(port);
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
/* Create connection queue and wait for clients */
listen(server_sockfd, 5);
/* Add server socket to the list to monitor */
FD_SET(server_sockfd, &readfds);
printf("server is listening on port %d\n", port);
printf("go to your browser: http://localhost:%d\n", port);
while (running) {
printf("server waiting...\n");
timeout.tv_sec = 2;
timeout.tv_usec = 500000;
testfds = readfds;
result = select(FD_SETSIZE, &testfds, (fd_set*) NULL, (fd_set*) NULL, &timeout);
if (result == -1) {
perror("select");
running = 0;
}
for (fd = 0; fd <= FD_SETSIZE; fd++) {
if (FD_ISSET(fd, &testfds)) {
if (fd == server_sockfd) {
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);
FD_SET(client_sockfd, &readfds);
printf("adding client on fd %d\n", client_sockfd);
} else {
ioctl(fd, FIONREAD, &nread);
if (nread == 0) {
close(fd);
FD_CLR(fd, &readfds);
printf("removing client on fd %d\n", fd);
} else {
while ((nread = read(fd, buf, MAX_BUF)) > 0) {
printf("bytes received: %d\n", nread);
parse(buf, nread);
if (page = parse(buf, nread)) {
payload = get_payload(&payload_length, page);
header = get_header(payload_length, &header_length);
printf("%s\n", header);
write(fd, header, header_length);
printf("%s\n", payload);
write(fd, payload, payload_length);
} else {
payload = error_payload(&payload_length);
header = error_header(payload_length, &header_length);
printf("%s\n", header);
write(fd, header, header_length);
printf("%s\n", payload);
write(fd, payload, payload_length);
}
free(header);
free(payload);
}
}
}
}
}
}
close(server_sockfd);
exit(EXIT_SUCCESS);
}