HttpWebResponse.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. //
  2. // System.Net.HttpWebResponse
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Daniel Nauck (dna(at)mono-project(dot)de)
  8. //
  9. // (c) 2002 Lawrence Pit
  10. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  11. // (c) 2008 Daniel Nauck
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.Collections;
  35. using System.Globalization;
  36. using System.IO;
  37. using System.IO.Compression;
  38. using System.Net.Sockets;
  39. using System.Runtime.Serialization;
  40. using System.Text;
  41. namespace System.Net
  42. {
  43. [Serializable]
  44. public class HttpWebResponse : WebResponse, ISerializable, IDisposable {
  45. Uri uri;
  46. WebHeaderCollection webHeaders;
  47. CookieCollection cookieCollection;
  48. string method;
  49. Version version;
  50. HttpStatusCode statusCode;
  51. string statusDescription;
  52. long contentLength;
  53. string contentType;
  54. CookieContainer cookie_container;
  55. bool disposed;
  56. Stream stream;
  57. // Constructors
  58. internal HttpWebResponse (Uri uri, string method, WebConnectionData data, CookieContainer container)
  59. {
  60. this.uri = uri;
  61. this.method = method;
  62. webHeaders = data.Headers;
  63. version = data.Version;
  64. statusCode = (HttpStatusCode) data.StatusCode;
  65. statusDescription = data.StatusDescription;
  66. stream = data.stream;
  67. contentLength = -1;
  68. try {
  69. string cl = webHeaders ["Content-Length"];
  70. if (String.IsNullOrEmpty (cl) || !Int64.TryParse (cl, out contentLength))
  71. contentLength = -1;
  72. } catch (Exception) {
  73. contentLength = -1;
  74. }
  75. if (container != null) {
  76. this.cookie_container = container;
  77. FillCookies ();
  78. }
  79. string content_encoding = webHeaders ["Content-Encoding"];
  80. if (content_encoding == "gzip" && (data.request.AutomaticDecompression & DecompressionMethods.GZip) != 0) {
  81. stream = new GZipStream (stream, CompressionMode.Decompress);
  82. webHeaders.Remove (HttpRequestHeader.ContentEncoding);
  83. }
  84. else if (content_encoding == "deflate" && (data.request.AutomaticDecompression & DecompressionMethods.Deflate) != 0) {
  85. stream = new DeflateStream (stream, CompressionMode.Decompress);
  86. webHeaders.Remove (HttpRequestHeader.ContentEncoding);
  87. }
  88. }
  89. [Obsolete ("Serialization is obsoleted for this type", false)]
  90. protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
  91. {
  92. SerializationInfo info = serializationInfo;
  93. uri = (Uri) info.GetValue ("uri", typeof (Uri));
  94. contentLength = info.GetInt64 ("contentLength");
  95. contentType = info.GetString ("contentType");
  96. method = info.GetString ("method");
  97. statusDescription = info.GetString ("statusDescription");
  98. cookieCollection = (CookieCollection) info.GetValue ("cookieCollection", typeof (CookieCollection));
  99. version = (Version) info.GetValue ("version", typeof (Version));
  100. statusCode = (HttpStatusCode) info.GetValue ("statusCode", typeof (HttpStatusCode));
  101. }
  102. // Properties
  103. public string CharacterSet {
  104. // Content-Type = "Content-Type" ":" media-type
  105. // media-type = type "/" subtype *( ";" parameter )
  106. // parameter = attribute "=" value
  107. // 3.7.1. default is ISO-8859-1
  108. get {
  109. string contentType = ContentType;
  110. if (contentType == null)
  111. return "ISO-8859-1";
  112. string val = contentType.ToLower ();
  113. int pos = val.IndexOf ("charset=", StringComparison.Ordinal);
  114. if (pos == -1)
  115. return "ISO-8859-1";
  116. pos += 8;
  117. int pos2 = val.IndexOf (';', pos);
  118. return (pos2 == -1)
  119. ? contentType.Substring (pos)
  120. : contentType.Substring (pos, pos2 - pos);
  121. }
  122. }
  123. public string ContentEncoding {
  124. get {
  125. CheckDisposed ();
  126. string h = webHeaders ["Content-Encoding"];
  127. return h != null ? h : "";
  128. }
  129. }
  130. public override long ContentLength {
  131. get {
  132. return contentLength;
  133. }
  134. }
  135. public override string ContentType {
  136. get {
  137. CheckDisposed ();
  138. if (contentType == null)
  139. contentType = webHeaders ["Content-Type"];
  140. return contentType;
  141. }
  142. }
  143. virtual
  144. public CookieCollection Cookies {
  145. get {
  146. CheckDisposed ();
  147. if (cookieCollection == null)
  148. cookieCollection = new CookieCollection ();
  149. return cookieCollection;
  150. }
  151. set {
  152. CheckDisposed ();
  153. cookieCollection = value;
  154. }
  155. }
  156. public override WebHeaderCollection Headers {
  157. get {
  158. return webHeaders;
  159. }
  160. }
  161. static Exception GetMustImplement ()
  162. {
  163. return new NotImplementedException ();
  164. }
  165. [MonoTODO]
  166. public override bool IsMutuallyAuthenticated
  167. {
  168. get {
  169. throw GetMustImplement ();
  170. }
  171. }
  172. public DateTime LastModified {
  173. get {
  174. CheckDisposed ();
  175. try {
  176. string dtStr = webHeaders ["Last-Modified"];
  177. return MonoHttpDate.Parse (dtStr);
  178. } catch (Exception) {
  179. return DateTime.Now;
  180. }
  181. }
  182. }
  183. virtual
  184. public string Method {
  185. get {
  186. CheckDisposed ();
  187. return method;
  188. }
  189. }
  190. public Version ProtocolVersion {
  191. get {
  192. CheckDisposed ();
  193. return version;
  194. }
  195. }
  196. public override Uri ResponseUri {
  197. get {
  198. CheckDisposed ();
  199. return uri;
  200. }
  201. }
  202. public string Server {
  203. get {
  204. CheckDisposed ();
  205. return webHeaders ["Server"] ?? "";
  206. }
  207. }
  208. virtual
  209. public HttpStatusCode StatusCode {
  210. get {
  211. return statusCode;
  212. }
  213. }
  214. virtual
  215. public string StatusDescription {
  216. get {
  217. CheckDisposed ();
  218. return statusDescription;
  219. }
  220. }
  221. public override bool SupportsHeaders {
  222. get {
  223. return true;
  224. }
  225. }
  226. // Methods
  227. public string GetResponseHeader (string headerName)
  228. {
  229. CheckDisposed ();
  230. string value = webHeaders [headerName];
  231. return (value != null) ? value : "";
  232. }
  233. internal void ReadAll ()
  234. {
  235. WebConnectionStream wce = stream as WebConnectionStream;
  236. if (wce == null)
  237. return;
  238. try {
  239. wce.ReadAll ();
  240. } catch {}
  241. }
  242. public override Stream GetResponseStream ()
  243. {
  244. CheckDisposed ();
  245. if (stream == null)
  246. return Stream.Null;
  247. if (string.Equals (method, "HEAD", StringComparison.OrdinalIgnoreCase)) // see par 4.3 & 9.4
  248. return Stream.Null;
  249. return stream;
  250. }
  251. void ISerializable.GetObjectData (SerializationInfo serializationInfo,
  252. StreamingContext streamingContext)
  253. {
  254. GetObjectData (serializationInfo, streamingContext);
  255. }
  256. protected override void GetObjectData (SerializationInfo serializationInfo,
  257. StreamingContext streamingContext)
  258. {
  259. SerializationInfo info = serializationInfo;
  260. info.AddValue ("uri", uri);
  261. info.AddValue ("contentLength", contentLength);
  262. info.AddValue ("contentType", contentType);
  263. info.AddValue ("method", method);
  264. info.AddValue ("statusDescription", statusDescription);
  265. info.AddValue ("cookieCollection", cookieCollection);
  266. info.AddValue ("version", version);
  267. info.AddValue ("statusCode", statusCode);
  268. }
  269. // Cleaning up stuff
  270. public override void Close ()
  271. {
  272. if (stream != null) {
  273. Stream st = stream;
  274. stream = null;
  275. if (st != null)
  276. st.Close ();
  277. }
  278. }
  279. void IDisposable.Dispose ()
  280. {
  281. Dispose (true);
  282. }
  283. protected override void Dispose (bool disposing)
  284. {
  285. this.disposed = true;
  286. base.Dispose (true);
  287. }
  288. private void CheckDisposed ()
  289. {
  290. if (disposed)
  291. throw new ObjectDisposedException (GetType ().FullName);
  292. }
  293. void FillCookies ()
  294. {
  295. if (webHeaders == null)
  296. return;
  297. //
  298. // Don't terminate response reading on bad cookie value
  299. //
  300. string value;
  301. CookieCollection cookies = null;
  302. try {
  303. value = webHeaders.Get ("Set-Cookie");
  304. if (value != null)
  305. cookies = cookie_container.CookieCutter (uri, HttpKnownHeaderNames.SetCookie, value, false);
  306. } catch {
  307. }
  308. try {
  309. value = webHeaders.Get ("Set-Cookie2");
  310. if (value != null) {
  311. var cookies2 = cookie_container.CookieCutter (uri, HttpKnownHeaderNames.SetCookie2, value, false);
  312. if (cookies != null && cookies.Count != 0) {
  313. cookies.Add (cookies2);
  314. } else {
  315. cookies = cookies2;
  316. }
  317. }
  318. } catch {
  319. }
  320. this.cookieCollection = cookies;
  321. }
  322. }
  323. }