2012-09-08 14:52:13 +00:00
|
|
|
|
using MediaBrowser.Common.Logging;
|
|
|
|
|
using System;
|
2012-09-06 18:38:29 +00:00
|
|
|
|
using System.Collections;
|
2012-07-20 02:22:44 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
2012-08-10 13:07:58 +00:00
|
|
|
|
using System.Linq;
|
2012-08-09 12:59:54 +00:00
|
|
|
|
using System.Net;
|
2012-08-11 18:07:07 +00:00
|
|
|
|
using System.Threading.Tasks;
|
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
|
|
|
|
|
{
|
2012-09-08 14:52:13 +00:00
|
|
|
|
public abstract bool HandlesRequest(HttpListenerRequest request);
|
|
|
|
|
|
2012-08-09 12:41:54 +00:00
|
|
|
|
private Stream CompressedStream { get; set; }
|
|
|
|
|
|
2012-08-10 13:07:58 +00:00
|
|
|
|
public virtual bool? UseChunkedEncoding
|
2012-08-06 22:10:07 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-08-10 13:07:58 +00:00
|
|
|
|
return null;
|
2012-08-06 22:10:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 19:37:14 +00:00
|
|
|
|
private bool _totalContentLengthDiscovered;
|
|
|
|
|
private long? _totalContentLength;
|
2012-08-10 13:07:58 +00:00
|
|
|
|
public long? TotalContentLength
|
2012-08-06 22:10:07 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-09-11 19:37:14 +00:00
|
|
|
|
if (!_totalContentLengthDiscovered)
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
2012-09-11 19:37:14 +00:00
|
|
|
|
_totalContentLength = GetTotalContentLength();
|
|
|
|
|
_totalContentLengthDiscovered = true;
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 19:37:14 +00:00
|
|
|
|
return _totalContentLength;
|
2012-08-06 22:10:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-10 13:07:58 +00:00
|
|
|
|
protected virtual bool SupportsByteRangeRequests
|
2012-08-06 13:21:56 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-08-10 13:07:58 +00:00
|
|
|
|
return false;
|
2012-08-06 13:21:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-20 02:22:44 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-08-10 13:07:58 +00:00
|
|
|
|
/// The original HttpListenerContext
|
2012-07-20 02:22:44 +00:00
|
|
|
|
/// </summary>
|
2012-08-11 03:06:25 +00:00
|
|
|
|
protected HttpListenerContext HttpListenerContext { get; set; }
|
2012-07-20 02:22:44 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The original QueryString
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected NameValueCollection QueryString
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-08-10 13:07:58 +00:00
|
|
|
|
return HttpListenerContext.Request.QueryString;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 19:37:14 +00:00
|
|
|
|
private List<KeyValuePair<long, long?>> _requestedRanges;
|
2012-08-10 13:07:58 +00:00
|
|
|
|
protected IEnumerable<KeyValuePair<long, long?>> RequestedRanges
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-09-11 19:37:14 +00:00
|
|
|
|
if (_requestedRanges == null)
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
2012-09-11 19:37:14 +00:00
|
|
|
|
_requestedRanges = new List<KeyValuePair<long, long?>>();
|
2012-08-10 13:07:58 +00:00
|
|
|
|
|
|
|
|
|
if (IsRangeRequest)
|
|
|
|
|
{
|
|
|
|
|
// Example: bytes=0-,32-63
|
|
|
|
|
string[] ranges = HttpListenerContext.Request.Headers["Range"].Split('=')[1].Split(',');
|
|
|
|
|
|
|
|
|
|
foreach (string range in ranges)
|
|
|
|
|
{
|
|
|
|
|
string[] vals = range.Split('-');
|
|
|
|
|
|
|
|
|
|
long start = 0;
|
|
|
|
|
long? end = null;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(vals[0]))
|
|
|
|
|
{
|
|
|
|
|
start = long.Parse(vals[0]);
|
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrEmpty(vals[1]))
|
|
|
|
|
{
|
|
|
|
|
end = long.Parse(vals[1]);
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 19:37:14 +00:00
|
|
|
|
_requestedRanges.Add(new KeyValuePair<long, long?>(start, end));
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 19:37:14 +00:00
|
|
|
|
return _requestedRanges;
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected bool IsRangeRequest
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return HttpListenerContext.Request.Headers.AllKeys.Contains("Range");
|
2012-07-20 02:22:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-09 12:59:54 +00:00
|
|
|
|
private bool ClientSupportsCompression
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-08-10 13:07:58 +00:00
|
|
|
|
string enc = HttpListenerContext.Request.Headers["Accept-Encoding"] ?? string.Empty;
|
2012-08-09 12:59:54 +00:00
|
|
|
|
|
|
|
|
|
return enc.IndexOf("deflate", StringComparison.OrdinalIgnoreCase) != -1 || enc.IndexOf("gzip", StringComparison.OrdinalIgnoreCase) != -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string CompressionMethod
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-08-10 13:07:58 +00:00
|
|
|
|
string enc = HttpListenerContext.Request.Headers["Accept-Encoding"] ?? string.Empty;
|
2012-08-09 12:59:54 +00:00
|
|
|
|
|
|
|
|
|
if (enc.IndexOf("deflate", StringComparison.OrdinalIgnoreCase) != -1)
|
|
|
|
|
{
|
|
|
|
|
return "deflate";
|
|
|
|
|
}
|
|
|
|
|
if (enc.IndexOf("gzip", StringComparison.OrdinalIgnoreCase) != -1)
|
|
|
|
|
{
|
|
|
|
|
return "gzip";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-15 13:20:29 +00:00
|
|
|
|
public virtual async Task ProcessRequest(HttpListenerContext ctx)
|
2012-08-09 12:59:54 +00:00
|
|
|
|
{
|
2012-08-10 13:07:58 +00:00
|
|
|
|
HttpListenerContext = ctx;
|
|
|
|
|
|
2012-09-04 19:23:15 +00:00
|
|
|
|
string url = ctx.Request.Url.ToString();
|
|
|
|
|
Logger.LogInfo("Http Server received request at: " + url);
|
2012-08-10 13:07:58 +00:00
|
|
|
|
Logger.LogInfo("Http Headers: " + string.Join(",", ctx.Request.Headers.AllKeys.Select(k => k + "=" + ctx.Request.Headers[k])));
|
|
|
|
|
|
|
|
|
|
ctx.Response.AddHeader("Access-Control-Allow-Origin", "*");
|
|
|
|
|
|
|
|
|
|
ctx.Response.KeepAlive = true;
|
|
|
|
|
|
2012-08-15 13:20:29 +00:00
|
|
|
|
try
|
2012-08-09 12:59:54 +00:00
|
|
|
|
{
|
2012-08-15 13:20:29 +00:00
|
|
|
|
if (SupportsByteRangeRequests && IsRangeRequest)
|
|
|
|
|
{
|
|
|
|
|
ctx.Response.Headers["Accept-Ranges"] = "bytes";
|
|
|
|
|
}
|
2012-08-10 13:07:58 +00:00
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
ResponseInfo responseInfo = await GetResponseInfo().ConfigureAwait(false);
|
2012-08-10 13:07:58 +00:00
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
if (responseInfo.IsResponseValid)
|
2012-09-18 22:50:26 +00:00
|
|
|
|
{
|
2012-09-19 16:51:37 +00:00
|
|
|
|
// Set the initial status code
|
|
|
|
|
// When serving a range request, we need to return status code 206 to indicate a partial response body
|
|
|
|
|
responseInfo.StatusCode = SupportsByteRangeRequests && IsRangeRequest ? 206 : 200;
|
2012-09-18 22:50:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
ctx.Response.ContentType = responseInfo.ContentType;
|
2012-08-10 13:07:58 +00:00
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(responseInfo.Etag))
|
|
|
|
|
{
|
|
|
|
|
ctx.Response.Headers["ETag"] = responseInfo.Etag;
|
|
|
|
|
}
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
2012-08-15 13:20:29 +00:00
|
|
|
|
if (ctx.Request.Headers.AllKeys.Contains("If-Modified-Since"))
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
2012-08-15 13:20:29 +00:00
|
|
|
|
DateTime ifModifiedSince;
|
|
|
|
|
|
2012-09-04 19:23:15 +00:00
|
|
|
|
if (DateTime.TryParse(ctx.Request.Headers["If-Modified-Since"], out ifModifiedSince))
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
2012-08-15 13:20:29 +00:00
|
|
|
|
// If the cache hasn't expired yet just return a 304
|
2012-09-19 16:51:37 +00:00
|
|
|
|
if (IsCacheValid(ifModifiedSince.ToUniversalTime(), responseInfo.CacheDuration, responseInfo.DateLastModified))
|
2012-08-15 13:20:29 +00:00
|
|
|
|
{
|
2012-09-18 22:50:26 +00:00
|
|
|
|
// ETag must also match (if supplied)
|
2012-09-19 16:51:37 +00:00
|
|
|
|
if ((responseInfo.Etag ?? string.Empty).Equals(ctx.Request.Headers["If-None-Match"] ?? string.Empty))
|
2012-09-18 22:50:26 +00:00
|
|
|
|
{
|
2012-09-19 16:51:37 +00:00
|
|
|
|
responseInfo.StatusCode = 304;
|
2012-09-18 22:50:26 +00:00
|
|
|
|
}
|
2012-08-15 13:20:29 +00:00
|
|
|
|
}
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-09 12:59:54 +00:00
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
Logger.LogInfo("Responding with status code {0} for url {1}", responseInfo.StatusCode, url);
|
2012-09-04 19:23:15 +00:00
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
if (responseInfo.IsResponseValid)
|
2012-08-15 13:20:29 +00:00
|
|
|
|
{
|
2012-09-19 16:51:37 +00:00
|
|
|
|
await ProcessUncachedRequest(ctx, responseInfo).ConfigureAwait(false);
|
2012-08-15 13:20:29 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-09-19 16:51:37 +00:00
|
|
|
|
ctx.Response.StatusCode = responseInfo.StatusCode;
|
2012-08-15 13:20:29 +00:00
|
|
|
|
ctx.Response.SendChunked = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
2012-08-09 12:59:54 +00:00
|
|
|
|
{
|
2012-08-15 13:20:29 +00:00
|
|
|
|
// It might be too late if some response data has already been transmitted, but try to set this
|
|
|
|
|
ctx.Response.StatusCode = 500;
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
2012-08-15 13:20:29 +00:00
|
|
|
|
Logger.LogException(ex);
|
2012-08-09 12:59:54 +00:00
|
|
|
|
}
|
2012-08-15 13:20:29 +00:00
|
|
|
|
finally
|
2012-08-09 12:59:54 +00:00
|
|
|
|
{
|
2012-08-10 13:07:58 +00:00
|
|
|
|
DisposeResponseStream();
|
2012-08-09 12:59:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
private async Task ProcessUncachedRequest(HttpListenerContext ctx, ResponseInfo responseInfo)
|
2012-08-09 12:59:54 +00:00
|
|
|
|
{
|
2012-08-10 13:07:58 +00:00
|
|
|
|
long? totalContentLength = TotalContentLength;
|
2012-08-09 12:59:54 +00:00
|
|
|
|
|
2012-08-10 13:07:58 +00:00
|
|
|
|
// By default, use chunked encoding if we don't know the content length
|
|
|
|
|
bool useChunkedEncoding = UseChunkedEncoding == null ? (totalContentLength == null) : UseChunkedEncoding.Value;
|
2012-08-09 12:59:54 +00:00
|
|
|
|
|
2012-08-10 13:07:58 +00:00
|
|
|
|
// Don't force this to true. HttpListener will default it to true if supported by the client.
|
|
|
|
|
if (!useChunkedEncoding)
|
|
|
|
|
{
|
|
|
|
|
ctx.Response.SendChunked = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the content length, if we know it
|
|
|
|
|
if (totalContentLength.HasValue)
|
|
|
|
|
{
|
|
|
|
|
ctx.Response.ContentLength64 = totalContentLength.Value;
|
|
|
|
|
}
|
2012-08-09 12:59:54 +00:00
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
var compressResponse = responseInfo.CompressResponse && ClientSupportsCompression;
|
|
|
|
|
|
2012-08-10 13:07:58 +00:00
|
|
|
|
// Add the compression header
|
2012-08-19 20:38:31 +00:00
|
|
|
|
if (compressResponse)
|
2012-07-20 02:22:44 +00:00
|
|
|
|
{
|
2012-08-10 13:07:58 +00:00
|
|
|
|
ctx.Response.AddHeader("Content-Encoding", CompressionMethod);
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
if (responseInfo.DateLastModified.HasValue)
|
|
|
|
|
{
|
|
|
|
|
ctx.Response.Headers[HttpResponseHeader.LastModified] = responseInfo.DateLastModified.Value.ToString("r");
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-10 13:07:58 +00:00
|
|
|
|
// Add caching headers
|
2012-09-19 16:51:37 +00:00
|
|
|
|
if (responseInfo.CacheDuration.Ticks > 0)
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
2012-09-19 16:51:37 +00:00
|
|
|
|
CacheResponse(ctx.Response, responseInfo.CacheDuration);
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the status code
|
2012-09-19 16:51:37 +00:00
|
|
|
|
ctx.Response.StatusCode = responseInfo.StatusCode;
|
2012-08-10 13:07:58 +00:00
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
if (responseInfo.IsResponseValid)
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
|
|
|
|
// Finally, write the response data
|
|
|
|
|
Stream outputStream = ctx.Response.OutputStream;
|
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
if (compressResponse)
|
2012-08-09 12:59:54 +00:00
|
|
|
|
{
|
2012-08-10 13:07:58 +00:00
|
|
|
|
if (CompressionMethod.Equals("deflate", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
CompressedStream = new DeflateStream(outputStream, CompressionLevel.Fastest, false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CompressedStream = new GZipStream(outputStream, CompressionLevel.Fastest, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
outputStream = CompressedStream;
|
2012-08-09 12:59:54 +00:00
|
|
|
|
}
|
2012-08-10 13:07:58 +00:00
|
|
|
|
|
2012-08-22 02:50:59 +00:00
|
|
|
|
await WriteResponseToOutputStream(outputStream).ConfigureAwait(false);
|
2012-07-20 02:22:44 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-08-10 13:07:58 +00:00
|
|
|
|
ctx.Response.SendChunked = false;
|
2012-07-20 02:22:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
private void CacheResponse(HttpListenerResponse response, TimeSpan duration)
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
|
|
|
|
response.Headers[HttpResponseHeader.CacheControl] = "public, max-age=" + Convert.ToInt32(duration.TotalSeconds);
|
2012-09-19 16:51:37 +00:00
|
|
|
|
response.Headers[HttpResponseHeader.Expires] = DateTime.UtcNow.Add(duration).ToString("r");
|
2012-08-15 13:20:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 18:07:07 +00:00
|
|
|
|
protected abstract Task WriteResponseToOutputStream(Stream stream);
|
2012-07-20 02:22:44 +00:00
|
|
|
|
|
2012-08-15 13:20:29 +00:00
|
|
|
|
protected virtual void DisposeResponseStream()
|
2012-08-09 12:41:54 +00:00
|
|
|
|
{
|
|
|
|
|
if (CompressedStream != null)
|
|
|
|
|
{
|
|
|
|
|
CompressedStream.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-10 13:07:58 +00:00
|
|
|
|
HttpListenerContext.Response.OutputStream.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2012-09-04 19:23:15 +00:00
|
|
|
|
if (DateTime.UtcNow < cacheExpirationDate)
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
2012-09-04 19:23:15 +00:00
|
|
|
|
return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind);
|
2012-08-10 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual long? GetTotalContentLength()
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
protected abstract Task<ResponseInfo> GetResponseInfo();
|
2012-09-06 18:38:29 +00:00
|
|
|
|
|
2012-09-11 19:37:14 +00:00
|
|
|
|
private Hashtable _formValues;
|
2012-09-06 18:38:29 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value from form POST data
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected async Task<string> GetFormValue(string name)
|
|
|
|
|
{
|
2012-09-11 19:37:14 +00:00
|
|
|
|
if (_formValues == null)
|
2012-09-06 18:38:29 +00:00
|
|
|
|
{
|
2012-09-11 19:37:14 +00:00
|
|
|
|
_formValues = await GetFormValues(HttpListenerContext.Request).ConfigureAwait(false);
|
2012-09-06 18:38:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 19:37:14 +00:00
|
|
|
|
if (_formValues.ContainsKey(name))
|
2012-09-06 18:38:29 +00:00
|
|
|
|
{
|
2012-09-11 19:37:14 +00:00
|
|
|
|
return _formValues[name].ToString();
|
2012-09-06 18:38:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Extracts form POST data from a request
|
|
|
|
|
/// </summary>
|
|
|
|
|
private async Task<Hashtable> GetFormValues(HttpListenerRequest request)
|
|
|
|
|
{
|
2012-09-11 19:37:14 +00:00
|
|
|
|
var formVars = new Hashtable();
|
2012-09-06 18:38:29 +00:00
|
|
|
|
|
|
|
|
|
if (request.HasEntityBody)
|
|
|
|
|
{
|
2012-09-08 01:33:49 +00:00
|
|
|
|
if (request.ContentType.IndexOf("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase) != -1)
|
2012-09-06 18:38:29 +00:00
|
|
|
|
{
|
|
|
|
|
using (Stream requestBody = request.InputStream)
|
|
|
|
|
{
|
2012-09-11 19:37:14 +00:00
|
|
|
|
using (var reader = new StreamReader(requestBody, request.ContentEncoding))
|
2012-09-06 18:38:29 +00:00
|
|
|
|
{
|
|
|
|
|
string s = await reader.ReadToEndAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
string[] pairs = s.Split('&');
|
|
|
|
|
|
|
|
|
|
for (int x = 0; x < pairs.Length; x++)
|
|
|
|
|
{
|
|
|
|
|
string pair = pairs[x];
|
|
|
|
|
|
|
|
|
|
int index = pair.IndexOf('=');
|
|
|
|
|
|
|
|
|
|
if (index != -1)
|
|
|
|
|
{
|
|
|
|
|
string name = pair.Substring(0, index);
|
|
|
|
|
string value = pair.Substring(index + 1);
|
|
|
|
|
formVars.Add(name, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return formVars;
|
|
|
|
|
}
|
2012-07-20 02:22:44 +00:00
|
|
|
|
}
|
2012-09-19 16:51:37 +00:00
|
|
|
|
|
|
|
|
|
public class ResponseInfo
|
|
|
|
|
{
|
|
|
|
|
public string ContentType { get; set; }
|
|
|
|
|
public string Etag { get; set; }
|
|
|
|
|
public DateTime? DateLastModified { get; set; }
|
|
|
|
|
public TimeSpan CacheDuration { get; set; }
|
|
|
|
|
public bool CompressResponse { get; set; }
|
|
|
|
|
public int StatusCode { get; set; }
|
|
|
|
|
|
|
|
|
|
public ResponseInfo()
|
|
|
|
|
{
|
|
|
|
|
CacheDuration = TimeSpan.FromTicks(0);
|
|
|
|
|
|
|
|
|
|
CompressResponse = true;
|
|
|
|
|
|
|
|
|
|
StatusCode = 200;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsResponseValid
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return StatusCode == 200 || StatusCode == 206;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-20 02:22:44 +00:00
|
|
|
|
}
|