source: binary-improvements2/WebServer/src/WebAPI/ExecuteConsoleCommand.cs@ 392

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

Major refactoring/cleanup

File size: 1.8 KB
Line 
1using System;
2using System.Net;
3using JetBrains.Annotations;
4
5namespace Webserver.WebAPI {
6 [UsedImplicitly]
7 public class ExecuteConsoleCommand : AbsWebAPI {
8 public override void HandleRequest (RequestContext _context) {
9 if (string.IsNullOrEmpty (_context.Request.QueryString ["command"])) {
10 WebUtils.WriteText (_context.Response, "No command given", HttpStatusCode.BadRequest);
11 return;
12 }
13
14 WebCommandResult.ResultType responseType = WebCommandResult.ResultType.Full;
15
16 string formatArg = _context.Request.QueryString ["format"];
17 if (formatArg != null) {
18 if (formatArg.EqualsCaseInsensitive ("raw")) {
19 responseType = WebCommandResult.ResultType.Raw;
20 } else if (formatArg.EqualsCaseInsensitive ("simple")) {
21 responseType = WebCommandResult.ResultType.ResultOnly;
22 }
23 }
24
25 string commandline = _context.Request.QueryString ["command"];
26 string commandPart = commandline.Split (' ') [0];
27 string argumentsPart = commandline.Substring (Math.Min (commandline.Length, commandPart.Length + 1));
28
29 IConsoleCommand command = SdtdConsole.Instance.GetCommand (commandline);
30
31 if (command == null) {
32 WebUtils.WriteText (_context.Response, "Unknown command", HttpStatusCode.NotFound);
33 return;
34 }
35
36 int commandPermissionLevel = GameManager.Instance.adminTools.GetCommandPermissionLevel (command.GetCommands ());
37
38 if (_context.PermissionLevel > commandPermissionLevel) {
39 WebUtils.WriteText (_context.Response, "You are not allowed to execute this command", HttpStatusCode.Forbidden);
40 return;
41 }
42
43 _context.Response.SendChunked = true;
44 WebCommandResult wcr = new WebCommandResult (commandPart, argumentsPart, responseType, _context.Response);
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.