forked from bloominstituteoftechnology/C-Web-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmime.c
46 lines (36 loc) · 1.08 KB
/
mime.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
#include <string.h>
#include <ctype.h>
#include "mime.h"
#define DEFAULT_MIME_TYPE "application/octet-stream"
/**
* Lowercase a string
*/
char *strlower(char *s)
{
for (char *p = s; *p != '\0'; p++) {
*p = tolower(*p);
}
return s;
}
/**
* Return a MIME type for a given filename
*/
char *mime_type_get(char *filename)
{
char *ext = strrchr(filename, '.');
if (ext == NULL) {
return DEFAULT_MIME_TYPE;
}
ext++;
strlower(ext);
// TODO: this is O(n) and it should be O(1)
if (strcmp(ext, "html") == 0 || strcmp(ext, "htm") == 0) { return "text/html"; }
if (strcmp(ext, "jpeg") == 0 || strcmp(ext, "jpg") == 0) { return "image/jpg"; }
if (strcmp(ext, "css") == 0) { return "text/css"; }
if (strcmp(ext, "js") == 0) { return "application/javascript"; }
if (strcmp(ext, "json") == 0) { return "application/json"; }
if (strcmp(ext, "txt") == 0) { return "text/plain"; }
if (strcmp(ext, "gif") == 0) { return "image/gif"; }
if (strcmp(ext, "png") == 0) { return "image/png"; }
return DEFAULT_MIME_TYPE;
}