source: TFP-WebServer/WebServer/src/UrlHandlers/ApiHandler.cs@ 453

Last change on this file since 453 was 453, checked in by alloc, 16 months ago

21.1.9 release, updated Sessions handler to be more flexible

File size: 3.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Net;
4using System.Reflection;
5using Webserver.WebAPI;
6
7namespace Webserver.UrlHandlers {
8 public class ApiHandler : AbsHandler {
9 private readonly Dictionary<string, AbsWebAPI> apis = new CaseInsensitiveStringDictionary<AbsWebAPI> ();
10
11 public ApiHandler () : base (null) {
12 }
13
14 private static readonly Type[] apiWithParentCtorTypes = { typeof (Web) };
15 private static readonly object[] apiWithParentCtorArgs = new object[1];
16 private static readonly Type[] apiEmptyCtorTypes = { };
17 private static readonly object[] apiEmptyCtorArgs = { };
18
19 public override void SetBasePathAndParent (Web _parent, string _relativePath) {
20 base.SetBasePathAndParent (_parent, _relativePath);
21
22 apiWithParentCtorArgs[0] = _parent;
23
24 ReflectionHelpers.FindTypesImplementingBase (typeof (AbsWebAPI), apiFoundCallback);
25
26 // Permissions that don't map to a real API
27 addApi (new Null ("viewallclaims"));
28 addApi (new Null ("viewallplayers"));
29 }
30
31 private void apiFoundCallback (Type _type) {
32 ConstructorInfo ctor = _type.GetConstructor (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, (Binder)null,
33 apiWithParentCtorTypes, (ParameterModifier[])null);
34 if (ctor != null) {
35 AbsWebAPI apiInstance = (AbsWebAPI)ctor.Invoke (apiWithParentCtorArgs);
36 addApi (apiInstance);
37 return;
38 }
39
40 ctor = _type.GetConstructor (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, (Binder)null,
41 apiEmptyCtorTypes, (ParameterModifier[])null);
42 if (ctor != null) {
43 AbsWebAPI apiInstance = (AbsWebAPI)ctor.Invoke (apiEmptyCtorArgs);
44 addApi (apiInstance);
45 }
46 }
47
48 private void addApi (AbsWebAPI _api) {
49 apis.Add (_api.Name, _api);
50 }
51
52 private static readonly UnityEngine.Profiling.CustomSampler apiHandlerSampler = UnityEngine.Profiling.CustomSampler.Create ("API_Handler");
53
54 public override void HandleRequest (RequestContext _context) {
55
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)) {
68 Log.Warning ($"[Web] In {nameof(ApiHandler)}.HandleRequest(): No handler found for API \"{apiName}\"");
69 _context.Response.StatusCode = (int) HttpStatusCode.NotFound;
70 return;
71 }
72
73 _context.RequestPath = subPath;
74
75 if (!api.Authorized (_context)) {
76 _context.Response.StatusCode = (int) HttpStatusCode.Forbidden;
77 if (_context.Connection != null) {
78 //Log.Out ($"{nameof(ApiHandler)}: user '{user.SteamID}' not allowed to execute '{apiName}'");
79 }
80
81 return;
82 }
83
84 try {
85 apiHandlerSampler.Begin ();
86 api.HandleRequest (_context);
87 apiHandlerSampler.End ();
88 } catch (Exception e) {
89 Log.Error ($"[Web] In {nameof(ApiHandler)}.HandleRequest(): Handler {api.Name} threw an exception:");
90 Log.Exception (e);
91 _context.Response.StatusCode = (int) HttpStatusCode.InternalServerError;
92 }
93 }
94 }
95}
Note: See TracBrowser for help on using the repository browser.