mime.c
Go to the documentation of this file.
1 /**
2  * @file mime.c
3  * @brief MIME (Multipurpose Internet Mail Extensions)
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 //Switch to the appropriate trace level
30 #define TRACE_LEVEL HTTP_TRACE_LEVEL
31 
32 //Dependencies
33 #include "core/net.h"
34 #include "http/mime.h"
35 #include "debug.h"
36 
37 //MIME type list
38 static const MimeType mimeTypeList[] =
39 {
40  //Custom MIME types
42 
43  //Text MIME types
44  {".css", "text/css"},
45  {".csv", "text/csv"},
46  {".htc", "text/x-component"},
47  {".htm", "text/html"},
48  {".html", "text/html"},
49  {".shtm", "text/html"},
50  {".shtml", "text/html"},
51  {".stm", "text/html"},
52  {".txt", "text/plain"},
53  {".vcf", "text/vcard"},
54  {".vcard", "text/vcard"},
55  {".xml", "text/xml"},
56 
57  //Image MIME types
58  {".gif", "image/gif"},
59  {".ico", "image/x-icon"},
60  {".jpg", "image/jpeg"},
61  {".jpeg", "image/jpeg"},
62  {".png", "image/png"},
63  {".svg", "image/svg+xml"},
64  {".tif", "image/tiff"},
65 
66  //Audio MIME types
67  {".aac", "audio/x-aac"},
68  {".aif", "audio/x-aiff"},
69  {".mp3", "audio/mpeg"},
70  {".wav", "audio/x-wav"},
71  {".wma", "audio/x-ms-wma"},
72 
73  //Video MIME types
74  {".avi", "video/x-msvideo"},
75  {".flv", "video/x-flv"},
76  {".mov", "video/quicktime"},
77  {".mp4", "video/mp4"},
78  {".mpg", "video/mpeg"},
79  {".mpeg", "video/mpeg"},
80  {".wmv", "video/x-ms-wmv"},
81 
82  //Application MIME types
83  {".doc", "application/msword"},
84  {".gz", "application/x-gzip"},
85  {".gzip", "application/x-gzip"},
86  {".js", "application/javascript"},
87  {".json", "application/json"},
88  {".ogg", "application/ogg"},
89  {".pdf", "application/pdf"},
90  {".ppt", "application/vnd.ms-powerpoint"},
91  {".rar", "application/x-rar-compressed"},
92  {".rtf", "application/rtf"},
93  {".tar", "application/x-tar"},
94  {".tgz", "application/x-gzip"},
95  {".xht", "application/xhtml+xml"},
96  {".xhtml", "application/xhtml+xml"},
97  {".xls", "application/vnd.ms-excel"},
98  {".zip", "application/zip"}
99 };
100 
101 
102 /**
103  * @brief Get the MIME type from a given extension
104  *
105  * This function translates a filename or a file extension into a MIME type
106  *
107  * @param[in] filename Filename from which to extract the MIME type
108  * @return NULL-terminated string containing the associated MIME type
109  **/
110 
112 {
113  uint_t i;
114  uint_t n;
115  uint_t m;
116 
117  //MIME type for unknown extensions
118  static const char_t defaultMimeType[] = "application/octet-stream";
119 
120  //Valid filename?
121  if(filename != NULL)
122  {
123  //Get the length of the specified filename
124  n = strlen(filename);
125 
126  //Search the MIME type that matches the specified extension
127  for(i = 0; i < arraysize(mimeTypeList); i++)
128  {
129  //Length of the extension
130  m = strlen(mimeTypeList[i].extension);
131  //Compare file extensions
132  if(m <= n && !strcasecmp(filename + n - m, mimeTypeList[i].extension))
133  return mimeTypeList[i].type;
134  }
135  }
136 
137  //Return the default MIME type when an unknown extension is encountered
138  return defaultMimeType;
139 }
char_t filename[]
Definition: tftp_common.h:89
const char_t * type
Definition: mime.h:53
char char_t
Definition: compiler_port.h:41
TCP/IP stack core.
Debugging facilities.
const char_t * mimeGetType(const char_t *filename)
Get the MIME type from a given extension.
Definition: mime.c:111
uint8_t m
Definition: ndp.h:299
#define strcasecmp
#define arraysize(a)
Definition: os_port.h:68
MIME (Multipurpose Internet Mail Extensions)
MIME type.
Definition: mime.h:50
unsigned int uint_t
Definition: compiler_port.h:43
#define MIME_CUSTOM_TYPES
Definition: mime.h:37
uint8_t n