11using System ;
22using System . Collections . Generic ;
33using System . IO ;
4+ using System . Linq ;
45using System . Net ;
56using System . Net . Http ;
67using System . Reflection ;
1516
1617namespace 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,12 +245,15 @@ 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
@@ -219,6 +263,8 @@ public bool GetSMap(string actualPath, string objectName, string objectNameUp,
219263 {
220264 if ( ! m . Path . Equals ( m . PathRegexp ) && GxRegex . IsMatch ( objectName , m . PathRegexp ) )
221265 {
266+ methodMismatch = false ;
267+ // regexp URL match
222268 mapName = m . Name ;
223269 mapRegexp = m . PathRegexp ;
224270 if ( m . Verb . Equals ( requestType ) )
@@ -232,8 +278,17 @@ public bool GetSMap(string actualPath, string objectName, string objectNameUp,
232278 routeParms . Add ( var , smatch ) ;
233279 i ++ ;
234280 }
281+ methodMismatch = false ;
235282 return true ;
236283 }
284+ else
285+ {
286+ mapName = null ;
287+ mapRegexp = null ;
288+ routeParms = null ;
289+ methodMismatch = true ;
290+ return false ;
291+ }
237292 }
238293 }
239294 }
0 commit comments