2012-07-13 03:50:50 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
2012-07-21 18:39:47 +00:00
|
|
|
|
using MediaBrowser.Common.Net.Handlers;
|
2012-07-13 03:50:50 +00:00
|
|
|
|
|
2012-07-21 18:39:47 +00:00
|
|
|
|
namespace MediaBrowser.Common.Net
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
|
|
|
|
public class RequestContext
|
|
|
|
|
{
|
|
|
|
|
public HttpListenerRequest Request { get; private set; }
|
|
|
|
|
public HttpListenerResponse Response { get; private set; }
|
|
|
|
|
|
2012-07-14 20:45:11 +00:00
|
|
|
|
public string LocalPath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Request.Url.LocalPath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-13 03:50:50 +00:00
|
|
|
|
public RequestContext(HttpListenerContext context)
|
|
|
|
|
{
|
|
|
|
|
Response = context.Response;
|
|
|
|
|
Request = context.Request;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-20 02:22:44 +00:00
|
|
|
|
public void Respond(BaseHandler handler)
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
|
|
|
|
Response.AddHeader("Access-Control-Allow-Origin", "*");
|
|
|
|
|
|
2012-07-14 20:45:11 +00:00
|
|
|
|
Response.KeepAlive = true;
|
2012-08-01 01:48:32 +00:00
|
|
|
|
|
2012-07-13 03:50:50 +00:00
|
|
|
|
foreach (var header in handler.Headers)
|
|
|
|
|
{
|
|
|
|
|
Response.AddHeader(header.Key, header.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int statusCode = handler.StatusCode;
|
|
|
|
|
Response.ContentType = handler.ContentType;
|
|
|
|
|
|
|
|
|
|
TimeSpan cacheDuration = handler.CacheDuration;
|
|
|
|
|
|
|
|
|
|
if (Request.Headers.AllKeys.Contains("If-Modified-Since"))
|
|
|
|
|
{
|
|
|
|
|
DateTime ifModifiedSince;
|
|
|
|
|
|
|
|
|
|
if (DateTime.TryParse(Request.Headers["If-Modified-Since"].Replace(" GMT", string.Empty), out ifModifiedSince))
|
|
|
|
|
{
|
|
|
|
|
// If the cache hasn't expired yet just return a 304
|
|
|
|
|
if (IsCacheValid(ifModifiedSince, cacheDuration, handler.LastDateModified))
|
|
|
|
|
{
|
|
|
|
|
statusCode = 304;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Response.StatusCode = statusCode;
|
|
|
|
|
|
2012-08-01 01:48:32 +00:00
|
|
|
|
if (statusCode == 200)
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
2012-08-01 01:48:32 +00:00
|
|
|
|
Response.SendChunked = true;
|
|
|
|
|
|
2012-08-05 18:58:24 +00:00
|
|
|
|
if (handler.CompressResponse)
|
2012-07-20 02:22:44 +00:00
|
|
|
|
{
|
2012-08-05 18:58:24 +00:00
|
|
|
|
Response.AddHeader("Content-Encoding", "deflate");
|
2012-07-20 02:22:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-13 03:50:50 +00:00
|
|
|
|
if (cacheDuration.Ticks > 0)
|
|
|
|
|
{
|
|
|
|
|
CacheResponse(Response, cacheDuration, handler.LastDateModified);
|
|
|
|
|
}
|
|
|
|
|
handler.WriteStream(Response.OutputStream);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-08-01 01:48:32 +00:00
|
|
|
|
Response.SendChunked = false;
|
|
|
|
|
Response.OutputStream.Dispose();
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CacheResponse(HttpListenerResponse response, TimeSpan duration, DateTime? dateModified)
|
|
|
|
|
{
|
|
|
|
|
DateTime lastModified = dateModified ?? DateTime.Now;
|
|
|
|
|
|
2012-08-01 01:48:32 +00:00
|
|
|
|
response.Headers[HttpResponseHeader.CacheControl] = "public, max-age=" + Convert.ToInt32(duration.TotalSeconds);
|
2012-07-13 03:50:50 +00:00
|
|
|
|
response.Headers[HttpResponseHeader.Expires] = DateTime.Now.Add(duration).ToString("r");
|
|
|
|
|
response.Headers[HttpResponseHeader.LastModified] = lastModified.ToString("r");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsCacheValid(DateTime ifModifiedSince, TimeSpan cacheDuration, DateTime? dateModified)
|
|
|
|
|
{
|
|
|
|
|
if (dateModified.HasValue)
|
|
|
|
|
{
|
|
|
|
|
DateTime lastModified = NormalizeDateForComparison(dateModified.Value);
|
|
|
|
|
ifModifiedSince = NormalizeDateForComparison(ifModifiedSince);
|
|
|
|
|
|
|
|
|
|
return lastModified <= ifModifiedSince;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DateTime cacheExpirationDate = ifModifiedSince.Add(cacheDuration);
|
|
|
|
|
|
|
|
|
|
if (DateTime.Now < cacheExpirationDate)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// When the browser sends the IfModifiedDate, it's precision is limited to seconds, so this will account for that
|
|
|
|
|
/// </summary>
|
|
|
|
|
private DateTime NormalizeDateForComparison(DateTime date)
|
|
|
|
|
{
|
|
|
|
|
return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|