[391] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.IO;
|
---|
| 4 | using System.Net.Sockets;
|
---|
| 5 | using System.Text;
|
---|
| 6 | using UnityEngine;
|
---|
[402] | 7 | using Utf8Json;
|
---|
[391] | 8 |
|
---|
| 9 | namespace Webserver {
|
---|
| 10 | public class WebCommandResult : IConsoleConnection {
|
---|
| 11 | public enum ResultType {
|
---|
| 12 | Full,
|
---|
| 13 | ResultOnly,
|
---|
| 14 | Raw
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | private readonly string command;
|
---|
| 18 | private readonly string parameters;
|
---|
| 19 |
|
---|
[402] | 20 | private readonly RequestContext context;
|
---|
[391] | 21 | private readonly ResultType responseType;
|
---|
| 22 |
|
---|
[402] | 23 | public WebCommandResult (string _command, string _parameters, ResultType _responseType, RequestContext _context) {
|
---|
| 24 | context = _context;
|
---|
[391] | 25 | command = _command;
|
---|
| 26 | parameters = _parameters;
|
---|
| 27 | responseType = _responseType;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
[402] | 30 | private static readonly byte[] jsonRawKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("resultRaw");
|
---|
| 31 |
|
---|
| 32 | private static readonly byte[] jsonCommandKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("command");
|
---|
| 33 | private static readonly byte[] jsonParametersKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("parameters");
|
---|
| 34 | private static readonly byte[] jsonResultKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("result");
|
---|
| 35 |
|
---|
[391] | 36 | public void SendLines (List<string> _output) {
|
---|
| 37 | StringBuilder sb = new StringBuilder ();
|
---|
| 38 | foreach (string line in _output) {
|
---|
| 39 | sb.AppendLine (line);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[402] | 42 | string commandOutput = sb.ToString ();
|
---|
| 43 |
|
---|
[391] | 44 | try {
|
---|
| 45 | if (responseType == ResultType.Raw) {
|
---|
[402] | 46 | WebUtils.WriteText (context.Response, commandOutput);
|
---|
[391] | 47 | } else {
|
---|
[402] | 48 | WebUtils.PrepareEnvelopedResult (out JsonWriter writer);
|
---|
| 49 |
|
---|
[391] | 50 | if (responseType == ResultType.ResultOnly) {
|
---|
[402] | 51 | writer.WriteRaw (jsonRawKey);
|
---|
| 52 | writer.WriteString (commandOutput);
|
---|
| 53 | writer.WriteEndObject ();
|
---|
[391] | 54 | } else {
|
---|
[402] | 55 | writer.WriteRaw (jsonCommandKey);
|
---|
| 56 | writer.WriteString (command);
|
---|
| 57 |
|
---|
| 58 | writer.WriteRaw (jsonParametersKey);
|
---|
| 59 | writer.WriteString (parameters);
|
---|
| 60 |
|
---|
| 61 | writer.WriteRaw (jsonResultKey);
|
---|
| 62 | writer.WriteString (commandOutput);
|
---|
| 63 |
|
---|
| 64 | writer.WriteEndObject ();
|
---|
[391] | 65 | }
|
---|
| 66 |
|
---|
[402] | 67 | WebUtils.SendEnvelopedResult (context, ref writer);
|
---|
[391] | 68 | }
|
---|
| 69 | } catch (IOException e) {
|
---|
| 70 | if (e.InnerException is SocketException) {
|
---|
[402] | 71 | Log.Warning ($"[Web] Error in WebCommandResult.SendLines(): Remote host closed connection: {e.InnerException.Message}");
|
---|
[391] | 72 | } else {
|
---|
[402] | 73 | Log.Warning ($"[Web] Error (IO) in WebCommandResult.SendLines(): {e}");
|
---|
[391] | 74 | }
|
---|
| 75 | } catch (Exception e) {
|
---|
[402] | 76 | Log.Warning ($"[Web] Error in WebCommandResult.SendLines(): {e}");
|
---|
[391] | 77 | } finally {
|
---|
[402] | 78 | context?.Response?.Close ();
|
---|
[391] | 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | public void SendLine (string _text) {
|
---|
| 83 | //throw new NotImplementedException ();
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | public void SendLog (string _formattedMessage, string _plainMessage, string _trace, LogType _type, DateTime _timestamp, long _uptime) {
|
---|
| 87 | //throw new NotImplementedException ();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | public void EnableLogLevel (LogType _type, bool _enable) {
|
---|
| 91 | //throw new NotImplementedException ();
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | public string GetDescription () {
|
---|
[402] | 95 | return $"WebCommandResult_for_{command}";
|
---|
[391] | 96 | }
|
---|
| 97 | }
|
---|
| 98 | }
|
---|