Ewl » Canonical The canonical version of EWL Read More
Clone URL:  
StaticFileHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System; using System.Globalization; using System.IO; using System.Linq; using System.Web; using MimeTypes; namespace EnterpriseWebLibrary.EnterpriseWebFramework { /// <summary> /// ISU and internal use only. /// </summary> public class StaticFileHandler: IHttpHandler { /// <summary> /// ISU and internal use only. /// </summary> public const string EwfFolderName = "Ewf"; /// <summary> /// ISU and internal use only. /// </summary> public const string VersionedFilesFolderName = "VersionedStaticFiles"; private const string urlVersionStringPrefix = "-"; /// <summary> /// Development Utility use only. /// </summary> public static string GetUrlVersionString( DateTimeOffset dateAndTime ) { return urlVersionStringPrefix + EwfSafeResponseWriter.GetUrlVersionString( dateAndTime ); } /// <summary> /// ISU and internal use only. /// </summary> public static string CombineNamespacesAndProcessEwfIfNecessary( string appNamespace, string appRelativeNamespace ) { var ewfNamespace = EwlStatics.EwfFolderBaseNamespace + "." + appNamespace; if( appRelativeNamespace == EwfFolderName ) return ewfNamespace; if( appRelativeNamespace.StartsWith( EwfFolderName + "." ) ) return ewfNamespace + "." + appRelativeNamespace.Substring( 4 ); // App-relative namespace can be empty when this method is called from the ISU. return appNamespace + appRelativeNamespace.PrependDelimiter( "." ); } void IHttpHandler.ProcessRequest( HttpContext context ) { var url = EwfApp.GetRequestAppRelativeUrl( context.Request ); var queryIndex = url.IndexOf( "?", StringComparison.Ordinal ); if( queryIndex >= 0 ) url = url.Remove( queryIndex ); var extensionIndex = url.LastIndexOf( ".", StringComparison.Ordinal ); var versionStringOrFileExtensionIndex = extensionIndex; var urlVersionString = ""; if( url.StartsWith( VersionedFilesFolderName + "/" ) || url.StartsWith( EwfFolderName + "/" + VersionedFilesFolderName + "/" ) ) urlVersionString = "invariant"; else { // We assume that all URL version strings will have the same length as the format string. var prefixedVersionStringIndex = extensionIndex - ( urlVersionStringPrefix.Length + EwfSafeResponseWriter.UrlVersionStringFormat.Length ); if( prefixedVersionStringIndex >= 0 ) { DateTimeOffset dateAndTime; var versionString = url.Substring( prefixedVersionStringIndex + urlVersionStringPrefix.Length, EwfSafeResponseWriter.UrlVersionStringFormat.Length ); if( DateTimeOffset.TryParseExact( versionString, EwfSafeResponseWriter.UrlVersionStringFormat, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dateAndTime ) ) { versionStringOrFileExtensionIndex = prefixedVersionStringIndex; urlVersionString = versionString; } } } if( versionStringOrFileExtensionIndex < 0 ) throw new ResourceNotAvailableException( "Failed to find the extension in the URL.", null ); var extension = url.Substring( extensionIndex ); var staticFileInfo = EwfApp.GlobalType.Assembly.CreateInstance( CombineNamespacesAndProcessEwfIfNecessary( EwfApp.GlobalType.Namespace, ( url.Remove( versionStringOrFileExtensionIndex ) + extension.CapitalizeString() ).Separate( "/", false ) .Select( i => EwlStatics.GetCSharpIdentifier( i.CapitalizeString() ) ) .Aggregate( ( a, b ) => a + "." + b ) + "+Info" ) ) as StaticFileInfo; if( staticFileInfo == null ) throw new ResourceNotAvailableException( "Failed to create an Info object for the request.", null ); var mediaTypeOverride = EwfApp.Instance.GetMediaTypeOverrides().SingleOrDefault( i => i.FileExtension == extension ); var contentType = mediaTypeOverride != null ? mediaTypeOverride.MediaType : MimeTypeMap.GetMimeType( extension ); Func<string> cacheKeyGetter = () => staticFileInfo.GetUrl( false, false, false ); EwfSafeResponseWriter responseWriter; if( contentType == ContentTypes.Css ) { Func<string> cssGetter = () => File.ReadAllText( staticFileInfo.FilePath ); responseWriter = urlVersionString.Any() ? new EwfSafeResponseWriter( cssGetter, urlVersionString, () => new ResponseMemoryCachingSetup( cacheKeyGetter(), staticFileInfo.GetResourceLastModificationDateAndTime() ) ) : new EwfSafeResponseWriter( () => EwfResponse.Create( ContentTypes.Css, new EwfResponseBodyCreator( () => CssPreprocessor.TransformCssFile( cssGetter() ) ) ), staticFileInfo.GetResourceLastModificationDateAndTime(), memoryCacheKeyGetter: cacheKeyGetter ); } else { Func<EwfResponse> responseCreator = () => EwfResponse.Create( contentType, new EwfResponseBodyCreator( responseStream => { using( var fileStream = File.OpenRead( staticFileInfo.FilePath ) ) fileStream.CopyTo( responseStream ); } ) ); responseWriter = urlVersionString.Any() ? new EwfSafeResponseWriter( responseCreator, urlVersionString, memoryCachingSetupGetter: () => new ResponseMemoryCachingSetup( cacheKeyGetter(), staticFileInfo.GetResourceLastModificationDateAndTime() ) ) : new EwfSafeResponseWriter( responseCreator, staticFileInfo.GetResourceLastModificationDateAndTime(), memoryCacheKeyGetter: cacheKeyGetter ); } responseWriter.WriteResponse(); } bool IHttpHandler.IsReusable { get { return true; } } } }