Files
duplicati/Duplicati/Server/WebServer/BodyWriter.cs
T

73 lines
2.3 KiB
C#

// Copyright (C) 2014, Kenneth Skovhede
// http://www.hexad.dk, opensource@hexad.dk
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// 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 Duplicati.Server.Serialization;
namespace Duplicati.Server.WebServer
{
internal class BodyWriter : System.IO.StreamWriter, IDisposable
{
private HttpServer.IHttpResponse m_resp;
private static 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; }
}
public BodyWriter(HttpServer.IHttpResponse resp)
: base(resp.Body, resp.Encoding)
{
m_resp = resp;
}
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";
m_resp.Status = System.Net.HttpStatusCode.OK;
}
public void OutputOK(object result = null)
{
SetOK();
WriteJsonObject(result ?? SUCCESS_RESPONSE);
}
public void WriteJsonObject(object o)
{
if (!m_resp.HeadersSent)
m_resp.ContentType = "application/json";
Serializer.SerializeJson(this, o);
}
}
}