Commit 7e2fa4fe authored by Honfika's avatar Honfika

- Charset in Content-Type can be in quotes, see: https://github.com/dotnet/corefx/issues/5014

- Charset sometimes contains x-user-defined. I don't know what is that, but it is not a valid encoding value, to do not try to convert it to Encoding object
- ReadLineAsync returns null when can't read any bytes + throw more accurate IOException in ReceiveResponse (Earlier it was IndexOutOfBoundsExceptionÖ
parent 88f63654
...@@ -26,10 +26,22 @@ namespace Titanium.Web.Proxy.Extensions ...@@ -26,10 +26,22 @@ namespace Titanium.Web.Proxy.Extensions
var contentTypes = response.ContentType.Split(ProxyConstants.SemiColonSplit); var contentTypes = response.ContentType.Split(ProxyConstants.SemiColonSplit);
foreach (var contentType in contentTypes) foreach (var contentType in contentTypes)
{ {
var encodingSplit = contentType.Split('='); var encodingSplit = contentType.Split(ProxyConstants.EqualSplit, 2);
if (encodingSplit.Length == 2 && encodingSplit[0].Trim().Equals("charset", StringComparison.CurrentCultureIgnoreCase)) if (encodingSplit.Length == 2 && encodingSplit[0].Trim().Equals("charset", StringComparison.CurrentCultureIgnoreCase))
{ {
return Encoding.GetEncoding(encodingSplit[1]); string value = encodingSplit[1];
if (value.Equals("x-user-defined", StringComparison.OrdinalIgnoreCase))
{
//todo: what is this?
continue;
}
if (value[0] == '"' && value[value.Length - 1] == '"')
{
value = value.Substring(1, value.Length - 2);
}
return Encoding.GetEncoding(value);
} }
} }
} }
......
...@@ -71,6 +71,11 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -71,6 +71,11 @@ namespace Titanium.Web.Proxy.Helpers
} }
} }
if (bufferDataLength == 0)
{
return null;
}
return encoding.GetString(buffer, 0, bufferDataLength); return encoding.GetString(buffer, 0, bufferDataLength);
} }
......
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Titanium.Web.Proxy.Models; using Titanium.Web.Proxy.Models;
...@@ -160,7 +161,13 @@ namespace Titanium.Web.Proxy.Http ...@@ -160,7 +161,13 @@ namespace Titanium.Web.Proxy.Http
//return if this is already read //return if this is already read
if (Response.ResponseStatusCode != null) return; if (Response.ResponseStatusCode != null) return;
var httpResult = (await ServerConnection.StreamReader.ReadLineAsync()).Split(ProxyConstants.SpaceSplit, 3); string line = await ServerConnection.StreamReader.ReadLineAsync();
if (line == null)
{
throw new IOException();
}
var httpResult = line.Split(ProxyConstants.SpaceSplit, 3);
if (string.IsNullOrEmpty(httpResult[0])) if (string.IsNullOrEmpty(httpResult[0]))
{ {
......
...@@ -10,6 +10,7 @@ namespace Titanium.Web.Proxy.Shared ...@@ -10,6 +10,7 @@ namespace Titanium.Web.Proxy.Shared
internal static readonly char[] SpaceSplit = { ' ' }; internal static readonly char[] SpaceSplit = { ' ' };
internal static readonly char[] ColonSplit = { ':' }; internal static readonly char[] ColonSplit = { ':' };
internal static readonly char[] SemiColonSplit = { ';' }; internal static readonly char[] SemiColonSplit = { ';' };
internal static readonly char[] EqualSplit = { '=' };
internal static readonly byte[] NewLineBytes = Encoding.ASCII.GetBytes(NewLine); internal static readonly byte[] NewLineBytes = Encoding.ASCII.GetBytes(NewLine);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment