More services working (licenses, systeminfo, serverstate)

This commit is contained in:
Thomas Suckow
2023-03-13 20:07:49 -07:00
parent c318c43002
commit 006af8fd78
5 changed files with 108 additions and 49 deletions
@@ -16,21 +16,18 @@
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Globalization;
using System.Threading.Tasks;
using Duplicati.Server.Serialization;
namespace Duplicati.Server.WebServer
{
public class BodyWriter : System.IO.StreamWriter, IDisposable
public class BodyWriter : IDisposable, IAsyncDisposable
{
private readonly HttpServer.IHttpResponse m_resp;
private readonly string m_jsonp;
private static readonly object SUCCESS_RESPONSE = new { Status = "OK" };
// We override the format provider so all JSON output uses US format
public override IFormatProvider FormatProvider
{
get { return System.Globalization.CultureInfo.InvariantCulture; }
}
private readonly System.IO.StreamWriter m_bodyStreamWriter;
public BodyWriter(HttpServer.IHttpResponse resp, HttpServer.IHttpRequest request)
: this(resp, request.QueryString["jsonp"].Value)
@@ -38,25 +35,13 @@ namespace Duplicati.Server.WebServer
}
public BodyWriter(HttpServer.IHttpResponse resp, string jsonp)
: base(resp.Body, resp.Encoding)
{
m_bodyStreamWriter = new System.IO.StreamWriter(resp.Body, resp.Encoding);
m_resp = resp;
m_jsonp = jsonp;
if (!m_resp.HeadersSent)
m_resp.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0");
}
protected override void Dispose (bool disposing)
{
if (!m_resp.HeadersSent)
{
base.Flush();
m_resp.ContentLength = base.BaseStream.Length;
m_resp.Send();
}
base.Dispose(disposing);
}
public void SetOK()
{
m_resp.Reason = "OK";
@@ -74,22 +59,61 @@ namespace Duplicati.Server.WebServer
if (!m_resp.HeadersSent)
m_resp.ContentType = "application/json";
using(this)
Task.Run(async () => {
if (!string.IsNullOrEmpty(m_jsonp))
{
await m_bodyStreamWriter.WriteAsync(m_jsonp);
await m_bodyStreamWriter.WriteAsync('(');
}
var oldCulture = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
await Serializer.SerializeJsonAsync(m_bodyStreamWriter, o, true);
CultureInfo.CurrentCulture = oldCulture;
if (!string.IsNullOrEmpty(m_jsonp))
{
await m_bodyStreamWriter.WriteAsync(')');
await m_bodyStreamWriter.FlushAsync();
}
}).GetAwaiter().GetResult();
}
public void Dispose()
{
Task.Run(async () => {
await DisposeAsync();
}).GetAwaiter().GetResult();
}
private bool disposed = false;
public async ValueTask DisposeAsync()
{
if(disposed) return;
disposed = true;
if (!m_resp.HeadersSent)
{
if (!string.IsNullOrEmpty(m_jsonp))
{
this.Write(m_jsonp);
this.Write('(');
}
Serializer.SerializeJson(this, o, true);
if (!string.IsNullOrEmpty(m_jsonp))
{
this.Write(')');
this.Flush();
}
await m_bodyStreamWriter.FlushAsync();
m_resp.ContentLength = m_bodyStreamWriter.BaseStream.Length;
m_resp.Send();
}
await m_bodyStreamWriter.DisposeAsync();
}
internal void Flush()
{
Task.Run(async () => {
await m_bodyStreamWriter.FlushAsync();
}).GetAwaiter().GetResult();
}
internal void Write(string v)
{
Task.Run(async () => {
await m_bodyStreamWriter.WriteAsync(v);
}).GetAwaiter().GetResult();
}
}