From 0df51d03d9f74d295434071006238e8ca9e53e64 Mon Sep 17 00:00:00 2001 From: Angelo Haller Date: Tue, 28 Aug 2012 18:07:06 +0200 Subject: [PATCH] Move mime type guessing by extension to separate file and export it Signed-off-by: Jean-Baptiste Kempf --- .../cmake/CMakeLists/src_CMakeLists.txt | 1 + include/vlc_mime.h | 31 +++++ src/Makefile.am | 2 + src/libvlccore.sym | 1 + src/misc/mime.c | 107 ++++++++++++++++++ src/network/httpd.c | 80 +------------ 6 files changed, 145 insertions(+), 77 deletions(-) create mode 100644 include/vlc_mime.h create mode 100644 src/misc/mime.c diff --git a/extras/buildsystem/cmake/CMakeLists/src_CMakeLists.txt b/extras/buildsystem/cmake/CMakeLists/src_CMakeLists.txt index f96a62ce1f..b178b81b8a 100644 --- a/extras/buildsystem/cmake/CMakeLists/src_CMakeLists.txt +++ b/extras/buildsystem/cmake/CMakeLists/src_CMakeLists.txt @@ -118,6 +118,7 @@ set( SOURCES_libvlccore_common misc/events.c misc/image.c misc/messages.c + misc/mime.c misc/objects.c misc/variables.h misc/variables.c diff --git a/include/vlc_mime.h b/include/vlc_mime.h new file mode 100644 index 0000000000..f04fc95d8f --- /dev/null +++ b/include/vlc_mime.h @@ -0,0 +1,31 @@ +/***************************************************************************** + * vlc_mime.h: Mime type recognition + ***************************************************************************** + * Copyright (C) 2012 VLC authors and VideoLAN + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#ifndef VLC_MIME_H +#define VLC_MIME_H 1 + +/** + * \file + * Mime type recognition helpers. + */ + +VLC_API const char * vlc_mime_Ext2Mime( const char *psz_url ); + +#endif /* _VLC_MIME_H */ diff --git a/src/Makefile.am b/src/Makefile.am index da40d004ed..ff25cff705 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -64,6 +64,7 @@ pluginsinclude_HEADERS = \ ../include/vlc_messages.h \ ../include/vlc_meta.h \ ../include/vlc_media_library.h \ + ../include/vlc_mime.h \ ../include/vlc_modules.h \ ../include/vlc_mouse.h \ ../include/vlc_mtime.h \ @@ -438,6 +439,7 @@ SOURCES_libvlc_common = \ misc/events.c \ misc/image.c \ misc/messages.c \ + misc/mime.c \ misc/objects.c \ misc/variables.h \ misc/variables.c \ diff --git a/src/libvlccore.sym b/src/libvlccore.sym index a8599345a7..be4858803b 100644 --- a/src/libvlccore.sym +++ b/src/libvlccore.sym @@ -535,6 +535,7 @@ vlc_meta_New vlc_meta_Set vlc_meta_SetStatus vlc_meta_TypeToLocalizedString +vlc_mime_Ext2Mime vlc_mutex_destroy vlc_mutex_init vlc_mutex_init_recursive diff --git a/src/misc/mime.c b/src/misc/mime.c new file mode 100644 index 0000000000..e56a7302df --- /dev/null +++ b/src/misc/mime.c @@ -0,0 +1,107 @@ +/***************************************************************************** + * mime.c + ***************************************************************************** + * Copyright © 2004-2012 VLC authors and VideoLAN + * Copyright © 2004-2007 Rémi Denis-Courmont + * + * Authors: Laurent Aimar + * Rémi Denis-Courmont + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include + +#include + +static const struct +{ + const char psz_ext[8]; + const char *psz_mime; +} ext_mime[] = +{ + { ".htm", "text/html" }, + { ".html", "text/html" }, + { ".txt", "text/plain" }, + { ".xml", "text/xml" }, + { ".dtd", "text/dtd" }, + + { ".css", "text/css" }, + + /* image mime */ + { ".gif", "image/gif" }, + { ".jpe", "image/jpeg" }, + { ".jpg", "image/jpeg" }, + { ".jpeg", "image/jpeg" }, + { ".png", "image/png" }, + /* same as modules/mux/mpjpeg.c here: */ + { ".mpjpeg","multipart/x-mixed-replace; boundary=7b3cc56e5f51db803f790dad720ed50a" }, + + /* media mime */ + { ".avi", "video/avi" }, + { ".asf", "video/x-ms-asf" }, + { ".m1a", "audio/mpeg" }, + { ".m2a", "audio/mpeg" }, + { ".m1v", "video/mpeg" }, + { ".m2v", "video/mpeg" }, + { ".mp2", "audio/mpeg" }, + { ".mp3", "audio/mpeg" }, + { ".mpa", "audio/mpeg" }, + { ".mpg", "video/mpeg" }, + { ".mpeg", "video/mpeg" }, + { ".mpe", "video/mpeg" }, + { ".mov", "video/quicktime" }, + { ".moov", "video/quicktime" }, + { ".oga", "audio/ogg" }, + { ".ogg", "application/ogg" }, + { ".ogm", "application/ogg" }, + { ".ogv", "video/ogg" }, + { ".ogx", "application/ogg" }, + { ".spx", "audio/ogg" }, + { ".wav", "audio/wav" }, + { ".wma", "audio/x-ms-wma" }, + { ".wmv", "video/x-ms-wmv" }, + { ".webm", "video/webm" }, + + /* end */ + { "", "" } +}; + +const char *vlc_mime_Ext2Mime( const char *psz_url ) +{ + + char *psz_ext; + + psz_ext = strrchr( psz_url, '.' ); + if( psz_ext ) + { + int i; + + for( i = 0; ext_mime[i].psz_ext[0] ; i++ ) + { + if( !strcasecmp( ext_mime[i].psz_ext, psz_ext ) ) + { + return ext_mime[i].psz_mime; + } + } + } + return "application/octet-stream"; +} + diff --git a/src/network/httpd.c b/src/network/httpd.c index e3195d3915..5052646557 100644 --- a/src/network/httpd.c +++ b/src/network/httpd.c @@ -38,6 +38,7 @@ #include #include #include +#include #include "../libvlc.h" #include @@ -172,81 +173,6 @@ struct httpd_client_t /***************************************************************************** * Various functions *****************************************************************************/ -static const struct -{ - const char psz_ext[8]; - const char *psz_mime; -} http_mime[] = -{ - { ".htm", "text/html" }, - { ".html", "text/html" }, - { ".txt", "text/plain" }, - { ".xml", "text/xml" }, - { ".dtd", "text/dtd" }, - - { ".css", "text/css" }, - - /* image mime */ - { ".gif", "image/gif" }, - { ".jpe", "image/jpeg" }, - { ".jpg", "image/jpeg" }, - { ".jpeg", "image/jpeg" }, - { ".png", "image/png" }, - /* same as modules/mux/mpjpeg.c here: */ - { ".mpjpeg","multipart/x-mixed-replace; boundary=7b3cc56e5f51db803f790dad720ed50a" }, - - /* media mime */ - { ".avi", "video/avi" }, - { ".asf", "video/x-ms-asf" }, - { ".m1a", "audio/mpeg" }, - { ".m2a", "audio/mpeg" }, - { ".m1v", "video/mpeg" }, - { ".m2v", "video/mpeg" }, - { ".mp2", "audio/mpeg" }, - { ".mp3", "audio/mpeg" }, - { ".mpa", "audio/mpeg" }, - { ".mpg", "video/mpeg" }, - { ".mpeg", "video/mpeg" }, - { ".mpe", "video/mpeg" }, - { ".mov", "video/quicktime" }, - { ".moov", "video/quicktime" }, - { ".oga", "audio/ogg" }, - { ".ogg", "application/ogg" }, - { ".ogm", "application/ogg" }, - { ".ogv", "video/ogg" }, - { ".ogx", "application/ogg" }, - { ".spx", "audio/ogg" }, - { ".wav", "audio/wav" }, - { ".wma", "audio/x-ms-wma" }, - { ".wmv", "video/x-ms-wmv" }, - { ".webm", "video/webm" }, - - /* end */ - { "", "" } -}; - -static const char *httpd_MimeFromUrl( const char *psz_url ) -{ - - char *psz_ext; - - psz_ext = strrchr( psz_url, '.' ); - if( psz_ext ) - { - int i; - - for( i = 0; http_mime[i].psz_ext[0] ; i++ ) - { - if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) ) - { - return http_mime[i].psz_mime; - } - } - } - return "application/octet-stream"; -} - - typedef struct { unsigned i_code; @@ -459,7 +385,7 @@ httpd_file_t *httpd_FileNew( httpd_host_t *host, } else { - file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) ); + file->psz_mime = strdup( vlc_mime_Ext2Mime( psz_url ) ); } file->pf_fill = pf_fill; @@ -857,7 +783,7 @@ httpd_stream_t *httpd_StreamNew( httpd_host_t *host, } else { - stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) ); + stream->psz_mime = strdup( vlc_mime_Ext2Mime( psz_url ) ); } stream->i_header = 0; stream->p_header = NULL;