// 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 cookieContainer;
+ 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.cookieContainer = container;
+ this.cookie_container = container;
FillCookies ();
- } else if (webHeaders != null) {
- webHeaders.RemoveInternal ("Set-Cookie");
- webHeaders.RemoveInternal ("Set-Cookie2");
}
+
+ 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);
}
+ [Obsolete ("Serialization is obsoleted for this type", false)]
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 ();
- return webHeaders ["Content-Encoding"];
+ 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;
}
}
+
+ static Exception GetMustImplement ()
+ {
+ return new NotImplementedException ();
+ }
+
+ [MonoTODO]
+ public override bool IsMutuallyAuthenticated
+ {
+ get {
+ throw GetMustImplement ();
+ }
+ }
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
- public override int GetHashCode ()
- {
- CheckDisposed ();
- return base.GetHashCode ();
- }
-
public string GetResponseHeader (string headerName)
{
CheckDisposed ();
void ISerializable.GetObjectData (SerializationInfo serializationInfo,
StreamingContext streamingContext)
+ {
+ GetObjectData (serializationInfo, streamingContext);
+ }
+
+ protected override void GetObjectData (SerializationInfo serializationInfo,
+ StreamingContext streamingContext)
{
SerializationInfo info = serializationInfo;
info.AddValue ("cookieCollection", cookieCollection);
info.AddValue ("version", version);
info.AddValue ("statusCode", statusCode);
- }
-
+ }
// Cleaning up stuff
Dispose (true);
GC.SuppressFinalize (this);
}
-
- protected virtual void Dispose (bool disposing)
+
+ void Dispose (bool disposing)
{
if (this.disposed)
return;
if (disposing) {
// release managed resources
uri = null;
- webHeaders = null;
cookieCollection = null;
method = null;
version = null;
CookieParser parser = new CookieParser (header);
while (parser.GetNextNameValue (out name, out val)) {
- if (name == null || name == "")
+ if ((name == null || name == "") && cookie == null)
continue;
if (cookie == 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)
- cookie.Expires = cookie.TimeStamp.AddSeconds (Int32.Parse (val));
+ if (cookie.Expires == DateTime.MinValue) {
+ try {
+ cookie.Expires = cookie.TimeStamp.AddSeconds (UInt32.Parse (val));
+ } catch {}
+ }
break;
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;
cookie.Secure = true;
break;
case "VERSION":
- cookie.Version = Int32.Parse (val);
+ try {
+ cookie.Version = (int) UInt32.Parse (val);
+ } catch {}
break;
}
}
cookie.Domain = uri.Host;
cookieCollection.Add (cookie);
- if (cookieContainer != null)
- cookieContainer.Add (uri, cookie);
+ if (cookie_container != null)
+ cookie_container.Add (uri, cookie);
}
void SetCookie2 (string cookies_str)
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 {
return false;
name = GetCookieName ();
- if (header [pos] == '=') {
+ if (pos < header.Length && header [pos] == '=') {
pos++;
val = GetCookieValue ();
- if (pos < length && header [pos] == ';')
- pos++;
}
+ if (pos < length && header [pos] == ';')
+ pos++;
+
return true;
}