Skip to content

Commit f9a48b4

Browse files
author
sjuarez
committed
Fix Post verb functions
1 parent fced285 commit f9a48b4

File tree

3 files changed

+71
-55
lines changed

3 files changed

+71
-55
lines changed

dotnet/src/dotnetcore/GxClasses.Web/Middleware/GXRestServices.cs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Net;
66
using System.Net.Http;
77
using System.Text;
8+
using System.Threading.Tasks;
89
using GeneXus.Application;
910
using GeneXus.Configuration;
1011
using GeneXus.Data;
@@ -14,9 +15,8 @@
1415
using Microsoft.AspNetCore.Http;
1516
using Microsoft.AspNetCore.Mvc;
1617
using Microsoft.AspNetCore.Mvc.Filters;
17-
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
1818
using Microsoft.AspNetCore.Mvc.ModelBinding;
19-
using System.Threading.Tasks;
19+
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
2020

2121

2222
namespace GeneXus.Utils
@@ -82,7 +82,7 @@ protected GxRestService()
8282
[NonAction]
8383
internal void Initialize()
8484
{
85-
context.HttpContext = HttpContext;
85+
context.HttpContext = GetHttpContext();
8686
context.HttpContext.NewSessionCheck();
8787
ServiceHeaders();
8888
}
@@ -127,7 +127,7 @@ protected string RestStringParameter(string parameterName, string parameterValue
127127
{
128128
try
129129
{
130-
if (HttpContext.Request.Query.TryGetValue(parameterName, out var value))
130+
if (GetHttpContext().Request.Query.TryGetValue(parameterName, out var value))
131131
return value.FirstOrDefault();
132132
else
133133
return parameterValue;
@@ -141,7 +141,7 @@ protected bool RestParameter(string parameterName, string parameterValue)
141141
{
142142
try
143143
{
144-
if (HttpContext.Request.Query.TryGetValue(parameterName, out var value))
144+
if (GetHttpContext().Request.Query.TryGetValue(parameterName, out var value))
145145
return value.FirstOrDefault().Equals(parameterValue, StringComparison.OrdinalIgnoreCase);
146146
return false;
147147
}
@@ -157,9 +157,9 @@ protected ActionResult UploadImpl()
157157
string fileToken;
158158
using (Stream stream = Request.Body)
159159
{
160-
fileToken = gxobject.ReadFileFromStream(stream, Request.ContentType, Request.ContentLength.GetValueOrDefault(), Request.Headers[HttpHeader.XGXFILENAME], out fileGuid);
160+
fileToken = gxobject.ReadFileFromStream(stream, GetHttpContext().Request.ContentType, GetHttpContext().Request.ContentLength.GetValueOrDefault(), GetHttpContext().Request.Headers[HttpHeader.XGXFILENAME], out fileGuid);
161161
}
162-
Response.Headers.Append(HttpHeader.GX_OBJECT_ID, fileGuid);
162+
GetHttpContext().Response.Headers.Append(HttpHeader.GX_OBJECT_ID, fileGuid);
163163
SetStatusCode(HttpStatusCode.Created);
164164
return GetResponse(new {object_id = fileToken});
165165
}
@@ -251,12 +251,12 @@ protected ObjectResult HandleException(Exception ex)
251251

252252
if (ex is FormatException || ex is NullReferenceException)
253253
{
254-
WrappedJsonError jsonError = HttpHelper.HandleUnexpectedError(HttpContext, HttpStatusCode.BadRequest, ex);
254+
WrappedJsonError jsonError = HttpHelper.HandleUnexpectedError(GetHttpContext(), HttpStatusCode.BadRequest, ex);
255255
return BadRequest(jsonError);
256256
}
257257
else
258258
{
259-
WrappedJsonError jsonError = HttpHelper.HandleUnexpectedError(HttpContext, HttpStatusCode.InternalServerError, ex);
259+
WrappedJsonError jsonError = HttpHelper.HandleUnexpectedError(GetHttpContext(), HttpStatusCode.InternalServerError, ex);
260260
return StatusCode((int)HttpStatusCode.InternalServerError, jsonError);
261261
}
262262
}
@@ -266,11 +266,11 @@ protected void WebException(Exception ex)
266266

267267
if (ex is FormatException)
268268
{
269-
HttpHelper.SetUnexpectedError(HttpContext, HttpStatusCode.BadRequest, ex);
269+
HttpHelper.SetUnexpectedError(GetHttpContext(), HttpStatusCode.BadRequest, ex);
270270
}
271271
else
272272
{
273-
HttpHelper.SetUnexpectedError(HttpContext, HttpStatusCode.InternalServerError, ex);
273+
HttpHelper.SetUnexpectedError(GetHttpContext(), HttpStatusCode.InternalServerError, ex);
274274
}
275275
}
276276

@@ -376,21 +376,21 @@ private bool IsAuthenticated(GAMSecurityLevel objIntegratedSecurityLevel, bool o
376376
internal WrappedJsonError HandleGamError(string code, string message, HttpStatusCode defaultCode = HttpStatusCode.Unauthorized)
377377
{
378378
HttpStatusCode httpStatusCode = HttpHelper.GamCodeToHttpStatus(code, defaultCode);
379-
SetErrorHeaders(HttpContext, httpStatusCode, message);
379+
SetErrorHeaders(GetHttpContext(), httpStatusCode, message);
380380
return HttpHelper.GetJsonError(code, message);
381381
}
382382
internal WrappedJsonError HandleError(string code, string message)
383383
{
384384
HttpStatusCode httpStatusCode = HttpHelper.MapStatusCode(code);
385-
SetErrorHeaders(HttpContext, httpStatusCode, message);
385+
SetErrorHeaders(GetHttpContext(), httpStatusCode, message);
386386
return HttpHelper.GetJsonError(code, message);
387387
}
388388
private void SetErrorHeaders(HttpContext httpContext, HttpStatusCode httpStatusCode, string message)
389389
{
390-
if (httpContext != null)
390+
if (GetHttpContext() != null)
391391
{
392392
SetStatusCode(httpStatusCode);
393-
HttpHelper.HandleUnauthorized(httpStatusCode, httpContext);
393+
HttpHelper.HandleUnauthorized(httpStatusCode, GetHttpContext());
394394
httpContext.SetReasonPhrase(message);
395395
GXLogging.Error(log, String.Format("ErrCode {0}, ErrDsc {1}", httpStatusCode, message));
396396
}
@@ -409,33 +409,33 @@ protected void SetStatusCode(HttpStatusCode code)
409409
}
410410
IHeaderDictionary GetHeaders()
411411
{
412-
if (HttpContext != null)
412+
if (GetHttpContext() != null)
413413
{
414-
return HttpContext.Request.Headers;
414+
return GetHttpContext().Request.Headers;
415415
}
416416
else return null;
417417
}
418418
string GetHeader(string header)
419419
{
420-
if (HttpContext != null)
420+
if (GetHttpContext() != null)
421421
{
422-
return HttpContext.Request.Headers[header];
422+
return GetHttpContext().Request.Headers[header];
423423
}
424424
else return null;
425425
}
426426
bool IsPost()
427427
{
428-
if (HttpContext != null)
428+
if (GetHttpContext() != null)
429429
{
430-
return HttpMethod.Post.Method == HttpContext.Request.GetMethod();
430+
return HttpMethod.Post.Method == GetHttpContext().Request.GetMethod();
431431
}
432432
else return false;
433433
}
434434
void AddHeader(string header, string value)
435435
{
436-
if (HttpContext != null)
436+
if (GetHttpContext() != null)
437437
{
438-
HttpContext.Response.Headers[header]= value;
438+
GetHttpContext().Response.Headers[header]= value;
439439
}
440440
}
441441
protected bool ProcessHeaders(string queryId)
@@ -490,9 +490,9 @@ private void SendCacheHeaders()
490490
private void ServiceHeaders()
491491
{
492492
SendCacheHeaders();
493-
if (HttpContext != null)
493+
if (GetHttpContext() != null)
494494
{
495-
HttpHelper.CorsHeaders(HttpContext);
495+
HttpHelper.CorsHeaders(GetHttpContext());
496496
}
497497

498498
}
@@ -508,6 +508,11 @@ string dateTimeToHTMLDate(DateTime dt)
508508
{
509509
return dt.ToUniversalTime().ToString(DateTimeFormatInfo.InvariantInfo.RFC1123Pattern, DateTimeFormatInfo.InvariantInfo);
510510
}
511+
512+
protected virtual HttpContext GetHttpContext()
513+
{
514+
return base.HttpContext;
515+
}
511516
protected virtual bool IsSynchronizer { get { return false; } }
512517
protected virtual bool IntegratedSecurityEnabled { get { return false; } }
513518
protected virtual GAMSecurityLevel ApiIntegratedSecurityLevel(string gxMethod) { return IntegratedSecurityLevel; }

dotnet/src/extensions/Azure/Libraries/HttpHandler/GXAzureRestServices.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Net.Http;
23
using GeneXus;
34
using GeneXus.Application;
45
using GeneXus.Cache;
@@ -12,6 +13,20 @@ public class GXAzureRestService : GxRestService
1213
{
1314
static readonly IGXLogger log = GXLoggerFactory.GetLogger<GXAzureRestService>();
1415
private readonly ICacheService2 _cacheService;
16+
private HttpContext _httpContext;
17+
18+
protected override HttpContext GetHttpContext()
19+
{
20+
return _httpContext;
21+
}
22+
23+
protected void setInitialization(HttpContext httpContext)
24+
{
25+
_httpContext = httpContext;
26+
context = GxContext.CreateDefaultInstance();
27+
SetServiceSession(httpContext.Request, httpContext.Response, httpContext);
28+
29+
}
1530
public GXAzureRestService(ICacheService2 redis) : base()
1631
{
1732
if (GxContext.IsAzureContext)
@@ -29,30 +44,18 @@ public GXAzureRestService(ICacheService2 redis) : base()
2944
throw new Exception("Operation Cancelled. Not an Azure context.");
3045
}
3146
}
32-
public void SetServiceSession(HttpRequest request, HttpResponse response, HttpContext httpContext)
47+
private void SetServiceSession(HttpRequest request, HttpResponse response, HttpContext httpContext)
3348
{
3449
if (GxContext.IsAzureContext)
3550
{
36-
if ((context != null && context.HttpContext != null) && (Request != null && Response != null))
51+
if (_httpContext != null)
3752
{
38-
GXHttpAzureContext httpAzureContext = new GXHttpAzureContext(Request, Response, _cacheService);
39-
if (httpAzureContext != null && httpAzureContext.Session != null && context != null && context.HttpContext != null)
40-
context.HttpContext.Session = httpAzureContext.Session;
53+
GXHttpAzureContext httpAzureContext = new GXHttpAzureContext(_httpContext.Request, _httpContext.Response, _cacheService);
54+
if (httpAzureContext != null && httpAzureContext.Session != null)
55+
_httpContext.Session = httpAzureContext.Session;
4156
else
4257
GXLogging.Debug(log, $"Error : Azure Serverless session could not be created.");
4358
}
44-
else
45-
{
46-
if (context != null)
47-
{
48-
context.HttpContext = httpContext;
49-
GXHttpAzureContext httpAzureContext = new GXHttpAzureContext(request, response, _cacheService);
50-
if (httpAzureContext != null && httpAzureContext.Session != null && context != null && context.HttpContext != null)
51-
context.HttpContext.Session = httpAzureContext.Session;
52-
else
53-
GXLogging.Debug(log, $"Error : Azure Serverless session could not be created.");
54-
}
55-
}
5659
}
5760
else
5861
{

dotnet/src/extensions/Azure/Libraries/HttpHandler/GXHttpAzureContext.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
using System.Threading;
66
using System.Threading.Tasks;
77
using GeneXus.Cache;
8+
using GeneXus.Configuration;
89
using GeneXus.Utils;
910
using Microsoft.AspNetCore.Http;
11+
using Microsoft.Azure.Functions.Worker.Http;
1012
using StackExchange.Redis;
1113

1214
namespace GeneXus.Deploy.AzureFunctions.HttpHandler
@@ -17,16 +19,16 @@ public class GXHttpAzureContext
1719
private string sessionId;
1820
private ISession session;
1921
private static readonly IGXLogger log = GXLoggerFactory.GetLogger<GXHttpAzureContext>();
20-
internal const string AzureSessionId = "GX_AZURE_SESSIONID";
22+
internal const string AZURE_SESSIONID = "GX_AZURE_SESSIONID";
2123

2224
public ISession Session => session;
2325

2426
public GXHttpAzureContext( HttpRequest request, HttpResponse response, ICacheService2 redis)
2527
{
2628
bool isSecure = IsSecureConnection(request);
2729

28-
if (request != null && request.Cookies != null && request.Cookies[AzureSessionId] != null)
29-
sessionId = request.Cookies[AzureSessionId];
30+
if (request != null && request.Cookies != null && request.Cookies[AZURE_SESSIONID] != null)
31+
sessionId = request.Cookies[AZURE_SESSIONID];
3032

3133
if (redis != null && redis.GetType() == typeof(Redis))
3234
_redis = redis;
@@ -82,6 +84,12 @@ private void CreateSessionId(bool isSecure, HttpResponse response, HttpRequest r
8284

8385
CookieOptions cookieOptions = new CookieOptions();
8486

87+
SameSiteMode sameSiteMode = SameSiteMode.Unspecified;
88+
if (Config.GetValueOf("SAMESITE_COOKIE", out string sameSite) && Enum.TryParse(sameSite, out sameSiteMode))
89+
{
90+
cookieOptions.SameSite = sameSiteMode;
91+
92+
}
8593
if (!DateTime.MinValue.Equals(DateTimeUtil.NullDate()))
8694
cookieOptions.Expires = DateTime.MinValue;
8795
cookieOptions.Path = "";
@@ -90,7 +98,7 @@ private void CreateSessionId(bool isSecure, HttpResponse response, HttpRequest r
9098
cookieOptions.Secure = isSecure;
9199

92100
if (response.Cookies != null)
93-
response.Cookies.Append(AzureSessionId,sessionId,cookieOptions);
101+
response.Cookies.Append(AZURE_SESSIONID,sessionId,cookieOptions);
94102
GXLogging.Debug(log, $"Create new Azure Session Id :{sessionId}");
95103
}
96104
public class MockHttpSession : ISession
@@ -145,7 +153,7 @@ bool ISession.TryGetValue(string key, out byte[] value)
145153
public class RedisHttpSession : ISession
146154
{
147155
const int SESSION_TIMEOUT_IN_MINUTES = 5;
148-
const string AzureRedisCacheId = "REDIS_CACHE_SESSION_ID";
156+
const string AZURE_REDIS_CACHE_ID = "REDIS_CACHE_SESSION_ID";
149157
string _sessionId;
150158
private Redis _redis;
151159
public Dictionary<string, byte[]> data;
@@ -168,17 +176,17 @@ private IEnumerable<string> convert<String>(IEnumerable<RedisKey> enumerable)
168176
}
169177
public void Clear()
170178
{
171-
_redis.ClearCache(AzureRedisCacheId);
179+
_redis.ClearCache(AZURE_REDIS_CACHE_ID);
172180
}
173181

174182
public bool RefreshSession(string sessionId)
175183
{
176-
if (_redis.Get(AzureRedisCacheId, sessionId, out Dictionary<string, byte[]> value))
184+
if (_redis.Get(AZURE_REDIS_CACHE_ID, sessionId, out Dictionary<string, byte[]> value))
177185
{
178186
int refreshTimeout = (_redis.redisSessionTimeout == 0) ? SESSION_TIMEOUT_IN_MINUTES : _redis.redisSessionTimeout;
179187
if (value != null)
180188
{
181-
return (_redis.KeyExpire(AzureRedisCacheId, sessionId, TimeSpan.FromMinutes(refreshTimeout), CommandFlags.None));
189+
return (_redis.KeyExpire(AZURE_REDIS_CACHE_ID, sessionId, TimeSpan.FromMinutes(refreshTimeout), CommandFlags.None));
182190
}
183191
}
184192
return false;
@@ -201,19 +209,19 @@ public void Remove(string key)
201209

202210
public void Set(string key, byte[] value)
203211
{
204-
if (!_redis.Get(AzureRedisCacheId, Id, out Dictionary<string, byte[]> data))
212+
if (!_redis.Get(AZURE_REDIS_CACHE_ID, Id, out Dictionary<string, byte[]> data))
205213
data = new Dictionary<string, byte[]>();
206214
data[key] = value;
207215

208216
if (_redis.redisSessionTimeout != 0)
209-
_redis.Set(AzureRedisCacheId, Id, data, _redis.redisSessionTimeout);
217+
_redis.Set(AZURE_REDIS_CACHE_ID, Id, data, _redis.redisSessionTimeout);
210218
else
211-
_redis.Set(AzureRedisCacheId, Id, data, SESSION_TIMEOUT_IN_MINUTES);
219+
_redis.Set(AZURE_REDIS_CACHE_ID, Id, data, SESSION_TIMEOUT_IN_MINUTES);
212220
}
213221

214222
public bool TryGetValue(string key, out byte[] value)
215223
{
216-
if (_redis.Get(AzureRedisCacheId, Id, out Dictionary<string, byte[]> data))
224+
if (_redis.Get(AZURE_REDIS_CACHE_ID, Id, out Dictionary<string, byte[]> data))
217225
{
218226
if (data != null)
219227
{
@@ -229,7 +237,7 @@ public bool TryGetValue(string key, out byte[] value)
229237
}
230238
public bool SessionKeyExists(string sessionId)
231239
{
232-
return (_redis.KeyExists(AzureRedisCacheId, sessionId));
240+
return (_redis.KeyExists(AZURE_REDIS_CACHE_ID, sessionId));
233241
}
234242

235243
}

0 commit comments

Comments
 (0)