This is the function that sends the message to the client:
Code:
void send_file(int remotefd, char file_addr[], int http_subver){
char *file;
char buf[FILE_BUF_SIZE + 60];
bzero(buf, FILE_BUF_SIZE + 60);
file= read_file(file_addr);
sprintf(buf, "HTTP/1.%d 200 OK\n", http_subver);
strncat(buf, file, FILE_BUF_SIZE+40);
free(file);
write(remotefd, buf, strlen(buf));
printf("%s", buf);
return;
}
This is the sub function that builds the message:
Code:
#define FILE_BUF_SIZE 1024*10 //10k
#define SEND_FILE "Content-Type: %s\n\n%s\n\r\n"
char * read_file(char * file_addr){
char * buf= calloc(FILE_BUF_SIZE + 40, sizeof(char));
int fd;
char file[FILE_BUF_SIZE];
char * filetype;
bzero(&file, FILE_BUF_SIZE);
filetype= file_type(file_addr);
fd= open(file_addr, O_RDONLY);
if(fd < 0)
perror("error opening file");
if(read(fd, file, FILE_BUF_SIZE) < 0)
perror("error reading file");
close(fd);
sprintf(buf, SEND_FILE, filetype, file);
return buf;
}