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