public static int Main(string[] args) { if (!Directory.Exists(CACHE_DIR)) { Directory.CreateDirectory(CACHE_DIR); } var compression = CompressedContent.Default() .Level(CompressionLevel.Optimal); var cache = ServerCache.Persistent(CACHE_DIR) .Invalidate(false); var project = Project.Create() .Add(compression) .Add(cache); var cachePolicy = ClientCache.Policy() .Duration(7) .Predicate((_, r) => r.ContentType?.KnownType != ContentType.TextHtml); return(Host.Create() .Handler(project) #if DEBUG .Development() #endif .Add(cachePolicy) .Defaults() .Console() .Run()); }
public void TestCustomCompression() { using var runner = new TestRunner(); runner.Host.Compression(CompressedContent.Default().Add(new CustomAlgorithm())).Start(); var request = runner.GetRequest(); request.Headers.Add("Accept-Encoding", "custom"); using var response = request.GetSafeResponse(); Assert.Equal("custom", response.ContentEncoding); }
/// <summary> /// Configures the server host with default policies for compression and security. /// </summary> /// <param name="host">The host to be configured</param> /// <param name="compression">Whether responses sent by the server should automatically be compressed</param> /// <param name="secureUpgrade">Whether the server should automatically upgrade insecure requests</param> /// <param name="strictTransport">Whether the server should send a strict transport policy</param> public static IServerHost Defaults(this IServerHost host, bool compression = true, bool secureUpgrade = true, bool strictTransport = true) { if (compression) { host.Compression(CompressedContent.Default()); } if (secureUpgrade) { host.SecureUpgrade(SecureUpgrade.Force); } if (strictTransport) { host.StrictTransport(new StrictTransportPolicy(TimeSpan.FromDays(365), true, true)); } return(host); }