Ejemplo n.º 1
0
        private async Task <bool> UpdateSyncedSettingsAsync()
        {
            try
            {
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri("<<URL TO Your SyncedSettings.xml>>"));

                var ca = new System.Net.Http.Headers.CacheControlHeaderValue();
                ca.NoCache        = true;
                ca.MustRevalidate = true;

                request.Headers.CacheControl = ca;

                HttpClient client = new HttpClient();

                var data = await client.SendAsync(request);

                string settingsXml = await data.Content.ReadAsStringAsync();

                ////yet-to-be-tested what if replacing will change the creation date or not
                //StorageFile syncedSettings = await ApplicationData.Current.LocalFolder.GetFileAsync("SyncedSettings.xml");
                //await syncedSettings.DeleteAsync();
                //syncedSettings = null;

                var syncedSettings = await ApplicationData.Current.LocalFolder.CreateFileAsync("SyncedSettings.xml", CreationCollisionOption.ReplaceExisting);

                await FileIO.WriteTextAsync(syncedSettings, settingsXml);

                return(true);
            }
            catch (Exception x)
            {
                Debug.WriteLine(x.Message);
                return(false);
            }
        }
Ejemplo n.º 2
0
 public CacheControlHeaderValue(System.Net.Http.Headers.CacheControlHeaderValue containedObject)
 {
     if ((containedObject == null))
     {
         throw new System.ArgumentNullException("containedObject");
     }
     this.containedObject = containedObject;
 }
Ejemplo n.º 3
0
        private static TimeSpan CacheAgeFromHeader(System.Net.Http.Headers.CacheControlHeaderValue headerValue)
        {
            if (headerValue == null)
            {
                return(TimeSpan.Zero);
            }

            return((TimeSpan)(headerValue.MaxAge ?? headerValue.SharedMaxAge ?? TimeSpan.Zero));
        }
Ejemplo n.º 4
0
        public void StaticFiles(IApplicationBuilder app)
        {
            // Serve my app-specific default file, if present.
            DefaultFilesOptions options = new DefaultFilesOptions();

            options.DefaultFileNames.Clear();
            options.DefaultFileNames.Add("mydefault.html");
            app.UseDefaultFiles(options);
            app.UseStaticFiles(new StaticFileOptions
            {
                //Enabling ServeUnknownFileTypes is a security risk. It's disabled by default, and its use is discouraged.
                //FileExtensionContentTypeProvider provides a safer alternative to serving files with non-standard extensions.
                ServeUnknownFileTypes = true,
                DefaultContentType    = "images/png"
            });// For the wwwroot folder
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(
                    Path.Combine(Directory.GetCurrentDirectory(), "StaticFiles")),
                RequestPath       = "/MyFiles",
                OnPrepareResponse = ctx =>
                {
                    //The files have been made publicly cacheable for 10 minutes (600 seconds):
                    ctx.Context.Response.Headers.Append("Cache-Control", "public,max-age=600");
                    System.Net.Http.Headers.CacheControlHeaderValue cc = new System.Net.Http.Headers.CacheControlHeaderValue
                    {
                        MaxAge = TimeSpan.FromSeconds(600),
                        Public = true
                    };
                }
            });
            var provider = new FileExtensionContentTypeProvider();

            // Add new mappings
            provider.Mappings[".myapp"] = "application/x-msdownload";
            provider.Mappings[".htm3"]  = "text/html";
            provider.Mappings[".image"] = "image/svg+xml";
            // Replace an existing mapping
            provider.Mappings[".rtf"] = "application/x-msdownload";
            // Remove MP4 videos.
            provider.Mappings.Remove(".mp4");
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(
                    Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images")),
                RequestPath         = "/MyImages",
                ContentTypeProvider = provider
            });
            //Disabling directory browsing in production is highly recommended
            //app.UseDirectoryBrowser(new DirectoryBrowserOptions
            //{
            //    FileProvider = new PhysicalFileProvider(
            //            Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images")),
            //    RequestPath = "/MyImages",

            //});
        }
