HttpWebResponse.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. //
  2. // System.Net.HttpWebResponse
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (c) 2002 Lawrence Pit
  9. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  10. //
  11. using System;
  12. using System.IO;
  13. using System.Net.Sockets;
  14. using System.Runtime.Serialization;
  15. using System.Text;
  16. namespace System.Net
  17. {
  18. [Serializable]
  19. public class HttpWebResponse : WebResponse, ISerializable, IDisposable
  20. {
  21. Uri uri;
  22. WebHeaderCollection webHeaders;
  23. CookieCollection cookieCollection = null;
  24. string method;
  25. Version version;
  26. HttpStatusCode statusCode;
  27. string statusDescription;
  28. bool chunked;
  29. long contentLength = -1;
  30. string contentType;
  31. bool disposed = false;
  32. WebConnectionStream stream;
  33. // Constructors
  34. internal HttpWebResponse (Uri uri, string method, WebConnectionData data)
  35. {
  36. this.uri = uri;
  37. this.method = method;
  38. webHeaders = data.Headers;
  39. version = data.Version;
  40. statusCode = (HttpStatusCode) data.StatusCode;
  41. statusDescription = data.StatusDescription;
  42. stream = data.stream;
  43. FillCookies ();
  44. }
  45. [MonoTODO("Check this out and update if needed")] //Gon
  46. protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
  47. {
  48. uri = (Uri) serializationInfo.GetValue ("uri", typeof (Uri));
  49. webHeaders = (WebHeaderCollection) serializationInfo.GetValue ("webHeaders",
  50. typeof (WebHeaderCollection));
  51. cookieCollection = (CookieCollection) serializationInfo.GetValue ("cookieCollection",
  52. typeof (CookieCollection));
  53. method = serializationInfo.GetString ("method");
  54. version = (Version) serializationInfo.GetValue ("version", typeof (Version));
  55. statusCode = (HttpStatusCode) serializationInfo.GetValue ("statusCode", typeof (HttpStatusCode));
  56. statusDescription = serializationInfo.GetString ("statusDescription");
  57. chunked = serializationInfo.GetBoolean ("chunked");
  58. }
  59. // Properties
  60. public string CharacterSet {
  61. // Content-Type = "Content-Type" ":" media-type
  62. // media-type = type "/" subtype *( ";" parameter )
  63. // parameter = attribute "=" value
  64. // 3.7.1. default is ISO-8859-1
  65. get {
  66. CheckDisposed ();
  67. string contentType = ContentType;
  68. if (contentType == null)
  69. return "ISO-8859-1";
  70. string val = contentType.ToLower ();
  71. int pos = val.IndexOf ("charset=");
  72. if (pos == -1)
  73. return "ISO-8859-1";
  74. pos += 8;
  75. int pos2 = val.IndexOf (';', pos);
  76. return (pos2 == -1)
  77. ? contentType.Substring (pos)
  78. : contentType.Substring (pos, pos2 - pos);
  79. }
  80. }
  81. public string ContentEncoding {
  82. get {
  83. CheckDisposed ();
  84. return webHeaders ["Content-Encoding"];
  85. }
  86. }
  87. public override long ContentLength {
  88. get {
  89. CheckDisposed ();
  90. if (contentLength != -1)
  91. return contentLength;
  92. try {
  93. contentLength = (long) UInt64.Parse (webHeaders ["Content-Length"]);
  94. } catch (Exception) {
  95. return -1;
  96. }
  97. return contentLength;
  98. }
  99. }
  100. public override string ContentType {
  101. get {
  102. CheckDisposed ();
  103. if (contentType == null)
  104. contentType = webHeaders ["Content-Type"];
  105. return contentType;
  106. }
  107. }
  108. public CookieCollection Cookies {
  109. get {
  110. CheckDisposed ();
  111. if (cookieCollection == null)
  112. cookieCollection = new CookieCollection ();
  113. return cookieCollection;
  114. }
  115. set {
  116. CheckDisposed ();
  117. cookieCollection = value;
  118. }
  119. }
  120. public override WebHeaderCollection Headers {
  121. get {
  122. CheckDisposed ();
  123. return webHeaders;
  124. }
  125. }
  126. public DateTime LastModified {
  127. get {
  128. CheckDisposed ();
  129. try {
  130. string dtStr = webHeaders ["Last-Modified"];
  131. return MonoHttpDate.Parse (dtStr);
  132. } catch (Exception) {
  133. return DateTime.Now;
  134. }
  135. }
  136. }
  137. public string Method {
  138. get {
  139. CheckDisposed ();
  140. return method;
  141. }
  142. }
  143. public Version ProtocolVersion {
  144. get {
  145. CheckDisposed ();
  146. return version;
  147. }
  148. }
  149. public override Uri ResponseUri {
  150. get {
  151. CheckDisposed ();
  152. return uri;
  153. }
  154. }
  155. public string Server {
  156. get {
  157. CheckDisposed ();
  158. return webHeaders ["Server"];
  159. }
  160. }
  161. public HttpStatusCode StatusCode {
  162. get {
  163. CheckDisposed ();
  164. return statusCode;
  165. }
  166. }
  167. public string StatusDescription {
  168. get {
  169. CheckDisposed ();
  170. return statusDescription;
  171. }
  172. }
  173. // Methods
  174. public override int GetHashCode ()
  175. {
  176. CheckDisposed ();
  177. return base.GetHashCode ();
  178. }
  179. public string GetResponseHeader (string headerName)
  180. {
  181. CheckDisposed ();
  182. string value = webHeaders [headerName];
  183. return (value != null) ? value : "";
  184. }
  185. public override Stream GetResponseStream ()
  186. {
  187. CheckDisposed ();
  188. if (0 == String.Compare (method, "HEAD", true)) // see par 4.3 & 9.4
  189. return Stream.Null;
  190. return stream;
  191. }
  192. void ISerializable.GetObjectData (SerializationInfo serializationInfo,
  193. StreamingContext streamingContext)
  194. {
  195. CheckDisposed ();
  196. serializationInfo.AddValue ("uri", uri);
  197. serializationInfo.AddValue ("webHeaders", webHeaders);
  198. serializationInfo.AddValue ("cookieCollection", cookieCollection);
  199. serializationInfo.AddValue ("method", method);
  200. serializationInfo.AddValue ("version", version);
  201. serializationInfo.AddValue ("statusCode", statusCode);
  202. serializationInfo.AddValue ("statusDescription", statusDescription);
  203. serializationInfo.AddValue ("chunked", chunked);
  204. }
  205. // Cleaning up stuff
  206. public override void Close ()
  207. {
  208. ((IDisposable) this).Dispose ();
  209. }
  210. void IDisposable.Dispose ()
  211. {
  212. Dispose (true);
  213. GC.SuppressFinalize (this);
  214. }
  215. protected virtual void Dispose (bool disposing)
  216. {
  217. if (this.disposed)
  218. return;
  219. this.disposed = true;
  220. if (disposing) {
  221. // release managed resources
  222. uri = null;
  223. webHeaders = null;
  224. cookieCollection = null;
  225. method = null;
  226. version = null;
  227. statusDescription = null;
  228. }
  229. // release unmanaged resources
  230. Stream st = stream;
  231. stream = null;
  232. if (st != null)
  233. st.Close ();
  234. }
  235. private void CheckDisposed ()
  236. {
  237. if (disposed)
  238. throw new ObjectDisposedException (GetType ().FullName);
  239. }
  240. void FillCookies ()
  241. {
  242. if (webHeaders == null)
  243. return;
  244. string val = webHeaders ["Set-Cookie"];
  245. if (val != null && val.Trim () != "")
  246. SetCookie (val);
  247. val = webHeaders ["Set-Cookie2"];
  248. if (val != null && val.Trim () != "")
  249. SetCookie2 (val);
  250. }
  251. static string [] SplitValue (string input)
  252. {
  253. string [] result = new string [2];
  254. int eq = input.IndexOf ('=');
  255. if (eq == -1) {
  256. result [0] = "invalid";
  257. } else {
  258. result [0] = input.Substring (0, eq).Trim ().ToUpper ();
  259. result [1] = input.Substring (eq + 1);
  260. }
  261. return result;
  262. }
  263. [MonoTODO ("Parse dates")]
  264. void SetCookie (string cookie_str)
  265. {
  266. string[] parts = null;
  267. Collections.Queue options = null;
  268. Cookie cookie = null;
  269. options = new Collections.Queue (cookie_str.Split (';'));
  270. parts = SplitValue ((string) options.Dequeue()); // NAME=VALUE must be first
  271. cookie = new Cookie (parts[0], parts[1]);
  272. while (options.Count > 0) {
  273. parts = SplitValue ((string) options.Dequeue());
  274. switch (parts [0]) {
  275. case "COMMENT":
  276. if (cookie.Comment == null)
  277. cookie.Comment = parts[1];
  278. break;
  279. case "COMMENTURL":
  280. if (cookie.CommentUri == null)
  281. cookie.CommentUri = new Uri(parts[1]);
  282. break;
  283. case "DISCARD":
  284. cookie.Discard = true;
  285. break;
  286. case "DOMAIN":
  287. if (cookie.Domain == "")
  288. cookie.Domain = parts[1];
  289. break;
  290. case "MAX-AGE": // RFC Style Set-Cookie2
  291. if (cookie.Expires == DateTime.MinValue)
  292. cookie.Expires = cookie.TimeStamp.AddSeconds (Int32.Parse (parts[1]));
  293. break;
  294. case "EXPIRES": // Netscape Style Set-Cookie
  295. if (cookie.Expires == DateTime.MinValue) {
  296. //FIXME: Does DateTime parse something like: "Sun, 17-Jan-2038 19:14:07 GMT"?
  297. //cookie.Expires = DateTime.ParseExact (parts[1]);
  298. cookie.Expires = DateTime.Now.AddDays (1);
  299. }
  300. break;
  301. case "PATH":
  302. cookie.Path = parts[1];
  303. break;
  304. case "PORT":
  305. if (cookie.Port == null)
  306. cookie.Port = parts[1];
  307. break;
  308. case "SECURE":
  309. cookie.Secure = true;
  310. break;
  311. case "VERSION":
  312. cookie.Version = Int32.Parse (parts[1]);
  313. break;
  314. } // switch
  315. } // while
  316. if (cookieCollection == null)
  317. cookieCollection = new CookieCollection();
  318. if (cookie.Domain == "")
  319. cookie.Domain = uri.Host;
  320. cookieCollection.Add (cookie);
  321. }
  322. void SetCookie2 (string cookies_str)
  323. {
  324. string [] cookies = cookies_str.Split (',');
  325. foreach (string cookie_str in cookies)
  326. SetCookie (cookie_str);
  327. }
  328. }
  329. }