// Copyright © 2015 The CefSharp Authors. All rights reserved. // // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. using System; using System.Text; namespace CefSharp { /// /// Post Data extension methods - Makes accessing post data easier /// public static class PostDataExtensions { /// /// A convenience extension method that extracts the Character set from /// the content-type header. Can be used in conjuncation with /// /// the request /// character set e.g. UTF-8 public static string GetCharSet(this IRequest request) { //Extract the Content-Type header value. var headers = request.Headers; string contentType = null; foreach (string key in headers) { if (key.Equals("content-type", StringComparison.InvariantCultureIgnoreCase)) { foreach (var element in headers.GetValues(key)) { contentType = element; break; } break; } } if (contentType == null) { return null; } //Look for charset after the mime-type. var semiColonIndex = contentType.IndexOf(";", StringComparison.InvariantCulture); if (semiColonIndex == -1) { return null; } var charsetArgument = contentType.Substring(semiColonIndex + 1).Trim(); var equalsIndex = charsetArgument.IndexOf("=", StringComparison.InvariantCulture); if (equalsIndex == -1) { return null; } var argumentName = charsetArgument.Substring(0, equalsIndex).Trim(); if (!argumentName.Equals("charset", StringComparison.InvariantCultureIgnoreCase)) { return null; } var charset = charsetArgument.Substring(equalsIndex + 1).Trim(); // Remove redundant characters (e.g. "UTF-8"; -> UTF-8) charset = charset.TrimStart(' ', '"'); charset = charset.TrimEnd(' ', '"', ';'); return charset; } /// /// Converts the property into a string /// using the specified charset (Encoding) or if unable to parse then uses /// the /// /// post data /// character set /// encoded string public static string GetBody(this IPostDataElement postDataElement, string charSet = null) { var bytes = postDataElement.Bytes; if (bytes.Length == 0) { return null; } var encoding = Encoding.Default; if (charSet != null) { try { encoding = Encoding.GetEncoding(charSet); } catch (ArgumentException) { } } return encoding.GetString(bytes, 0, bytes.Length); } /// /// Add a new that represents the specified file /// /// post data instance /// file name public static void AddFile(this IPostData postData, string fileName) { if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException("fileName"); } var element = postData.CreatePostDataElement(); element.File = fileName; postData.AddElement(element); } /// /// Add a new that represents the key and value /// The data is encoded using /// /// Post Data /// Data to be encoded for the post data element /// Specified Encoding. If null then will be used public static void AddData(this IPostData postData, string data, Encoding encoding = null) { if (string.IsNullOrEmpty(data)) { throw new ArgumentNullException("data"); } if (encoding == null) { encoding = Encoding.Default; } var element = postData.CreatePostDataElement(); element.Bytes = encoding.GetBytes(data); postData.AddElement(element); } /// /// Add a new that represents the key and value /// /// Post Data /// byte array that represents the post data public static void AddData(this IPostData postData, byte[] bytes) { if (bytes == null) { throw new ArgumentNullException("bytes"); } var element = postData.CreatePostDataElement(); element.Bytes = bytes; postData.AddElement(element); } } }