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

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

More cleanup, allocation improvements

File size: 2.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Net;
4using System.Reflection;
5using AllocsFixes.NetConnections.Servers.Web.API;
6
7namespace AllocsFixes.NetConnections.Servers.Web.Handlers {
8        public class ApiHandler : PathHandler {
9                private readonly Dictionary<string, WebAPI> apis = new CaseInsensitiveStringDictionary<WebAPI> ();
10                private readonly string staticPart;
11
12                public ApiHandler (string staticPart, string moduleName = null) : base (moduleName) {
13                        this.staticPart = staticPart;
14
15                        foreach (Type t in Assembly.GetExecutingAssembly ().GetTypes ()) {
16                                if (!t.IsAbstract && t.IsSubclassOf (typeof (WebAPI))) {
17                                        ConstructorInfo ctor = t.GetConstructor (new Type [0]);
18                                        if (ctor != null) {
19                                                WebAPI apiInstance = (WebAPI) ctor.Invoke (new object [0]);
20                                                addApi (apiInstance.Name, apiInstance);
21                                        }
22                                }
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                        }
35                }
36
37                private void addApi (string _apiName, WebAPI _api) {
38                        apis.Add (_apiName, _api);
39                        WebPermissions.Instance.AddKnownModule ("webapi." + _apiName, _api.DefaultPermissionLevel ());
40                }
41
42                public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user,
43                        int permissionLevel) {
44                        string apiName = req.Url.AbsolutePath.Remove (0, staticPart.Length);
45                        if (!AuthorizeForCommand (apiName, user, permissionLevel)) {
46                                resp.StatusCode = (int) HttpStatusCode.Forbidden;
47                                if (user != null) {
48                                        //Log.Out ("ApiHandler: user '{0}' not allowed to execute '{1}'", user.SteamID, apiName);
49                                }
50
51                                return;
52                        }
53
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;
64                                }
65                        }
66                       
67                        Log.Out ("Error in ApiHandler.HandleRequest(): No handler found for API \"" + apiName + "\"");
68                        resp.StatusCode = (int) HttpStatusCode.NotFound;
69                }
70
71                private bool AuthorizeForCommand (string apiName, WebConnection user, int permissionLevel) {
72                        return WebPermissions.Instance.ModuleAllowedWithLevel ("webapi." + apiName, permissionLevel);
73                }
74        }
75}
Note: See TracBrowser for help on using the repository browser.