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

Last change on this file since 340 was 332, checked in by alloc, 6 years ago

*Latest optimizations

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