[230] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Net;
|
---|
[250] | 4 | using System.Reflection;
|
---|
[325] | 5 | using AllocsFixes.NetConnections.Servers.Web.API;
|
---|
[382] | 6 | using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
|
---|
| 7 | using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
|
---|
[230] | 8 |
|
---|
[325] | 9 | namespace AllocsFixes.NetConnections.Servers.Web.Handlers {
|
---|
[382] | 10 | public class ApiHandler : AbsHandler {
|
---|
[326] | 11 | private readonly Dictionary<string, WebAPI> apis = new CaseInsensitiveStringDictionary<WebAPI> ();
|
---|
[230] | 12 |
|
---|
[384] | 13 | public ApiHandler () : base (null) {
|
---|
[250] | 14 |
|
---|
[384] | 15 | }
|
---|
| 16 |
|
---|
| 17 | public override void SetBasePathAndParent (Web _parent, string _relativePath) {
|
---|
| 18 | base.SetBasePathAndParent (_parent, _relativePath);
|
---|
| 19 |
|
---|
| 20 | Type[] apiWithParentCtorTypes = { typeof (Web) };
|
---|
| 21 | Object[] apiWithParentCtorArgs = { _parent };
|
---|
| 22 |
|
---|
| 23 | Type[] apiEmptyCtorTypes = { };
|
---|
| 24 | Object[] apiEmptyCtorArgs = { };
|
---|
| 25 |
|
---|
[250] | 26 | foreach (Type t in Assembly.GetExecutingAssembly ().GetTypes ()) {
|
---|
[325] | 27 | if (!t.IsAbstract && t.IsSubclassOf (typeof (WebAPI))) {
|
---|
[384] | 28 | ConstructorInfo ctor = t.GetConstructor (apiWithParentCtorTypes);
|
---|
[250] | 29 | if (ctor != null) {
|
---|
[384] | 30 | WebAPI apiInstance = (WebAPI) ctor.Invoke (apiWithParentCtorArgs);
|
---|
[367] | 31 | addApi (apiInstance);
|
---|
[384] | 32 | continue;
|
---|
[250] | 33 | }
|
---|
[384] | 34 |
|
---|
| 35 | ctor = t.GetConstructor (apiEmptyCtorTypes);
|
---|
| 36 | if (ctor != null) {
|
---|
| 37 | WebAPI apiInstance = (WebAPI) ctor.Invoke (apiEmptyCtorArgs);
|
---|
| 38 | addApi (apiInstance);
|
---|
| 39 | }
|
---|
[250] | 40 | }
|
---|
[325] | 41 | }
|
---|
| 42 |
|
---|
[367] | 43 | // Permissions that don't map to a real API
|
---|
| 44 | addApi (new Null ("viewallclaims"));
|
---|
| 45 | addApi (new Null ("viewallplayers"));
|
---|
[230] | 46 | }
|
---|
| 47 |
|
---|
[367] | 48 | private void addApi (WebAPI _api) {
|
---|
| 49 | apis.Add (_api.Name, _api);
|
---|
| 50 | WebPermissions.Instance.AddKnownModule ("webapi." + _api.Name, _api.DefaultPermissionLevel ());
|
---|
[244] | 51 | }
|
---|
| 52 |
|
---|
[332] | 53 | #if ENABLE_PROFILER
|
---|
[367] | 54 | private static readonly UnityEngine.Profiling.CustomSampler apiHandlerSampler = UnityEngine.Profiling.CustomSampler.Create ("API_Handler");
|
---|
[332] | 55 | #endif
|
---|
| 56 |
|
---|
[382] | 57 | public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con,
|
---|
[351] | 58 | int _permissionLevel) {
|
---|
[382] | 59 | string apiName = _requestPath.Remove (0, urlBasePath.Length);
|
---|
[351] | 60 |
|
---|
[367] | 61 | if (!apis.TryGetValue (apiName, out WebAPI api)) {
|
---|
| 62 | Log.Out ($"Error in {nameof(ApiHandler)}.HandleRequest(): No handler found for API \"{apiName}\"");
|
---|
[351] | 63 | _resp.StatusCode = (int) HttpStatusCode.NotFound;
|
---|
| 64 | return;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[367] | 67 | if (!AuthorizeForApi (apiName, _permissionLevel)) {
|
---|
[351] | 68 | _resp.StatusCode = (int) HttpStatusCode.Forbidden;
|
---|
[382] | 69 | if (_con != null) {
|
---|
[367] | 70 | //Log.Out ($"{nameof(ApiHandler)}: user '{user.SteamID}' not allowed to execute '{apiName}'");
|
---|
[244] | 71 | }
|
---|
[325] | 72 |
|
---|
[244] | 73 | return;
|
---|
[325] | 74 | }
|
---|
| 75 |
|
---|
[351] | 76 | try {
|
---|
[332] | 77 | #if ENABLE_PROFILER
|
---|
[351] | 78 | apiHandlerSampler.Begin ();
|
---|
[332] | 79 | #endif
|
---|
[382] | 80 | api.HandleRequest (_req, _resp, _con, _permissionLevel);
|
---|
[332] | 81 | #if ENABLE_PROFILER
|
---|
[351] | 82 | apiHandlerSampler.End ();
|
---|
[332] | 83 | #endif
|
---|
[351] | 84 | } catch (Exception e) {
|
---|
[367] | 85 | Log.Error ($"Error in {nameof(ApiHandler)}.HandleRequest(): Handler {api.Name} threw an exception:");
|
---|
[351] | 86 | Log.Exception (e);
|
---|
| 87 | _resp.StatusCode = (int) HttpStatusCode.InternalServerError;
|
---|
[230] | 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[367] | 91 | private bool AuthorizeForApi (string _apiName, int _permissionLevel) {
|
---|
[351] | 92 | return WebPermissions.Instance.ModuleAllowedWithLevel ("webapi." + _apiName, _permissionLevel);
|
---|
[230] | 93 | }
|
---|
| 94 | }
|
---|
[325] | 95 | }
|
---|