Changeset 367 for binary-improvements/MapRendering/Web/Handlers
- Timestamp:
- Aug 11, 2021, 6:22:03 PM (3 years ago)
- Location:
- binary-improvements/MapRendering/Web/Handlers
- Files:
-
- 5 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 } -
binary-improvements/MapRendering/Web/Handlers/ItemIconHandler.cs
r354 r367 11 11 private readonly bool logMissingFiles; 12 12 13 private readonly string staticPart;14 13 private bool loaded; 15 14 … … 18 17 } 19 18 20 public ItemIconHandler (string _staticPart, bool _logMissingFiles, string _moduleName = null) : base (_moduleName) { 21 staticPart = _staticPart; 19 public ItemIconHandler (bool _logMissingFiles, string _moduleName = null) : base (_moduleName) { 22 20 logMissingFiles = _logMissingFiles; 23 21 Instance = this; … … 34 32 } 35 33 36 string requestFileName = _req.Url.AbsolutePath.Remove (0, staticPart.Length);34 string requestFileName = _req.Url.AbsolutePath.Remove (0, urlBasePath.Length); 37 35 requestFileName = requestFileName.Remove (requestFileName.LastIndexOf ('.')); 38 36 -
binary-improvements/MapRendering/Web/Handlers/PathHandler.cs
r351 r367 3 3 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 4 4 public abstract class PathHandler { 5 private readonly string moduleName; 5 protected readonly string moduleName; 6 protected string urlBasePath; 7 protected Web parent; 6 8 7 9 protected PathHandler (string _moduleName, int _defaultPermissionLevel = 0) { … … 24 26 return true; 25 27 } 28 29 public virtual void Shutdown () { 30 } 31 32 public virtual void SetBasePathAndParent (Web _parent, string _relativePath) { 33 parent = _parent; 34 urlBasePath = _relativePath; 35 } 26 36 } 27 37 } -
binary-improvements/MapRendering/Web/Handlers/SessionHandler.cs
r351 r367 7 7 private readonly string footer = ""; 8 8 private readonly string header = ""; 9 private readonly Web parent;10 private readonly string staticPart;11 9 12 public SessionHandler (string _staticPart, string _dataFolder, Web _parent, string _moduleName = null) : 13 base (_moduleName) { 14 staticPart = _staticPart; 15 parent = _parent; 16 10 public SessionHandler (string _dataFolder, string _moduleName = null) : base (_moduleName) { 17 11 if (File.Exists (_dataFolder + "/sessionheader.tmpl")) { 18 12 header = File.ReadAllText (_dataFolder + "/sessionheader.tmpl"); … … 26 20 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 27 21 int _permissionLevel) { 28 string subpath = _req.Url.AbsolutePath.Remove (0, staticPart.Length);22 string subpath = _req.Url.AbsolutePath.Remove (0, urlBasePath.Length); 29 23 30 24 StringBuilder result = new StringBuilder (); … … 52 46 "<h1>Not logged in, <a href=\"/static/index.html\">click to return to main page</a>.</h1>"); 53 47 } else if (subpath.StartsWith ("login")) { 54 string host = (Web. isSslRedirected (_req) ? "https://" : "http://") + _req.UserHostName;48 string host = (Web.IsSslRedirected (_req) ? "https://" : "http://") + _req.UserHostName; 55 49 string url = OpenID.GetOpenIdLoginUrl (host, host + "/session/verify"); 56 50 _resp.Redirect (url); -
binary-improvements/MapRendering/Web/Handlers/StaticHandler.cs
r351 r367 8 8 private readonly string datapath; 9 9 private readonly bool logMissingFiles; 10 private readonly string staticPart;11 10 12 public StaticHandler (string _ staticPart, string _filePath, AbstractCache _cache, bool _logMissingFiles,11 public StaticHandler (string _filePath, AbstractCache _cache, bool _logMissingFiles, 13 12 string _moduleName = null) : base (_moduleName) { 14 staticPart = _staticPart;15 13 datapath = _filePath + (_filePath [_filePath.Length - 1] == '/' ? "" : "/"); 16 14 cache = _cache; … … 20 18 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 21 19 int _permissionLevel) { 22 string fn = _req.Url.AbsolutePath.Remove (0, staticPart.Length);20 string fn = _req.Url.AbsolutePath.Remove (0, urlBasePath.Length); 23 21 24 22 byte[] content = cache.GetFileContent (datapath + fn);
Note:
See TracChangeset
for help on using the changeset viewer.