HttpWebResponse.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. return MonoHttpDate.Parse (dtStr);
  115. } catch (Exception) {
  116. return DateTime.Now;
  117. }
  118. }
  119. }
  120. public string Method {
  121. get {
  122. CheckDisposed ();
  123. return method;
  124. }
  125. }
  126. public Version ProtocolVersion {
  127. get {
  128. CheckDisposed ();
  129. return version;
  130. }
  131. }
  132. public override Uri ResponseUri {
  133. get {
  134. CheckDisposed ();
  135. return uri;
  136. }
  137. }
  138. public string Server {
  139. get {
  140. try {
  141. return webHeaders ["Server"];
  142. } finally {
  143. CheckDisposed ();
  144. }
  145. }
  146. }
  147. public HttpStatusCode StatusCode {
  148. get {
  149. CheckDisposed ();
  150. return statusCode;
  151. }
  152. }
  153. public string StatusDescription {
  154. get {
  155. CheckDisposed ();
  156. return statusDescription;
  157. }
  158. }
  159. // Methods
  160. public override int GetHashCode ()
  161. {
  162. try {
  163. return base.GetHashCode ();
  164. } finally {
  165. CheckDisposed ();
  166. }
  167. }
  168. public string GetResponseHeader (string headerName)
  169. {
  170. try {
  171. return webHeaders [headerName];
  172. } finally {
  173. CheckDisposed ();
  174. }
  175. }
  176. public override Stream GetResponseStream ()
  177. {
  178. try {
  179. if (method.Equals ("HEAD")) // see par 4.3 & 9.4
  180. return Stream.Null;
  181. return responseStream;
  182. } finally {
  183. CheckDisposed ();
  184. }
  185. }
  186. [MonoTODO]
  187. void ISerializable.GetObjectData (SerializationInfo serializationInfo,
  188. StreamingContext streamingContext)
  189. {
  190. CheckDisposed ();
  191. throw new NotImplementedException ();
  192. }
  193. // Cleaning up stuff
  194. ~HttpWebResponse ()
  195. {
  196. Dispose (false);
  197. }
  198. public override void Close ()
  199. {
  200. ((IDisposable) this).Dispose ();
  201. }
  202. void IDisposable.Dispose ()
  203. {
  204. Dispose (true);
  205. GC.SuppressFinalize (this);
  206. }
  207. protected virtual void Dispose (bool disposing)
  208. {
  209. if (this.disposed)
  210. return;
  211. this.disposed = true;
  212. if (disposing) {
  213. // release managed resources
  214. uri = null;
  215. webHeaders = null;
  216. cookieCollection = null;
  217. method = null;
  218. version = null;
  219. // statusCode = null;
  220. statusDescription = null;
  221. }
  222. // release unmanaged resources
  223. Stream stream = responseStream;
  224. responseStream = null;
  225. if (stream != null)
  226. stream.Close (); // also closes webRequest
  227. }
  228. private void CheckDisposed ()
  229. {
  230. if (disposed)
  231. throw new ObjectDisposedException (GetType ().FullName);
  232. }
  233. }
  234. }