using System.Collections;
using System.Globalization;
using System.IO;
+using System.IO.Compression;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Text;
namespace System.Net
{
+#if MOONLIGHT
+ internal class HttpWebResponse : WebResponse, ISerializable, IDisposable {
+#else
[Serializable]
- public class HttpWebResponse : WebResponse, ISerializable, IDisposable
- {
+ public class HttpWebResponse : WebResponse, ISerializable, IDisposable {
+#endif
Uri uri;
WebHeaderCollection webHeaders;
CookieCollection cookieCollection;
Version version;
HttpStatusCode statusCode;
string statusDescription;
- long contentLength = -1;
+ long contentLength;
string contentType;
CookieContainer cookie_container;
- bool disposed = false;
+ bool disposed;
Stream stream;
// Constructors
statusCode = (HttpStatusCode) data.StatusCode;
statusDescription = data.StatusDescription;
stream = data.stream;
+ contentLength = -1;
+
+ try {
+ string cl = webHeaders ["Content-Length"];
+ if (String.IsNullOrEmpty (cl) || !Int64.TryParse (cl, out contentLength))
+ contentLength = -1;
+ } catch (Exception) {
+ contentLength = -1;
+ }
+
if (container != null) {
this.cookie_container = container;
FillCookies ();
}
+
+ string content_encoding = webHeaders ["Content-Encoding"];
+ if (content_encoding == "gzip" && (data.request.AutomaticDecompression & DecompressionMethods.GZip) != 0)
+ stream = new GZipStream (stream, CompressionMode.Decompress);
+ else if (content_encoding == "deflate" && (data.request.AutomaticDecompression & DecompressionMethods.Deflate) != 0)
+ stream = new DeflateStream (stream, CompressionMode.Decompress);
}
-#if NET_2_0
[Obsolete ("Serialization is obsoleted for this type", false)]
-#endif
protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
{
SerializationInfo info = serializationInfo;
}
public string ContentEncoding {
- get {
+ get {
+ CheckDisposed ();
string h = webHeaders ["Content-Encoding"];
return h != null ? h : "";
}
public override long ContentLength {
get {
- if (contentLength != -1)
- return contentLength;
-
- try {
- contentLength = (long) UInt64.Parse (webHeaders ["Content-Length"]);
- } catch (Exception) {
- return -1;
- }
-
return contentLength;
}
}
public override string ContentType {
get {
+ CheckDisposed ();
+
if (contentType == null)
contentType = webHeaders ["Content-Type"];
}
public CookieCollection Cookies {
- get {
+ get {
+ CheckDisposed ();
if (cookieCollection == null)
cookieCollection = new CookieCollection ();
return cookieCollection;
}
set {
+ CheckDisposed ();
cookieCollection = value;
}
}
public override WebHeaderCollection Headers {
- get {
+ get {
return webHeaders;
}
}
-#if NET_2_0
static Exception GetMustImplement ()
{
return new NotImplementedException ();
throw GetMustImplement ();
}
}
-#endif
public DateTime LastModified {
get {
+ CheckDisposed ();
try {
string dtStr = webHeaders ["Last-Modified"];
return MonoHttpDate.Parse (dtStr);
}
public string Method {
- get {
+ get {
+ CheckDisposed ();
return method;
}
}
public Version ProtocolVersion {
- get {
+ get {
+ CheckDisposed ();
return version;
}
}
public override Uri ResponseUri {
- get {
+ get {
+ CheckDisposed ();
return uri;
}
}
public string Server {
- get {
+ get {
+ CheckDisposed ();
return webHeaders ["Server"];
}
}
public HttpStatusCode StatusCode {
- get {
+ get {
return statusCode;
}
}
public string StatusDescription {
- get {
+ get {
+ CheckDisposed ();
return statusDescription;
}
}
// Methods
-#if !NET_2_0
- public override int GetHashCode ()
- {
- return base.GetHashCode ();
- }
-#endif
public string GetResponseHeader (string headerName)
{
+ CheckDisposed ();
string value = webHeaders [headerName];
return (value != null) ? value : "";
}
GetObjectData (serializationInfo, streamingContext);
}
-#if NET_2_0
- protected override
-#endif
- void GetObjectData (SerializationInfo serializationInfo,
- StreamingContext streamingContext)
+ protected override void GetObjectData (SerializationInfo serializationInfo,
+ StreamingContext streamingContext)
{
SerializationInfo info = serializationInfo;
GC.SuppressFinalize (this);
}
-#if !NET_2_0
- protected virtual
-#endif
void Dispose (bool disposing)
{
if (this.disposed)
if (disposing) {
// release managed resources
uri = null;
- webHeaders = null;
cookieCollection = null;
method = null;
version = null;
if (cookie.Domain == "")
cookie.Domain = val;
break;
-#if NET_2_0
case "HTTPONLY":
cookie.HttpOnly = true;
break;
-#endif
case "MAX-AGE": // RFC Style Set-Cookie2
if (cookie.Expires == DateTime.MinValue) {
try {
DateTime cookieExpiresUtc = DateTime.ParseExact (value, cookieExpiresFormats [i], CultureInfo.InvariantCulture);
//convert UTC/GMT time to local time
-#if NET_2_0
cookieExpiresUtc = DateTime.SpecifyKind (cookieExpiresUtc, DateTimeKind.Utc);
return TimeZone.CurrentTimeZone.ToLocalTime (cookieExpiresUtc);
-#else
- //DateTime.Kind is only available on .NET 2.0, so do some calculation
- TimeSpan localOffset = TimeZone.CurrentTimeZone.GetUtcOffset (cookieExpiresUtc.Date);
- return cookieExpiresUtc.Add (localOffset);
-#endif
} catch {}
}