From db66f0b3e983dbeba17b7b14e50ede4239996729 Mon Sep 17 00:00:00 2001 From: cmurialdo Date: Mon, 7 Aug 2023 10:32:23 -0300 Subject: [PATCH 1/2] Setting Header no-store was throwing an exception when assigned with the expression HttpContext.Current.Response.CacheControl = parsedValue.ToString(); --- .../GxClasses/Core/GXApplication.cs | 63 +++++++++---------- .../DotNetUnitTest/Domain/GxHttpClientTest.cs | 19 ++++++ 2 files changed, 50 insertions(+), 32 deletions(-) diff --git a/dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs b/dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs index ab0536f02..633758f03 100644 --- a/dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs +++ b/dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs @@ -2425,11 +2425,10 @@ public byte SetHeader(string name, string value) _httpHeaders = new NameValueCollection(); } _httpHeaders[name] = value; - SetCustomHttpHeader(name, value); - return 0; + return SetCustomHttpHeader(name, value); } - private void SetCustomHttpHeader(string name, string value) + private byte SetCustomHttpHeader(string name, string value) { try { @@ -2437,34 +2436,27 @@ private void SetCustomHttpHeader(string name, string value) if (name.Equals(HeaderNames.CacheControl, StringComparison.OrdinalIgnoreCase)) { - if (System.Net.Http.Headers.CacheControlHeaderValue.TryParse(value, out System.Net.Http.Headers.CacheControlHeaderValue parsedValue)) - { - HttpContext.Current.Response.CacheControl = parsedValue.ToString(); - } - else + var Cache = _HttpContext.Response.Cache; + string[] values = value.Split(','); + foreach (string v in values) { - var Cache = _HttpContext.Response.Cache; - string[] values = value.Split(','); - foreach (string v in values) + switch (v.Trim().ToUpper()) { - switch (v.Trim().ToUpper()) - { - case "PUBLIC": - Cache.SetCacheability(HttpCacheability.Public); - break; - case "PRIVATE": - Cache.SetCacheability(HttpCacheability.Private); - break; - case "NO-CACHE": - Cache.SetCacheability(HttpCacheability.NoCache); - break; - case "NO-STORE": - Cache.AppendCacheExtension("no-store, must-revalidate"); - break; - default: - GXLogging.Warn(log, String.Format("Could not set Cache Control Http Header Value '{0}' to HttpResponse", value)); - break; - } + case "PUBLIC": + Cache.SetCacheability(HttpCacheability.Public); + break; + case "PRIVATE": + Cache.SetCacheability(HttpCacheability.Private); + break; + case "NO-CACHE": + Cache.SetCacheability(HttpCacheability.NoCache); + break; + case "NO-STORE": + Cache.AppendCacheExtension("no-store, must-revalidate"); + break; + default: + GXLogging.Warn(log, String.Format("Could not set Cache Control Http Header Value '{0}' to HttpResponse", value)); + break; } } } @@ -2472,15 +2464,20 @@ private void SetCustomHttpHeader(string name, string value) { _HttpContext.Response.ContentType = value; } + else if (name.Equals(HeaderNames.Location, StringComparison.OrdinalIgnoreCase)) + { + _HttpContext.Response.RedirectLocation = value; + } else { - if (!string.IsNullOrEmpty(_HttpContext.Response.Headers[name])) + try { _HttpContext.Response.Headers[name] = value; - } - else + + }catch (PlatformNotSupportedException ex) { _HttpContext.Response.AppendHeader(name, value); + GXLogging.Warn(log, ex, "SetHeader ", name, value); } } #else @@ -2510,10 +2507,12 @@ private void SetCustomHttpHeader(string name, string value) _HttpContext.Response.AddHeader(name, value); } #endif + return 0; } catch (Exception ex) { GXLogging.Error(log, ex, "Error adding header ", name, value); + return 1; } } diff --git a/dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs b/dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs index 29e460d76..d587816df 100644 --- a/dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs +++ b/dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs @@ -1,4 +1,9 @@ +using System.IO; using System.Net; +using System.Reflection; +using System.Web.SessionState; +using System.Web; +using GeneXus.Application; using GeneXus.Http.Client; using Xunit; @@ -40,5 +45,19 @@ public void HttpClientInvalidURLWithCustomPort() } } + + [Fact] + public void NoStoreHeader() + { + var httpRequest = new HttpRequest("", "http://localhost/", ""); + var httpResponce = new HttpResponse(new StringWriter()); + var httpContext = new HttpContext(httpRequest, httpResponce); + HttpContext.Current = httpContext; + + GxContext gxcontext = new GxContext(); + gxcontext.HttpContext = HttpContext.Current; + byte result = gxcontext.SetHeader("CACHE", "no-store"); + Assert.Equal(0, result); + } } } From c78146d702504d147124f68842de35504747e2ed Mon Sep 17 00:00:00 2001 From: cmurialdo Date: Mon, 7 Aug 2023 15:57:53 -0300 Subject: [PATCH 2/2] Fix build error. --- dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs b/dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs index d587816df..4dd50cdd7 100644 --- a/dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs +++ b/dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs @@ -1,11 +1,12 @@ using System.IO; using System.Net; -using System.Reflection; -using System.Web.SessionState; -using System.Web; using GeneXus.Application; using GeneXus.Http.Client; using Xunit; +#if !NETCORE +using System.Web.SessionState; +using System.Web; +#endif namespace xUnitTesting { @@ -44,8 +45,7 @@ public void HttpClientInvalidURLWithCustomPort() Assert.NotEqual(((int)HttpStatusCode.InternalServerError), httpclient.StatusCode); } } - - +#if !NETCORE [Fact] public void NoStoreHeader() { @@ -59,5 +59,6 @@ public void NoStoreHeader() byte result = gxcontext.SetHeader("CACHE", "no-store"); Assert.Equal(0, result); } +#endif } }