Ejemplo n.º 5
0
 public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
 {
     base.OnActionExecuted(actionExecutedContext);
     if (actionExecutedContext.Response.IsSuccessStatusCode)
     {
         var cc = new System.Net.Http.Headers.CacheControlHeaderValue();
         cc.NoStore = true;
         cc.NoCache = true;
         cc.Private = true;
         cc.MaxAge  = TimeSpan.Zero;
         actionExecutedContext.Response.Headers.CacheControl = cc;
     }
 }
 public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
 {
     base.OnActionExecuted(actionExecutedContext);
     if (actionExecutedContext.Response.IsSuccessStatusCode)
     {
         var cc = new System.Net.Http.Headers.CacheControlHeaderValue();
         cc.NoStore = true;
         cc.NoCache = true;
         cc.Private = true;
         cc.MaxAge = TimeSpan.Zero;
         actionExecutedContext.Response.Headers.CacheControl = cc;
     }
 }
Ejemplo n.º 7
0
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            if (actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps)
            {
                var request = actionContext.Request;
                HttpResponseMessage response;

                if (request.Method.Equals(HttpMethod.Get))
                {
                    response = request.CreateResponse(System.Net.HttpStatusCode.Found);

                    UriBuilder uri = new UriBuilder(request.RequestUri);
                    uri.Scheme = Uri.UriSchemeHttps;
                    uri.Port   = 443;

                    response.Headers.Location = uri.Uri;

                    var cacheControlHeader = new System.Net.Http.Headers.CacheControlHeaderValue();
                    cacheControlHeader.Private    = true;
                    response.Headers.CacheControl = cacheControlHeader;

                    string body = "<html><head><title>Object moved</title></head><body>\r<h2>Object moved to <a href=\""
                                  + uri.Uri.AbsoluteUri
                                  + "\">here</a>.</h2>\r</body></html>";

                    response.Content = new StringContent(body, System.Text.Encoding.UTF8, "text/html");
                }
                else
                {
                    response = request.CreateResponse(System.Net.HttpStatusCode.NotFound);

                    var cacheControlHeader = new System.Net.Http.Headers.CacheControlHeaderValue();
                    cacheControlHeader.Private    = true;
                    response.Headers.CacheControl = cacheControlHeader;

                    response.Headers.Pragma.Clear();

                    string body = "<!DOCTYPE html><html><head><title>404</title></head><body>Not Found</body></html>";

                    response.Content = new StringContent(body, System.Text.Encoding.UTF8, "text/html");
                }


                actionContext.Response = response;
            }
            else
            {
                base.OnAuthorization(actionContext);
            }
        }
Ejemplo n.º 8
0
        private async Task StoreFragmentInCache(
            HttpContext context, Uri pageUri, EsiExecutionContext executionContext, IEsiFragment fragment)
        {
            CacheControlHeaderValue.TryParse(
                context.Response.Headers[HeaderNames.CacheControl], out var cacheControl);

            if (ShouldSetCache(context))
            {
                var headers      = context.Response.Headers.ToDictionary();
                var pageResponse = new FragmentPageResponse(fragment, headers);

                var vary          = context.Response.Headers[HeaderNames.Vary];
                var cacheResponse = CacheResponse.Create(pageResponse, cacheControl, vary);

                await _cache.Set(pageUri, executionContext, cacheResponse);
            }
        }
Ejemplo n.º 9
0
        public override void OnActionExecuted(ActionExecutedContext actionExecutedContext)
        {
            base.OnActionExecuted(actionExecutedContext);
            if (actionExecutedContext != null &&
                actionExecutedContext.HttpContext.Response != null &&
                actionExecutedContext.HttpContext.Response.StatusCode ==)
            {
                var cc = new System.Net.Http.Headers.CacheControlHeaderValue();
                cc.NoStore = true;
                cc.NoCache = true;
                cc.Private = true;
                cc.MaxAge  = TimeSpan.Zero;
                actionExecutedContext.HttpContext.Response.Headers. = cc;

                actionExecutedContext.HttpContext.Response.Headers.Add("Pragma", "no-cache");
            }
        }
