in CachingProxy/src/CachingProxy.cs [49:99]
public CachingProxy(RequestDelegate next, IWebHostEnvironment hostingEnv, CachingProxyMetrics metrics,
ILoggerFactory loggerFactory, IOptions<CachingProxyConfig> config, ProxyHttpClient httpClient, ResponseCache responseCache)
{
myLogger = loggerFactory.CreateLogger<CachingProxy>();
myLogger.LogInformation("Initialising. Config:\n{CachingProxyConfig}", config.Value);
myNext = next;
myMetrics = metrics;
myHttpClient = httpClient;
myResponseCache = responseCache;
myMinimumFreeDiskSpaceMb = config.Value.MinimumFreeDiskSpaceMb;
myLocalCachePath = config.Value.LocalCachePath;
if (myLocalCachePath == null)
throw new ArgumentNullException(nameof(myLocalCachePath), "LocalCachePath could not be null");
if (!Directory.Exists(myLocalCachePath))
throw new ArgumentException("LocalCachePath doesn't exist: " + myLocalCachePath);
myRemoteServers = new RemoteServers(config.Value.Prefixes.ToList(), config.Value.ContentTypeValidationPrefixes.ToList());
myContentTypeProvider = new FileExtensionContentTypeProvider();
myCacheFileProvider = new CacheFileProvider(myLocalCachePath);
var staticFileOptions = new StaticFileOptions
{
FileProvider = myCacheFileProvider,
ServeUnknownFileTypes = true,
HttpsCompression = HttpsCompressionMode.DoNotCompress,
ContentTypeProvider = myContentTypeProvider,
OnPrepareResponse = ctx =>
{
var contentEncoding = myCacheFileProvider.GetContentEncoding(ctx.File);
if (contentEncoding != null)
ctx.Context.Response.Headers.ContentEncoding = contentEncoding;
SetStatusHeader(ctx.Context, CachingProxyStatus.HIT);
AddEternalCachingControl(ctx.Context);
}
};
myStaticFileMiddleware =
new StaticFileMiddleware(next, hostingEnv, Options.Create(staticFileOptions), loggerFactory);
myBlacklistRegex = !string.IsNullOrWhiteSpace(config.Value.BlacklistUrlRegex)
? new Regex(config.Value.BlacklistUrlRegex, RegexOptions.Compiled)
: null;
myRedirectToRemoteUrlsRegex = !string.IsNullOrWhiteSpace(config.Value.RedirectToRemoteUrlsRegex)
? new Regex(config.Value.RedirectToRemoteUrlsRegex, RegexOptions.Compiled)
: null;
}