// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.ObjectModel;
using System.IO;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
namespace System.Net.Http
{
///
/// An implementation examines the headers provided by the MIME multipart parser
/// as part of the MIME multipart extension methods (see ) and decides
/// what kind of stream to return for the body part to be written to.
///
public abstract class MultipartStreamProvider
{
private Collection _contents = new Collection();
///
/// Initializes a new instance of the class.
///
protected MultipartStreamProvider()
{
}
///
/// Gets the collection of instances where each instance represents a MIME body part.
///
public Collection Contents
{
get { return _contents; }
}
///
/// When a MIME multipart body part has been parsed this method is called to get a stream for where to write the body part to.
///
/// The parent MIME multipart instance.
/// The header fields describing the body parts content. Looking for header fields such as
/// Content-Type and Content-Disposition can help provide the appropriate stream. In addition to using the information
/// in the provided header fields, it is also possible to add new header fields or modify existing header fields. This can
/// be useful to get around situations where the Content-type may say application/octet-stream but based on
/// analyzing the Content-Disposition header field it is found that the content in fact is application/json, for example.
/// A stream instance where the contents of a body part will be written to.
public abstract Stream GetStream(HttpContent parent, HttpContentHeaders headers);
///
/// Immediately upon reading the last MIME body part but before completing the read task, this method is
/// called to enable the to do any post processing on the
/// instances that have been read. For example, it can be used to copy the data to another location, or perform
/// some other kind of post processing on the data before completing the read operation.
///
/// A representing the post processing.
public virtual Task ExecutePostProcessingAsync()
{
return TaskHelpers.Completed();
}
///
/// Immediately upon reading the last MIME body part but before completing the read task, this method is
/// called to enable the to do any post processing on the
/// instances that have been read. For example, it can be used to copy the data to another location, or perform
/// some other kind of post processing on the data before completing the read operation.
///
/// The token to monitor for cancellation requests.
/// A representing the post processing.
public virtual Task ExecutePostProcessingAsync(CancellationToken cancellationToken)
{
// Call the other overload to maintain backward compatibility.
return ExecutePostProcessingAsync();
}
}
}