- Timestamp:
- Aug 11, 2021, 6:22:03 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/MapRendering/Web/Handlers/ApiHandler.cs
r351 r367 4 4 using System.Reflection; 5 5 using AllocsFixes.NetConnections.Servers.Web.API; 6 using UnityEngine.Profiling;7 6 8 7 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 9 8 public class ApiHandler : PathHandler { 10 9 private readonly Dictionary<string, WebAPI> apis = new CaseInsensitiveStringDictionary<WebAPI> (); 11 private readonly string staticPart;12 10 13 public ApiHandler (string _staticPart, string _moduleName = null) : base (_moduleName) { 14 staticPart = _staticPart; 11 public ApiHandler (string _moduleName = null) : base (_moduleName) { 15 12 16 13 foreach (Type t in Assembly.GetExecutingAssembly ().GetTypes ()) { … … 19 16 if (ctor != null) { 20 17 WebAPI apiInstance = (WebAPI) ctor.Invoke (new object [0]); 21 addApi (apiInstance .Name, apiInstance);18 addApi (apiInstance); 22 19 } 23 20 } 24 21 } 25 22 26 // Add dummy types 27 Type dummy_t = typeof (Null); 28 ConstructorInfo dummy_ctor = dummy_t.GetConstructor (new Type [0]); 29 if (dummy_ctor != null) { 30 WebAPI dummy_apiInstance = (WebAPI) dummy_ctor.Invoke (new object[0]); 31 32 // Permissions that don't map to a real API 33 addApi ("viewallclaims", dummy_apiInstance); 34 addApi ("viewallplayers", dummy_apiInstance); 35 } 23 // Permissions that don't map to a real API 24 addApi (new Null ("viewallclaims")); 25 addApi (new Null ("viewallplayers")); 36 26 } 37 27 38 private void addApi ( string _apiName,WebAPI _api) {39 apis.Add (_api Name, _api);40 WebPermissions.Instance.AddKnownModule ("webapi." + _api Name, _api.DefaultPermissionLevel ());28 private void addApi (WebAPI _api) { 29 apis.Add (_api.Name, _api); 30 WebPermissions.Instance.AddKnownModule ("webapi." + _api.Name, _api.DefaultPermissionLevel ()); 41 31 } 42 32 43 33 #if ENABLE_PROFILER 44 private static readonly CustomSampler apiHandlerSampler =CustomSampler.Create ("API_Handler");34 private static readonly UnityEngine.Profiling.CustomSampler apiHandlerSampler = UnityEngine.Profiling.CustomSampler.Create ("API_Handler"); 45 35 #endif 46 36 47 37 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 48 38 int _permissionLevel) { 49 string apiName = _req.Url.AbsolutePath.Remove (0, staticPart.Length);39 string apiName = _req.Url.AbsolutePath.Remove (0, urlBasePath.Length); 50 40 51 WebAPI api; 52 if (!apis.TryGetValue (apiName, out api)) { 53 Log.Out ("Error in ApiHandler.HandleRequest(): No handler found for API \"" + apiName + "\""); 41 if (!apis.TryGetValue (apiName, out WebAPI api)) { 42 Log.Out ($"Error in {nameof(ApiHandler)}.HandleRequest(): No handler found for API \"{apiName}\""); 54 43 _resp.StatusCode = (int) HttpStatusCode.NotFound; 55 44 return; 56 45 } 57 46 58 if (!AuthorizeFor Command (apiName, _user, _permissionLevel)) {47 if (!AuthorizeForApi (apiName, _permissionLevel)) { 59 48 _resp.StatusCode = (int) HttpStatusCode.Forbidden; 60 49 if (_user != null) { 61 //Log.Out ( "ApiHandler: user '{0}' not allowed to execute '{1}'", user.SteamID, apiName);50 //Log.Out ($"{nameof(ApiHandler)}: user '{user.SteamID}' not allowed to execute '{apiName}'"); 62 51 } 63 52 … … 74 63 #endif 75 64 } catch (Exception e) { 76 Log.Error ( "Error in ApiHandler.HandleRequest(): Handler {0} threw an exception:", api.Name);65 Log.Error ($"Error in {nameof(ApiHandler)}.HandleRequest(): Handler {api.Name} threw an exception:"); 77 66 Log.Exception (e); 78 67 _resp.StatusCode = (int) HttpStatusCode.InternalServerError; … … 80 69 } 81 70 82 private bool AuthorizeFor Command (string _apiName, WebConnection _user, int _permissionLevel) {71 private bool AuthorizeForApi (string _apiName, int _permissionLevel) { 83 72 return WebPermissions.Instance.ModuleAllowedWithLevel ("webapi." + _apiName, _permissionLevel); 84 73 }
Note:
See TracChangeset
for help on using the changeset viewer.