// Authors:
// Lawrence Pit (loz@cable.a2000.nl)
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
+// Daniel Nauck (dna(at)mono-project(dot)de)
//
// (c) 2002 Lawrence Pit
// (c) 2003 Ximian, Inc. (http://www.ximian.com)
+// (c) 2008 Daniel Nauck
//
//
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;
// parameter = attribute "=" value
// 3.7.1. default is ISO-8859-1
get {
- CheckDisposed ();
string contentType = ContentType;
if (contentType == null)
return "ISO-8859-1";
}
public string ContentEncoding {
- get {
+ get {
CheckDisposed ();
string h = webHeaders ["Content-Encoding"];
return h != null ? h : "";
public override long ContentLength {
get {
- CheckDisposed ();
- 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;
}
public override WebHeaderCollection Headers {
- get {
- CheckDisposed ();
+ get {
return webHeaders;
}
}
-#if NET_2_0
static Exception GetMustImplement ()
{
return new NotImplementedException ();
throw GetMustImplement ();
}
}
-#endif
public DateTime LastModified {
get {
}
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 {
- CheckDisposed ();
+ get {
return statusCode;
}
}
public string StatusDescription {
- get {
+ get {
CheckDisposed ();
return statusDescription;
}
}
// Methods
-#if !NET_2_0
- public override int GetHashCode ()
- {
- CheckDisposed ();
- return base.GetHashCode ();
- }
-#endif
public string GetResponseHeader (string headerName)
{
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;
+ case "HTTPONLY":
+ cookie.HttpOnly = true;
+ break;
case "MAX-AGE": // RFC Style Set-Cookie2
if (cookie.Expires == DateTime.MinValue) {
try {
case "EXPIRES": // Netscape Style Set-Cookie
if (cookie.Expires != DateTime.MinValue)
break;
- try {
- cookie.Expires = DateTime.ParseExact (val, "r", CultureInfo.InvariantCulture);
- } catch {
- try {
- cookie.Expires = DateTime.ParseExact (val,
- "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'",
- CultureInfo.InvariantCulture);
- } catch {
- cookie.Expires = DateTime.Now.AddDays (1);
- }
- }
+
+ cookie.Expires = TryParseCookieExpires (val);
break;
case "PATH":
cookie.Path = val;
foreach (string cookie_str in cookies)
SetCookie (cookie_str);
}
+
+ string[] cookieExpiresFormats =
+ new string[] { "r",
+ "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'",
+ "ddd, dd'-'MMM'-'yy HH':'mm':'ss 'GMT'" };
+
+ DateTime TryParseCookieExpires (string value)
+ {
+ if (value == null || value.Length == 0)
+ return DateTime.MinValue;
+
+ for (int i = 0; i < cookieExpiresFormats.Length; i++)
+ {
+ try {
+ DateTime cookieExpiresUtc = DateTime.ParseExact (value, cookieExpiresFormats [i], CultureInfo.InvariantCulture);
+
+ //convert UTC/GMT time to local time
+ cookieExpiresUtc = DateTime.SpecifyKind (cookieExpiresUtc, DateTimeKind.Utc);
+ return TimeZone.CurrentTimeZone.ToLocalTime (cookieExpiresUtc);
+ } catch {}
+ }
+
+ //If we can't parse Expires, use cookie as session cookie (expires is DateTime.MinValue)
+ return DateTime.MinValue;
+ }
}
class CookieParser {