-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiny.c
599 lines (538 loc) · 16.2 KB
/
tiny.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/* $begin tinymain */
/*
* tiny.c - A simple, iterative HTTP/1.0 Web server that uses the
* GET method to serve static and dynamic content.
Fom csapp
Modified by Paul
*/
#include "csapp.h"
queue *head = NULL;
queue *tail = NULL;
queue *h_dynamic = NULL;
queue *t_dynamic = NULL;
void enqueue(int *fd)
{
queue *novo = malloc(sizeof(queue));
novo->socket = fd;
novo->Prox = NULL;
if (head == NULL)
{
head = novo;
}
else
{
tail->Prox = novo;
}
tail = novo;
}
void enqueue_dynamic(int *fd)
{
queue *novo = malloc(sizeof(queue));
novo->socket = fd;
novo->Prox = NULL;
if (h_dynamic == NULL)
{
h_dynamic = novo;
}
else
{
t_dynamic->Prox = novo;
}
t_dynamic = novo;
}
// saida da fila
int *dequeue()
{
if (head == NULL)
{
return NULL;
}
else
{
int *fd = head->socket;
queue *aux = head;
head = head->Prox;
if (head == NULL)
{
tail = NULL;
}
free(aux);
return fd;
}
}
void *dequeue_dynamic()
{
if (h_dynamic == NULL)
{
return NULL;
}
else
{
int *fd = h_dynamic->socket;
queue *aux = h_dynamic;
h_dynamic = h_dynamic->Prox;
if (h_dynamic == NULL)
{
t_dynamic = NULL;
}
free(aux);
return fd;
}
}
void doit(int *fd);
void read_requesthdrs(rio_t *rp);
int parse_uri(char *uri, char *filename, char *cgiargs);
void serve_static(int fd, char *filename, int filesize);
void get_filetype(char *filename, char *filetype);
void serve_dynamic(int fd, char *filename, char *cgiargs);
void clienterror(int fd, char *cause, char *errnum, char *shortmsg, char *longmsg);
void * verQueue(void *arg);
int verifica_static(int fd);
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condition_var = PTHREAD_COND_INITIALIZER;
sem_t sem;
int numeroRequestStat = 0;
int numeroRequestDyn = 0;
int queueSize = 0;
char *schedalg;
// pthread_barrier_wait() init
int main(int argc, char **argv)
{
sem.__align = 0;
int listenfd, connfd, port;
unsigned int clientlen; // change to unsigned as sizeof returns unsigned
struct sockaddr_in clientaddr;
/* Check command line args */
if (argc != 5)
{
fprintf(stderr, "usage: %s <port> <threads> <queue> <schedalg>\n", argv[0]);
//
// schedalg: the scheduling algorithm to be performed. One of ANY, FIFO, HPSC, or HPDC.
exit(1);
}
// inicializar variaveis
port = atoi(argv[1]);
int nthreads = atoi(argv[2]);
queueSize = atoi(argv[3]);
schedalg = argv[4];
// create thread pool
pthread_t thread_p[nthreads];
int i;
for (i = 0; i < nthreads; i++)
{
pthread_create(&thread_p[i], NULL, verQueue, NULL);
}
fprintf(stderr, "Server : %s Running on <%d> port with <%d> threads and <%d> buff-size schedalg: <%s>\n", argv[0], port, nthreads, queueSize, schedalg);
// ./tiny.c 8080 4 4 ANY
// Abriu o ouvinte da porta
listenfd = Open_listenfd(port);
if (strcmp(schedalg, "FIFO") == 0 || strcmp(schedalg, "ANY") == 0)
{
fprintf(stderr, "Scheduling algorithm: FIFO\n");
while (1)
{
// se o numero de request for igual a queueSize, espera para diminuir a queue
if (numeroRequestStat == queueSize)
{
sem_wait(&sem); // blocka enquanto espera que alguem saia da queue
}
clientlen = sizeof(clientaddr);
connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); // line:netp:tiny:accept
int *p_connfd = malloc(sizeof(int));
*p_connfd = connfd;
pthread_mutex_lock(&mutex);
enqueue(p_connfd);
numeroRequestStat++;
pthread_cond_signal(&condition_var); // manda signal para trabalhar
pthread_mutex_unlock(&mutex);
}
}
else if (strcmp(schedalg, "HPSC") == 0)
{
fprintf(stderr, "Scheduling algorithm: HPSC\n");
while (1)
{
// se o numero de request for igual a queueSize, espera para diminuir a queue
if (numeroRequestStat == queueSize)
{
sem_wait(&sem); // blocka enquanto espera que alguem saia da queue
}
clientlen = sizeof(clientaddr);
connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); // line:netp:tiny:accept
int *p_connfd = malloc(sizeof(int));
*p_connfd = connfd;
int verifica = verifica_static(connfd);
if (verifica == 0)
{ // adiciona na fila dinamica
/* code */
pthread_mutex_lock(&mutex);
enqueue_dynamic(p_connfd);
numeroRequestDyn++;
pthread_cond_signal(&condition_var); // manda signal para trabalhar
pthread_mutex_unlock(&mutex);
}
else
{
pthread_mutex_lock(&mutex);
enqueue(p_connfd);
numeroRequestStat++;
pthread_cond_signal(&condition_var); // manda signal para trabalhar
pthread_mutex_unlock(&mutex);
}
}
}
else if (strcmp(schedalg, "HPDC") == 0)
{
fprintf(stderr, "Scheduling algorithm: HPDC\n");
while (1)
{
// se o numero de request for igual a queueSize, espera para diminuir a queue
if (numeroRequestStat == queueSize)
{
sem_wait(&sem); // blocka enquanto espera que alguem saia da queue
}
clientlen = sizeof(clientaddr);
connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); // line:netp:tiny:accept
int *p_connfd = malloc(sizeof(int));
*p_connfd = connfd;
int verifica = verifica_static(connfd);
if (verifica == 0)
{ // adiciona na fila dinamica
/* code */
pthread_mutex_lock(&mutex);
enqueue_dynamic(p_connfd);
numeroRequestDyn++;
pthread_cond_signal(&condition_var); // manda signal para trabalhar
pthread_mutex_unlock(&mutex);
}
else
{
pthread_mutex_lock(&mutex);
enqueue(p_connfd);
numeroRequestStat++;
pthread_cond_signal(&condition_var); // manda signal para trabalhar
pthread_mutex_unlock(&mutex);
}
}
}
else
{
fprintf(stderr, "Scheduling algorithm: <%s> not supported\n", schedalg);
exit(1);
}
sem_destroy(&sem);
}
// Funcao das threads que esta sempre a verificar queue
void * verQueue(void *arg)
{
/* verificar se o schedalg é fifo
Se for fifo ele adiciona na queue e le da queue pois nao importa se é dinamico ou nao
*/
if (strcmp(schedalg, "FIFO") == 0)
{
while (1)
{
pthread_mutex_lock(&mutex);
int *fd = dequeue();
pthread_mutex_unlock(&mutex);
if (fd != NULL)
{
numeroRequestStat--;
doit(fd);
sem_post(&sem); // liberta o semaforo para trabalhar a main
if (numeroRequestStat == 0)
{
pthread_cond_wait(&condition_var, &mutex);
}
}
}
}
/* verificar se o schedalg é HPSC
Se for HPSC vai ver se a queue static tem algo se tiver le da queue static e quando tiver fazia le
a queue dinamica e quando estao as duas fazias manda parar na main
*/
else if (strcmp(schedalg, "HPSC") == 0)
{
while (1)
{
pthread_mutex_lock(&mutex);
int *fd = dequeue();
pthread_mutex_unlock(&mutex);
if (fd != NULL)
{
numeroRequestStat--;
doit(fd);
sem_post(&sem); // liberta o semaforo para trabalhar a main
if (numeroRequestStat == 0)
{
pthread_cond_wait(&condition_var, &mutex);
}
}
else if (numeroRequestDyn > 0)
{
pthread_mutex_lock(&mutex);
int *fd = dequeue_dynamic();
pthread_mutex_unlock(&mutex);
if (fd != NULL)
{
numeroRequestDyn--;
doit(fd);
sem_post(&sem); // liberta o semaforo para trabalhar a main
if (numeroRequestDyn == 0)
{
pthread_cond_wait(&condition_var, &mutex);
}
}
}
}
}
else if (strcmp(schedalg, "HPDC") == 0)
{
while (1)
{
pthread_mutex_lock(&mutex);
int *fd = dequeue_dynamic();
pthread_mutex_unlock(&mutex);
if (fd != NULL)
{
numeroRequestDyn--;
doit(fd);
sem_post(&sem); // liberta o semaforo para trabalhar a main
if (numeroRequestDyn == 0)
{
}
}
else if (numeroRequestStat > 0)
{
// faz os doit da queue statica
pthread_mutex_lock(&mutex);
int *fdS = dequeue();
pthread_mutex_unlock(&mutex);
if (fdS != NULL)
{
numeroRequestStat--;
doit(fdS);
sem_post(&sem); // liberta o semaforo para trabalhar a main
if (numeroRequestStat == 0)
{
pthread_cond_wait(&condition_var, &mutex);
}
}
}
}
}
}
// Function to verifica se a ligacao é statica ou dinamica
int verifica_static(int fd)
{
int is_static;
char uri[MAXLINE];
char filename[MAXLINE], cgiargs[MAXLINE];
/* verifica se o fd é statico */
is_static = parse_uri(uri, filename, cgiargs); // line:netp:doit:staticcheck
return is_static;
}
/* $end tinymain */
/*
* doit - handle one HTTP request/response transaction
*/
/* $begin doit */
void doit(int *p_fd)
{
int fd = *p_fd;
free(p_fd);
int is_static;
struct stat sbuf;
char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
char filename[MAXLINE], cgiargs[MAXLINE];
rio_t rio;
/* Read request line and headers */
Rio_readinitb(&rio, fd);
Rio_readlineb(&rio, buf, MAXLINE); // line:netp:doit:readrequest
sscanf(buf, "%s %s %s", method, uri, version); // line:netp:doit:parserequest
if (strcasecmp(method, "GET"))
{ // line:netp:doit:beginrequesterr
clienterror(fd, method, "501", "Not Implemented", "Tiny does not implement this method");
return;
} // line:netp:doit:endrequesterr
read_requesthdrs(&rio); // line:netp:doit:readrequesthdrs
/* Parse URI from GET request */
is_static = parse_uri(uri, filename, cgiargs); // line:netp:doit:staticcheck
if (stat(filename, &sbuf) < 0)
{ // line:netp:doit:beginnotfound
clienterror(fd, filename, "404", "Not found", "Tiny couldn't find this file");
return ;
} // line:netp:doit:endnotfound
if (is_static)
{ /* Serve static content */
if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode))
{ // line:netp:doit:readable
clienterror(fd, filename, "403", "Forbidden", "Tiny couldn't read the file");
return;
}
serve_static(fd, filename, sbuf.st_size); // line:netp:doit:servestatic
}
else
{ /* Serve dynamic content */
if (!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode))
{ // line:netp:doit:executable
clienterror(fd, filename, "403", "Forbidden", "Tiny couldn't run the CGI program");
return;
}
serve_dynamic(fd, filename, cgiargs); // line:netp:doit:servedynamic
}
Close(fd);
}
/* $end doit */
/*
* read_requesthdrs - read and parse HTTP request headers
*/
/* $begin read_requesthdrs */
void read_requesthdrs(rio_t *rp)
{
char buf[MAXLINE];
Rio_readlineb(rp, buf, MAXLINE);
while (strcmp(buf, "\r\n"))
{ // line:netp:readhdrs:checkterm
Rio_readlineb(rp, buf, MAXLINE);
printf("%s", buf);
}
return;
}
/* $end read_requesthdrs */
/*
* parse_uri - parse URI into filename and CGI args
* return 0 if dynamic content, 1 if static
*/
/* $begin parse_uri */
int parse_uri(char *uri, char *filename, char *cgiargs)
{
char *ptr;
if (!strstr(uri, "cgi-bin"))
{ /* Static content */ // line:netp:parseuri:isstatic
strcpy(cgiargs, ""); // line:netp:parseuri:clearcgi
strcpy(filename, "."); // line:netp:parseuri:beginconvert1
strcat(filename, uri); // line:netp:parseuri:endconvert1
if (uri[strlen(uri) - 1] == '/') // line:netp:parseuri:slashcheck
strcat(filename, "home.html"); // line:netp:parseuri:appenddefault
return 1;
}
else
{ /* Dynamic content */ // line:netp:parseuri:isdynamic
ptr = index(uri, '?'); // line:netp:parseuri:beginextract
if (ptr)
{
strcpy(cgiargs, ptr + 1);
*ptr = '\0';
}
else
strcpy(cgiargs, ""); // line:netp:parseuri:endextract
strcpy(filename, "."); // line:netp:parseuri:beginconvert2
strcat(filename, uri); // line:netp:parseuri:endconvert2
return 0;
}
}
/* $end parse_uri */
/*
* serve_static - copy a file back to the client
*/
/* $begin serve_static */
void serve_static(int fd, char *filename, int filesize)
{
int srcfd;
char *srcp, filetype[MAXLINE], buf[MAXBUF];
/* Send response headers to client */
get_filetype(filename, filetype); // line:netp:servestatic:getfiletype
sprintf(buf, "HTTP/1.0 200 OK\r\n"); // line:netp:servestatic:beginserve
sprintf(buf, "%sServer: Tiny Web Server\r\n", buf);
sprintf(buf, "%sRequestStat: %d\r\n", buf, numeroRequestStat++);
sprintf(buf, "%sContent-length: %d\r\n", buf, filesize);
sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype);
Rio_writen(fd, buf, strlen(buf)); // line:netp:servestatic:endserve
/* Send response body to client */
srcfd = Open(filename, O_RDONLY, 0); // line:netp:servestatic:open
srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); // line:netp:servestatic:mmap
Close(srcfd); // line:netp:servestatic:close
Rio_writen(fd, srcp, filesize); // line:netp:servestatic:write
Munmap(srcp, filesize); // line:netp:servestatic:munmap
}
/*
* get_filetype - derive file type from file name
* Deverá adicionar mais tipos
*/
void get_filetype(char *filename, char *filetype)
{
if (strstr(filename, ".html"))
strcpy(filetype, "text/html");
else if (strstr(filename, ".gif"))
strcpy(filetype, "image/gif");
else if (strstr(filename, ".jpg"))
strcpy(filetype, "image/jpeg");
else
strcpy(filetype, "text/plain");
}
/* $end serve_static */
/*
* serve_dynamic - run a CGI program on behalf of the client
*/
/* $begin serve_dynamic */
void serve_dynamic(int fd, char *filename, char *cgiargs)
{
char buf[MAXLINE], *emptylist[] = {NULL};
int pipefd[2];
/*Paul Crocker
Changed so that client content is piped back to parent
*/
Pipe(pipefd);
if (Fork() == 0)
{ /* child */ // line:netp:servedynamic:fork
/* Real server would set all CGI vars here */
setenv("QUERY_STRING", cgiargs, 1); // line:netp:servedynamic:setenv
// Dup2 (fd, STDOUT_FILENO); /* Redirect stdout to client *///line:netp:servedynamic:dup2
Dup2(pipefd[1], STDOUT_FILENO);
Execve(filename, emptylist, environ); /* Run CGI program */ // line:netp:servedynamic:execve
}
close(pipefd[1]);
char content[1024]; // max size that cgi program will return
int contentLength = read(pipefd[0], content, 1024);
Wait(NULL); /* Parent waits for and reaps child */ // line:netp:servedynamic:wait
/* Generate the HTTP response */
sprintf(buf, "HTTP/1.0 200 OK\r\n"); // line:netp:servestatic:beginserve
sprintf(buf, "%sServer: Tiny Web Server\r\n", buf);
sprintf(buf, "%sRequestStat: %d\r\n", buf, numeroRequestStat++);
sprintf(buf, "%sContent-length: %d\r\n", buf, contentLength);
sprintf(buf, "%sContent-type: text/html\r\n\r\n", buf);
Rio_writen(fd, buf, strlen(buf)); // line:netp:servestatic:endserve
Rio_writen(fd, content, contentLength);
}
/* $end serve_dynamic */
/*
* clienterror - returns an error message to the client
*/
/* $begin clienterror */
void clienterror(int fd, char *cause, char *errnum, char *shortmsg, char *longmsg)
{
char buf[MAXLINE], body[MAXBUF];
/* Build the HTTP response body */
/* Fazer primeiro visto que precisamos de saber o tamanho do body */
sprintf(body, "<html><title>Tiny Error</title>");
sprintf(body, "%s<body bgcolor="
"ffffff"
">\r\n",
body);
sprintf(body, "%s%s: %s\r\n", body, errnum, shortmsg);
sprintf(body, "%s<p>%s: %s\r\n", body, longmsg, cause);
sprintf(body, "%s<hr><em>The Tiny Web server</em>\r\n", body);
/* Print the HTTP response */
sprintf(buf, "HTTP/1.0 %s %s\r\n", errnum, shortmsg);
Rio_writen(fd, buf, strlen(buf));
sprintf(buf, "%sServer: Tiny Web Server\r\n", buf);
sprintf(buf, "%sRequestStat: %d\r\n", buf, numeroRequestStat++);
Rio_writen(fd, buf, strlen(buf));
sprintf(buf, "Content-length: %d\r\n\r\n", (int)strlen(body));
Rio_writen(fd, buf, strlen(buf));
sprintf(buf, "Content-type: text/html\r\n");
Rio_writen(fd, buf, strlen(buf));
Rio_writen(fd, body, strlen(body));
}
/* $end clienterror */