update stream reading

This commit is contained in:
Luke Pulverenti 2016-07-21 14:40:49 -04:00
parent 6cebecb88c
commit 011fa515c5
4 changed files with 78 additions and 75 deletions

View File

@ -3,6 +3,7 @@ using System.Collections.Specialized;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Threading.Tasks;
using System.Web; using System.Web;
using ServiceStack; using ServiceStack;
using ServiceStack.Web; using ServiceStack.Web;
@ -32,53 +33,54 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
return header.Substring(ap + 1, end - ap - 1); return header.Substring(ap + 1, end - ap - 1);
} }
void LoadMultiPart() async Task LoadMultiPart()
{ {
string boundary = GetParameter(ContentType, "; boundary="); string boundary = GetParameter(ContentType, "; boundary=");
if (boundary == null) if (boundary == null)
return; return;
var input = GetSubStream(InputStream); using (var requestStream = GetSubStream(InputStream))
//DB: 30/01/11 - Hack to get around non-seekable stream and received HTTP request
//Not ending with \r\n?
var ms = new MemoryStream(32 * 1024);
input.CopyTo(ms);
input = ms;
ms.WriteByte((byte)'\r');
ms.WriteByte((byte)'\n');
input.Position = 0;
//Uncomment to debug
//var content = new StreamReader(ms).ReadToEnd();
//Console.WriteLine(boundary + "::" + content);
//input.Position = 0;
var multi_part = new HttpMultipart(input, boundary, ContentEncoding);
HttpMultipart.Element e;
while ((e = multi_part.ReadNextElement()) != null)
{ {
if (e.Filename == null) //DB: 30/01/11 - Hack to get around non-seekable stream and received HTTP request
{ //Not ending with \r\n?
byte[] copy = new byte[e.Length]; var ms = new MemoryStream(32 * 1024);
await requestStream.CopyToAsync(ms).ConfigureAwait(false);
input.Position = e.Start; var input = ms;
input.Read(copy, 0, (int)e.Length); ms.WriteByte((byte)'\r');
ms.WriteByte((byte)'\n');
form.Add(e.Name, (e.Encoding ?? ContentEncoding).GetString(copy)); input.Position = 0;
}
else //Uncomment to debug
//var content = new StreamReader(ms).ReadToEnd();
//Console.WriteLine(boundary + "::" + content);
//input.Position = 0;
var multi_part = new HttpMultipart(input, boundary, ContentEncoding);
HttpMultipart.Element e;
while ((e = multi_part.ReadNextElement()) != null)
{ {
// if (e.Filename == null)
// We use a substream, as in 2.x we will support large uploads streamed to disk, {
// byte[] copy = new byte[e.Length];
HttpPostedFile sub = new HttpPostedFile(e.Filename, e.ContentType, input, e.Start, e.Length);
files.AddFile(e.Name, sub); input.Position = e.Start;
input.Read(copy, 0, (int)e.Length);
form.Add(e.Name, (e.Encoding ?? ContentEncoding).GetString(copy));
}
else
{
//
// We use a substream, as in 2.x we will support large uploads streamed to disk,
//
HttpPostedFile sub = new HttpPostedFile(e.Filename, e.ContentType, input, e.Start, e.Length);
files.AddFile(e.Name, sub);
}
} }
} }
EndSubStream(input);
} }
public NameValueCollection Form public NameValueCollection Form
@ -91,10 +93,15 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
files = new HttpFileCollection(); files = new HttpFileCollection();
if (IsContentType("multipart/form-data", true)) if (IsContentType("multipart/form-data", true))
LoadMultiPart(); {
else if ( var task = LoadMultiPart();
IsContentType("application/x-www-form-urlencoded", true)) Task.WaitAll(task);
LoadWwwForm(); }
else if (IsContentType("application/x-www-form-urlencoded", true))
{
var task = LoadWwwForm();
Task.WaitAll(task);
}
form.Protect(); form.Protect();
} }
@ -220,50 +227,50 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
return String.Compare(ContentType, ct, true, Helpers.InvariantCulture) == 0; return String.Compare(ContentType, ct, true, Helpers.InvariantCulture) == 0;
} }
async Task LoadWwwForm()
void LoadWwwForm()
{ {
using (Stream input = GetSubStream(InputStream)) using (Stream input = GetSubStream(InputStream))
{ {
using (StreamReader s = new StreamReader(input, ContentEncoding)) using (var ms = new MemoryStream())
{ {
StringBuilder key = new StringBuilder(); await input.CopyToAsync(ms).ConfigureAwait(false);
StringBuilder value = new StringBuilder(); ms.Position = 0;
int c;
while ((c = s.Read()) != -1) using (StreamReader s = new StreamReader(ms, ContentEncoding))
{ {
if (c == '=') StringBuilder key = new StringBuilder();
StringBuilder value = new StringBuilder();
int c;
while ((c = s.Read()) != -1)
{ {
value.Length = 0; if (c == '=')
while ((c = s.Read()) != -1)
{ {
if (c == '&') value.Length = 0;
while ((c = s.Read()) != -1)
{
if (c == '&')
{
AddRawKeyValue(key, value);
break;
}
else
value.Append((char)c);
}
if (c == -1)
{ {
AddRawKeyValue(key, value); AddRawKeyValue(key, value);
break; return;
} }
else
value.Append((char)c);
} }
if (c == -1) else if (c == '&')
{
AddRawKeyValue(key, value); AddRawKeyValue(key, value);
return; else
} key.Append((char)c);
} }
else if (c == '&') if (c == -1)
AddRawKeyValue(key, value); AddRawKeyValue(key, value);
else
key.Append((char)c);
} }
if (c == -1)
AddRawKeyValue(key, value);
EndSubStream(input);
} }
} }
} }

View File

@ -465,10 +465,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
return stream; return stream;
} }
static void EndSubStream(Stream stream)
{
}
public static string GetHandlerPathIfAny(string listenerUrl) public static string GetHandlerPathIfAny(string listenerUrl)
{ {
if (listenerUrl == null) return null; if (listenerUrl == null) return null;

View File

@ -73,8 +73,8 @@
<HintPath>..\packages\SimpleInjector.3.2.0\lib\net45\SimpleInjector.dll</HintPath> <HintPath>..\packages\SimpleInjector.3.2.0\lib\net45\SimpleInjector.dll</HintPath>
<Private>True</Private> <Private>True</Private>
</Reference> </Reference>
<Reference Include="SocketHttpListener, Version=1.0.6039.31236, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="SocketHttpListener, Version=1.0.6046.26351, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SocketHttpListener.1.0.0.33\lib\net45\SocketHttpListener.dll</HintPath> <HintPath>..\packages\SocketHttpListener.1.0.0.35\lib\net45\SocketHttpListener.dll</HintPath>
<Private>True</Private> <Private>True</Private>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />

View File

@ -9,5 +9,5 @@
<package id="morelinq" version="1.4.0" targetFramework="net45" /> <package id="morelinq" version="1.4.0" targetFramework="net45" />
<package id="Patterns.Logging" version="1.0.0.2" targetFramework="net45" /> <package id="Patterns.Logging" version="1.0.0.2" targetFramework="net45" />
<package id="SimpleInjector" version="3.2.0" targetFramework="net45" /> <package id="SimpleInjector" version="3.2.0" targetFramework="net45" />
<package id="SocketHttpListener" version="1.0.0.33" targetFramework="net45" /> <package id="SocketHttpListener" version="1.0.0.35" targetFramework="net45" />
</packages> </packages>