Skip to content

Commit 7065828

Browse files
AlejandroPAlejandro Panizza Carve
andauthored
- Method not allowed Fixed (#1127)
* - Method not allowed in API objects response was not working in NET 4.X . Fixed. * - fix errors when rest methods override with path parameters. * - Fixed OPTIONS methods --------- Co-authored-by: Alejandro Panizza Carve <a.panizza@globant.com>
1 parent bc6104f commit 7065828

File tree

1 file changed

+62
-5
lines changed

1 file changed

+62
-5
lines changed

dotnet/src/dotnetframework/GxClasses/Middleware/HandlerFactory.cs

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Net;
56
using System.Net.Http;
67
using System.Reflection;
@@ -15,6 +16,36 @@
1516

1617
namespace GeneXus.HttpHandlerFactory
1718
{
19+
20+
internal class ErrorRequestHandler : IHttpHandler
21+
{
22+
string message;
23+
HttpStatusCode httpCode;
24+
25+
internal ErrorRequestHandler(string message, HttpStatusCode httpCode)
26+
{
27+
this.message = message;
28+
this.httpCode = httpCode;
29+
}
30+
31+
public void ProcessRequest(HttpContext context)
32+
{
33+
context.Response.StatusCode = (int)httpCode;
34+
context.Response.StatusDescription = message;
35+
if (context.Request.AcceptTypes.Contains("application/json"))
36+
{
37+
context.Response.ContentType = "application/json";
38+
HttpHelper.SetError(context, "0", "Method not Allowed");
39+
}
40+
}
41+
42+
public bool IsReusable
43+
{
44+
get { return false; }
45+
}
46+
}
47+
48+
1849
internal class OptionsApiObjectRequestHandler : IHttpHandler
1950
{
2051
string actualPath;
@@ -56,7 +87,8 @@ public bool IsReusable
5687
get { return false; }
5788
}
5889
}
59-
class HandlerFactory : IHttpHandlerFactory
90+
91+
class HandlerFactory : IHttpHandlerFactory
6092
{
6193
private static readonly IGXLogger log = GXLoggerFactory.GetLogger<HandlerFactory>();
6294
private static List<string> GxNamespaces;
@@ -100,6 +132,7 @@ public IHttpHandler GetHandler(HttpContext context, string requestType, string u
100132
if (GXAPIModule.serviceInPath(pathTranslated, actualPath: out actualPath))
101133
{
102134
string nspace;
135+
bool methodMismatch = false;
103136
Config.GetValueOf("AppMainNamespace", out nspace);
104137
string objClass = GXAPIModule.servicesBase[actualPath];
105138
//
@@ -109,7 +142,7 @@ public IHttpHandler GetHandler(HttpContext context, string requestType, string u
109142
Dictionary<string, object> routeParms;
110143
if (GXAPIModule.servicesMapData.ContainsKey(actualPath))
111144
{
112-
bool IsServiceCall = GetSMap(actualPath, objectName, objectNameUp, requestType, out string mapName, out string mapRegExp, out routeParms);
145+
bool IsServiceCall = GetSMap(actualPath, objectName, objectNameUp, requestType, out string mapName, out string mapRegExp, out routeParms, out methodMismatch);
113146
if (IsServiceCall)
114147
{
115148
if (!string.IsNullOrEmpty(mapName) && GXAPIModule.servicesMap[actualPath].TryGetValue(mapName, out SingleMap value))
@@ -137,6 +170,14 @@ public IHttpHandler GetHandler(HttpContext context, string requestType, string u
137170
{
138171
return new OptionsApiObjectRequestHandler(actualPath, objectName, mapRegExp);
139172
}
173+
else
174+
{
175+
if (methodMismatch)
176+
{
177+
return new ErrorRequestHandler("Method not allowed", HttpStatusCode.MethodNotAllowed);
178+
}
179+
180+
}
140181
}
141182
}
142183
return null;
@@ -204,21 +245,28 @@ public string GetObjFromPath(string cname, string apath)
204245
return objectName;
205246
}
206247

207-
public bool GetSMap(string actualPath, string objectName, string objectNameUp, string requestType, out string mapName, out string mapRegexp, out Dictionary<string, object> routeParms)
248+
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)
208249
{
209250
routeParms = null;
251+
methodMismatch = false;
210252
if (GXAPIModule.servicesMapData[actualPath].TryGetValue(Tuple.Create(objectName, requestType), out mapName))
211253
{
254+
// Url exact match
212255
mapRegexp = mapName;
256+
methodMismatch = false;
213257
return true;
214258
}
215259
else
216260
{
261+
bool pathFound = false;
217262
mapRegexp = mapName;
218263
foreach (SingleMap m in GXAPIModule.servicesMap[actualPath].Values)
219-
{
264+
{
220265
if (!m.Path.Equals(m.PathRegexp) && GxRegex.IsMatch(objectName, m.PathRegexp))
221266
{
267+
pathFound = true;
268+
methodMismatch = false;
269+
// regexp URL match
222270
mapName = m.Name;
223271
mapRegexp = m.PathRegexp;
224272
if (m.Verb.Equals(requestType))
@@ -232,10 +280,19 @@ public bool GetSMap(string actualPath, string objectName, string objectNameUp,
232280
routeParms.Add(var, smatch);
233281
i++;
234282
}
283+
methodMismatch = false;
235284
return true;
236-
}
285+
}
237286
}
238287
}
288+
if (pathFound && !requestType.Equals( HttpMethod.Options.Method))
289+
{
290+
mapName = null;
291+
mapRegexp = null;
292+
routeParms = null;
293+
methodMismatch = true;
294+
return false;
295+
}
239296
}
240297
return false;
241298
}

0 commit comments

Comments
 (0)