[154] | 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.Threading;
|
---|
| 7 |
|
---|
| 8 | namespace AllocsFixes.NetConnections.Servers.Web
|
---|
| 9 | {
|
---|
| 10 | public class ApiHandler : PathHandler
|
---|
| 11 | {
|
---|
| 12 | private string staticPart;
|
---|
| 13 | private Dictionary<String, WebAPI> apis = new Dictionary<string, WebAPI> ();
|
---|
| 14 |
|
---|
| 15 | public ApiHandler (string staticPart)
|
---|
| 16 | {
|
---|
| 17 | this.staticPart = staticPart;
|
---|
| 18 | apis.Add ("getplayersonline", new GetPlayersOnline ());
|
---|
| 19 | apis.Add ("getplayerslocation", new GetPlayersLocation ());
|
---|
[163] | 20 | apis.Add ("getplayerinventory", new GetPlayerInventory ());
|
---|
[154] | 21 | }
|
---|
| 22 |
|
---|
| 23 | public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, HttpListenerBasicIdentity user)
|
---|
| 24 | {
|
---|
| 25 | string apiName = req.Url.AbsolutePath.Remove (0, staticPart.Length);
|
---|
| 26 | if (!AuthorizeForCommand (apiName, user)) {
|
---|
| 27 | resp.StatusCode = (int)HttpStatusCode.Forbidden;
|
---|
| 28 | } else {
|
---|
[158] | 29 | foreach (KeyValuePair<string, WebAPI> kvp in apis) {
|
---|
| 30 | try {
|
---|
[156] | 31 | if (apiName.StartsWith (kvp.Key)) {
|
---|
| 32 | kvp.Value.HandleRequest (req, resp, user);
|
---|
| 33 | return;
|
---|
| 34 | }
|
---|
[158] | 35 | } catch (Exception e) {
|
---|
| 36 | Log.Out ("Error in ApiHandler.HandleRequest(): Handler threw an exception: " + e);
|
---|
| 37 | resp.StatusCode = (int)HttpStatusCode.InternalServerError;
|
---|
| 38 | return;
|
---|
[154] | 39 | }
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | Log.Out ("Error in ApiHandler.HandleRequest(): No handler found for API \"" + apiName + "\"");
|
---|
| 44 | resp.StatusCode = (int)HttpStatusCode.NotFound;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | private bool AuthorizeForCommand (string apiName, HttpListenerBasicIdentity user)
|
---|
| 48 | {
|
---|
| 49 | return true;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | }
|
---|
| 55 |
|
---|