//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Messaging {
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using Validation;
///
/// A live network HTTP response
///
[DebuggerDisplay("{Status} {ContentType.MediaType}")]
internal class NetworkDirectWebResponse : IncomingWebResponse, IDisposable {
///
/// The network response object, used to initialize this instance, that still needs
/// to be closed if applicable.
///
private HttpWebResponse httpWebResponse;
///
/// The incoming network response stream.
///
private Stream responseStream;
///
/// A value indicating whether a stream reader has already been
/// created on this instance.
///
private bool streamReadBegun;
///
/// Initializes a new instance of the class.
///
/// The request URI.
/// The response.
internal NetworkDirectWebResponse(Uri requestUri, HttpWebResponse response)
: base(requestUri, response) {
Requires.NotNull(requestUri, "requestUri");
Requires.NotNull(response, "response");
this.httpWebResponse = response;
this.responseStream = response.GetResponseStream();
}
///
/// Gets the body of the HTTP response.
///
public override Stream ResponseStream {
get { return this.responseStream; }
}
///
/// Creates a text reader for the response stream.
///
/// The text reader, initialized for the proper encoding.
public override StreamReader GetResponseReader() {
this.streamReadBegun = true;
if (this.responseStream == null) {
throw new ObjectDisposedException(GetType().Name);
}
string contentEncoding = this.Headers[HttpResponseHeader.ContentEncoding];
if (string.IsNullOrEmpty(contentEncoding)) {
return new StreamReader(this.ResponseStream);
} else {
return new StreamReader(this.ResponseStream, Encoding.GetEncoding(contentEncoding));
}
}
///
/// Gets an offline snapshot version of this instance.
///
/// The maximum bytes from the response stream to cache.
/// A snapshot version of this instance.
///
/// If this instance is a creating a snapshot
/// will automatically close and dispose of the underlying response stream.
/// If this instance is a , the result will
/// be the self same instance.
///
internal override CachedDirectWebResponse GetSnapshot(int maximumBytesToCache) {
ErrorUtilities.VerifyOperation(!this.streamReadBegun, "Network stream reading has already begun.");
ErrorUtilities.VerifyOperation(this.httpWebResponse != null, "httpWebResponse != null");
this.streamReadBegun = true;
var result = new CachedDirectWebResponse(this.RequestUri, this.httpWebResponse, maximumBytesToCache);
this.Dispose();
return result;
}
///
/// Releases unmanaged and - optionally - managed resources
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
protected override void Dispose(bool disposing) {
if (disposing) {
if (this.responseStream != null) {
this.responseStream.Dispose();
this.responseStream = null;
}
if (this.httpWebResponse != null) {
this.httpWebResponse.Close();
this.httpWebResponse = null;
}
}
base.Dispose(disposing);
}
}
}