Changeset 325 for binary-improvements/MapRendering/Web/Handlers
- Timestamp:
- Sep 4, 2018, 1:00:48 PM (6 years ago)
- Location:
- binary-improvements/MapRendering/Web/Handlers
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/MapRendering/Web/Handlers/ApiHandler.cs
r279 r325 1 using AllocsFixes.NetConnections.Servers.Web.API;2 1 using System; 3 2 using System.Collections.Generic; 4 using System.IO;5 3 using System.Net; 6 4 using System.Reflection; 7 using System.Threading;5 using AllocsFixes.NetConnections.Servers.Web.API; 8 6 9 namespace AllocsFixes.NetConnections.Servers.Web.Handlers 10 { 7 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 11 8 public class ApiHandler : PathHandler { 12 private string staticPart;13 private Dictionary<String, WebAPI> apis = new Dictionary<string, WebAPI> ();9 private readonly Dictionary<string, WebAPI> apis = new Dictionary<string, WebAPI> (); 10 private readonly string staticPart; 14 11 15 12 public ApiHandler (string staticPart, string moduleName = null) : base (moduleName) { … … 17 14 18 15 foreach (Type t in Assembly.GetExecutingAssembly ().GetTypes ()) { 19 if (!t.IsAbstract && t.IsSubclassOf (typeof (WebAPI))) {16 if (!t.IsAbstract && t.IsSubclassOf (typeof (WebAPI))) { 20 17 ConstructorInfo ctor = t.GetConstructor (new Type [0]); 21 18 if (ctor != null) { 22 WebAPI apiInstance = (WebAPI) ctor.Invoke (new object [0]);19 WebAPI apiInstance = (WebAPI) ctor.Invoke (new object [0]); 23 20 addApi (t.Name.ToLower (), apiInstance); 24 21 } … … 26 23 } 27 24 28 29 Type dummy_t = typeof (API.Null);30 31 32 WebAPI dummy_apiInstance = (WebAPI)dummy_ctor.Invoke (new object[0]);25 // Add dummy types 26 Type dummy_t = typeof (Null); 27 ConstructorInfo dummy_ctor = dummy_t.GetConstructor (new Type [0]); 28 if (dummy_ctor != null) { 29 WebAPI dummy_apiInstance = (WebAPI) dummy_ctor.Invoke (new object[0]); 33 30 34 35 addApi("viewallclaims", dummy_apiInstance);36 addApi("viewallplayers", dummy_apiInstance);37 31 // Permissions that don't map to a real API 32 addApi ("viewallclaims", dummy_apiInstance); 33 addApi ("viewallplayers", dummy_apiInstance); 34 } 38 35 } 39 36 … … 43 40 } 44 41 45 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel) { 42 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, 43 int permissionLevel) { 46 44 string apiName = req.Url.AbsolutePath.Remove (0, staticPart.Length); 47 45 if (!AuthorizeForCommand (apiName, user, permissionLevel)) { 48 resp.StatusCode = (int) HttpStatusCode.Forbidden;46 resp.StatusCode = (int) HttpStatusCode.Forbidden; 49 47 if (user != null) { 50 48 //Log.Out ("ApiHandler: user '{0}' not allowed to execute '{1}'", user.SteamID, apiName); 51 } else {52 //Log.Out ("ApiHandler: unidentified user from '{0}' not allowed to execute '{1}'", req.RemoteEndPoint.Address, apiName);53 49 } 50 54 51 return; 55 } else {56 foreach (KeyValuePair<string, WebAPI> kvp in apis) { 57 if (apiName.StartsWith (kvp.Key)) {58 try{59 kvp.Value.HandleRequest (req, resp, user, permissionLevel);60 return;61 } catch (Exception e) {62 Log.Error ("Error in ApiHandler.HandleRequest(): Handler {0} threw an exception:", kvp.Key);63 Log.Exception (e);64 resp.StatusCode = (int)HttpStatusCode.InternalServerError;65 return;66 }52 } 53 54 foreach (KeyValuePair<string, WebAPI> kvp in apis) { 55 if (apiName.StartsWith (kvp.Key)) { 56 try { 57 kvp.Value.HandleRequest (req, resp, user, permissionLevel); 58 return; 59 } catch (Exception e) { 60 Log.Error ("Error in ApiHandler.HandleRequest(): Handler {0} threw an exception:", kvp.Key); 61 Log.Exception (e); 62 resp.StatusCode = (int) HttpStatusCode.InternalServerError; 63 return; 67 64 } 68 65 } 69 66 } 70 67 71 68 Log.Out ("Error in ApiHandler.HandleRequest(): No handler found for API \"" + apiName + "\""); 72 resp.StatusCode = (int) HttpStatusCode.NotFound;69 resp.StatusCode = (int) HttpStatusCode.NotFound; 73 70 } 74 71 … … 76 73 return WebPermissions.Instance.ModuleAllowedWithLevel ("webapi." + apiName, permissionLevel); 77 74 } 78 79 75 } 80 81 76 } 82 -
binary-improvements/MapRendering/Web/Handlers/ItemIconHandler.cs
r306 r325 1 1 using System; 2 2 using System.Collections.Generic; 3 using System.IO; 3 4 using System.Net; 4 using System.Threading; 5 using UnityEngine; 6 using Object = UnityEngine.Object; 5 7 6 using UnityEngine; 7 using System.IO; 8 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 9 public class ItemIconHandler : PathHandler { 10 private readonly Dictionary<string, byte[]> icons = new Dictionary<string, byte[]> (); 11 private readonly bool logMissingFiles; 8 12 9 namespace AllocsFixes.NetConnections.Servers.Web.Handlers 10 { 11 public class ItemIconHandler : PathHandler { 12 private static ItemIconHandler instance = null; 13 public static ItemIconHandler Instance { 14 get { return instance; } 13 private readonly string staticPart; 14 private bool loaded; 15 16 static ItemIconHandler () { 17 Instance = null; 15 18 } 16 19 17 private string staticPart; 18 private bool logMissingFiles; 19 private Dictionary<string, byte[]> icons = new Dictionary<string, byte[]> (); 20 private bool loaded = false; 21 22 public ItemIconHandler (string staticPart, bool logMissingFiles, string moduleName = null) : base(moduleName) { 20 public ItemIconHandler (string staticPart, bool logMissingFiles, string moduleName = null) : base (moduleName) { 23 21 this.staticPart = staticPart; 24 22 this.logMissingFiles = logMissingFiles; 25 I temIconHandler.instance = this;23 Instance = this; 26 24 } 27 25 28 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel) { 26 public static ItemIconHandler Instance { get; private set; } 27 28 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, 29 int permissionLevel) { 29 30 if (!loaded) { 30 resp.StatusCode = (int) HttpStatusCode.InternalServerError;31 resp.StatusCode = (int) HttpStatusCode.InternalServerError; 31 32 Log.Out ("Web:IconHandler: Icons not loaded"); 32 33 return; … … 44 45 resp.OutputStream.Write (itemIconData, 0, itemIconData.Length); 45 46 } else { 46 resp.StatusCode = (int) HttpStatusCode.NotFound;47 resp.StatusCode = (int) HttpStatusCode.NotFound; 47 48 if (logMissingFiles) { 48 49 Log.Out ("Web:IconHandler:FileNotFound: \"" + req.Url.AbsolutePath + "\" "); 49 50 } 50 return;51 51 } 52 52 } … … 66 66 return false; 67 67 } 68 68 69 DynamicUIAtlas atlas = atlasObj.GetComponent<DynamicUIAtlas> (); 69 70 if (atlas == null) { … … 78 79 Texture2D atlasTex; 79 80 80 if (!DynamicUIAtlasTools.ReadPrebakedAtlasDescriptor (textureResourceName, out sprites, out elementWidth, out elementHeight)) { 81 if (!DynamicUIAtlasTools.ReadPrebakedAtlasDescriptor (textureResourceName, out sprites, 82 out elementWidth, out elementHeight)) { 81 83 SdtdConsole.Instance.Output ("Web:IconHandler: Could not read dynamic atlas descriptor"); 82 84 return false; … … 98 100 tintedIcons.Add (name, new List<Color> ()); 99 101 } 102 100 103 List<Color> list = tintedIcons [name]; 101 104 list.Add (tintColor); … … 108 111 string name = data.name; 109 112 Texture2D tex = new Texture2D (data.width, data.height, TextureFormat.ARGB32, false); 110 tex.SetPixels (atlasTex.GetPixels (data.x, atlasTex.height - data.height - data.y, data.width, data.height)); 113 tex.SetPixels (atlasTex.GetPixels (data.x, atlasTex.height - data.height - data.y, data.width, 114 data.height)); 111 115 112 116 AddIcon (name, tex, tintedIcons); 113 117 114 UnityEngine.Object.Destroy (tex);118 Object.Destroy (tex); 115 119 } 116 Resources.UnloadAsset(atlasTex); 120 121 Resources.UnloadAsset (atlasTex); 117 122 118 123 // Load icons from mods … … 131 136 } 132 137 133 UnityEngine.Object.Destroy (tex);138 Object.Destroy (tex); 134 139 } 135 140 } … … 168 173 icons [tintedName] = tintedTex.EncodeToPNG (); 169 174 170 UnityEngine.Object.Destroy (tintedTex);175 Object.Destroy (tintedTex); 171 176 } 172 177 } 173 178 } 174 179 } 175 176 180 } 177 181 } 178 -
binary-improvements/MapRendering/Web/Handlers/PathHandler.cs
r279 r325 1 using System;2 1 using System.Net; 3 2 4 namespace AllocsFixes.NetConnections.Servers.Web.Handlers 5 { 6 public abstract class PathHandler 7 { 8 private string moduleName = null; 3 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 4 public abstract class PathHandler { 5 private readonly string moduleName; 6 7 protected PathHandler (string _moduleName, int _defaultPermissionLevel = 0) { 8 moduleName = _moduleName; 9 WebPermissions.Instance.AddKnownModule (_moduleName, _defaultPermissionLevel); 10 } 11 9 12 public string ModuleName { 10 13 get { return moduleName; } 11 14 } 12 15 13 protected PathHandler (string _moduleName, int _defaultPermissionLevel = 0) { 14 this.moduleName = _moduleName; 15 WebPermissions.Instance.AddKnownModule (_moduleName, _defaultPermissionLevel); 16 } 17 18 public abstract void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel); 16 public abstract void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, 17 int permissionLevel); 19 18 20 19 public bool IsAuthorizedForHandler (WebConnection user, int permissionLevel) { 21 20 if (moduleName != null) { 22 21 return WebPermissions.Instance.ModuleAllowedWithLevel (moduleName, permissionLevel); 23 } else {24 return true;25 22 } 23 24 return true; 26 25 } 27 26 } 28 27 } 29 -
binary-improvements/MapRendering/Web/Handlers/SessionHandler.cs
r311 r325 1 using System;2 using System.Collections.Generic;3 1 using System.IO; 4 2 using System.Net; 5 3 using System.Text; 6 using System.Threading;7 4 8 namespace AllocsFixes.NetConnections.Servers.Web.Handlers 9 { 5 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 10 6 public class SessionHandler : PathHandler { 11 private string staticPart;12 private Web parent;13 private string header = "";14 private string footer = "";7 private readonly string footer = ""; 8 private readonly string header = ""; 9 private readonly Web parent; 10 private readonly string staticPart; 15 11 16 public SessionHandler (string _staticPart, string _dataFolder, Web _parent, string moduleName = null) : base(moduleName) { 17 this.staticPart = _staticPart; 18 this.parent = _parent; 12 public SessionHandler (string _staticPart, string _dataFolder, Web _parent, string moduleName = null) : 13 base (moduleName) { 14 staticPart = _staticPart; 15 parent = _parent; 19 16 20 17 if (File.Exists (_dataFolder + "/sessionheader.tmpl")) { … … 27 24 } 28 25 29 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel) { 26 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, 27 int permissionLevel) { 30 28 string subpath = req.Url.AbsolutePath.Remove (0, staticPart.Length); 31 29 … … 37 35 resp.Redirect ("/static/index.html"); 38 36 return; 39 } else {40 result.Append ("<h1>Login failed, <a href=\"/static/index.html\">click to return to main page</a>.</h1>");41 37 } 38 39 result.Append ( 40 "<h1>Login failed, <a href=\"/static/index.html\">click to return to main page</a>.</h1>"); 42 41 } else if (subpath.StartsWith ("logout")) { 43 42 if (user != null) { … … 48 47 resp.Redirect ("/static/index.html"); 49 48 return; 50 } else {51 result.Append ("<h1>Not logged in, <a href=\"/static/index.html\">click to return to main page</a>.</h1>");52 49 } 50 51 result.Append ( 52 "<h1>Not logged in, <a href=\"/static/index.html\">click to return to main page</a>.</h1>"); 53 53 } else if (subpath.StartsWith ("login")) { 54 54 string host = (Web.isSslRedirected (req) ? "https://" : "http://") + req.UserHostName; … … 57 57 return; 58 58 } else { 59 result.Append ("<h1>Unknown command, <a href=\"/static/index.html\">click to return to main page</a>.</h1>"); 59 result.Append ( 60 "<h1>Unknown command, <a href=\"/static/index.html\">click to return to main page</a>.</h1>"); 60 61 } 61 62 … … 68 69 resp.OutputStream.Write (buf, 0, buf.Length); 69 70 } 70 71 71 } 72 73 72 } 74 -
binary-improvements/MapRendering/Web/Handlers/SimpleRedirectHandler.cs
r244 r325 1 using System;2 1 using System.Net; 3 2 4 namespace AllocsFixes.NetConnections.Servers.Web.Handlers 5 { 6 public class SimpleRedirectHandler : PathHandler 7 { 8 string target; 3 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 4 public class SimpleRedirectHandler : PathHandler { 5 private readonly string target; 9 6 10 public SimpleRedirectHandler (string target, string moduleName = null) : base(moduleName) 11 { 7 public SimpleRedirectHandler (string target, string moduleName = null) : base (moduleName) { 12 8 this.target = target; 13 9 } 14 10 15 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel)16 {11 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, 12 int permissionLevel) { 17 13 resp.Redirect (target); 18 14 } 19 15 } 20 16 } 21 -
binary-improvements/MapRendering/Web/Handlers/StaticHandler.cs
r251 r325 1 using System;2 using System.Collections.Generic;3 1 using System.IO; 4 2 using System.Net; 5 using System.Threading;3 using AllocsFixes.FileCache; 6 4 7 namespace AllocsFixes.NetConnections.Servers.Web.Handlers 8 { 9 public class StaticHandler : PathHandler 10 { 11 private string datapath; 12 private string staticPart; 13 private AllocsFixes.FileCache.AbstractCache cache; 14 private bool logMissingFiles; 5 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 6 public class StaticHandler : PathHandler { 7 private readonly AbstractCache cache; 8 private readonly string datapath; 9 private readonly bool logMissingFiles; 10 private readonly string staticPart; 15 11 16 public StaticHandler (string staticPart, string filePath, A llocsFixes.FileCache.AbstractCache cache, bool logMissingFiles, string moduleName = null) : base(moduleName)17 {12 public StaticHandler (string staticPart, string filePath, AbstractCache cache, bool logMissingFiles, 13 string moduleName = null) : base (moduleName) { 18 14 this.staticPart = staticPart; 19 this.datapath = filePath;15 datapath = filePath; 20 16 this.cache = cache; 21 17 this.logMissingFiles = logMissingFiles; 22 18 } 23 19 24 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel)25 {20 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, 21 int permissionLevel) { 26 22 string fn = req.Url.AbsolutePath.Remove (0, staticPart.Length); 27 23 … … 33 29 resp.OutputStream.Write (content, 0, content.Length); 34 30 } else { 35 resp.StatusCode = (int)HttpStatusCode.NotFound; 36 if (logMissingFiles) 37 Log.Out ("Web:Static:FileNotFound: \"" + req.Url.AbsolutePath + "\" @ \"" + datapath + "/" + req.Url.AbsolutePath.Remove (0, staticPart.Length) + "\""); 38 return; 31 resp.StatusCode = (int) HttpStatusCode.NotFound; 32 if (logMissingFiles) { 33 Log.Out ("Web:Static:FileNotFound: \"" + req.Url.AbsolutePath + "\" @ \"" + datapath + "/" + 34 req.Url.AbsolutePath.Remove (0, staticPart.Length) + "\""); 35 } 39 36 } 40 37 } 41 38 } 42 39 } 43 -
binary-improvements/MapRendering/Web/Handlers/UserStatusHandler.cs
r309 r325 1 using System;2 using System.Collections.Generic;3 using System.IO;4 1 using System.Net; 5 using System.Threading;6 using System.Text;7 2 using AllocsFixes.JSON; 3 using AllocsFixes.NetConnections.Servers.Web.API; 8 4 9 namespace AllocsFixes.NetConnections.Servers.Web.Handlers 10 { 5 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 11 6 public class UserStatusHandler : PathHandler { 12 public UserStatusHandler (string moduleName = null) : base (moduleName) {7 public UserStatusHandler (string moduleName = null) : base (moduleName) { 13 8 } 14 9 15 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel) { 10 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, 11 int permissionLevel) { 16 12 JSONObject result = new JSONObject (); 17 13 … … 23 19 JSONObject permObj = new JSONObject (); 24 20 permObj.Add ("module", new JSONString (perm.module)); 25 permObj.Add ("allowed", new JSONBoolean (WebPermissions.Instance.ModuleAllowedWithLevel (perm.module, permissionLevel))); 21 permObj.Add ("allowed", 22 new JSONBoolean (WebPermissions.Instance.ModuleAllowedWithLevel (perm.module, permissionLevel))); 26 23 perms.Add (permObj); 27 24 } 25 28 26 result.Add ("permissions", perms); 29 27 30 AllocsFixes.NetConnections.Servers.Web.API.WebAPI.WriteJSON (resp, result);28 WebAPI.WriteJSON (resp, result); 31 29 } 32 33 30 } 34 35 31 } 36
Note:
See TracChangeset
for help on using the changeset viewer.