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

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

Finished up REST API base
Made API handler look for APIs in all loaded mods

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