source: binary-improvements/MapRendering/Web/Handlers/ApiHandler.cs@ 246

Last change on this file since 246 was 244, checked in by alloc, 9 years ago

Fixes intermediate state

File size: 2.2 KB
RevLine 
[230]1using AllocsFixes.NetConnections.Servers.Web.API;
2using System;
3using System.Collections.Generic;
4using System.IO;
5using System.Net;
6using System.Threading;
7
[244]8namespace AllocsFixes.NetConnections.Servers.Web.Handlers
[230]9{
[244]10 public class ApiHandler : PathHandler {
[230]11 private string staticPart;
12 private Dictionary<String, WebAPI> apis = new Dictionary<string, WebAPI> ();
13
[244]14 public ApiHandler (string staticPart, string moduleName = null) : base(moduleName) {
[230]15 this.staticPart = staticPart;
[244]16 addApi ("getlandclaims", new GetLandClaims ());
17 addApi ("getplayersonline", new GetPlayersOnline ());
18 addApi ("getplayerslocation", new GetPlayersLocation ());
19 addApi ("getplayerinventory", new GetPlayerInventory ());
20 addApi ("getstats", new GetStats ());
[230]21 }
22
[244]23 private void addApi (string _apiName, WebAPI _api) {
24 apis.Add (_apiName, _api);
25 WebPermissions.Instance.AddKnownModule ("webapi." + _apiName);
26 }
27
28 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel) {
[230]29 string apiName = req.Url.AbsolutePath.Remove (0, staticPart.Length);
[244]30 if (!AuthorizeForCommand (apiName, user, permissionLevel)) {
[230]31 resp.StatusCode = (int)HttpStatusCode.Forbidden;
[244]32 if (user != null) {
33 Log.Out ("ApiHandler: user '{0}' not allowed to execute '{1}'", user.SteamID, apiName);
34 } else {
35 Log.Out ("ApiHandler: unidentified user from '{0}' not allowed to execute '{1}'", req.RemoteEndPoint.Address, apiName);
36 }
37 return;
[230]38 } else {
39 foreach (KeyValuePair<string, WebAPI> kvp in apis) {
40 try {
41 if (apiName.StartsWith (kvp.Key)) {
[244]42 kvp.Value.HandleRequest (req, resp, user, permissionLevel);
[230]43 return;
44 }
45 } catch (Exception e) {
46 Log.Out ("Error in ApiHandler.HandleRequest(): Handler threw an exception: " + e);
47 resp.StatusCode = (int)HttpStatusCode.InternalServerError;
48 return;
49 }
50 }
51 }
52
53 Log.Out ("Error in ApiHandler.HandleRequest(): No handler found for API \"" + apiName + "\"");
54 resp.StatusCode = (int)HttpStatusCode.NotFound;
55 }
56
[244]57 private bool AuthorizeForCommand (string apiName, WebConnection user, int permissionLevel) {
58 return WebPermissions.Instance.ModuleAllowedWithLevel ("webapi." + apiName, permissionLevel);
[230]59 }
60
61 }
62
63}
64
Note: See TracBrowser for help on using the repository browser.