Skip to content

Commit

Permalink
Setting Header no-store was throwing an exception when assigned with …
Browse files Browse the repository at this point in the history
…the expression HttpContext.Current.Response.CacheControl = parsedValue.ToString();
  • Loading branch information
claudiamurialdo committed Aug 7, 2023
1 parent c116699 commit db66f0b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 32 deletions.
63 changes: 31 additions & 32 deletions dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2425,62 +2425,59 @@ 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
{
#if !NETCORE

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;
}
}
}
else if (name.Equals(HeaderNames.ContentType, StringComparison.OrdinalIgnoreCase))
{
_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
Expand Down Expand Up @@ -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;
}
}

Expand Down
19 changes: 19 additions & 0 deletions dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -40,5 +45,19 @@ public void HttpClientInvalidURLWithCustomPort()
}
}


[Fact]
public void NoStoreHeader()
{
var httpRequest = new HttpRequest("", "http://localhost/", "");

Check failure on line 52 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / test-external-storages

The type or namespace name 'HttpRequest' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 52 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / test-external-storages

The type or namespace name 'HttpRequest' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 52 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'HttpRequest' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 52 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'HttpRequest' could not be found (are you missing a using directive or an assembly reference?)
var httpResponce = new HttpResponse(new StringWriter());

Check failure on line 53 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / test-external-storages

The type or namespace name 'HttpResponse' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 53 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / test-external-storages

The type or namespace name 'HttpResponse' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 53 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'HttpResponse' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 53 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'HttpResponse' could not be found (are you missing a using directive or an assembly reference?)
var httpContext = new HttpContext(httpRequest, httpResponce);

Check failure on line 54 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / test-external-storages

The type or namespace name 'HttpContext' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 54 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / test-external-storages

The type or namespace name 'HttpContext' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 54 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'HttpContext' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 54 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'HttpContext' could not be found (are you missing a using directive or an assembly reference?)
HttpContext.Current = httpContext;

Check failure on line 55 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / test-external-storages

The name 'HttpContext' does not exist in the current context

Check failure on line 55 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / test-external-storages

The name 'HttpContext' does not exist in the current context

Check failure on line 55 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / build

The name 'HttpContext' does not exist in the current context

Check failure on line 55 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / build

The name 'HttpContext' does not exist in the current context

GxContext gxcontext = new GxContext();
gxcontext.HttpContext = HttpContext.Current;

Check failure on line 58 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / test-external-storages

The name 'HttpContext' does not exist in the current context

Check failure on line 58 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / test-external-storages

The name 'HttpContext' does not exist in the current context

Check failure on line 58 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / build

The name 'HttpContext' does not exist in the current context

Check failure on line 58 in dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

View workflow job for this annotation

GitHub Actions / build

The name 'HttpContext' does not exist in the current context
byte result = gxcontext.SetHeader("CACHE", "no-store");
Assert.Equal(0, result);
}
}
}

0 comments on commit db66f0b

Please sign in to comment.