|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
| 4 | +using System.ClientModel; |
| 5 | +using System.Globalization; |
| 6 | +using System; |
4 | 7 | using System.IO; |
| 8 | +using System.Net.Http.Headers; |
| 9 | +using System.Net.Http; |
| 10 | +using System.Threading; |
5 | 11 | using System.Threading.Tasks; |
6 | 12 | using NUnit.Framework; |
7 | 13 | using Payload.MultiPart; |
@@ -219,5 +225,185 @@ private Task MultiBinaryParts(bool hasPicture) => Test(async (host) => |
219 | 225 | var response = await new MultiPartClient(host, null).GetFormDataClient().MultiBinaryPartsAsync(content, content.ContentType, null); |
220 | 226 | Assert.AreEqual(204, response.GetRawResponse().Status); |
221 | 227 | }); |
| 228 | + |
| 229 | + internal partial class MultiPartFormDataBinaryContent : BinaryContent |
| 230 | + { |
| 231 | + private readonly MultipartFormDataContent _multipartContent; |
| 232 | + private static readonly Random _random = new Random(); |
| 233 | + private static readonly char[] _boundaryValues = "0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".ToCharArray(); |
| 234 | + |
| 235 | + public MultiPartFormDataBinaryContent() |
| 236 | + { |
| 237 | + _multipartContent = new MultipartFormDataContent(CreateBoundary()); |
| 238 | + } |
| 239 | + |
| 240 | + public string? ContentType |
| 241 | + { |
| 242 | + get |
| 243 | + { |
| 244 | + return _multipartContent.Headers.ContentType?.ToString(); |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + internal HttpContent HttpContent => _multipartContent; |
| 249 | + |
| 250 | + private static string CreateBoundary() |
| 251 | + { |
| 252 | + Span<char> chars = new char[70]; |
| 253 | + byte[] random = new byte[70]; |
| 254 | + _random.NextBytes(random); |
| 255 | + int mask = 255 >> 2; |
| 256 | + int i = 0; |
| 257 | + for (; i < 70; i++) |
| 258 | + { |
| 259 | + chars[i] = _boundaryValues[random[i] & mask]; |
| 260 | + } |
| 261 | + return chars.ToString(); |
| 262 | + } |
| 263 | + |
| 264 | + public void Add(string content, string name, string? filename = default, string? contentType = default) |
| 265 | + { |
| 266 | + ArgumentNullException.ThrowIfNull(content, nameof(content)); |
| 267 | + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); |
| 268 | + |
| 269 | + Add(new StringContent(content), name, filename, contentType); |
| 270 | + } |
| 271 | + |
| 272 | + public void Add(int content, string name, string? filename = default, string? contentType = default) |
| 273 | + { |
| 274 | + ArgumentNullException.ThrowIfNull(content, nameof(content)); |
| 275 | + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); |
| 276 | + |
| 277 | + string value = content.ToString("G", CultureInfo.InvariantCulture); |
| 278 | + Add(new StringContent(value), name, filename, contentType); |
| 279 | + } |
| 280 | + |
| 281 | + public void Add(long content, string name, string? filename = default, string? contentType = default) |
| 282 | + { |
| 283 | + ArgumentNullException.ThrowIfNull(content, nameof(content)); |
| 284 | + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); |
| 285 | + |
| 286 | + string value = content.ToString("G", CultureInfo.InvariantCulture); |
| 287 | + Add(new StringContent(value), name, filename, contentType); |
| 288 | + } |
| 289 | + |
| 290 | + public void Add(float content, string name, string? filename = default, string? contentType = default) |
| 291 | + { |
| 292 | + ArgumentNullException.ThrowIfNull(content, nameof(content)); |
| 293 | + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); |
| 294 | + |
| 295 | + string value = content.ToString("G", CultureInfo.InvariantCulture); |
| 296 | + Add(new StringContent(value), name, filename, contentType); |
| 297 | + } |
| 298 | + |
| 299 | + public void Add(double content, string name, string? filename = default, string? contentType = default) |
| 300 | + { |
| 301 | + ArgumentNullException.ThrowIfNull(content, nameof(content)); |
| 302 | + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); |
| 303 | + |
| 304 | + string value = content.ToString("G", CultureInfo.InvariantCulture); |
| 305 | + Add(new StringContent(value), name, filename, contentType); |
| 306 | + } |
| 307 | + |
| 308 | + public void Add(decimal content, string name, string? filename = default, string? contentType = default) |
| 309 | + { |
| 310 | + ArgumentNullException.ThrowIfNull(content, nameof(content)); |
| 311 | + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); |
| 312 | + |
| 313 | + string value = content.ToString("G", CultureInfo.InvariantCulture); |
| 314 | + Add(new StringContent(value), name, filename, contentType); |
| 315 | + } |
| 316 | + |
| 317 | + public void Add(bool content, string name, string? filename = default, string? contentType = default) |
| 318 | + { |
| 319 | + ArgumentNullException.ThrowIfNull(content, nameof(content)); |
| 320 | + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); |
| 321 | + |
| 322 | + string value = content ? "true" : "false"; |
| 323 | + Add(new StringContent(value), name, filename, contentType); |
| 324 | + } |
| 325 | + |
| 326 | + public void Add(Stream content, string name, string? filename = default, string? contentType = default) |
| 327 | + { |
| 328 | + ArgumentNullException.ThrowIfNull(content, nameof(content)); |
| 329 | + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); |
| 330 | + |
| 331 | + Add(new StreamContent(content), name, filename, contentType); |
| 332 | + } |
| 333 | + |
| 334 | + public void Add(byte[] content, string name, string? filename = default, string? contentType = default) |
| 335 | + { |
| 336 | + ArgumentNullException.ThrowIfNull(content, nameof(content)); |
| 337 | + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); |
| 338 | + |
| 339 | + Add(new ByteArrayContent(content), name, filename, contentType); |
| 340 | + } |
| 341 | + |
| 342 | + public void Add(BinaryData content, string name, string? filename = default, string? contentType = default) |
| 343 | + { |
| 344 | + ArgumentNullException.ThrowIfNull(content, nameof(content)); |
| 345 | + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); |
| 346 | + |
| 347 | + Add(new ByteArrayContent(content.ToArray()), name, filename, contentType); |
| 348 | + } |
| 349 | + |
| 350 | + private void Add(HttpContent content, string name, string? filename, string? contentType) |
| 351 | + { |
| 352 | + if (contentType != null) |
| 353 | + { |
| 354 | + ArgumentNullException.ThrowIfNullOrEmpty(contentType, nameof(contentType)); |
| 355 | + AddContentTypeHeader(content, contentType); |
| 356 | + } |
| 357 | + if (filename != null) |
| 358 | + { |
| 359 | + ArgumentNullException.ThrowIfNullOrEmpty(filename, nameof(filename)); |
| 360 | + _multipartContent.Add(content, name, filename); |
| 361 | + } |
| 362 | + else |
| 363 | + { |
| 364 | + _multipartContent.Add(content, name); |
| 365 | + } |
| 366 | + } |
| 367 | + |
| 368 | + public static void AddContentTypeHeader(HttpContent content, string contentType) |
| 369 | + { |
| 370 | + MediaTypeHeaderValue header = new MediaTypeHeaderValue(contentType); |
| 371 | + content.Headers.ContentType = header; |
| 372 | + } |
| 373 | + |
| 374 | + public override bool TryComputeLength(out long length) |
| 375 | + { |
| 376 | + if (_multipartContent.Headers.ContentLength is long contentLength) |
| 377 | + { |
| 378 | + length = contentLength; |
| 379 | + return true; |
| 380 | + } |
| 381 | + length = 0; |
| 382 | + return false; |
| 383 | + } |
| 384 | + |
| 385 | + public override void WriteTo(Stream stream, CancellationToken cancellationToken = default) |
| 386 | + { |
| 387 | +#if NET6_0_OR_GREATER |
| 388 | + _multipartContent.CopyTo(stream, default, cancellationToken); |
| 389 | +#else |
| 390 | + _multipartContent.CopyToAsync(stream).GetAwaiter().GetResult(); |
| 391 | +#endif |
| 392 | + } |
| 393 | + |
| 394 | + public override async Task WriteToAsync(Stream stream, CancellationToken cancellationToken = default) |
| 395 | + { |
| 396 | +#if NET6_0_OR_GREATER |
| 397 | + await _multipartContent.CopyToAsync(stream).ConfigureAwait(false); |
| 398 | +#else |
| 399 | + await _multipartContent.CopyToAsync(stream).ConfigureAwait(false); |
| 400 | +#endif |
| 401 | + } |
| 402 | + |
| 403 | + public override void Dispose() |
| 404 | + { |
| 405 | + _multipartContent.Dispose(); |
| 406 | + } |
| 407 | + } |
222 | 408 | } |
223 | 409 | } |
0 commit comments