-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarLord.c
370 lines (302 loc) · 7.75 KB
/
starLord.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "set.h"
#include "common.h"
struct config
{
const char * port;
}config;
struct request
{
char * method;
char * path;
char * data;
char * protocol;
char * host;
}request;
struct response
{
//have to have below this
char date[BUF_MAX];
char modified[BUF_MAX];
char content_len[BUF_MAX];
char content_typ[BUF_MAX];
char server_head[BUF_MAX];
}response;
/* HTTP response and header for a successful request. */
static char* ok_response =
"HTTP/1.1 200 OK\r\n"
"Content-type: text/html\r\n"
"Connection: close\r\n"
"Date: %s\r\n"
"Last-Modified: %s\r\n"
//"Content-Length: %d\r\n\r\n"
"\r\n"
"%s";
/* HTTP response, header, and body indicating that the we didn't
understand the request. */
static char* bad_request_response =
"HTTP/1.1 400 Bad Request\r\n"
"Content-type: text/html\r\n"
"Connection: close\r\n"
"Date: %s\r\n"
"Last-Modified: %s\r\n"
"Server: Group15/v1.0\r\n\r\n";
/* HTTP response, header, and body template indicating that the
requested document was not found. */
static char* not_found_response =
"HTTP/1.1 404 Not Found\r\n"
"Content-type: text/html\r\n"
"Connection: close\r\n"
"Date: %s\r\n"
"Last-Modified: %s\r\n"
"Server: Group15/v1.0\r\n\r\n";
/* HTTP response, header, and body template indicating that the
method was not understood. */
static char* bad_method_response =
"HTTP/1.1 405 Method Not Implemented\r\n"
"Content-type: text/html\r\n"
"Connection: close\r\n"
"Allow: GET\r\n"
"Date: %s\r\n"
"Last-Modified: %s\r\n"
"Server: Group15/v1.0\r\n\r\n";
int parse_args(int argc, char * argv[], struct config * cfg);
int parse_head(char * , struct request * );
void respond(int status);
void terminate(int signum);
int buf_add(char *);
int buf_view(char *);
//Keep state information
struct set st;
struct addrinfo * addr_list;
struct request * req;
struct response * resp;
long long connections = 0;
int connfd;
char buffer[BUF_MAX];
int main(int argc, char * argv[])
{
req = malloc(sizeof(request));
req->method = NULL;
req->path = NULL;
req->data = NULL;
req->protocol = NULL;
req->host = NULL;
resp = malloc(sizeof(response));
char buf[BUF_MAX];
// handles return values from our functions for error checking
int ret;
struct config cfg;
// parse args into config structure
ret = parse_args(argc, argv, &cfg);
if(ret!=0)
return 1;
struct addrinfo * addr;
//create tcp addrinfo for port and check for success
ret = create_tcp_addrinfo(&addr_list, NULL, cfg.port);
if(ret != 0)
return 1;
// grab first address
addr = addr_list;
// bind to a socket and check for success
int sockfd = create_socket(addr);
if(sockfd < 0)
return 1;
// bind socket
ret = bind_addr(sockfd, addr);
if (ret != 0)
return 1;
// listen on socket
ret = listen_socket(sockfd);
if (ret != 0)
return 1;
set_init(&st);
signal(SIGINT, &terminate);
// set some response info
char date[BUF_MAX] = "Date: %a, %d %b %Y %H:%M:%S %Z";
time_t sec = time(NULL);
struct tm * tm = gmtime(&sec);
strftime(resp->date, sizeof(resp->date),date,tm);
// accept incoming connections
while(1)
{
struct sockaddr remote;
socklen_t rlen = sizeof(remote);
connfd = tcp_accept(sockfd,&remote,&rlen);
connections++;
set_add(&st,get_addr(&remote));
//TODO Remove print
printf("Connection:%lld\n",connections);
int len = read(connfd, buf, sizeof(buf));
if( len <= 0)
continue;
//printf("%s", buf);
ret = parse_head(buf, req);
//TODO Remove print statements
printf("Method:%s\n", req->method);
printf("Path:%s\n", req->path);
printf("Data:%s\n", req->data);
printf("Protocol:%s\n", req->protocol);
printf("Host:%s\n", req->host);
req->method = NULL;
req->path = NULL;
req->data = NULL;
req->protocol = NULL;
req->host = NULL;
}
terminate(0);
return 0;
}
int buf_add(char * msg)
{
char mod[BUF_MAX] = "Date: %a, %d %b %Y %H:%M:%S %Z";
time_t sec = time(NULL);
struct tm * tm = gmtime(&sec);
strftime(resp->modified, sizeof(resp->modified),mod,tm);
if(msg != NULL)
{
strcat(buffer,req->host);
strcat(buffer,"\n");
strcat(buffer,msg);
strcat(buffer,"\n");
}
return 1;
}
int parse_args(int argc, char * argv[], struct config * cfg)
{
int usage = 0;
// show usage on zero arguments
if (argc == 1 || strcmp(argv[1], "-") == 0)
usage = 1;
// loop through all arguments with getopt and set variables as needed
int c;
while ((c = getopt(argc, argv, "p:")) != -1)
{
switch (c)
{
case 'p':
cfg->port = optarg;
break;
case '?':
usage = 1;
break;
default:
usage = 1;
break;
}
}
if (usage)
{
fprintf(stderr, "Usage: %s -p <port>\n", argv[0]);
return 1;
}
return 0;
}
int parse_head(char * msg, struct request * req){
char * b = " ";
char *token;
/* get the first token */
token = strtok(msg,b);
if(token == NULL)
{
respond(400);
}
req->method = token;
if(strcmp(req->method, "GET") != 0){
respond(405);
}
token = strtok(NULL,b);
req->path=token;
token = strtok(NULL,"\r\n");
req->protocol=token;
if(strcmp(req->protocol, "HTTP/1.1") != 0)
{
respond(400);
}
token=strstr(req->path,"add");
if(token==NULL) {
token=strstr(req->path,"view");
if(token==NULL) {
respond(404);
return 1;
}
}
token=strstr(req->path,"view");
if(token==NULL) {
token=strstr(req->path,"add");
if(token==NULL) {
respond(404);
return 1;
}
}
/* walk through other tokens */
while(token)
{
token = strtok(NULL, ":");
if(token == NULL)
{
break;
}
if(strcmp(token, "\nHost") == 0)
{
req->host = strtok(NULL,"\r\n")+1;
break;
}
}
if(req->host == NULL){
respond(400);
return 1;
}
token = strstr(req->path,"data=");
if(token != NULL)
{
token = strtok(token, "&\r\n");
req->data = token+5;
}
respond(200);
return 0;
}
void respond(int status)
{
int len;
char response[BUF_MAX];
switch(status)
{
case 200:
if(strstr(req->path, "add"))
{
len = buf_add(req->data);
}
snprintf(response, sizeof(response), ok_response,resp->date,resp->modified, buffer);
len = write(connfd, response, strlen(response));
close(connfd);
break;
case 400:
len = write(connfd, bad_request_response, strlen(bad_request_response));
close(connfd);
break;
case 404:
snprintf(response, sizeof(response), not_found_response,resp->date,resp->modified, req->path);
len = write(connfd, response, strlen(response));
close(connfd);
break;
case 405:
snprintf(response, sizeof(response), bad_method_response,resp->date,resp->modified, req->method);
len = write(connfd, response, strlen(response));
close(connfd);
break;
}
}
void terminate(int signum)
{
printf("%lld\t", connections);
struct node * ptr = set_first(&st);
while(ptr != NULL)
{
printf("%s, ", set_data(ptr));
ptr = set_next(ptr);
}
printf("\n");
set_free(&st);
freeaddrinfo(addr_list);
exit(0);
}