~duncan-bayne/halp

4405910972932f7c993fc9fab6c011f04427b9bc — Duncan Bayne 6 months ago 4d8b143
Extract MIME types into a separate module

... just because it was making me sad that every time I opened
WebServer.pm I saw a list of (not Web specific!) MIME types.
2 files changed, 76 insertions(+), 67 deletions(-)

A lib/Halp/MimeTypes.pm
M lib/Halp/WebServer.pm
A lib/Halp/MimeTypes.pm => lib/Halp/MimeTypes.pm +74 -0
@@ 0,0 1,74 @@
package Halp::MimeTypes;

use Exporter 'import';
our @EXPORT = qw(mime_types);

sub mime_types {
    return {
	'ai'    => 'application/postscript',
	    'aif'   => 'audio/x-aiff',
	    'aifc'  => 'audio/x-aiff',
	    'aiff'  => 'audio/x-aiff',
	    'au'    => 'audio/basic',
	    'avi'   => 'video/x-msvideo',
	    'bmp'   => 'image/bmp',
	    'bz2'   => 'application/x-bzip2',
	    'class' => 'application/java',
	    'css'   => 'text/css',
	    'csv'   => 'text/csv',
	    'eps'   => 'application/postscript',
	    'flac'  => 'audio/flac',
	    'gif'   => 'image/gif',
	    'gz'    => 'application/gzip',
	    'htm'   => 'text/html',
	    'html'  => 'text/html',
	    'ics'   => 'text/calendar',
	    'jar'   => 'application/java-archive',
	    'jfif'  => 'image/jpeg',
	    'jpe'   => 'image/jpeg',
	    'jpeg'  => 'image/jpeg',
	    'jpg'   => 'image/jpeg',
	    'js'    => 'text/javascript',
	    'json'  => 'application/json',
	    'm3u'   => 'audio/x-mpegurl',
	    'm4a'   => 'audio/mp4',
	    'mid'   => 'audio/midi',
	    'midi'  => 'audio/midi',
	    'mov'   => 'video/quicktime',
	    'mp3'   => 'audio/mpeg',
	    'mp4'   => 'video/mp4',
	    'mpeg'  => 'video/mpeg',
	    'mpg'   => 'video/mpeg',
	    'odp'   => 'application/vnd.oasis.opendocument.presentation',
	    'ods'   => 'application/vnd.oasis.opendocument.spreadsheet',
	    'odt'   => 'application/vnd.oasis.opendocument.text',
	    'ogg'   => 'audio/ogg',
	    'pdf'   => 'application/pdf',
	    'php'   => 'application/x-httpd-php',
	    'png'   => 'image/png',
	    'ps'    => 'application/postscript',
	    'qt'    => 'video/quicktime',
	    'rar'   => 'application/x-rar-compressed',
	    'rtf'   => 'application/rtf',
	    'sh'    => 'application/x-sh',
	    'svg'   => 'image/svg+xml',
	    'swf'   => 'application/x-shockwave-flash',
	    'tar'   => 'application/x-tar',
	    'tex'   => 'application/x-tex',
	    'tif'   => 'image/tiff',
	    'tiff'  => 'image/tiff',
	    'ts'    => 'video/mp2t',
	    'txt'   => 'text/plain',
	    'wav'   => 'audio/wav',
	    'weba'  => 'audio/webm',
	    'webm'  => 'video/webm',
	    'webp'  => 'image/webp',
	    'woff'  => 'font/woff',
	    'woff2' => 'font/woff2',
	    'xml'   => 'application/xml',
	    'xslt'  => 'application/xslt+xml',
	    'zip'   => 'application/zip',
    };
}

1;

M lib/Halp/WebServer.pm => lib/Halp/WebServer.pm +2 -67
@@ 5,6 5,7 @@ use File::Slurp;
use File::Spec;
use Halp::AtomFeed;
use Halp::ContentUtils;
use Halp::MimeTypes;
use HTTP::Server::Simple::CGI;
use Text::Template;
use Time::Piece;


@@ 24,72 25,6 @@ sub new {
    $self->{template_path} = "./www/templates";
    $self->{default_site_path} = "./www/site";

    $self->{mime_types} = {
	'ai'    => 'application/postscript',
	    'aif'   => 'audio/x-aiff',
	    'aifc'  => 'audio/x-aiff',
	    'aiff'  => 'audio/x-aiff',
	    'au'    => 'audio/basic',
	    'avi'   => 'video/x-msvideo',
	    'bmp'   => 'image/bmp',
	    'bz2'   => 'application/x-bzip2',
	    'class' => 'application/java',
	    'css'   => 'text/css',
	    'csv'   => 'text/csv',
	    'eps'   => 'application/postscript',
	    'flac'  => 'audio/flac',
	    'gif'   => 'image/gif',
	    'gz'    => 'application/gzip',
	    'htm'   => 'text/html',
	    'html'  => 'text/html',
	    'ics'   => 'text/calendar',
	    'jar'   => 'application/java-archive',
	    'jfif'  => 'image/jpeg',
	    'jpe'   => 'image/jpeg',
	    'jpeg'  => 'image/jpeg',
	    'jpg'   => 'image/jpeg',
	    'js'    => 'text/javascript',
	    'json'  => 'application/json',
	    'm3u'   => 'audio/x-mpegurl',
	    'm4a'   => 'audio/mp4',
	    'mid'   => 'audio/midi',
	    'midi'  => 'audio/midi',
	    'mov'   => 'video/quicktime',
	    'mp3'   => 'audio/mpeg',
	    'mp4'   => 'video/mp4',
	    'mpeg'  => 'video/mpeg',
	    'mpg'   => 'video/mpeg',
	    'odp'   => 'application/vnd.oasis.opendocument.presentation',
	    'ods'   => 'application/vnd.oasis.opendocument.spreadsheet',
	    'odt'   => 'application/vnd.oasis.opendocument.text',
	    'ogg'   => 'audio/ogg',
	    'pdf'   => 'application/pdf',
	    'php'   => 'application/x-httpd-php',
	    'png'   => 'image/png',
	    'ps'    => 'application/postscript',
	    'qt'    => 'video/quicktime',
	    'rar'   => 'application/x-rar-compressed',
	    'rtf'   => 'application/rtf',
	    'sh'    => 'application/x-sh',
	    'svg'   => 'image/svg+xml',
	    'swf'   => 'application/x-shockwave-flash',
	    'tar'   => 'application/x-tar',
	    'tex'   => 'application/x-tex',
	    'tif'   => 'image/tiff',
	    'tiff'  => 'image/tiff',
	    'ts'    => 'video/mp2t',
	    'txt'   => 'text/plain',
	    'wav'   => 'audio/wav',
	    'weba'  => 'audio/webm',
	    'webm'  => 'video/webm',
	    'webp'  => 'image/webp',
	    'woff'  => 'font/woff',
	    'woff2' => 'font/woff2',
	    'xml'   => 'application/xml',
	    'xslt'  => 'application/xslt+xml',
	    'zip'   => 'application/zip',
    };

    return $self;
}



@@ 201,7 136,7 @@ sub not_found_404 {
sub handle_file {
    my ($self, $request_path, $local_path) = @_;
    my ($extension) = $local_path =~ /\.([^.]*)$/;
    my $mime_type = $self->{mime_types}->{$extension};
    my $mime_type = mime_types->{$extension};
    my $content = read_file($local_path, binmode => ':bytes');

    if ($extension eq "html") {