1 | using AllocsFixes.NetConnections.Servers.Web.API;
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.IO;
|
---|
5 | using System.Net;
|
---|
6 | using System.Reflection;
|
---|
7 | using System.Threading;
|
---|
8 |
|
---|
9 | namespace AllocsFixes.NetConnections.Servers.Web.Handlers
|
---|
10 | {
|
---|
11 | public class ApiHandler : PathHandler {
|
---|
12 | private string staticPart;
|
---|
13 | private Dictionary<String, WebAPI> apis = new Dictionary<string, WebAPI> ();
|
---|
14 |
|
---|
15 | public ApiHandler (string staticPart, string moduleName = null) : base(moduleName) {
|
---|
16 | this.staticPart = staticPart;
|
---|
17 |
|
---|
18 | foreach (Type t in Assembly.GetExecutingAssembly ().GetTypes ()) {
|
---|
19 | if (!t.IsAbstract && t.IsSubclassOf (typeof(WebAPI))) {
|
---|
20 | ConstructorInfo ctor = t.GetConstructor (new Type[0]);
|
---|
21 | if (ctor != null) {
|
---|
22 | WebAPI apiInstance = (WebAPI)ctor.Invoke (new object[0]);
|
---|
23 | addApi (t.Name.ToLower (), apiInstance);
|
---|
24 | }
|
---|
25 | }
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | private void addApi (string _apiName, WebAPI _api) {
|
---|
30 | apis.Add (_apiName, _api);
|
---|
31 | WebPermissions.Instance.AddKnownModule ("webapi." + _apiName);
|
---|
32 | }
|
---|
33 |
|
---|
34 | public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel) {
|
---|
35 | string apiName = req.Url.AbsolutePath.Remove (0, staticPart.Length);
|
---|
36 | if (!AuthorizeForCommand (apiName, user, permissionLevel)) {
|
---|
37 | resp.StatusCode = (int)HttpStatusCode.Forbidden;
|
---|
38 | if (user != null) {
|
---|
39 | //Log.Out ("ApiHandler: user '{0}' not allowed to execute '{1}'", user.SteamID, apiName);
|
---|
40 | } else {
|
---|
41 | //Log.Out ("ApiHandler: unidentified user from '{0}' not allowed to execute '{1}'", req.RemoteEndPoint.Address, apiName);
|
---|
42 | }
|
---|
43 | return;
|
---|
44 | } else {
|
---|
45 | foreach (KeyValuePair<string, WebAPI> kvp in apis) {
|
---|
46 | if (apiName.StartsWith (kvp.Key)) {
|
---|
47 | try {
|
---|
48 | kvp.Value.HandleRequest (req, resp, user, permissionLevel);
|
---|
49 | return;
|
---|
50 | } catch (Exception e) {
|
---|
51 | Log.Error ("Error in ApiHandler.HandleRequest(): Handler {0} threw an exception:", kvp.Key);
|
---|
52 | Log.Exception (e);
|
---|
53 | resp.StatusCode = (int)HttpStatusCode.InternalServerError;
|
---|
54 | return;
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | Log.Out ("Error in ApiHandler.HandleRequest(): No handler found for API \"" + apiName + "\"");
|
---|
61 | resp.StatusCode = (int)HttpStatusCode.NotFound;
|
---|
62 | }
|
---|
63 |
|
---|
64 | private bool AuthorizeForCommand (string apiName, WebConnection user, int permissionLevel) {
|
---|
65 | return WebPermissions.Instance.ModuleAllowedWithLevel ("webapi." + apiName, permissionLevel);
|
---|
66 | }
|
---|
67 |
|
---|
68 | }
|
---|
69 |
|
---|
70 | }
|
---|
71 |
|
---|