Webserver+3d print

Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mime.c Source File

mime.c

Go to the documentation of this file.
00001 /**
00002  * @file mime.c
00003  * @brief MIME (Multipurpose Internet Mail Extensions)
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This file is part of CycloneTCP Open.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024  *
00025  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00026  * @version 1.7.6
00027  **/
00028 
00029 //Switch to the appropriate trace level
00030 #define TRACE_LEVEL HTTP_TRACE_LEVEL
00031 
00032 //Dependencies
00033 #include "core/net.h"
00034 #include "http/mime.h"
00035 #include "debug.h"
00036 
00037 //MIME type list
00038 static const MimeType mimeTypeList[] =
00039 {
00040    //Custom MIME types
00041    MIME_CUSTOM_TYPES
00042 
00043    //Text MIME types
00044    {".css",   "text/css"},
00045    {".csv",   "text/csv"},
00046    {".htc",   "text/x-component"},
00047    {".htm",   "text/html"},
00048    {".html",  "text/html"},
00049    {".shtm",  "text/html"},
00050    {".shtml", "text/html"},
00051    {".stm",   "text/html"},
00052    {".txt",   "text/plain"},
00053    {".vcf",   "text/vcard"},
00054    {".vcard", "text/vcard"},
00055    {".xml",   "text/xml"},
00056 
00057    //Image MIME types
00058    {".gif",   "image/gif"},
00059    {".ico",   "image/x-icon"},
00060    {".jpg",   "image/jpeg"},
00061    {".jpeg",  "image/jpeg"},
00062    {".png",   "image/png"},
00063    {".svg",   "image/svg+xml"},
00064    {".tif",   "image/tiff"},
00065 
00066    //Audio MIME types
00067    {".aac",   "audio/x-aac"},
00068    {".aif",   "audio/x-aiff"},
00069    {".mp3",   "audio/mpeg"},
00070    {".wav",   "audio/x-wav"},
00071    {".wma",   "audio/x-ms-wma"},
00072 
00073    //Video MIME types
00074    {".avi",   "video/x-msvideo"},
00075    {".flv",   "video/x-flv"},
00076    {".mov",   "video/quicktime"},
00077    {".mp4",   "video/mp4"},
00078    {".mpg",   "video/mpeg"},
00079    {".mpeg",  "video/mpeg"},
00080    {".wmv",   "video/x-ms-wmv"},
00081 
00082    //Application MIME types
00083    {".doc",   "application/msword"},
00084    {".gz",    "application/x-gzip"},
00085    {".gzip",  "application/x-gzip"},
00086    {".js",    "application/javascript"},
00087    {".json",  "application/json"},
00088    {".ogg",   "application/ogg"},
00089    {".pdf",   "application/pdf"},
00090    {".ppt",   "application/vnd.ms-powerpoint"},
00091    {".rar",   "application/x-rar-compressed"},
00092    {".rtf",   "application/rtf"},
00093    {".tar",   "application/x-tar"},
00094    {".tgz",   "application/x-gzip"},
00095    {".xht",   "application/xhtml+xml"},
00096    {".xhtml", "application/xhtml+xml"},
00097    {".xls",   "application/vnd.ms-excel"},
00098    {".zip",   "application/zip"}
00099 };
00100 
00101 
00102 /**
00103  * @brief Get the MIME type from a given extension
00104  *
00105  * This function translates a filename or a file extension into a MIME type
00106  *
00107  * @param[in] filename Filename from which to extract the MIME type
00108  * @return NULL-terminated string containing the associated MIME type
00109  **/
00110 
00111 const char_t *mimeGetType(const char_t *filename)
00112 {
00113    uint_t i;
00114    uint_t n;
00115    uint_t m;
00116 
00117    //MIME type for unknown extensions
00118    static const char_t defaultMimeType[] = "application/octet-stream";
00119 
00120    //Valid filename?
00121    if(filename != NULL)
00122    {
00123       //Get the length of the specified filename
00124       n = strlen(filename);
00125 
00126       //Search the MIME type that matches the specified extension
00127       for(i = 0; i < arraysize(mimeTypeList); i++)
00128       {
00129          //Length of the extension
00130          m = strlen(mimeTypeList[i].extension);
00131          //Compare file extensions
00132          if(m <= n && !strcasecmp(filename + n - m, mimeTypeList[i].extension))
00133             return mimeTypeList[i].type;
00134       }
00135    }
00136 
00137    //Return the default MIME type when an unknown extension is encountered
00138    return defaultMimeType;
00139 }
00140