Added GetEncoding method

This commit is contained in:
sta 2014-08-16 16:30:22 +09:00
parent d3027834ca
commit 3854f218b4
3 changed files with 33 additions and 42 deletions

View File

@ -72,9 +72,16 @@ namespace WebSocketSharp
public string EntityBody {
get {
return EntityBodyData != null && EntityBodyData.LongLength > 0
? getEncoding (_headers["Content-Type"]).GetString (EntityBodyData)
: String.Empty;
if (EntityBodyData == null || EntityBodyData.LongLength == 0)
return String.Empty;
Encoding enc = null;
var contentType = _headers["Content-Type"];
if (contentType != null && contentType.Length > 0)
enc = HttpUtility.GetEncoding (contentType);
return (enc ?? Encoding.UTF8).GetString (EntityBodyData);
}
}
@ -94,23 +101,6 @@ namespace WebSocketSharp
#region Private Methods
private static Encoding getEncoding (string contentType)
{
if (contentType == null || contentType.Length == 0)
return Encoding.UTF8;
var i = contentType.IndexOf ("charset=", StringComparison.Ordinal);
if (i == -1)
return Encoding.UTF8;
var charset = contentType.Substring (i + 8);
i = charset.IndexOf (';');
if (i != -1)
charset = charset.Substring (0, i).TrimEnd ();
return Encoding.GetEncoding (charset.Trim ('"'));
}
private static byte[] readEntityBody (Stream stream, string length)
{
long len;

View File

@ -135,9 +135,9 @@ namespace WebSocketSharp.Net
/// Gets the encoding for the entity body data included in the request.
/// </summary>
/// <value>
/// A <see cref="Encoding"/> that represents the encoding for the entity body data, or
/// <see cref="Encoding.Default"/> if the request didn't include the information about
/// the encoding.
/// A <see cref="Encoding"/> that represents the encoding for the entity body data,
/// or <see cref="Encoding.Default"/> if the request didn't include the information
/// about the encoding.
/// </value>
public Encoding ContentEncoding {
get {
@ -518,22 +518,11 @@ namespace WebSocketSharp.Net
}
if (lower == "content-type") {
var parts = val.Split (';');
foreach (var p in parts) {
var part = p.Trim ();
if (part.StartsWith ("charset", StringComparison.OrdinalIgnoreCase)) {
var charset = part.GetValue ('=', true);
if (charset != null && charset.Length > 0) {
try {
_contentEncoding = Encoding.GetEncoding (charset);
}
catch {
_context.ErrorMessage = "Invalid Content-Type header";
}
}
break;
}
try {
_contentEncoding = HttpUtility.GetEncoding (val);
}
catch {
_context.ErrorMessage = "Invalid Content-Type header";
}
return;
@ -561,9 +550,9 @@ namespace WebSocketSharp.Net
return;
}
var encoding = Headers["Transfer-Encoding"];
if (_version > HttpVersion.Version10 && encoding != null && encoding.Length > 0) {
_chunked = encoding.ToLower () == "chunked";
var enc = Headers["Transfer-Encoding"];
if (_version > HttpVersion.Version10 && enc != null && enc.Length > 0) {
_chunked = enc.ToLower () == "chunked";
if (!_chunked) {
_context.ErrorMessage = String.Empty;
_context.ErrorStatus = 501;

View File

@ -543,6 +543,18 @@ namespace WebSocketSharp.Net
return res;
}
internal static Encoding GetEncoding (string contentType)
{
var parts = contentType.Split (';');
foreach (var p in parts) {
var part = p.Trim ();
if (part.StartsWith ("charset", StringComparison.OrdinalIgnoreCase))
return Encoding.GetEncoding (part.GetValue ('=', true));
}
return null;
}
internal static NameValueCollection ParseQueryStringInternally (string query, Encoding encoding)
{
int len;