[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;
|
---|
[332] | 6 | using UnityEngine.Profiling;
|
---|
[230] | 7 |
|
---|
[325] | 8 | namespace AllocsFixes.NetConnections.Servers.Web.Handlers {
|
---|
[244] | 9 | public class ApiHandler : PathHandler {
|
---|
[326] | 10 | private readonly Dictionary<string, WebAPI> apis = new CaseInsensitiveStringDictionary<WebAPI> ();
|
---|
[325] | 11 | private readonly string staticPart;
|
---|
[230] | 12 |
|
---|
[351] | 13 | public ApiHandler (string _staticPart, string _moduleName = null) : base (_moduleName) {
|
---|
| 14 | staticPart = _staticPart;
|
---|
[250] | 15 |
|
---|
| 16 | foreach (Type t in Assembly.GetExecutingAssembly ().GetTypes ()) {
|
---|
[325] | 17 | if (!t.IsAbstract && t.IsSubclassOf (typeof (WebAPI))) {
|
---|
[251] | 18 | ConstructorInfo ctor = t.GetConstructor (new Type [0]);
|
---|
[250] | 19 | if (ctor != null) {
|
---|
[325] | 20 | WebAPI apiInstance = (WebAPI) ctor.Invoke (new object [0]);
|
---|
[326] | 21 | addApi (apiInstance.Name, apiInstance);
|
---|
[250] | 22 | }
|
---|
| 23 | }
|
---|
[325] | 24 | }
|
---|
| 25 |
|
---|
| 26 | // Add dummy types
|
---|
| 27 | Type dummy_t = typeof (Null);
|
---|
| 28 | ConstructorInfo dummy_ctor = dummy_t.GetConstructor (new Type [0]);
|
---|
| 29 | if (dummy_ctor != null) {
|
---|
| 30 | WebAPI dummy_apiInstance = (WebAPI) dummy_ctor.Invoke (new object[0]);
|
---|
| 31 |
|
---|
| 32 | // Permissions that don't map to a real API
|
---|
| 33 | addApi ("viewallclaims", dummy_apiInstance);
|
---|
| 34 | addApi ("viewallplayers", dummy_apiInstance);
|
---|
| 35 | }
|
---|
[230] | 36 | }
|
---|
| 37 |
|
---|
[244] | 38 | private void addApi (string _apiName, WebAPI _api) {
|
---|
| 39 | apis.Add (_apiName, _api);
|
---|
[279] | 40 | WebPermissions.Instance.AddKnownModule ("webapi." + _apiName, _api.DefaultPermissionLevel ());
|
---|
[244] | 41 | }
|
---|
| 42 |
|
---|
[332] | 43 | #if ENABLE_PROFILER
|
---|
| 44 | private static readonly CustomSampler apiHandlerSampler = CustomSampler.Create ("API_Handler");
|
---|
| 45 | #endif
|
---|
| 46 |
|
---|
[351] | 47 | public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
|
---|
| 48 | int _permissionLevel) {
|
---|
| 49 | string apiName = _req.Url.AbsolutePath.Remove (0, staticPart.Length);
|
---|
| 50 |
|
---|
| 51 | WebAPI api;
|
---|
| 52 | if (!apis.TryGetValue (apiName, out api)) {
|
---|
| 53 | Log.Out ("Error in ApiHandler.HandleRequest(): No handler found for API \"" + apiName + "\"");
|
---|
| 54 | _resp.StatusCode = (int) HttpStatusCode.NotFound;
|
---|
| 55 | return;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | if (!AuthorizeForCommand (apiName, _user, _permissionLevel)) {
|
---|
| 59 | _resp.StatusCode = (int) HttpStatusCode.Forbidden;
|
---|
| 60 | if (_user != null) {
|
---|
[247] | 61 | //Log.Out ("ApiHandler: user '{0}' not allowed to execute '{1}'", user.SteamID, apiName);
|
---|
[244] | 62 | }
|
---|
[325] | 63 |
|
---|
[244] | 64 | return;
|
---|
[325] | 65 | }
|
---|
| 66 |
|
---|
[351] | 67 | try {
|
---|
[332] | 68 | #if ENABLE_PROFILER
|
---|
[351] | 69 | apiHandlerSampler.Begin ();
|
---|
[332] | 70 | #endif
|
---|
[351] | 71 | api.HandleRequest (_req, _resp, _user, _permissionLevel);
|
---|
[332] | 72 | #if ENABLE_PROFILER
|
---|
[351] | 73 | apiHandlerSampler.End ();
|
---|
[332] | 74 | #endif
|
---|
[351] | 75 | } catch (Exception e) {
|
---|
| 76 | Log.Error ("Error in ApiHandler.HandleRequest(): Handler {0} threw an exception:", api.Name);
|
---|
| 77 | Log.Exception (e);
|
---|
| 78 | _resp.StatusCode = (int) HttpStatusCode.InternalServerError;
|
---|
[230] | 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[351] | 82 | private bool AuthorizeForCommand (string _apiName, WebConnection _user, int _permissionLevel) {
|
---|
| 83 | return WebPermissions.Instance.ModuleAllowedWithLevel ("webapi." + _apiName, _permissionLevel);
|
---|
[230] | 84 | }
|
---|
| 85 | }
|
---|
[325] | 86 | }
|
---|