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

Last change on this file since 409 was 367, checked in by alloc, 3 years ago

Web:

  • Added SSE (ServerSentEvents) subsystem
  • Added log endpoint to SSE. Less heavy weight and more responsive way of watching the server log
  • Bunch of refactoring
File size: 2.5 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> ();
[230]10
[367]11 public ApiHandler (string _moduleName = null) : base (_moduleName) {
[250]12
13 foreach (Type t in Assembly.GetExecutingAssembly ().GetTypes ()) {
[325]14 if (!t.IsAbstract && t.IsSubclassOf (typeof (WebAPI))) {
[251]15 ConstructorInfo ctor = t.GetConstructor (new Type [0]);
[250]16 if (ctor != null) {
[325]17 WebAPI apiInstance = (WebAPI) ctor.Invoke (new object [0]);
[367]18 addApi (apiInstance);
[250]19 }
20 }
[325]21 }
22
[367]23 // Permissions that don't map to a real API
24 addApi (new Null ("viewallclaims"));
25 addApi (new Null ("viewallplayers"));
[230]26 }
27
[367]28 private void addApi (WebAPI _api) {
29 apis.Add (_api.Name, _api);
30 WebPermissions.Instance.AddKnownModule ("webapi." + _api.Name, _api.DefaultPermissionLevel ());
[244]31 }
32
[332]33#if ENABLE_PROFILER
[367]34 private static readonly UnityEngine.Profiling.CustomSampler apiHandlerSampler = UnityEngine.Profiling.CustomSampler.Create ("API_Handler");
[332]35#endif
36
[351]37 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
38 int _permissionLevel) {
[367]39 string apiName = _req.Url.AbsolutePath.Remove (0, urlBasePath.Length);
[351]40
[367]41 if (!apis.TryGetValue (apiName, out WebAPI api)) {
42 Log.Out ($"Error in {nameof(ApiHandler)}.HandleRequest(): No handler found for API \"{apiName}\"");
[351]43 _resp.StatusCode = (int) HttpStatusCode.NotFound;
44 return;
45 }
46
[367]47 if (!AuthorizeForApi (apiName, _permissionLevel)) {
[351]48 _resp.StatusCode = (int) HttpStatusCode.Forbidden;
49 if (_user != null) {
[367]50 //Log.Out ($"{nameof(ApiHandler)}: user '{user.SteamID}' not allowed to execute '{apiName}'");
[244]51 }
[325]52
[244]53 return;
[325]54 }
55
[351]56 try {
[332]57#if ENABLE_PROFILER
[351]58 apiHandlerSampler.Begin ();
[332]59#endif
[351]60 api.HandleRequest (_req, _resp, _user, _permissionLevel);
[332]61#if ENABLE_PROFILER
[351]62 apiHandlerSampler.End ();
[332]63#endif
[351]64 } catch (Exception e) {
[367]65 Log.Error ($"Error in {nameof(ApiHandler)}.HandleRequest(): Handler {api.Name} threw an exception:");
[351]66 Log.Exception (e);
67 _resp.StatusCode = (int) HttpStatusCode.InternalServerError;
[230]68 }
69 }
70
[367]71 private bool AuthorizeForApi (string _apiName, int _permissionLevel) {
[351]72 return WebPermissions.Instance.ModuleAllowedWithLevel ("webapi." + _apiName, _permissionLevel);
[230]73 }
74 }
[325]75}
Note: See TracBrowser for help on using the repository browser.