using System; using System.IO; using System.Web; /* * Copyright (C) 2008-2010 by EDR. * Written by EDR info@kendar.org * All Rights Reserved * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 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 General Public License for more details. * * You should have received a copy of the GNU 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. */ /* * To let this thing works you should add the following httpHandler to your * Web.config, this way all static files will be intercepted by this "trasnparent" * http handler that will throw exceptions if file are not found */ namespace com.kendar.utils { /// /// For debuggin purpose to find forgotten files, is a kind of wrapper to avoid seeking desperately for /// unknown files /// Should add some section to Web.config to intercept the various static files /// configuration, for exeample to check allthe gif files: /// -system.web /// --httpHandlers /// ---add verb="*" path="*.gif" type="com.kendar.utils.DebugHttpHandler" /// public class DebugHttpHandler : IHttpHandler //StaticFileHandler { /// /// Process the request /// /// public void ProcessRequest(HttpContext context) { string objPath = HttpContext.Current.Request.MapPath(context.Request.FilePath); string mimeType = DebugHttpHandler.DetectMime(objPath); context.Response.ContentType = mimeType; if (File.Exists(objPath)) { FileStream fs = File.OpenRead(objPath); DebugHttpHandler.CopyStreamToStream(fs, context.Response.OutputStream); } else { throw new FileNotFoundException(context.Request.FilePath); } } /// /// Is reusable always because is only a simple static file manager /// public bool IsReusable { get { return true; } } /// /// Write from stream to another stream /// /// /// private static void CopyStreamToStream(FileStream sourceStream, Stream targetStream) { // What's the size of the source stream: int size = (int)sourceStream.Length; // Create a buffer that same size: byte[] buffer = new byte[size]; // Move the source stream to the beginning: sourceStream.Seek(0, SeekOrigin.Begin); // copy the whole sourceStream into our buffer: sourceStream.Read(buffer, 0, size); // Write out buffer to the target stream: targetStream.Write(buffer, 0, size); } /// /// Detect some standard mime types /// /// /// private static string DetectMime(string objPath) { int typ = objPath.LastIndexOf('.'); string ext = ""; if (typ >= 0) { ext = objPath.Substring(typ); } switch (ext) { case ("323"): return "text/h323"; case ("asx"): return "video/x-ms-asf"; case ("acx"): return "application/internet-property-stream"; case ("ai"): return "application/postscript"; case ("aif"): return "audio/x-aiff"; case ("aiff"): return "audio/aiff"; case ("axs"): return "application/olescript"; case ("aifc"): return "audio/aiff"; case ("asr"): return "video/x-ms-asf"; case ("avi"): return "video/x-msvideo"; case ("asf"): return "video/x-ms-asf"; case ("au"): return "audio/basic"; case ("application"): return "application/x-ms-application"; case ("bin"): return "application/octet-stream"; case ("bas"): return "text/plain"; case ("bcpio"): return "application/x-bcpio"; case ("bmp"): return "image/bmp"; case ("cdf"): return "application/x-cdf"; case ("cat"): return "application/vndms-pkiseccat"; case ("crt"): return "application/x-x509-ca-cert"; case ("c"): return "text/plain"; case ("css"): return "text/css"; case ("cer"): return "application/x-x509-ca-cert"; case ("crl"): return "application/pkix-crl"; case ("cmx"): return "image/x-cmx"; case ("csh"): return "application/x-csh"; case ("cod"): return "image/cis-cod"; case ("cpio"): return "application/x-cpio"; case ("clp"): return "application/x-msclip"; case ("crd"): return "application/x-mscardfile"; case ("deploy"): return "application/octet-stream"; case ("dll"): return "application/x-msdownload"; case ("dot"): return "application/msword"; case ("doc"): return "application/msword"; case ("dvi"): return "application/x-dvi"; case ("dir"): return "application/x-director"; case ("dxr"): return "application/x-director"; case ("der"): return "application/x-x509-ca-cert"; case ("dib"): return "image/bmp"; case ("dcr"): return "application/x-director"; case ("disco"): return "text/xml"; case ("exe"): return "application/octet-stream"; case ("etx"): return "text/x-setext"; case ("evy"): return "application/envoy"; case ("eml"): return "message/rfc822"; case ("eps"): return "application/postscript"; case ("flr"): return "x-world/x-vrml"; case ("fif"): return "application/fractals"; case ("gtar"): return "application/x-gtar"; case ("gif"): return "image/gif"; case ("gz"): return "application/x-gzip"; case ("hta"): return "application/hta"; case ("htc"): return "text/x-component"; case ("htt"): return "text/webviewhtml"; case ("h"): return "text/plain"; case ("hdf"): return "application/x-hdf"; case ("hlp"): return "application/winhlp"; case ("html"): return "text/html"; case ("shtml"): return "text/html"; case ("htm"): return "text/html"; case ("stm"): return "text/html"; case ("hqx"): return "application/mac-binhex40"; case ("isp"): return "application/x-internet-signup"; case ("iii"): return "application/x-iphone"; case ("ief"): return "image/ief"; case ("ivf"): return "video/x-ivf"; case ("ins"): return "application/x-internet-signup"; case ("ico"): return "image/x-icon"; case ("jpg"): return "image/jpeg"; case ("jfif"): return "image/pjpeg"; case ("jpe"): return "image/jpeg"; case ("jpeg"): return "image/jpeg"; case ("png"): return "image/png"; case ("js"): return "application/x-javascript"; case ("lsx"): return "video/x-la-asf"; case ("latex"): return "application/x-latex"; case ("lsf"): return "video/x-la-asf"; case ("manifest"): return "application/x-ms-manifest"; case ("mhtml"): return "message/rfc822"; case ("mny"): return "application/x-msmoney"; case ("mht"): return "message/rfc822"; case ("mid"): return "audio/mid"; case ("mpv2"): return "video/mpeg"; case ("man"): return "application/x-troff-man"; case ("mvb"): return "application/x-msmediaview"; case ("mpeg"): return "video/mpeg"; case ("m3u"): return "audio/x-mpegurl"; case ("mdb"): return "application/x-msaccess"; case ("mpp"): return "application/vndcase.ms-project"; case ("m1v"): return "video/mpeg"; case ("mpa"): return "video/mpeg"; case ("me"): return "application/x-troff-me"; case ("m13"): return "application/x-msmediaview"; case ("movie"): return "video/x-sgi-movie"; case ("m14"): return "application/x-msmediaview"; case ("mpe"): return "video/mpeg"; case ("mp2"): return "video/mpeg"; case ("mov"): return "video/quicktime"; case ("mp3"): return "audio/mpeg"; case ("mpg"): return "video/mpeg"; case ("ms"): return "application/x-troff-ms"; case ("nc"): return "application/x-netcdf"; case ("nws"): return "message/rfc822"; case ("oda"): return "application/oda"; case ("ods"): return "application/oleobject"; case ("pmc"): return "application/x-perfmon"; case ("p7r"): return "application/x-pkcs7-certreqresp"; case ("p7b"): return "application/x-pkcs7-certificates"; case ("p7s"): return "application/pkcs7-signature"; case ("pmw"): return "application/x-perfmon"; case ("ps"): return "application/postscript"; case ("p7c"): return "application/pkcs7-mime"; case ("pbm"): return "image/x-portable-bitmap"; case ("ppm"): return "image/x-portable-pixmap"; case ("pub"): return "application/x-mspublisher"; case ("pnm"): return "image/x-portable-anymap"; case ("pml"): return "application/x-perfmon"; case ("p10"): return "application/pkcs10"; case ("pfx"): return "application/x-pkcs12"; case ("p12"): return "application/x-pkcs12"; case ("pdf"): return "application/pdf"; case ("pps"): return "application/vndcase.ms-powerpoint"; case ("p7m"): return "application/pkcs7-mime"; case ("pko"): return "application/vndms-pkipko"; case ("ppt"): return "application/vndcase.ms-powerpoint"; case ("pmr"): return "application/x-perfmon"; case ("pma"): return "application/x-perfmon"; case ("pot"): return "application/vndcase.ms-powerpoint"; case ("prf"): return "application/pics-rules"; case ("pgm"): return "image/x-portable-graymap"; case ("qt"): return "video/quicktime"; case ("ra"): return "audio/x-pn-realaudio"; case ("rgb"): return "image/x-rgb"; case ("ram"): return "audio/x-pn-realaudio"; case ("rmi"): return "audio/mid"; case ("ras"): return "image/x-cmu-raster"; case ("roff"): return "application/x-troff"; case ("rtf"): return "application/rtf"; case ("rtx"): return "text/richtext"; case ("sv4crc"): return "application/x-sv4crc"; case ("spc"): return "application/x-pkcs7-certificates"; case ("setreg"): return "application/set-registration-initiation"; case ("snd"): return "audio/basic"; case ("stl"): return "application/vndms-pkistl"; case ("setpay"): return "application/set-payment-initiation"; case ("shar"): return "application/x-shar"; case ("sh"): return "application/x-sh"; case ("sit"): return "application/x-stuffit"; case ("spl"): return "application/futuresplash"; case ("sct"): return "text/scriptlet"; case ("scd"): return "application/x-msschedule"; case ("sst"): return "application/vndms-pkicertstore"; case ("src"): return "application/x-wais-source"; case ("sv4cpio"): return "application/x-sv4cpio"; case ("tex"): return "application/x-tex"; case ("tgz"): return "application/x-compressed"; case ("t"): return "application/x-troff"; case ("tar"): return "application/x-tar"; case ("tr"): return "application/x-troff"; case ("tif"): return "image/tiff"; case ("txt"): return "text/plain"; case ("texinfo"): return "application/x-texinfo"; case ("trm"): return "application/x-msterminal"; case ("tiff"): return "image/tiff"; case ("tcl"): return "application/x-tcl"; case ("texi"): return "application/x-texinfo"; case ("tsv"): return "text/tab-separated-values"; case ("ustar"): return "application/x-ustar"; case ("uls"): return "text/iuls"; case ("vcf"): return "text/x-vcard"; case ("wps"): return "application/vndcase.ms-works"; case ("wav"): return "audio/wav"; case ("wrz"): return "x-world/x-vrml"; case ("wri"): return "application/x-mswrite"; case ("wks"): return "application/vndcase.ms-works"; case ("wmf"): return "application/x-msmetafile"; case ("wcm"): return "application/vndcase.ms-works"; case ("wrl"): return "x-world/x-vrml"; case ("wdb"): return "application/vndcase.ms-works"; case ("wsdl"): return "text/xml"; case ("xml"): return "text/xml"; case ("xlm"): return "application/vndcase.ms-excel"; case ("xaf"): return "x-world/x-vrml"; case ("xla"): return "application/vndcase.ms-excel"; case ("xls"): return "application/vndcase.ms-excel"; case ("xof"): return "x-world/x-vrml"; case ("xlt"): return "application/vndcase.ms-excel"; case ("xlc"): return "application/vndcase.ms-excel"; case ("xsl"): return "text/xml"; case ("xbm"): return "image/x-xbitmap"; case ("xlw"): return "application/vndcase.ms-excel"; case ("xpm"): return "image/x-xpixmap"; case ("xwd"): return "image/x-xwindowdump"; case ("xsd"): return "text/xml"; case ("z"): return "application/x-compress"; case ("zip"): return "application/x-zip-compressed"; default: return "application/octet-stream"; } } } }