HttpWebResponse.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //
  2. // System.Net.HttpWebResponse
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using System;
  8. using System.IO;
  9. using System.Runtime.Serialization;
  10. namespace System.Net
  11. {
  12. [Serializable]
  13. public class HttpWebResponse : WebResponse, ISerializable, IDisposable
  14. {
  15. private Uri uri;
  16. private WebHeaderCollection webHeaders;
  17. private CookieCollection cookieCollection = null;
  18. private string method = null;
  19. private Version version = null;
  20. private HttpStatusCode statusCode;
  21. private string statusDescription = null;
  22. private Stream responseStream;
  23. private bool disposed = false;
  24. // Constructors
  25. internal HttpWebResponse (Uri uri, string method, Stream responseStream)
  26. {
  27. this.uri = uri;
  28. this.method = method;
  29. this.responseStream = responseStream;
  30. // TODO: parse headers from responseStream
  31. this.statusCode = HttpStatusCode.OK;
  32. }
  33. protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
  34. {
  35. throw new NotSupportedException ();
  36. }
  37. // Properties
  38. public string CharacterSet {
  39. // Content-Type = "Content-Type" ":" media-type
  40. // media-type = type "/" subtype *( ";" parameter )
  41. // parameter = attribute "=" value
  42. // 3.7.1. default is ISO-8859-1
  43. get {
  44. try {
  45. string contentType = ContentType;
  46. if (contentType == null)
  47. return "ISO-8859-1";
  48. string val = contentType.ToLower ();
  49. int pos = val.IndexOf ("charset=");
  50. if (pos == -1)
  51. return "ISO-8859-1";
  52. pos += 8;
  53. int pos2 = val.IndexOf (';', pos);
  54. return (pos2 == -1)
  55. ? contentType.Substring (pos)
  56. : contentType.Substring (pos, pos2 - pos);
  57. } finally {
  58. CheckDisposed ();
  59. }
  60. }
  61. }
  62. public string ContentEncoding {
  63. get {
  64. try { return webHeaders ["Content-Encoding"]; }
  65. finally { CheckDisposed (); }
  66. }
  67. }
  68. public override long ContentLength {
  69. get {
  70. try {
  71. return Int64.Parse (webHeaders ["Content-Length"]);
  72. } catch (Exception) {
  73. return -1;
  74. } finally {
  75. CheckDisposed ();
  76. }
  77. }
  78. }
  79. public override string ContentType {
  80. get {
  81. try { return webHeaders ["Content-Type"]; }
  82. finally { CheckDisposed (); }
  83. }
  84. }
  85. public CookieCollection Cookies {
  86. get {
  87. CheckDisposed ();
  88. // LAMESPEC: a simple test reveal this always
  89. // returns an empty collection. It is not filled
  90. // with the values from the Set-Cookie or
  91. // Set-Cookie2 response headers, which is a bit
  92. // of a shame..
  93. if (cookieCollection == null)
  94. cookieCollection = new CookieCollection ();
  95. return cookieCollection;
  96. }
  97. set {
  98. CheckDisposed ();
  99. // ?? don't understand how you can set cookies on a response.
  100. throw new NotSupportedException ();
  101. }
  102. }
  103. public override WebHeaderCollection Headers {
  104. get {
  105. CheckDisposed ();
  106. return webHeaders;
  107. }
  108. }
  109. public DateTime LastModified {
  110. get {
  111. CheckDisposed ();
  112. try {
  113. string dtStr = webHeaders ["Last-Modified"];
  114. // TODO: accept more than rfc1123 dates
  115. DateTime dt = DateTime.ParseExact (dtStr, "r", null);
  116. return dt;
  117. } catch (Exception) {
  118. return DateTime.Now;
  119. }
  120. }
  121. }
  122. public string Method {
  123. get {
  124. CheckDisposed ();
  125. return method;
  126. }
  127. }
  128. public Version ProtocolVersion {
  129. get {
  130. CheckDisposed ();
  131. return version;
  132. }
  133. }
  134. public override Uri ResponseUri {
  135. get {
  136. CheckDisposed ();
  137. return uri;
  138. }
  139. }
  140. public string Server {
  141. get {
  142. try {
  143. return webHeaders ["Server"];
  144. } finally {
  145. CheckDisposed ();
  146. }
  147. }
  148. }
  149. public HttpStatusCode StatusCode {
  150. get {
  151. CheckDisposed ();
  152. return statusCode;
  153. }
  154. }
  155. public string StatusDescription {
  156. get {
  157. CheckDisposed ();
  158. return statusDescription;
  159. }
  160. }
  161. // Methods
  162. public override int GetHashCode ()
  163. {
  164. try {
  165. return base.GetHashCode ();
  166. } finally {
  167. CheckDisposed ();
  168. }
  169. }
  170. public string GetResponseHeader (string headerName)
  171. {
  172. try {
  173. return webHeaders [headerName];
  174. } finally {
  175. CheckDisposed ();
  176. }
  177. }
  178. public override Stream GetResponseStream ()
  179. {
  180. try {
  181. if (method.Equals ("HEAD")) // see par 4.3 & 9.4
  182. return Stream.Null;
  183. return responseStream;
  184. } finally {
  185. CheckDisposed ();
  186. }
  187. }
  188. [MonoTODO]
  189. void ISerializable.GetObjectData (SerializationInfo serializationInfo,
  190. StreamingContext streamingContext)
  191. {
  192. CheckDisposed ();
  193. throw new NotImplementedException ();
  194. }
  195. // Cleaning up stuff
  196. ~HttpWebResponse ()
  197. {
  198. Dispose (false);
  199. }
  200. public override void Close ()
  201. {
  202. ((IDisposable) this).Dispose ();
  203. }
  204. void IDisposable.Dispose ()
  205. {
  206. Dispose (true);
  207. GC.SuppressFinalize (this);
  208. }
  209. protected virtual void Dispose (bool disposing)
  210. {
  211. if (this.disposed)
  212. return;
  213. this.disposed = true;
  214. if (disposing) {
  215. // release managed resources
  216. uri = null;
  217. webHeaders = null;
  218. cookieCollection = null;
  219. method = null;
  220. version = null;
  221. // statusCode = null;
  222. statusDescription = null;
  223. }
  224. // release unmanaged resources
  225. Stream stream = responseStream;
  226. responseStream = null;
  227. if (stream != null)
  228. stream.Close (); // also closes webRequest
  229. }
  230. private void CheckDisposed ()
  231. {
  232. if (disposed)
  233. throw new ObjectDisposedException (GetType ().FullName);
  234. }
  235. }
  236. }