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

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

Switched to use SpaceWizards.HttpListener

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