| | | 1 | | using System.Text; |
| | | 2 | | |
| | | 3 | | namespace Elsa.Http.ContentWriters; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// HTTP content based on a string that preserves the provided media type as-is (no automatic charset parameter). |
| | | 7 | | /// Uses a pre-encoded byte array to guarantee Content-Length == bytes written. |
| | | 8 | | /// </summary> |
| | | 9 | | public sealed class RawStringContent : ByteArrayContent |
| | | 10 | | { |
| | | 11 | | public RawStringContent(string content, Encoding encoding, string mediaType) |
| | 13 | 12 | | : base(GetBytes(content, encoding)) |
| | | 13 | | { |
| | | 14 | | // Set media type exactly as provided, without charset parameter. |
| | 13 | 15 | | Headers.ContentType = new(mediaType); |
| | 13 | 16 | | } |
| | | 17 | | |
| | | 18 | | private static byte[] GetBytes(string content, Encoding encoding) |
| | | 19 | | { |
| | 13 | 20 | | ArgumentNullException.ThrowIfNull(content); |
| | 13 | 21 | | ArgumentNullException.ThrowIfNull(encoding); |
| | | 22 | | |
| | | 23 | | // Ensure we don't include a BOM/preamble. Most Encoding instances can expose a preamble. |
| | | 24 | | // If encoding has a preamble, we must not include it, so use GetBytes directly (preamble is only returned by Ge |
| | | 25 | | // For safety, if the encoding instance is a BOM-producing variant (e.g., new UTF8Encoding(true)), |
| | | 26 | | // GetBytes does not include the BOM; only GetPreamble() would. So this is safe. |
| | 13 | 27 | | return encoding.GetBytes(content); |
| | | 28 | | } |
| | | 29 | | } |