source: binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Web/ApiHandler.cs@ 154

Last change on this file since 154 was 154, checked in by alloc, 10 years ago

fixes

File size: 1.3 KB
Line 
1using AllocsFixes.NetConnections.Servers.Web.API;
2using System;
3using System.Collections.Generic;
4using System.IO;
5using System.Net;
6using System.Threading;
7
8namespace 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 ());
20 }
21
22 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, HttpListenerBasicIdentity user)
23 {
24 string apiName = req.Url.AbsolutePath.Remove (0, staticPart.Length);
25 if (!AuthorizeForCommand (apiName, user)) {
26 resp.StatusCode = (int)HttpStatusCode.Forbidden;
27 } else {
28 foreach (KeyValuePair<string, WebAPI> kvp in apis) {
29 if (apiName.StartsWith (kvp.Key)) {
30 kvp.Value.HandleRequest (req, resp, user);
31 return;
32 }
33 }
34 }
35
36 Log.Out ("Error in ApiHandler.HandleRequest(): No handler found for API \"" + apiName + "\"");
37 resp.StatusCode = (int)HttpStatusCode.NotFound;
38 }
39
40 private bool AuthorizeForCommand (string apiName, HttpListenerBasicIdentity user)
41 {
42 return true;
43 }
44
45 }
46
47}
48
Note: See TracBrowser for help on using the repository browser.