-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathGXHttpModules.cs
More file actions
449 lines (413 loc) · 14.4 KB
/
Copy pathGXHttpModules.cs
File metadata and controls
449 lines (413 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Security;
using System.Web;
using System.Web.Configuration;
using System.Web.Hosting;
using System.Web.SessionState;
using GeneXus.Application;
using GeneXus.Configuration;
using GeneXus.Utils;
using ManagedFusion.Rewriter;
using ManagedFusion.Rewriter.Engines;
using ManagedFusion.Rewriter.Rules;
namespace GeneXus.Http.HttpModules
{
public class SingleMap
{
string name;
string implementation;
string methodName;
string verb;
string path;
string pathregexp;
Dictionary<string, string> variableAlias;
public string Name { get => name; set => name = value; }
public string ServiceMethod { get => methodName; set => methodName = value; }
public string Implementation { get => implementation; set => implementation = value; }
public string Verb { get => verb; set => verb = value; }
public string Path { get => path; set => path = value; }
public string PathRegexp { get => pathregexp; set => pathregexp = value; }
public Dictionary<string, string> VariableAlias { get => variableAlias; set => variableAlias = value; }
}
public class MapGroup
{
String _objectType;
String _name;
String _basePath;
List<SingleMap> _mappings;
public string ObjectType { get => _objectType; set => _objectType = value; }
public string Name { get => _name; set => _name = value; }
public string BasePath { get => _basePath; set => _basePath = value; }
public List<SingleMap> Mappings { get => _mappings; set => _mappings = value; }
}
public class GXAPIModule : IHttpModule
{
static private readonly object syncObject = new object();
public static List<String> servicesPathUrl;
public static Dictionary<String, String> servicesBase;
public static Dictionary<String, String> servicesClass;
public static Dictionary<String, Dictionary<string, SingleMap>> servicesMap;
//public static Dictionary<String, Dictionary<String, String>> servicesVerbs;
public static Dictionary<String, Dictionary<Tuple<string, string>, String>> servicesMapData = new Dictionary<String, Dictionary<Tuple<string, string>, string>>();
const string REST_BASE_URL = "rest/";
const string PRIVATE_DIR = "private";
private static bool moduleStarted;
void IHttpModule.Init(HttpApplication context)
{
if (!GXAPIModule.moduleStarted)
{
// Load API Map
ServicesGroupSetting(GxContext.StaticPhysicalPath());
GXAPIModule.moduleStarted = true;
}
context.PostMapRequestHandler += context_PostMapRequestHandler;
context.PostResolveRequestCache += onPostResolveRequestCache;
}
private void onPostResolveRequestCache(object sender, EventArgs eventArgs)
{
if (string.Equals(HttpContext.Current.Request.HttpMethod, HttpMethod.Options.Method, StringComparison.OrdinalIgnoreCase))
{
IHttpHandler apiHandler = MapHandler(sender, eventArgs);
if (apiHandler != null)
HttpContext.Current.RemapHandler(apiHandler);
}
else if (string.Equals(HttpContext.Current.Request.HttpMethod, HttpMethod.Get.Method, StringComparison.OrdinalIgnoreCase) &&
HttpContext.Current.Request.Path.EndsWith("/" + REST_BASE_URL, StringComparison.OrdinalIgnoreCase))
{
CSRFHelper.ValidateAntiforgery(HttpContext.Current);
}
}
void IHttpModule.Dispose()
{
}
private void context_PostMapRequestHandler(object sender, EventArgs eventArgs)
{
HttpApplication httpApp = sender as HttpApplication;
IHttpHandler apiHandler = MapHandler(sender, eventArgs);
if (apiHandler != null)
httpApp.Context.Handler = apiHandler;
}
private IHttpHandler MapHandler(object sender, EventArgs e)
{
HttpApplication httpApp = sender as HttpApplication;
HttpContext context = httpApp.Context;
if (GXAPIModule.serviceInPath(context.Request.FilePath, actualPath: out _))
{
return new GeneXus.HttpHandlerFactory.HandlerFactory().GetHandler(context, context.Request.RequestType, context.Request.Url.AbsoluteUri, context.Request.FilePath);
}
else
return null;
}
public static Boolean serviceInPath(String path, out String actualPath)
{
actualPath = "";
if (servicesPathUrl != null)
{
foreach (String subPath in servicesPathUrl)
{
if (path.ToLower().Contains($"/{subPath.ToLower()}"))
{
actualPath = subPath.ToLower();
return true;
}
}
}
return false;
}
public void ServicesGroupSetting(string webPath)
{
if (!String.IsNullOrEmpty(webPath) && servicesMap == null)
{
lock (syncObject)
{
if (servicesMap == null)
{
servicesPathUrl = new List<string>();
servicesBase = new Dictionary<string, string>();
servicesMap = new Dictionary<string, Dictionary<string, SingleMap>>();
//servicesVerbs = new Dictionary<string, Dictionary<string, string>>();
servicesMapData = new Dictionary<string, Dictionary<Tuple<string, string>, string>>();
servicesClass = new Dictionary<String, String>();
if (Directory.Exists(Path.Combine(webPath, PRIVATE_DIR)))
{
String[] grpFiles = Directory.GetFiles(Path.Combine(webPath, PRIVATE_DIR), "*.grp.json");
foreach (String grp in grpFiles)
{
string content = File.ReadAllText(grp);
if (!string.IsNullOrEmpty(content))
{
#pragma warning disable SCS0018 // Path traversal: injection possible in {1} argument passed to '{0}'
object p = JSONHelper.Deserialize<MapGroup>(content);
#pragma warning restore SCS0018
MapGroup m = p as MapGroup;
if (m != null && m.Name != null && m.Mappings != null)
{
if (String.IsNullOrEmpty(m.BasePath))
{
m.BasePath = REST_BASE_URL;
}
String mapPath = (m.BasePath.EndsWith("/")) ? m.BasePath : m.BasePath + "/";
String mapPathLower = mapPath.ToLower();
servicesPathUrl.Add(mapPathLower);
servicesBase[mapPathLower]= m.Name.ToLower();
servicesClass[mapPathLower]= m.Name.ToLower() + "_services";
foreach (SingleMap sm in m.Mappings)
{
if (String.IsNullOrEmpty(sm.Verb))
sm.Verb = "GET";
if (sm.VariableAlias == null)
sm.VariableAlias = new Dictionary<string, string>();
else
{
Dictionary<string, string> vMap = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> v in sm.VariableAlias)
{
vMap.Add(v.Key.ToLower(), v.Value.ToLower());
}
sm.VariableAlias = vMap;
}
if (servicesMap.ContainsKey(mapPathLower))
{
if (!servicesMap[mapPathLower].ContainsKey(sm.Name.ToLower()))
{
servicesMapData[mapPathLower].Add(Tuple.Create(sm.Path.ToLower(), sm.Verb), sm.Name.ToLower());
servicesMap[mapPathLower].Add(sm.Name.ToLower(), sm);
}
}
else
{
servicesMapData.Add(mapPathLower, new Dictionary<Tuple<string, string>, string>());
servicesMapData[mapPathLower].Add(Tuple.Create(sm.Path.ToLower(), sm.Verb), sm.Name.ToLower());
servicesMap.Add(mapPathLower, new Dictionary<string, SingleMap>());
servicesMap[mapPathLower].Add(sm.Name.ToLower(), sm);
}
}
}
}
}
}
}
}
}
}
}
public class GXSessionModule : IHttpModule
{
private static readonly IGXLogger log = GXLoggerFactory.GetLogger<GXSessionModule>();
HttpApplication App;
const string ASPNETSESSION_COOKIE = "ASP.NET_SessionId";
string cookieName= ASPNETSESSION_COOKIE;
public void Init(HttpApplication app)
{
App = app;
try
{
SessionStateSection sessionStateSection = (SessionStateSection)System.Configuration.ConfigurationManager.GetSection("system.web/sessionState");
if (sessionStateSection != null)
cookieName = sessionStateSection.CookieName;
IHttpModule module = app.Modules["Session"];
if (module.GetType() == typeof(SessionStateModule))
{
SessionStateModule stateModule = (SessionStateModule)module;
stateModule.Start += (Session_Start);
}
}catch(SecurityException ex)
{
GXLogging.Info(log, ".NET trust level is lower than full", ex.Message);
app.EndRequest += Session_Start;
}
}
private void Session_Start(object sender, EventArgs e)
{
if (App.Request.GetIsSecureFrontEnd() || App.Request.GetIsSecureConnection() == 1)
{
HttpCookie sessionCookie = RetrieveResponseCookie(App.Response, cookieName);
if (sessionCookie != null && !sessionCookie.Secure)
{
sessionCookie.Secure = true;
App.Response.SetCookie(sessionCookie);
}
}
}
private HttpCookie RetrieveResponseCookie(HttpResponse currentResponse, string cookieName)
{
foreach (string key in App.Response.Cookies.Keys)
{
if (key.Equals(cookieName, StringComparison.OrdinalIgnoreCase))
{
return App.Response.Cookies[key];
}
}
return null;
}
public void Dispose()
{
App = null;
}
}
public class GXStaticCacheModule : IHttpModule
{
#region IHttpModule Members
private static string[] cachingTypes = { ".jpg", ".jpeg", ".bmp", ".gif", ".js", ".css" ,".png"};
private static int cacheExpirationHours;
private static bool moduleStarted;
void IHttpModule.Init(HttpApplication context)
{
if (!GXStaticCacheModule.moduleStarted)
{
string cacheExpirationHoursS = string.Empty;
if (Config.GetValueOf("CACHE_CONTENT_EXPIRATION", out cacheExpirationHoursS))
{
Int32.TryParse(cacheExpirationHoursS, out GXStaticCacheModule.cacheExpirationHours);
}
GXStaticCacheModule.moduleStarted = true;
}
context.EndRequest += new EventHandler(context_EndRequest);
}
void context_EndRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
if (context.Request != null && context.Request.RequestType == "GET" && context.Response.StatusCode == 200)
{
string filePath = context.Request.FilePath;
if (filePath.IndexOf(".svc") < 0 && filePath.IndexOf(HttpHelper.ASPX) < 0 && isCacheableMimeType(filePath))
{
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetMaxAge(new TimeSpan(GXStaticCacheModule.cacheExpirationHours, 0, 0));
}
}
}
private bool isCacheableMimeType(string path)
{
string ext = System.IO.Path.GetExtension(path);
return Array.Exists(GXStaticCacheModule.cachingTypes, element => element.Equals(ext));
}
void IHttpModule.Dispose()
{
}
#endregion
}
public class GXVirtualPathCasingModule : IHttpModule
{
private static readonly IGXLogger log = GXLoggerFactory.GetLogger<GXVirtualPathCasingModule>();
public void Init(HttpApplication context)
{
context.BeginRequest += OnBeginRequest;
}
public void Dispose()
{
}
private static void OnBeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
string appPath = HttpRuntime.AppDomainAppVirtualPath;
if (string.IsNullOrEmpty(appPath) || appPath == "/")
return;
string rawUrl = context.Request.RawUrl;
if (rawUrl == null)
return;
if (!rawUrl.StartsWith(appPath, StringComparison.Ordinal) &&
rawUrl.StartsWith(appPath, StringComparison.OrdinalIgnoreCase))
{
string canonical = appPath + rawUrl.Substring(appPath.Length);
GXLogging.Debug(log, "Redirecting non-canonical app path '", rawUrl, "' to '", canonical, "'");
#pragma warning disable SCS0027 // Open redirect: target is built from AppDomainAppVirtualPath (server config) and the original request path, always same-origin relative URL
context.Response.RedirectPermanent(canonical, true);
#pragma warning restore SCS0027
}
}
}
public class GXRewriter : IHttpModule
{
private static readonly IGXLogger log = GXLoggerFactory.GetLogger<GXRewriter>();
private static RewriterModule rewriter;
private static bool moduleStarted;
private static bool enabled;
internal static string physicalApplicationPath;
public void Dispose()
{
}
public void Init(HttpApplication context)
{
if (!moduleStarted)
{
try
{
physicalApplicationPath = HostingEnvironment.ApplicationPhysicalPath;
}
finally
{
if (String.IsNullOrEmpty(physicalApplicationPath))
physicalApplicationPath = GxContext.StaticPhysicalPath();
}
if (File.Exists(Path.Combine(physicalApplicationPath, Preferences.DefaultRewriteFile)))
{
ChangeApacheDefaultEngine();
Manager.Configuration.Rewriter.AllowIis7TransferRequest = false; //Avoid Too Many Redirects with inverse urles.
enabled = true;
}
moduleStarted = true;
}
if (enabled)
{
rewriter = new RewriterModule();
rewriter.Init(context);
}
}
private void ChangeApacheDefaultEngine()
{
try
{
GxApacheEngine engine = new GxApacheEngine();
typeof(Manager).GetField("_rewriterEngine", BindingFlags.Static | BindingFlags.NonPublic).SetValue(null, engine);
engine.Init();
}catch(Exception ex)
{
GXLogging.Error(log, "Error changing ChangeApacheDefaultEngine", ex);
}
}
}
public class GxApacheEngine : ApacheEngine
{
public GxApacheEngine() : base()
{
}
public override void Init()
{
Paths.Clear();
DirectoryInfo refreshDir = new DirectoryInfo(GXRewriter.physicalApplicationPath);
FileInfo file = new FileInfo(Path.Combine(refreshDir.FullName, Preferences.DefaultRewriteFile));
Add(HttpContext.Current.Request.ApplicationPath, file);
RefreshRules();
}
}
public class GxInverseRuleAction: DefaultRuleAction
{
public override void Execute(RuleContext context)
{
base.Execute(context);
context.SubstitutedUrl = AddBase(context.RuleSet.VirtualBase, context.SubstitutedUrl);
}
public override bool IsMatch(RuleContext context)
{
return base.IsMatch(context);
}
private Uri AddBase(string baseFrom, Uri url)
{
if (!String.IsNullOrEmpty(baseFrom) && baseFrom != "/")
{
string urlPath = url.GetComponents(UriComponents.PathAndQuery, UriFormat.SafeUnescaped);
if (!urlPath.StartsWith(baseFrom))
urlPath = baseFrom + urlPath;
while (urlPath.Contains("//"))
urlPath = urlPath.Replace("//", "/");
return new Uri(url, urlPath);
}
return url;
}
}
}