source: binary-improvements2/MapRendering/Web/API/ExecuteConsoleCommand.cs@ 382

Last change on this file since 382 was 382, checked in by alloc, 2 years ago

Switched to use SpaceWizards.HttpListener

File size: 1.9 KB
Line 
1using System;
2using System.Net;
3using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
4using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
5
6namespace AllocsFixes.NetConnections.Servers.Web.API {
7 public class ExecuteConsoleCommand : WebAPI {
8 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
9 int _permissionLevel) {
10 if (string.IsNullOrEmpty (_req.QueryString ["command"])) {
11 _resp.StatusCode = (int) HttpStatusCode.BadRequest;
12 Web.SetResponseTextContent (_resp, "No command given");
13 return;
14 }
15
16 WebCommandResult.ResultType responseType =
17 _req.QueryString ["raw"] != null
18 ? WebCommandResult.ResultType.Raw
19 : (_req.QueryString ["simple"] != null
20 ? WebCommandResult.ResultType.ResultOnly
21 : WebCommandResult.ResultType.Full);
22
23 string commandline = _req.QueryString ["command"];
24 string commandPart = commandline.Split (' ') [0];
25 string argumentsPart = commandline.Substring (Math.Min (commandline.Length, commandPart.Length + 1));
26
27 IConsoleCommand command = SdtdConsole.Instance.GetCommand (commandline);
28
29 if (command == null) {
30 _resp.StatusCode = (int) HttpStatusCode.NotFound;
31 Web.SetResponseTextContent (_resp, "Unknown command");
32 return;
33 }
34
35 int commandPermissionLevel = GameManager.Instance.adminTools.GetCommandPermissionLevel (command.GetCommands ());
36
37 if (_permissionLevel > commandPermissionLevel) {
38 _resp.StatusCode = (int) HttpStatusCode.Forbidden;
39 Web.SetResponseTextContent (_resp, "You are not allowed to execute this command");
40 return;
41 }
42
43 _resp.SendChunked = true;
44 WebCommandResult wcr = new WebCommandResult (commandPart, argumentsPart, responseType, _resp);
45 SdtdConsole.Instance.ExecuteAsync (commandline, wcr);
46 }
47
48 public override int DefaultPermissionLevel () {
49 return 2000;
50 }
51 }
52}
Note: See TracBrowser for help on using the repository browser.