Skip to content

Commit 649f58e

Browse files
Setting Header no-store was throwing an exception (#859)
* Setting Header no-store was throwing an exception when assigned with the expression HttpContext.Current.Response.CacheControl = parsedValue.ToString(); * Fix build error.
1 parent c116699 commit 649f58e

File tree

2 files changed

+51
-32
lines changed

2 files changed

+51
-32
lines changed

dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,62 +2425,59 @@ public byte SetHeader(string name, string value)
24252425
_httpHeaders = new NameValueCollection();
24262426
}
24272427
_httpHeaders[name] = value;
2428-
SetCustomHttpHeader(name, value);
2429-
return 0;
2428+
return SetCustomHttpHeader(name, value);
24302429
}
24312430

2432-
private void SetCustomHttpHeader(string name, string value)
2431+
private byte SetCustomHttpHeader(string name, string value)
24332432
{
24342433
try
24352434
{
24362435
#if !NETCORE
24372436

24382437
if (name.Equals(HeaderNames.CacheControl, StringComparison.OrdinalIgnoreCase))
24392438
{
2440-
if (System.Net.Http.Headers.CacheControlHeaderValue.TryParse(value, out System.Net.Http.Headers.CacheControlHeaderValue parsedValue))
2441-
{
2442-
HttpContext.Current.Response.CacheControl = parsedValue.ToString();
2443-
}
2444-
else
2439+
var Cache = _HttpContext.Response.Cache;
2440+
string[] values = value.Split(',');
2441+
foreach (string v in values)
24452442
{
2446-
var Cache = _HttpContext.Response.Cache;
2447-
string[] values = value.Split(',');
2448-
foreach (string v in values)
2443+
switch (v.Trim().ToUpper())
24492444
{
2450-
switch (v.Trim().ToUpper())
2451-
{
2452-
case "PUBLIC":
2453-
Cache.SetCacheability(HttpCacheability.Public);
2454-
break;
2455-
case "PRIVATE":
2456-
Cache.SetCacheability(HttpCacheability.Private);
2457-
break;
2458-
case "NO-CACHE":
2459-
Cache.SetCacheability(HttpCacheability.NoCache);
2460-
break;
2461-
case "NO-STORE":
2462-
Cache.AppendCacheExtension("no-store, must-revalidate");
2463-
break;
2464-
default:
2465-
GXLogging.Warn(log, String.Format("Could not set Cache Control Http Header Value '{0}' to HttpResponse", value));
2466-
break;
2467-
}
2445+
case "PUBLIC":
2446+
Cache.SetCacheability(HttpCacheability.Public);
2447+
break;
2448+
case "PRIVATE":
2449+
Cache.SetCacheability(HttpCacheability.Private);
2450+
break;
2451+
case "NO-CACHE":
2452+
Cache.SetCacheability(HttpCacheability.NoCache);
2453+
break;
2454+
case "NO-STORE":
2455+
Cache.AppendCacheExtension("no-store, must-revalidate");
2456+
break;
2457+
default:
2458+
GXLogging.Warn(log, String.Format("Could not set Cache Control Http Header Value '{0}' to HttpResponse", value));
2459+
break;
24682460
}
24692461
}
24702462
}
24712463
else if (name.Equals(HeaderNames.ContentType, StringComparison.OrdinalIgnoreCase))
24722464
{
24732465
_HttpContext.Response.ContentType = value;
24742466
}
2467+
else if (name.Equals(HeaderNames.Location, StringComparison.OrdinalIgnoreCase))
2468+
{
2469+
_HttpContext.Response.RedirectLocation = value;
2470+
}
24752471
else
24762472
{
2477-
if (!string.IsNullOrEmpty(_HttpContext.Response.Headers[name]))
2473+
try
24782474
{
24792475
_HttpContext.Response.Headers[name] = value;
2480-
}
2481-
else
2476+
2477+
}catch (PlatformNotSupportedException ex)
24822478
{
24832479
_HttpContext.Response.AppendHeader(name, value);
2480+
GXLogging.Warn(log, ex, "SetHeader ", name, value);
24842481
}
24852482
}
24862483
#else
@@ -2510,10 +2507,12 @@ private void SetCustomHttpHeader(string name, string value)
25102507
_HttpContext.Response.AddHeader(name, value);
25112508
}
25122509
#endif
2510+
return 0;
25132511
}
25142512
catch (Exception ex)
25152513
{
25162514
GXLogging.Error(log, ex, "Error adding header ", name, value);
2515+
return 1;
25172516
}
25182517
}
25192518

dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
using System.IO;
12
using System.Net;
3+
using GeneXus.Application;
24
using GeneXus.Http.Client;
35
using Xunit;
6+
#if !NETCORE
7+
using System.Web.SessionState;
8+
using System.Web;
9+
#endif
410

511
namespace xUnitTesting
612
{
@@ -39,6 +45,20 @@ public void HttpClientInvalidURLWithCustomPort()
3945
Assert.NotEqual(((int)HttpStatusCode.InternalServerError), httpclient.StatusCode);
4046
}
4147
}
48+
#if !NETCORE
49+
[Fact]
50+
public void NoStoreHeader()
51+
{
52+
var httpRequest = new HttpRequest("", "http://localhost/", "");
53+
var httpResponce = new HttpResponse(new StringWriter());
54+
var httpContext = new HttpContext(httpRequest, httpResponce);
55+
HttpContext.Current = httpContext;
4256

57+
GxContext gxcontext = new GxContext();
58+
gxcontext.HttpContext = HttpContext.Current;
59+
byte result = gxcontext.SetHeader("CACHE", "no-store");
60+
Assert.Equal(0, result);
61+
}
62+
#endif
4363
}
4464
}

0 commit comments

Comments
 (0)