[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 |
|
---|
[251] | 13 | public ApiHandler (string staticPart, string moduleName = null) : base (moduleName) {
|
---|
[230] | 14 | this.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 |
|
---|
[325] | 47 | public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user,
|
---|
| 48 | int permissionLevel) {
|
---|
[230] | 49 | string apiName = req.Url.AbsolutePath.Remove (0, staticPart.Length);
|
---|
[244] | 50 | if (!AuthorizeForCommand (apiName, user, permissionLevel)) {
|
---|
[325] | 51 | resp.StatusCode = (int) HttpStatusCode.Forbidden;
|
---|
[244] | 52 | if (user != null) {
|
---|
[247] | 53 | //Log.Out ("ApiHandler: user '{0}' not allowed to execute '{1}'", user.SteamID, apiName);
|
---|
[244] | 54 | }
|
---|
[325] | 55 |
|
---|
[244] | 56 | return;
|
---|
[325] | 57 | }
|
---|
| 58 |
|
---|
[326] | 59 | WebAPI api;
|
---|
| 60 | if (apis.TryGetValue (apiName, out api)) {
|
---|
| 61 | try {
|
---|
[332] | 62 | #if ENABLE_PROFILER
|
---|
| 63 | apiHandlerSampler.Begin ();
|
---|
| 64 | #endif
|
---|
[326] | 65 | api.HandleRequest (req, resp, user, permissionLevel);
|
---|
[332] | 66 | #if ENABLE_PROFILER
|
---|
| 67 | apiHandlerSampler.End ();
|
---|
| 68 | #endif
|
---|
[326] | 69 | return;
|
---|
| 70 | } catch (Exception e) {
|
---|
| 71 | Log.Error ("Error in ApiHandler.HandleRequest(): Handler {0} threw an exception:", api.Name);
|
---|
| 72 | Log.Exception (e);
|
---|
| 73 | resp.StatusCode = (int) HttpStatusCode.InternalServerError;
|
---|
| 74 | return;
|
---|
[230] | 75 | }
|
---|
| 76 | }
|
---|
[326] | 77 |
|
---|
[230] | 78 | Log.Out ("Error in ApiHandler.HandleRequest(): No handler found for API \"" + apiName + "\"");
|
---|
[325] | 79 | resp.StatusCode = (int) HttpStatusCode.NotFound;
|
---|
[230] | 80 | }
|
---|
| 81 |
|
---|
[244] | 82 | private bool AuthorizeForCommand (string apiName, WebConnection user, int permissionLevel) {
|
---|
| 83 | return WebPermissions.Instance.ModuleAllowedWithLevel ("webapi." + apiName, permissionLevel);
|
---|
[230] | 84 | }
|
---|
| 85 | }
|
---|
[325] | 86 | }
|
---|