Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 62 additions & 5 deletions dotnet/src/dotnetframework/GxClasses/Middleware/HandlerFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
Expand All @@ -15,6 +16,36 @@

namespace GeneXus.HttpHandlerFactory
{

internal class ErrorRequestHandler : IHttpHandler
{
string message;
HttpStatusCode httpCode;

internal ErrorRequestHandler(string message, HttpStatusCode httpCode)
{
this.message = message;
this.httpCode = httpCode;
}

public void ProcessRequest(HttpContext context)
{
context.Response.StatusCode = (int)httpCode;
context.Response.StatusDescription = message;
if (context.Request.AcceptTypes.Contains("application/json"))
{
context.Response.ContentType = "application/json";
HttpHelper.SetError(context, "0", "Method not Allowed");
}
}

public bool IsReusable
{
get { return false; }
}
}


internal class OptionsApiObjectRequestHandler : IHttpHandler
{
string actualPath;
Expand Down Expand Up @@ -56,7 +87,8 @@ public bool IsReusable
get { return false; }
}
}
class HandlerFactory : IHttpHandlerFactory

class HandlerFactory : IHttpHandlerFactory
{
private static readonly IGXLogger log = GXLoggerFactory.GetLogger<HandlerFactory>();
private static List<string> GxNamespaces;
Expand Down Expand Up @@ -100,6 +132,7 @@ public IHttpHandler GetHandler(HttpContext context, string requestType, string u
if (GXAPIModule.serviceInPath(pathTranslated, actualPath: out actualPath))
{
string nspace;
bool methodMismatch = false;
Config.GetValueOf("AppMainNamespace", out nspace);
string objClass = GXAPIModule.servicesBase[actualPath];
//
Expand All @@ -109,7 +142,7 @@ public IHttpHandler GetHandler(HttpContext context, string requestType, string u
Dictionary<string, object> routeParms;
if (GXAPIModule.servicesMapData.ContainsKey(actualPath))
{
bool IsServiceCall = GetSMap(actualPath, objectName, objectNameUp, requestType, out string mapName, out string mapRegExp, out routeParms);
bool IsServiceCall = GetSMap(actualPath, objectName, objectNameUp, requestType, out string mapName, out string mapRegExp, out routeParms, out methodMismatch);
if (IsServiceCall)
{
if (!string.IsNullOrEmpty(mapName) && GXAPIModule.servicesMap[actualPath].TryGetValue(mapName, out SingleMap value))
Expand Down Expand Up @@ -137,6 +170,14 @@ public IHttpHandler GetHandler(HttpContext context, string requestType, string u
{
return new OptionsApiObjectRequestHandler(actualPath, objectName, mapRegExp);
}
else
{
if (methodMismatch)
{
return new ErrorRequestHandler("Method not allowed", HttpStatusCode.MethodNotAllowed);
}

}
}
}
return null;
Expand Down Expand Up @@ -204,21 +245,28 @@ public string GetObjFromPath(string cname, string apath)
return objectName;
}

public bool GetSMap(string actualPath, string objectName, string objectNameUp, string requestType, out string mapName, out string mapRegexp, out Dictionary<string, object> routeParms)
public bool GetSMap(string actualPath, string objectName, string objectNameUp, string requestType, out string mapName, out string mapRegexp, out Dictionary<string, object> routeParms, out bool methodMismatch)
{
routeParms = null;
methodMismatch = false;
if (GXAPIModule.servicesMapData[actualPath].TryGetValue(Tuple.Create(objectName, requestType), out mapName))
{
// Url exact match
mapRegexp = mapName;
methodMismatch = false;
return true;
}
else
{
bool pathFound = false;
mapRegexp = mapName;
foreach (SingleMap m in GXAPIModule.servicesMap[actualPath].Values)
{
{
if (!m.Path.Equals(m.PathRegexp) && GxRegex.IsMatch(objectName, m.PathRegexp))
{
pathFound = true;
methodMismatch = false;
// regexp URL match
mapName = m.Name;
mapRegexp = m.PathRegexp;
if (m.Verb.Equals(requestType))
Expand All @@ -232,10 +280,19 @@ public bool GetSMap(string actualPath, string objectName, string objectNameUp,
routeParms.Add(var, smatch);
i++;
}
methodMismatch = false;
return true;
}
}
}
}
if (pathFound && !requestType.Equals( HttpMethod.Options.Method))
{
mapName = null;
mapRegexp = null;
routeParms = null;
methodMismatch = true;
return false;
}
}
return false;
}
Expand Down
Loading