-
Notifications
You must be signed in to change notification settings - Fork 0
/
messageHandler.c
110 lines (93 loc) · 2.72 KB
/
messageHandler.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
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "messageHandler.h"
char *readFileContent(char *filename, char *fileContent)
{
//size_t fileLength = -1;
FILE *fp = fopen(filename, "r");
char *line = NULL;
size_t linecap = 0;
ssize_t linelen;
while ((linelen = getline(&line, &linecap, fp)) > 0)
{
int newLenght = linelen + strlen(fileContent) + 2;
fileContent = realloc(fileContent, newLenght);
strcat(fileContent, line);
}
if (fclose(fp) < 0)
{
fprintf(stderr, "Cannot close the file (%s) the client requested!", filename);
exit(EXIT_FAILURE);
}
return fileContent;
}
void sendTextWithHeader(char *message, char *httpHeaderAsString, int clientFd)
{
// write response
FILE *cl = fdopen(clientFd, "w");
fputs(httpHeaderAsString, cl);
fputs(message, cl);
if (fflush(cl) != 0)
{
//error
fprintf(stderr, "Error flushing to client!\n");
}
if (fclose(cl) != 0)
{
//error
fprintf(stderr, "Error closing connection to client!\n");
}
}
void sentTextFileContent(char *filename, char *httpHeaderAsString, int clientFd)
{
// reading the file content
char *fileContent = calloc(2, sizeof(char));
fileContent = readFileContent(filename, fileContent);
sendTextWithHeader(fileContent, httpHeaderAsString, clientFd);
free(fileContent);
}
void sendBinaryFile(char *filename, char *httpHeaderAsString, int clientFd)
{
// read binary file
struct stat fileInfo;
stat(filename, &fileInfo);
int binData[fileInfo.st_size];
FILE *file = fopen(filename, "r");
fread(binData, 1, fileInfo.st_size, file);
if (fclose(file) != 0)
{
//error
fprintf(stderr, "Error closing file bin read!\n");
}
FILE *cl = fdopen(clientFd, "w");
fputs(httpHeaderAsString, cl);
fwrite(binData, 1, fileInfo.st_size, cl);
if (fflush(cl) != 0)
{
//error
fprintf(stderr, "Error flushing to client!\n");
}
if (fclose(cl) != 0)
{
//error
fprintf(stderr, "Error closing connection to client!\n");
}
}
void sentFileContent(httpheader_t responseHttpheader, char *filename, int clientFd)
{
char *httpHeaderAsString = calloc(2, sizeof(char));
httpHeaderAsString = responseheaderToString(&responseHttpheader, httpHeaderAsString);
if (isBinaryMimeType(responseHttpheader.content_type) == 1)
{
// send binary data to the client
sendBinaryFile(filename, httpHeaderAsString, clientFd);
}
else
{
// send text data to the client
sentTextFileContent(filename, httpHeaderAsString, clientFd);
}
free(httpHeaderAsString);
}