source: binary-improvements/MapRendering/Web/Handlers/ApiHandler.cs@ 329

Last change on this file since 329 was 326, checked in by alloc, 6 years ago

More cleanup, allocation improvements

File size: 2.6 KB
RevLine 
[230]1using System;
2using System.Collections.Generic;
3using System.Net;
[250]4using System.Reflection;
[325]5using AllocsFixes.NetConnections.Servers.Web.API;
[230]6
[325]7namespace AllocsFixes.NetConnections.Servers.Web.Handlers {
[244]8 public class ApiHandler : PathHandler {
[326]9 private readonly Dictionary<string, WebAPI> apis = new CaseInsensitiveStringDictionary<WebAPI> ();
[325]10 private readonly string staticPart;
[230]11
[251]12 public ApiHandler (string staticPart, string moduleName = null) : base (moduleName) {
[230]13 this.staticPart = staticPart;
[250]14
15 foreach (Type t in Assembly.GetExecutingAssembly ().GetTypes ()) {
[325]16 if (!t.IsAbstract && t.IsSubclassOf (typeof (WebAPI))) {
[251]17 ConstructorInfo ctor = t.GetConstructor (new Type [0]);
[250]18 if (ctor != null) {
[325]19 WebAPI apiInstance = (WebAPI) ctor.Invoke (new object [0]);
[326]20 addApi (apiInstance.Name, apiInstance);
[250]21 }
22 }
[325]23 }
24
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]);
30
31 // Permissions that don't map to a real API
32 addApi ("viewallclaims", dummy_apiInstance);
33 addApi ("viewallplayers", dummy_apiInstance);
34 }
[230]35 }
36
[244]37 private void addApi (string _apiName, WebAPI _api) {
38 apis.Add (_apiName, _api);
[279]39 WebPermissions.Instance.AddKnownModule ("webapi." + _apiName, _api.DefaultPermissionLevel ());
[244]40 }
41
[325]42 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user,
43 int permissionLevel) {
[230]44 string apiName = req.Url.AbsolutePath.Remove (0, staticPart.Length);
[244]45 if (!AuthorizeForCommand (apiName, user, permissionLevel)) {
[325]46 resp.StatusCode = (int) HttpStatusCode.Forbidden;
[244]47 if (user != null) {
[247]48 //Log.Out ("ApiHandler: user '{0}' not allowed to execute '{1}'", user.SteamID, apiName);
[244]49 }
[325]50
[244]51 return;
[325]52 }
53
[326]54 WebAPI api;
55 if (apis.TryGetValue (apiName, out api)) {
56 try {
57 api.HandleRequest (req, resp, user, permissionLevel);
58 return;
59 } catch (Exception e) {
60 Log.Error ("Error in ApiHandler.HandleRequest(): Handler {0} threw an exception:", api.Name);
61 Log.Exception (e);
62 resp.StatusCode = (int) HttpStatusCode.InternalServerError;
63 return;
[230]64 }
65 }
[326]66
[230]67 Log.Out ("Error in ApiHandler.HandleRequest(): No handler found for API \"" + apiName + "\"");
[325]68 resp.StatusCode = (int) HttpStatusCode.NotFound;
[230]69 }
70
[244]71 private bool AuthorizeForCommand (string apiName, WebConnection user, int permissionLevel) {
72 return WebPermissions.Instance.ModuleAllowedWithLevel ("webapi." + apiName, permissionLevel);
[230]73 }
74 }
[325]75}
Note: See TracBrowser for help on using the repository browser.