-
Notifications
You must be signed in to change notification settings - Fork 0
/
permissions.c
62 lines (50 loc) · 1.6 KB
/
permissions.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
#include <string.h>
#ifndef COMMON_HEADERS
#define COMMON_HEADERS
#include "httpHeaderManager.h"
#include "httpStatusCodes.h"
#include "permissions.h"
#include "mimeTypeManager.h"
#include "messageHandler.h"
#endif
void sendFileNotExistsMessage(int clientFd)
{
httpheader_t responseHttpheader = {.statuscode = 404, .httpVersion = "HTTP/1.1", .server = "httpc"};
char *httpHeaderAsString = calloc(2, sizeof(char));
httpHeaderAsString = responseheaderToString(&responseHttpheader, httpHeaderAsString);
sendTextWithHeader(FILE_NOT_FOUND_MESSAGE,httpHeaderAsString, clientFd);
free(httpHeaderAsString);
}
void sendNoPermissionMessage(int clientFd)
{
httpheader_t responseHttpheader = {.statuscode = 403, .httpVersion = "HTTP/1.1", .server = "httpc"};
char *httpHeaderAsString = calloc(2, sizeof(char));
httpHeaderAsString = responseheaderToString(&responseHttpheader, httpHeaderAsString);
sendTextWithHeader(FILE_NO_PERMISSION_MESSAGE,httpHeaderAsString, clientFd);
free(httpHeaderAsString);
}
permission_t checkFileForPermissionAndExistence(httpheader_t *requestHeader)
{
if (access(requestHeader->file, F_OK) != -1)
{
// file exists
if (access(requestHeader->file, R_OK) != -1)
{
// permission ok
return FILE_EXISTS;
}
}
else
{
// file doesn't exist
return FILE_NOT_EXISTS;
}
return PERMISSION_DENIED;
}
const char *getExt(const char *fspec)
{
char *e = strrchr(fspec, '.');
if (e == NULL)
e = ""; // fast method, could also use &(fspec[strlen(fspec)]).
return e;
}