protected override void Dispose(bool disposing) { if (disposing) { _originalContent.Dispose(); } base.Dispose(disposing); }
public HttpResponseMessage EnsureSuccessStatusCode() { if (!IsSuccessStatusCode) { // Disposing the content should help users: If users call EnsureSuccessStatusCode(), an exception is // thrown if the response status code is != 2xx. I.e. the behavior is similar to a failed request (e.g. // connection failure). Users don't expect to dispose the content in this case: If an exception is // thrown, the object is responsible fore cleaning up its state. if (_content != null) { _content.Dispose(); } throw new HttpRequestException(string.Format(System.Globalization.CultureInfo.InvariantCulture, SR.net_http_message_not_success_statuscode, (int)_statusCode, ReasonPhrase)); } return(this); }
/// <summary> /// In the success case, the HttpContent is to be used after this Part has been parsed and disposed of. /// Only if Dispose has been called on a non-completed part, the parsed HttpContent needs to be disposed of as well. /// </summary> private void CleanupHttpContent() { if (!IsComplete && _content != null) { _content.Dispose(); } _content = null; }
public HttpResponseMessage EnsureSuccessStatusCode() { if (!IsSuccessStatusCode) { // Disposing the content should help users: If users call EnsureSuccessStatusCode(), an exception is // thrown if the response status code is != 2xx. I.e. the behavior is similar to a failed request (e.g. // connection failure). Users don't expect to dispose the content in this case: If an exception is // thrown, the object is responsible fore cleaning up its state. if (content != null) { content.Dispose(); } throw new HttpException(string.Format("Response status code does not indicate success: {0} ({1})", (int)statusCode, reasonPhrase)); } return(this); }
protected virtual void Dispose(bool disposing) { // The reason for this type to implement IDisposable is that it contains instances of types that implement // IDisposable (content). if (disposing && !_disposed) { _disposed = true; _content?.Dispose(); } }
private void DisposeRequestContent(HttpRequestMessage request) { Contract.Requires(request != null); // When a request completes, HttpClient disposes the request content so the user doesn't have to. This also // ensures that a HttpContent object is only sent once using HttpClient (similar to HttpRequestMessages // that can also be sent only once). HttpContent content = request.Content; if (content != null) { content.Dispose(); } }
/// <summary> /// <see cref="HttpContent"/> implementation which provides a byte range view over a stream used to generate HTTP /// 206 (Partial Content) byte range responses. If none of the requested ranges overlap with the current extend /// of the selected resource represented by the <paramref name="content"/> parameter then an /// <see cref="InvalidByteRangeException"/> is thrown indicating the valid Content-Range of the content. /// </summary> /// <param name="content">The stream over which to generate a byte range view.</param> /// <param name="range">The range or ranges, typically obtained from the Range HTTP request header field.</param> /// <param name="mediaType">The media type of the content stream.</param> /// <param name="bufferSize">The buffer size used when copying the content stream.</param> public ByteRangeStreamContent(Stream content, RangeHeaderValue range, MediaTypeHeaderValue mediaType, int bufferSize) { if (content == null) { throw Error.ArgumentNull("content"); } if (!content.CanSeek) { throw Error.Argument("content", Properties.Resources.ByteRangeStreamNotSeekable, typeof(ByteRangeStreamContent).Name); } if (range == null) { throw Error.ArgumentNull("range"); } if (mediaType == null) { throw Error.ArgumentNull("mediaType"); } if (bufferSize < MinBufferSize) { throw Error.ArgumentMustBeGreaterThanOrEqualTo("bufferSize", bufferSize, MinBufferSize); } if (!range.Unit.Equals(SupportedRangeUnit, StringComparison.OrdinalIgnoreCase)) { throw Error.Argument("range", Properties.Resources.ByteRangeStreamContentNotBytesRange, range.Unit, SupportedRangeUnit); } try { // If we have more than one range then we use a multipart/byteranges content type as wrapper. // Otherwise we use a non-multipart response. if (range.Ranges.Count > 1) { // Create Multipart content and copy headers to this content MultipartContent rangeContent = new MultipartContent(ByteRangesContentSubtype); _byteRangeContent = rangeContent; foreach (RangeItemHeaderValue rangeValue in range.Ranges) { try { ByteRangeStream rangeStream = new ByteRangeStream(content, rangeValue); HttpContent rangeBodyPart = new StreamContent(rangeStream, bufferSize); rangeBodyPart.Headers.ContentType = mediaType; rangeBodyPart.Headers.ContentRange = rangeStream.ContentRange; rangeContent.Add(rangeBodyPart); } catch (ArgumentOutOfRangeException) { // We ignore range errors until we check that we have at least one valid range } } // If no overlapping ranges were found then stop if (!rangeContent.Any()) { ContentRangeHeaderValue actualContentRange = new ContentRangeHeaderValue(content.Length); string msg = Error.Format(Properties.Resources.ByteRangeStreamNoneOverlap, range.ToString()); throw new InvalidByteRangeException(actualContentRange, msg); } } else if (range.Ranges.Count == 1) { try { ByteRangeStream rangeStream = new ByteRangeStream(content, range.Ranges.First()); _byteRangeContent = new StreamContent(rangeStream, bufferSize); _byteRangeContent.Headers.ContentType = mediaType; _byteRangeContent.Headers.ContentRange = rangeStream.ContentRange; } catch (ArgumentOutOfRangeException) { ContentRangeHeaderValue actualContentRange = new ContentRangeHeaderValue(content.Length); string msg = Error.Format(Properties.Resources.ByteRangeStreamNoOverlap, range.ToString()); throw new InvalidByteRangeException(actualContentRange, msg); } } else { throw Error.Argument("range", Properties.Resources.ByteRangeStreamContentNoRanges); } // Copy headers from byte range content so that we get the right content type etc. _byteRangeContent.Headers.CopyTo(Headers); _content = content; _start = content.Position; } catch { if (_byteRangeContent != null) { _byteRangeContent.Dispose(); } throw; } }