| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Net.Http.Headers; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | |
| | | 11 | | namespace System.Net.Http |
| | | 12 | | { |
| | | 13 | | /// <summary>Provides HTTP content based on a string.</summary> |
| | | 14 | | public class StringContent : ByteArrayContent |
| | | 15 | | { |
| | | 16 | | /// <summary>The media type to use when none is specified.</summary> |
| | | 17 | | private const string DefaultMediaType = "text/plain"; |
| | | 18 | | |
| | | 19 | | /// <summary>Creates a new instance of the <see cref="StringContent"/> class.</summary> |
| | | 20 | | /// <param name="content">The content used to initialize the <see cref="StringContent"/>.</param> |
| | | 21 | | /// <remarks>The media type for the <see cref="StringContent"/> created defaults to text/plain.</remarks> |
| | | 22 | | public StringContent(string content) |
| | 0 | 23 | | : this(content, DefaultStringEncoding, DefaultMediaType) |
| | 0 | 24 | | { |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary>Creates a new instance of the <see cref="StringContent"/> class.</summary> |
| | | 28 | | /// <param name="content">The content used to initialize the <see cref="StringContent"/>.</param> |
| | | 29 | | /// <param name="mediaType">The media type to use for the content.</param> |
| | | 30 | | public StringContent(string content, MediaTypeHeaderValue? mediaType) |
| | 0 | 31 | | : this(content, DefaultStringEncoding, mediaType) |
| | 0 | 32 | | { |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary>Creates a new instance of the <see cref="StringContent"/> class.</summary> |
| | | 36 | | /// <param name="content">The content used to initialize the <see cref="StringContent"/>.</param> |
| | | 37 | | /// <param name="encoding">The encoding to use for the content.</param> |
| | | 38 | | /// <remarks>The media type for the <see cref="StringContent"/> created defaults to text/plain.</remarks> |
| | | 39 | | public StringContent(string content, Encoding? encoding) |
| | 0 | 40 | | : this(content, encoding, DefaultMediaType) |
| | 0 | 41 | | { |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary>Creates a new instance of the <see cref="StringContent"/> class.</summary> |
| | | 45 | | /// <param name="content">The content used to initialize the <see cref="StringContent"/>.</param> |
| | | 46 | | /// <param name="encoding">The encoding to use for the content.</param> |
| | | 47 | | /// <param name="mediaType">The media type to use for the content.</param> |
| | | 48 | | public StringContent(string content, Encoding? encoding, string? mediaType) |
| | 0 | 49 | | : base(GetContentByteArray(content, encoding)) |
| | 0 | 50 | | { |
| | 0 | 51 | | Debug.Assert(DefaultStringEncoding.WebName == "utf-8"); |
| | | 52 | | |
| | 0 | 53 | | encoding ??= DefaultStringEncoding; |
| | 0 | 54 | | mediaType ??= DefaultMediaType; |
| | | 55 | | |
| | | 56 | | // Avoid allocating MediaTypeHeaderValue and related objects for common media types. |
| | 0 | 57 | | if (ReferenceEquals(encoding, DefaultStringEncoding)) |
| | 0 | 58 | | { |
| | 0 | 59 | | string? knownValue = mediaType switch |
| | 0 | 60 | | { |
| | 0 | 61 | | "text/plain" => "text/plain; charset=utf-8", |
| | 0 | 62 | | "application/json" => "application/json; charset=utf-8", |
| | 0 | 63 | | _ => null |
| | 0 | 64 | | }; |
| | | 65 | | |
| | 0 | 66 | | if (knownValue is not null) |
| | 0 | 67 | | { |
| | 0 | 68 | | Headers.TryAddWithoutValidation(KnownHeaders.ContentType.Descriptor, knownValue); |
| | 0 | 69 | | return; |
| | | 70 | | } |
| | 0 | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | Headers.ContentType = new MediaTypeHeaderValue(mediaType, encoding.WebName); |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary>Creates a new instance of the <see cref="StringContent"/> class.</summary> |
| | | 77 | | /// <param name="content">The content used to initialize the <see cref="StringContent"/>.</param> |
| | | 78 | | /// <param name="encoding">The encoding to use for the content.</param> |
| | | 79 | | /// <param name="mediaType">The media type to use for the content.</param> |
| | | 80 | | public StringContent(string content, Encoding? encoding, MediaTypeHeaderValue? mediaType) |
| | 0 | 81 | | : base(GetContentByteArray(content, encoding)) |
| | 0 | 82 | | { |
| | 0 | 83 | | Headers.ContentType = mediaType; |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary>Serialize the string into a byte-array using encoding information provided by the caller (if any).< |
| | | 87 | | /// <param name="content">The content used to initialize the <see cref="StringContent"/>.</param> |
| | | 88 | | /// <param name="encoding">The encoding to use for the content.</param> |
| | | 89 | | /// <returns>The serialized byte array.</returns> |
| | | 90 | | private static byte[] GetContentByteArray(string content, Encoding? encoding) |
| | 0 | 91 | | { |
| | 0 | 92 | | ArgumentNullException.ThrowIfNull(content); |
| | | 93 | | |
| | | 94 | | // In this case we treat 'null' strings differently from string.Empty in order to be consistent with our |
| | | 95 | | // other *Content constructors: 'null' throws, empty values are allowed. |
| | | 96 | | |
| | 0 | 97 | | return (encoding ?? DefaultStringEncoding).GetBytes(content); |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary>Serialize and write the byte array provided in the constructor to an HTTP content stream as an asyn |
| | | 101 | | /// <param name="stream">The target stream.</param> |
| | | 102 | | /// <param name="context">Information about the transport, like channel binding token. This parameter may be <se |
| | | 103 | | /// <param name="cancellationToken">The cancellation token to cancel the operation.</param> |
| | | 104 | | /// <returns>The task object representing the asynchronous operation.</returns> |
| | | 105 | | protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cance |
| | | 106 | | // Only skip the original protected virtual SerializeToStreamAsync if this |
| | | 107 | | // isn't a derived type that may have overridden the behavior. |
| | 0 | 108 | | GetType() == typeof(StringContent) ? SerializeToStreamAsyncCore(stream, cancellationToken) : |
| | 0 | 109 | | base.SerializeToStreamAsync(stream, context, cancellationToken); |
| | | 110 | | |
| | | 111 | | internal override Stream? TryCreateContentReadStream() => |
| | 0 | 112 | | GetType() == typeof(StringContent) ? CreateMemoryStreamForByteArray() : // type check ensures we use possibl |
| | 0 | 113 | | null; |
| | | 114 | | } |
| | | 115 | | } |