- Timestamp:
- Sep 4, 2018, 1:00:48 PM (6 years ago)
- File:
-
- 1 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
Note:
See TracChangeset
for help on using the changeset viewer.