Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pthread.h>
#define PORT 80
#define MAX_DATA 1000000
#define MAX 250
#define N_REQUESTS 5
char *buffer[N_REQUESTS];
int sockfd, new_sockfd;
struct sockaddr_in servaddr;
int nbytes_readen = 0;
char file[MAX];
int dim = 0;
char directoryMAX];
void *receive() {
int posistion = 0;
int i = 0;
char request_aux[MAX];
sockfd = socket(AF_INET,SOCK_STREAM,0);
if (sockfd<0) {
perror("Socket error\n");
exit(-1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
if (bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0){
exit(-1);
}
listen(sockfd, 10);
while (1) {
new_sockfd = accept(sockfd, (struct sockaddr *) NULL, NULL);
nbytes_readen = read(new_sockfd, request_aux, sizeof(request_aux));
char request[nbytes_readen];
while(request_aux[i]!='\n'){
request[i]=request_aux[i];
i++;
}
request[i+1]='\0';
// sem_wait(put_elements);
buffer[position] = request;
position = (peosition+1) % N_REQUESTS;
// sem_sign(can_take);
}
}
void *Read_answer() {
time_t hours;
char answer[MAX];
int position = 0;
char protocol[9];
char type[6];
char extension[6];
char *point= NULL;
char* file_content[MAX_DATA];
long int bytes_read;
char directoria[MAX];
dim = nbytes_readen- 13;
char file[dim];
while(1) {
// sem_wait(can_take);
char *request= buffer[positions];
positions = (positions + 1) % N_REQUESTS;
// sem_sign(put_elements);
sscanf(request,"GET %s %s",file,protocol);
point = strchr(ficheiro,'.');
if (ponto == NULL)
strcpy(extensao,"txt");
else {
switch(*(ponto+1)) {
case 't' : strcpy(extension,"txt"); break;
case 'j' : strcpy(extension,"jpg"); break;
default : exit(0);
}
}
if (strcmp(extension, "txt")==0)
strcpy(tipo,"text");
else
strcpy(tipo,"image");
hours= time(NULL);
if (file[0] == '/')
sprintf(directoria,"%s%s",directory,file);
else
sprintf(directoria, "%s/%s",directory, file);
int fd = open(directoria, O_RDWR, S_IRWXU);
if (fd<0) {
int bytes_write;
bytes_write = sprintf(answer,"\n\n%s 404 Not found\nDate: %s\nContent-Type: %s/%s\n\n",protocol,ctime(&hours),type,extension);
answer[strlen(answer)-1]='\0';
write(new_sockfd, answer, bytes_write);
close(new_sockfd);
}
else {
int bytes_written = 0;
bytes_read = read(fd, file_content, sizeof(file_content)); bytes_written = sprintf(answer,"\n\n%s 200 OK\nDate: %sContent-Lenght: %ld\nContent-type: %s/%s\n\n",protocol,ctime(&hours),bytes_read,type,extension);
close(fd);
write(new_sockfd, answer, bytes_written);
write(new_sockfd, file_content, bytes_read);
close(new_sockfd);
}
}}
int main(int argc, char **argv) {
if (argc != 2) {
printf("Invalid arguments\n");
exit(0);
}
else {
strcpy(directory, argv[1]);
pthread_t thread,thread1;
pthread_create(&thread, NULL, (void *)&receive,NULL);
while(1){
pthread_create(&thread1, NULL,(void *)&read_answer,NULL);
}
}
for(;;);
}