From 3538789e46d3332a2f20cd6b3db13b2aa9fbe475 Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Thu, 6 Sep 2012 14:38:29 -0400 Subject: Added User authentication --- MediaBrowser.Common/Net/Handlers/BaseHandler.cs | 63 +++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'MediaBrowser.Common/Net') diff --git a/MediaBrowser.Common/Net/Handlers/BaseHandler.cs b/MediaBrowser.Common/Net/Handlers/BaseHandler.cs index 2462833ffb..19674fadd8 100644 --- a/MediaBrowser.Common/Net/Handlers/BaseHandler.cs +++ b/MediaBrowser.Common/Net/Handlers/BaseHandler.cs @@ -1,11 +1,14 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.IO.Compression; using System.Linq; using System.Net; +using System.Text; using System.Threading.Tasks; +using System.Web; using MediaBrowser.Common.Logging; namespace MediaBrowser.Common.Net.Handlers @@ -374,5 +377,65 @@ namespace MediaBrowser.Common.Net.Handlers return StatusCode == 200 || StatusCode == 206; } } + + private Hashtable _FormValues = null; + + /// + /// Gets a value from form POST data + /// + protected async Task GetFormValue(string name) + { + if (_FormValues == null) + { + _FormValues = await GetFormValues(HttpListenerContext.Request).ConfigureAwait(false); + } + + if (_FormValues.ContainsKey(name)) + { + return _FormValues[name].ToString(); + } + + return null; + } + + /// + /// Extracts form POST data from a request + /// + private async Task GetFormValues(HttpListenerRequest request) + { + Hashtable formVars = new Hashtable(); + + if (request.HasEntityBody) + { + if (request.ContentType.Equals("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase)) + { + using (Stream requestBody = request.InputStream) + { + using (StreamReader reader = new StreamReader(requestBody, request.ContentEncoding)) + { + 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; + } } } \ No newline at end of file -- cgit v1.2.3