[391] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Net;
|
---|
| 4 | using System.Reflection;
|
---|
| 5 | using Webserver.WebAPI;
|
---|
| 6 |
|
---|
| 7 | namespace Webserver.UrlHandlers {
|
---|
| 8 | public class ApiHandler : AbsHandler {
|
---|
| 9 | private readonly Dictionary<string, AbsWebAPI> apis = new CaseInsensitiveStringDictionary<AbsWebAPI> ();
|
---|
| 10 |
|
---|
| 11 | public ApiHandler () : base (null) {
|
---|
| 12 | }
|
---|
[404] | 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 = { };
|
---|
[391] | 18 |
|
---|
| 19 | public override void SetBasePathAndParent (Web _parent, string _relativePath) {
|
---|
| 20 | base.SetBasePathAndParent (_parent, _relativePath);
|
---|
| 21 |
|
---|
[404] | 22 | apiWithParentCtorArgs[0] = _parent;
|
---|
[391] | 23 |
|
---|
[404] | 24 | ReflectionHelpers.FindTypesImplementingBase (typeof (AbsWebAPI), apiFoundCallback);
|
---|
[391] | 25 |
|
---|
| 26 | // Permissions that don't map to a real API
|
---|
| 27 | addApi (new Null ("viewallclaims"));
|
---|
| 28 | addApi (new Null ("viewallplayers"));
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[404] | 31 | private void apiFoundCallback (Type _type) {
|
---|
[453] | 32 | ConstructorInfo ctor = _type.GetConstructor (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, (Binder)null,
|
---|
| 33 | apiWithParentCtorTypes, (ParameterModifier[])null);
|
---|
[404] | 34 | if (ctor != null) {
|
---|
| 35 | AbsWebAPI apiInstance = (AbsWebAPI)ctor.Invoke (apiWithParentCtorArgs);
|
---|
| 36 | addApi (apiInstance);
|
---|
| 37 | return;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[453] | 40 | ctor = _type.GetConstructor (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, (Binder)null,
|
---|
| 41 | apiEmptyCtorTypes, (ParameterModifier[])null);
|
---|
[404] | 42 | if (ctor != null) {
|
---|
| 43 | AbsWebAPI apiInstance = (AbsWebAPI)ctor.Invoke (apiEmptyCtorArgs);
|
---|
| 44 | addApi (apiInstance);
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[391] | 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)) {
|
---|
[399] | 68 | Log.Warning ($"[Web] In {nameof(ApiHandler)}.HandleRequest(): No handler found for API \"{apiName}\"");
|
---|
[391] | 69 | _context.Response.StatusCode = (int) HttpStatusCode.NotFound;
|
---|
| 70 | return;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[418] | 73 | _context.RequestPath = subPath;
|
---|
| 74 |
|
---|
| 75 | if (!api.Authorized (_context)) {
|
---|
[391] | 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) {
|
---|
[399] | 89 | Log.Error ($"[Web] In {nameof(ApiHandler)}.HandleRequest(): Handler {api.Name} threw an exception:");
|
---|
[391] | 90 | Log.Exception (e);
|
---|
| 91 | _context.Response.StatusCode = (int) HttpStatusCode.InternalServerError;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|