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 { public string EntityBody {
get { get {
return EntityBodyData != null && EntityBodyData.LongLength > 0 if (EntityBodyData == null || EntityBodyData.LongLength == 0)
? getEncoding (_headers["Content-Type"]).GetString (EntityBodyData) return String.Empty;
: 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 #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) private static byte[] readEntityBody (Stream stream, string length)
{ {
long len; long len;

View File

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

View File

@ -543,6 +543,18 @@ namespace WebSocketSharp.Net
return res; 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) internal static NameValueCollection ParseQueryStringInternally (string query, Encoding encoding)
{ {
int len; int len;