/// <summary> /// Gets the output stream. /// </summary> /// <returns>The output stream to write the body part to.</returns> public Stream GetOutputStream() { if (_outputStream == null) { try { _outputStream = _streamProvider.GetStream(_headers); } catch (Exception e) { throw new InvalidOperationException(RS.Format(Properties.Resources.ReadAsMimeMultipartStreamProviderException, _streamProvider.GetType().Name), e); } if (_outputStream == null) { throw new InvalidOperationException( RS.Format(Properties.Resources.ReadAsMimeMultipartStreamProviderNull, _streamProvider.GetType().Name, _streamType.Name)); } if (!_outputStream.CanWrite) { throw new InvalidOperationException( RS.Format(Properties.Resources.ReadAsMimeMultipartStreamProviderReadOnly, _streamProvider.GetType().Name, _streamType.Name)); } HttpContent = new StreamContent(_outputStream); _headers.CopyTo(HttpContent.Headers); } return(_outputStream); }
/// <summary> /// Gets the output stream. /// </summary> /// <returns>The output stream to write the body part to.</returns> public Stream GetOutputStream(HttpContent parent) { Contract.Assert(parent != null); if (_outputStream == null) { try { _outputStream = _streamProvider.GetStream(parent, _headers); } catch (Exception e) { throw Error.InvalidOperation(e, Properties.Resources.ReadAsMimeMultipartStreamProviderException, _streamProvider.GetType().Name); } if (_outputStream == null) { throw Error.InvalidOperation(Properties.Resources.ReadAsMimeMultipartStreamProviderNull, _streamProvider.GetType().Name, _streamType.Name); } if (!_outputStream.CanWrite) { throw Error.InvalidOperation(Properties.Resources.ReadAsMimeMultipartStreamProviderReadOnly, _streamProvider.GetType().Name, _streamType.Name); } HttpContent = new StreamContent(_outputStream); _headers.CopyTo(HttpContent.Headers); } return(_outputStream); }
/// <summary> /// Gets the part's content as an HttpContent. /// </summary> /// <value> /// The part's content, or null if the part had no content. /// </value> public HttpContent GetCompletedHttpContent() { Contract.Assert(IsComplete); if (_content == null) { return(null); } _headers.CopyTo(_content.Headers); return(_content); }
/// <summary> /// Copies the unsorted header fields to a sorted collection. /// </summary> /// <param name="source">The unsorted source headers</param> /// <param name="destination">The destination <see cref="HttpRequestHeaders"/> or <see cref="HttpResponseHeaders"/>.</param> /// <param name="contentStream">The input <see cref="Stream"/> used to form any <see cref="HttpContent"/> being part of this HTTP request.</param> /// <param name="rewind">Start location of any request entity within the <paramref name="contentStream"/>.</param> /// <returns>An <see cref="HttpContent"/> instance if header fields contained and <see cref="HttpContentHeaders"/>.</returns> private static HttpContent CreateHeaderFields( HttpHeaders source, HttpHeaders destination, Stream contentStream, int rewind ) { Contract.Assert(source != null, "source headers cannot be null"); Contract.Assert(destination != null, "destination headers cannot be null"); Contract.Assert(contentStream != null, "contentStream must be non null"); HttpContentHeaders contentHeaders = null; HttpContent content = null; // Set the header fields foreach (KeyValuePair <string, IEnumerable <string> > header in source) { if (!destination.TryAddWithoutValidation(header.Key, header.Value)) { if (contentHeaders == null) { contentHeaders = FormattingUtilities.CreateEmptyContentHeaders(); } contentHeaders.TryAddWithoutValidation(header.Key, header.Value); } } // If we have content headers then create an HttpContent for this Response if (contentHeaders != null) { // Need to rewind the input stream to be at the position right after the HTTP header // which we may already have parsed as we read the content stream. if (!contentStream.CanSeek) { throw Error.InvalidOperation( Properties.Resources.HttpMessageContentStreamMustBeSeekable, "ContentReadStream", FormattingUtilities.HttpResponseMessageType.Name ); } contentStream.Seek(0 - rewind, SeekOrigin.Current); content = new StreamContent(contentStream); contentHeaders.CopyTo(content.Headers); } return(content); }
public void CopyTo_CopiesContentHeaders() { // Arrange HttpContentHeaders source = FormattingUtilities.CreateEmptyContentHeaders(); source.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf8; parameter=value"); source.ContentLength = 1234; source.ContentLocation = new Uri("http://some.host"); source.Add("test-name1", "test-value1"); source.Add("test-name2", "test-value2"); HttpContentHeaders destination = FormattingUtilities.CreateEmptyContentHeaders(); // Act source.CopyTo(destination); // Assert Assert.Equal(source.ToString(), destination.ToString()); }