source: binary-improvements2/MapRendering/Web/Handlers/ApiHandler.cs@ 387

Last change on this file since 387 was 387, checked in by alloc, 2 years ago

Big refactoring in Web to pass around a Context instead of a bunch of individual arguments all the time

File size: 3.3 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 : AbsHandler {
9 private readonly Dictionary<string, AbsWebAPI> apis = new CaseInsensitiveStringDictionary<AbsWebAPI> ();
10
11 public ApiHandler () : base (null) {
12
13 }
14
15 public override void SetBasePathAndParent (Web _parent, string _relativePath) {
16 base.SetBasePathAndParent (_parent, _relativePath);
17
18 Type[] apiWithParentCtorTypes = { typeof (Web) };
19 Object[] apiWithParentCtorArgs = { _parent };
20
21 Type[] apiEmptyCtorTypes = { };
22 Object[] apiEmptyCtorArgs = { };
23
24 foreach (Type t in Assembly.GetExecutingAssembly ().GetTypes ()) {
25 if (!t.IsAbstract && t.IsSubclassOf (typeof (AbsWebAPI))) {
26 ConstructorInfo ctor = t.GetConstructor (apiWithParentCtorTypes);
27 if (ctor != null) {
28 AbsWebAPI apiInstance = (AbsWebAPI) ctor.Invoke (apiWithParentCtorArgs);
29 addApi (apiInstance);
30 continue;
31 }
32
33 ctor = t.GetConstructor (apiEmptyCtorTypes);
34 if (ctor != null) {
35 AbsWebAPI apiInstance = (AbsWebAPI) ctor.Invoke (apiEmptyCtorArgs);
36 addApi (apiInstance);
37 }
38 }
39 }
40
41 // Permissions that don't map to a real API
42 addApi (new Null ("viewallclaims"));
43 addApi (new Null ("viewallplayers"));
44 }
45
46 private void addApi (AbsWebAPI _api) {
47 apis.Add (_api.Name, _api);
48 WebPermissions.Instance.AddKnownModule ("webapi." + _api.Name, _api.DefaultPermissionLevel ());
49 }
50
51#if ENABLE_PROFILER
52 private static readonly UnityEngine.Profiling.CustomSampler apiHandlerSampler = UnityEngine.Profiling.CustomSampler.Create ("API_Handler");
53#endif
54
55 public override void HandleRequest (RequestContext _context) {
56
57 string apiName;
58 string subPath = null;
59
60 int pathSeparatorIndex = _context.RequestPath.IndexOf ('/', urlBasePath.Length);
61 if (pathSeparatorIndex >= 0) {
62 apiName = _context.RequestPath.Substring (urlBasePath.Length, pathSeparatorIndex - urlBasePath.Length);
63 subPath = _context.RequestPath.Substring (pathSeparatorIndex + 1);
64 } else {
65 apiName = _context.RequestPath.Substring (urlBasePath.Length);
66 }
67
68 if (!apis.TryGetValue (apiName, out AbsWebAPI api)) {
69 Log.Out ($"Error in {nameof(ApiHandler)}.HandleRequest(): No handler found for API \"{apiName}\"");
70 _context.Response.StatusCode = (int) HttpStatusCode.NotFound;
71 return;
72 }
73
74 if (!AuthorizeForApi (apiName, _context.PermissionLevel)) {
75 _context.Response.StatusCode = (int) HttpStatusCode.Forbidden;
76 if (_context.Connection != null) {
77 //Log.Out ($"{nameof(ApiHandler)}: user '{user.SteamID}' not allowed to execute '{apiName}'");
78 }
79
80 return;
81 }
82
83 _context.RequestPath = subPath;
84
85 try {
86#if ENABLE_PROFILER
87 apiHandlerSampler.Begin ();
88#endif
89 api.HandleRequest (_context);
90#if ENABLE_PROFILER
91 apiHandlerSampler.End ();
92#endif
93 } catch (Exception e) {
94 Log.Error ($"Error in {nameof(ApiHandler)}.HandleRequest(): Handler {api.Name} threw an exception:");
95 Log.Exception (e);
96 _context.Response.StatusCode = (int) HttpStatusCode.InternalServerError;
97 }
98 }
99
100 private bool AuthorizeForApi (string _apiName, int _permissionLevel) {
101 return WebPermissions.Instance.ModuleAllowedWithLevel ("webapi." + _apiName, _permissionLevel);
102 }
103 }
104}
Note: See TracBrowser for help on using the repository browser.