diff --git a/websocket-sharp/HttpBase.cs b/websocket-sharp/HttpBase.cs index 9daea3d4..85220b35 100644 --- a/websocket-sharp/HttpBase.cs +++ b/websocket-sharp/HttpBase.cs @@ -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; diff --git a/websocket-sharp/Net/HttpListenerRequest.cs b/websocket-sharp/Net/HttpListenerRequest.cs index ec0211ff..a7aa4643 100644 --- a/websocket-sharp/Net/HttpListenerRequest.cs +++ b/websocket-sharp/Net/HttpListenerRequest.cs @@ -135,9 +135,9 @@ namespace WebSocketSharp.Net /// Gets the encoding for the entity body data included in the request. /// /// - /// A that represents the encoding for the entity body data, or - /// if the request didn't include the information about - /// the encoding. + /// A that represents the encoding for the entity body data, + /// or if the request didn't include the information + /// about the encoding. /// 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; diff --git a/websocket-sharp/Net/HttpUtility.cs b/websocket-sharp/Net/HttpUtility.cs index ab0d0311..f79cc488 100644 --- a/websocket-sharp/Net/HttpUtility.cs +++ b/websocket-sharp/Net/HttpUtility.cs @@ -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;