- Timestamp:
- Aug 6, 2022, 11:32:32 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/MapRendering/Web/Handlers/ApiHandler.cs
r384 r387 4 4 using System.Reflection; 5 5 using AllocsFixes.NetConnections.Servers.Web.API; 6 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;7 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;8 6 9 7 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 10 8 public class ApiHandler : AbsHandler { 11 private readonly Dictionary<string, WebAPI> apis = new CaseInsensitiveStringDictionary<WebAPI> ();9 private readonly Dictionary<string, AbsWebAPI> apis = new CaseInsensitiveStringDictionary<AbsWebAPI> (); 12 10 13 11 public ApiHandler () : base (null) { … … 25 23 26 24 foreach (Type t in Assembly.GetExecutingAssembly ().GetTypes ()) { 27 if (!t.IsAbstract && t.IsSubclassOf (typeof ( WebAPI))) {25 if (!t.IsAbstract && t.IsSubclassOf (typeof (AbsWebAPI))) { 28 26 ConstructorInfo ctor = t.GetConstructor (apiWithParentCtorTypes); 29 27 if (ctor != null) { 30 WebAPI apiInstance = (WebAPI) ctor.Invoke (apiWithParentCtorArgs);28 AbsWebAPI apiInstance = (AbsWebAPI) ctor.Invoke (apiWithParentCtorArgs); 31 29 addApi (apiInstance); 32 30 continue; … … 35 33 ctor = t.GetConstructor (apiEmptyCtorTypes); 36 34 if (ctor != null) { 37 WebAPI apiInstance = (WebAPI) ctor.Invoke (apiEmptyCtorArgs);35 AbsWebAPI apiInstance = (AbsWebAPI) ctor.Invoke (apiEmptyCtorArgs); 38 36 addApi (apiInstance); 39 37 } … … 46 44 } 47 45 48 private void addApi ( WebAPI _api) {46 private void addApi (AbsWebAPI _api) { 49 47 apis.Add (_api.Name, _api); 50 48 WebPermissions.Instance.AddKnownModule ("webapi." + _api.Name, _api.DefaultPermissionLevel ()); … … 55 53 #endif 56 54 57 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 58 int _permissionLevel) { 59 string apiName = _requestPath.Remove (0, urlBasePath.Length); 55 public override void HandleRequest (RequestContext _context) { 60 56 61 if (!apis.TryGetValue (apiName, out WebAPI api)) { 57 string apiName; 58 string subPath = null; 59 60 int pathSeparatorIndex = _context.RequestPath.IndexOf ('/', urlBasePath.Length); 61 if (pathSeparatorIndex >= 0) { 62 apiName = _context.RequestPath.Substring (urlBasePath.Length, pathSeparatorIndex - urlBasePath.Length); 63 subPath = _context.RequestPath.Substring (pathSeparatorIndex + 1); 64 } else { 65 apiName = _context.RequestPath.Substring (urlBasePath.Length); 66 } 67 68 if (!apis.TryGetValue (apiName, out AbsWebAPI api)) { 62 69 Log.Out ($"Error in {nameof(ApiHandler)}.HandleRequest(): No handler found for API \"{apiName}\""); 63 _ resp.StatusCode = (int) HttpStatusCode.NotFound;70 _context.Response.StatusCode = (int) HttpStatusCode.NotFound; 64 71 return; 65 72 } 66 73 67 if (!AuthorizeForApi (apiName, _ permissionLevel)) {68 _ resp.StatusCode = (int) HttpStatusCode.Forbidden;69 if (_con != null) {74 if (!AuthorizeForApi (apiName, _context.PermissionLevel)) { 75 _context.Response.StatusCode = (int) HttpStatusCode.Forbidden; 76 if (_context.Connection != null) { 70 77 //Log.Out ($"{nameof(ApiHandler)}: user '{user.SteamID}' not allowed to execute '{apiName}'"); 71 78 } … … 74 81 } 75 82 83 _context.RequestPath = subPath; 84 76 85 try { 77 86 #if ENABLE_PROFILER 78 87 apiHandlerSampler.Begin (); 79 88 #endif 80 api.HandleRequest (_ req, _resp, _con, _permissionLevel);89 api.HandleRequest (_context); 81 90 #if ENABLE_PROFILER 82 91 apiHandlerSampler.End (); … … 85 94 Log.Error ($"Error in {nameof(ApiHandler)}.HandleRequest(): Handler {api.Name} threw an exception:"); 86 95 Log.Exception (e); 87 _ resp.StatusCode = (int) HttpStatusCode.InternalServerError;96 _context.Response.StatusCode = (int) HttpStatusCode.InternalServerError; 88 97 } 89 98 }
Note:
See TracChangeset
for help on using the changeset viewer.