| 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 ("getlandclaims", new GetLandClaims ());
|
|---|
| 19 | apis.Add ("getplayersonline", new GetPlayersOnline ());
|
|---|
| 20 | apis.Add ("getplayerslocation", new GetPlayersLocation ());
|
|---|
| 21 | apis.Add ("getplayerinventory", new GetPlayerInventory ());
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, HttpListenerBasicIdentity user)
|
|---|
| 25 | {
|
|---|
| 26 | string apiName = req.Url.AbsolutePath.Remove (0, staticPart.Length);
|
|---|
| 27 | if (!AuthorizeForCommand (apiName, user)) {
|
|---|
| 28 | resp.StatusCode = (int)HttpStatusCode.Forbidden;
|
|---|
| 29 | } else {
|
|---|
| 30 | foreach (KeyValuePair<string, WebAPI> kvp in apis) {
|
|---|
| 31 | try {
|
|---|
| 32 | if (apiName.StartsWith (kvp.Key)) {
|
|---|
| 33 | kvp.Value.HandleRequest (req, resp, user);
|
|---|
| 34 | return;
|
|---|
| 35 | }
|
|---|
| 36 | } catch (Exception e) {
|
|---|
| 37 | Log.Out ("Error in ApiHandler.HandleRequest(): Handler threw an exception: " + e);
|
|---|
| 38 | resp.StatusCode = (int)HttpStatusCode.InternalServerError;
|
|---|
| 39 | return;
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | Log.Out ("Error in ApiHandler.HandleRequest(): No handler found for API \"" + apiName + "\"");
|
|---|
| 45 | resp.StatusCode = (int)HttpStatusCode.NotFound;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | private bool AuthorizeForCommand (string apiName, HttpListenerBasicIdentity user)
|
|---|
| 49 | {
|
|---|
| 50 | return true;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|