2012-07-20 02:22:44 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
2012-08-09 12:59:54 +00:00
|
|
|
|
using System.Net;
|
2012-07-20 02:22:44 +00:00
|
|
|
|
|
2012-07-21 18:39:47 +00:00
|
|
|
|
namespace MediaBrowser.Common.Net.Handlers
|
2012-07-20 02:22:44 +00:00
|
|
|
|
{
|
|
|
|
|
public abstract class BaseHandler
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Response headers
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IDictionary<string, string> Headers = new Dictionary<string, string>();
|
|
|
|
|
|
2012-08-09 12:41:54 +00:00
|
|
|
|
private Stream CompressedStream { get; set; }
|
|
|
|
|
|
2012-08-06 22:10:07 +00:00
|
|
|
|
public virtual bool UseChunkedEncoding
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual long? ContentLength
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-06 13:21:56 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns true or false indicating if the handler writes to the stream asynchronously.
|
|
|
|
|
/// If so the subclass will be responsible for disposing the stream when complete.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual bool IsAsyncHandler
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-20 02:22:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The action to write the response to the output stream
|
|
|
|
|
/// </summary>
|
2012-08-09 12:59:54 +00:00
|
|
|
|
public Action<Stream> WriteStream
|
2012-08-06 13:21:56 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return s =>
|
|
|
|
|
{
|
|
|
|
|
WriteReponse(s);
|
|
|
|
|
|
|
|
|
|
if (!IsAsyncHandler)
|
|
|
|
|
{
|
2012-08-09 12:41:54 +00:00
|
|
|
|
DisposeResponseStream();
|
2012-08-06 13:21:56 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-20 02:22:44 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The original RequestContext
|
|
|
|
|
/// </summary>
|
|
|
|
|
public RequestContext RequestContext { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The original QueryString
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected NameValueCollection QueryString
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return RequestContext.Request.QueryString;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the MIME type to include in the response headers
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract string ContentType { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the status code to include in the response headers
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual int StatusCode
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return 200;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the cache duration to include in the response headers
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual TimeSpan CacheDuration
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return TimeSpan.FromTicks(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 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.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual DateTime? LastDateModified
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-05 18:58:24 +00:00
|
|
|
|
public virtual bool CompressResponse
|
2012-07-20 02:22:44 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-09 12:59:54 +00:00
|
|
|
|
private bool ClientSupportsCompression
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string enc = RequestContext.Request.Headers["Accept-Encoding"] ?? string.Empty;
|
|
|
|
|
|
|
|
|
|
return enc.IndexOf("deflate", StringComparison.OrdinalIgnoreCase) != -1 || enc.IndexOf("gzip", StringComparison.OrdinalIgnoreCase) != -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string CompressionMethod
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string enc = RequestContext.Request.Headers["Accept-Encoding"] ?? string.Empty;
|
|
|
|
|
|
|
|
|
|
if (enc.IndexOf("deflate", StringComparison.OrdinalIgnoreCase) != -1)
|
|
|
|
|
{
|
|
|
|
|
return "deflate";
|
|
|
|
|
}
|
|
|
|
|
if (enc.IndexOf("gzip", StringComparison.OrdinalIgnoreCase) != -1)
|
|
|
|
|
{
|
|
|
|
|
return "gzip";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void PrepareResponseBeforeWriteOutput(HttpListenerResponse response)
|
|
|
|
|
{
|
|
|
|
|
// Don't force this to true. HttpListener will default it to true if supported by the client.
|
|
|
|
|
if (!UseChunkedEncoding)
|
|
|
|
|
{
|
|
|
|
|
response.SendChunked = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ContentLength.HasValue)
|
|
|
|
|
{
|
|
|
|
|
response.ContentLength64 = ContentLength.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (CompressResponse && ClientSupportsCompression)
|
|
|
|
|
{
|
|
|
|
|
response.AddHeader("Content-Encoding", CompressionMethod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TimeSpan cacheDuration = CacheDuration;
|
|
|
|
|
|
|
|
|
|
if (cacheDuration.Ticks > 0)
|
|
|
|
|
{
|
|
|
|
|
CacheResponse(response, cacheDuration, LastDateModified);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CacheResponse(HttpListenerResponse response, TimeSpan duration, DateTime? dateModified)
|
|
|
|
|
{
|
|
|
|
|
DateTime lastModified = dateModified ?? DateTime.Now;
|
|
|
|
|
|
|
|
|
|
response.Headers[HttpResponseHeader.CacheControl] = "public, max-age=" + Convert.ToInt32(duration.TotalSeconds);
|
|
|
|
|
response.Headers[HttpResponseHeader.Expires] = DateTime.Now.Add(duration).ToString("r");
|
|
|
|
|
response.Headers[HttpResponseHeader.LastModified] = lastModified.ToString("r");
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-20 02:22:44 +00:00
|
|
|
|
private void WriteReponse(Stream stream)
|
|
|
|
|
{
|
2012-08-09 12:59:54 +00:00
|
|
|
|
PrepareResponseBeforeWriteOutput(RequestContext.Response);
|
|
|
|
|
|
|
|
|
|
if (CompressResponse && ClientSupportsCompression)
|
2012-07-20 02:22:44 +00:00
|
|
|
|
{
|
2012-08-09 12:59:54 +00:00
|
|
|
|
if (CompressionMethod.Equals("deflate", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
CompressedStream = new DeflateStream(stream, CompressionLevel.Fastest, false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CompressedStream = new GZipStream(stream, CompressionLevel.Fastest, false);
|
|
|
|
|
}
|
2012-08-09 12:41:54 +00:00
|
|
|
|
|
|
|
|
|
WriteResponseToOutputStream(CompressedStream);
|
2012-07-20 02:22:44 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
WriteResponseToOutputStream(stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract void WriteResponseToOutputStream(Stream stream);
|
|
|
|
|
|
2012-08-09 12:41:54 +00:00
|
|
|
|
protected void DisposeResponseStream()
|
|
|
|
|
{
|
|
|
|
|
if (CompressedStream != null)
|
|
|
|
|
{
|
|
|
|
|
CompressedStream.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RequestContext.Response.OutputStream.Dispose();
|
|
|
|
|
}
|
2012-07-20 02:22:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|