using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.IO.Compression;
namespace MediaBrowser.Common.Net.Handlers
{
public abstract class BaseHandler
{
///
/// Response headers
///
public IDictionary Headers = new Dictionary();
///
/// The action to write the response to the output stream
///
public Action WriteStream { get; set; }
///
/// The original RequestContext
///
public RequestContext RequestContext { get; set; }
///
/// The original QueryString
///
protected NameValueCollection QueryString
{
get
{
return RequestContext.Request.QueryString;
}
}
///
/// Gets the MIME type to include in the response headers
///
public abstract string ContentType { get; }
///
/// Gets the status code to include in the response headers
///
public virtual int StatusCode
{
get
{
return 200;
}
}
///
/// Gets the cache duration to include in the response headers
///
public virtual TimeSpan CacheDuration
{
get
{
return TimeSpan.FromTicks(0);
}
}
///
/// Gets the last date modified of the content being returned, if this can be determined.
/// This will be used to invalidate the cache, so it's not needed if CacheDuration is 0.
///
public virtual DateTime? LastDateModified
{
get
{
return null;
}
}
public virtual bool CompressResponse
{
get
{
return true;
}
}
public BaseHandler()
{
WriteStream = s =>
{
WriteReponse(s);
s.Dispose();
};
}
private void WriteReponse(Stream stream)
{
if (CompressResponse)
{
using (DeflateStream compressedStream = new DeflateStream(stream, CompressionLevel.Fastest, false))
{
WriteResponseToOutputStream(compressedStream);
}
}
else
{
WriteResponseToOutputStream(stream);
}
}
protected abstract void WriteResponseToOutputStream(Stream stream);
}
}