Ejemplo n.º 10
0
 public override void OnActionExecuted(ActionExecutedContext actionExecutedContext)
 {
     base.OnActionExecuted(actionExecutedContext);
     if (actionExecutedContext != null &&
         actionExecutedContext.HttpContext.Request != null &&
         // TODO: 移植 EnsureSuccessStatusCode 方法过来
         actionExecutedContext.HttpContext.Response.StatusCode == 200
         )
     {
         var cc = new System.Net.Http.Headers.CacheControlHeaderValue();
         cc.NoStore = true;
         cc.NoCache = true;
         cc.Private = true;
         cc.MaxAge  = TimeSpan.Zero;
         actionExecutedContext.HttpContext.Response.Headers.Add("Cache-Control", cc.ToString());
         actionExecutedContext.HttpContext.Response.Headers.Add("Pragma", "no-cache");
     }
 }
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            base.OnActionExecuted(actionExecutedContext);
            if (actionExecutedContext != null &&
                actionExecutedContext.Response != null &&
                actionExecutedContext.Response.IsSuccessStatusCode)
            {
                var cc = new System.Net.Http.Headers.CacheControlHeaderValue
                {
                    NoStore = true,
                    NoCache = true,
                    Private = true,
                    MaxAge = TimeSpan.Zero
                };
                actionExecutedContext.Response.Headers.CacheControl = cc;

                actionExecutedContext.Response.Headers.Add("Pragma", "no-cache");
            }
        }
Ejemplo n.º 12
0
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            base.OnActionExecuted(actionExecutedContext);
            if (actionExecutedContext != null &&
                actionExecutedContext.Response != null &&
                actionExecutedContext.Response.IsSuccessStatusCode)
            {
                var cc = new System.Net.Http.Headers.CacheControlHeaderValue
                {
                    NoStore = true,
                    NoCache = true,
                    Private = true,
                    MaxAge  = TimeSpan.Zero
                };
                actionExecutedContext.Response.Headers.CacheControl = cc;

                actionExecutedContext.Response.Headers.Add("Pragma", "no-cache");
            }
        }
Ejemplo n.º 13
0
 private void SetCache(HttpCachePolicyBase cache, System.Net.Http.Headers.CacheControlHeaderValue cacheIn)
 {
     if (cacheIn.Public)
     {
         cache.SetCacheability(HttpCacheability.Public);
     }
     if (cacheIn.Private)
     {
         cache.SetCacheability(HttpCacheability.Private);
     }
     if (cacheIn.MaxAge != null)
     {
         var maxAge = cacheIn.MaxAge.Value;
         if (maxAge.TotalDays < 1)
         {
             maxAge = TimeSpan.FromDays(30);
         }
         cache.SetCacheability(HttpCacheability.Public);
         cache.SetMaxAge(maxAge);
         cache.SetSlidingExpiration(true);
     }
 }
Ejemplo n.º 14
0
 public static bool TryParse(string input, out System.Net.Http.Headers.CacheControlHeaderValue parsedValue)
 {
     throw null;
 }
Ejemplo n.º 15
0
        public async Task UpdateRss()
        {
            if (progrssRing.IsActive == false)
            {
                List.Items.Clear();

                progrssRing.IsActive = true;
                RefreshRotate.Resume();

                HttpClient downloader = new HttpClient();
                downloader.DefaultRequestHeaders.IfModifiedSince = new DateTimeOffset(2000, 2, 2, 2, 1, 1, 3, TimeSpan.FromHours(5));
                var cacheControl = new System.Net.Http.Headers.CacheControlHeaderValue();
                cacheControl.NoCache = true;
                downloader.DefaultRequestHeaders.CacheControl = cacheControl;

                var rssContent = await downloader.GetStringAsync("http://www.repeatserver.com/Users/Kappspot/paradigm.xml");
                var rssData = from rss in XElement.Parse(rssContent).Descendants("item")
                              select new FeedItem
                                  {
                                      headLine = rss.Element("title").Value,
                                      description = rss.Element("description").Value,
                                      lastUpdate = rss.Element("pubDate").Value,
                                  };

                foreach (var i in rssData)
                {
                    List.Items.Add(AddFeed(i));
                }

                RefreshRotate.Pause();
                progrssRing.IsActive = false;
            }
        }