|
| 1 | +using System.Buffers; |
| 2 | +using System.Net; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace Cnblogs.DashScope.Core; |
| 6 | + |
| 7 | +internal class DashScopeMultipartContent : MultipartContent |
| 8 | +{ |
| 9 | + private const string CrLf = "\r\n"; |
| 10 | + private readonly string _boundary; |
| 11 | + |
| 12 | + private DashScopeMultipartContent(string boundary) |
| 13 | + : base("form-data", boundary) |
| 14 | + { |
| 15 | + _boundary = boundary; |
| 16 | + } |
| 17 | + |
| 18 | + /// <inheritdoc /> |
| 19 | + protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context) |
| 20 | + { |
| 21 | + // Write start boundary. |
| 22 | + await EncodeStringToStreamAsync(stream, "--" + _boundary + CrLf); |
| 23 | + |
| 24 | + // Write each nested content. |
| 25 | + var output = new MemoryStream(); |
| 26 | + var contentIndex = 0; |
| 27 | + foreach (var content in this) |
| 28 | + { |
| 29 | + output.SetLength(0); |
| 30 | + SerializeHeadersToStream(output, content, writeDivider: contentIndex != 0); |
| 31 | + output.Position = 0; |
| 32 | + await output.CopyToAsync(stream); |
| 33 | + await content.CopyToAsync(stream, context); |
| 34 | + contentIndex++; |
| 35 | + } |
| 36 | + |
| 37 | + // Write footer boundary. |
| 38 | + await EncodeStringToStreamAsync(stream, CrLf + "--" + _boundary + "--" + CrLf); |
| 39 | + } |
| 40 | + |
| 41 | + /// <inheritdoc /> |
| 42 | + protected override bool TryComputeLength(out long length) |
| 43 | + { |
| 44 | + var success = base.TryComputeLength(out length); |
| 45 | + return success; |
| 46 | + } |
| 47 | + |
| 48 | + private void SerializeHeadersToStream(Stream stream, HttpContent content, bool writeDivider) |
| 49 | + { |
| 50 | + // Add divider. |
| 51 | + if (writeDivider) |
| 52 | + { |
| 53 | + WriteToStream(stream, CrLf + "--"); |
| 54 | + WriteToStream(stream, _boundary); |
| 55 | + WriteToStream(stream, CrLf); |
| 56 | + } |
| 57 | + |
| 58 | + // Add headers. |
| 59 | + foreach (var headerPair in content.Headers.NonValidated) |
| 60 | + { |
| 61 | + var headerValueEncoding = HeaderEncodingSelector?.Invoke(headerPair.Key, content) |
| 62 | + ?? Encoding.UTF8; |
| 63 | + |
| 64 | + WriteToStream(stream, headerPair.Key); |
| 65 | + WriteToStream(stream, ": "); |
| 66 | + var delim = string.Empty; |
| 67 | + foreach (var value in headerPair.Value) |
| 68 | + { |
| 69 | + WriteToStream(stream, delim); |
| 70 | + WriteToStream(stream, value, headerValueEncoding); |
| 71 | + delim = ", "; |
| 72 | + } |
| 73 | + |
| 74 | + WriteToStream(stream, CrLf); |
| 75 | + } |
| 76 | + |
| 77 | + WriteToStream(stream, CrLf); |
| 78 | + } |
| 79 | + |
| 80 | + private static void WriteToStream(Stream stream, string content) => WriteToStream(stream, content, Encoding.UTF8); |
| 81 | + |
| 82 | + private static void WriteToStream(Stream stream, string content, Encoding encoding) |
| 83 | + { |
| 84 | + const int stackallocThreshold = 1024; |
| 85 | + |
| 86 | + var maxLength = encoding.GetMaxByteCount(content.Length); |
| 87 | + |
| 88 | + byte[]? rentedBuffer = null; |
| 89 | + var buffer = maxLength <= stackallocThreshold |
| 90 | + ? stackalloc byte[stackallocThreshold] |
| 91 | + : (rentedBuffer = ArrayPool<byte>.Shared.Rent(maxLength)); |
| 92 | + |
| 93 | + try |
| 94 | + { |
| 95 | + var written = encoding.GetBytes(content, buffer); |
| 96 | + stream.Write(buffer.Slice(0, written)); |
| 97 | + } |
| 98 | + finally |
| 99 | + { |
| 100 | + if (rentedBuffer != null) |
| 101 | + { |
| 102 | + ArrayPool<byte>.Shared.Return(rentedBuffer); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static ValueTask EncodeStringToStreamAsync(Stream stream, string input) |
| 108 | + { |
| 109 | + var buffer = Encoding.UTF8.GetBytes(input); |
| 110 | + return stream.WriteAsync(new ReadOnlyMemory<byte>(buffer)); |
| 111 | + } |
| 112 | + |
| 113 | + public static DashScopeMultipartContent Create() |
| 114 | + { |
| 115 | + return Create(Guid.NewGuid().ToString()); |
| 116 | + } |
| 117 | + |
| 118 | + internal static DashScopeMultipartContent Create(string boundary) |
| 119 | + { |
| 120 | + var content = new DashScopeMultipartContent(boundary); |
| 121 | + content.Headers.ContentType = null; |
| 122 | + content.Headers.TryAddWithoutValidation( |
| 123 | + "Content-Type", |
| 124 | + $"multipart/form-data; boundary={boundary}"); |
| 125 | + return content; |
| 126 | + } |
| 127 | +} |
0 commit comments