HttpWebResponse.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Globalization;
  34. using System.IO;
  35. using System.Net.Sockets;
  36. using System.Runtime.Serialization;
  37. using System.Text;
  38. namespace System.Net
  39. {
  40. [Serializable]
  41. public class HttpWebResponse : WebResponse, ISerializable, IDisposable
  42. {
  43. Uri uri;
  44. WebHeaderCollection webHeaders;
  45. CookieCollection cookieCollection;
  46. string method;
  47. Version version;
  48. HttpStatusCode statusCode;
  49. string statusDescription;
  50. long contentLength = -1;
  51. string contentType;
  52. CookieContainer cookieContainer;
  53. bool disposed = false;
  54. Stream stream;
  55. // Constructors
  56. internal HttpWebResponse (Uri uri, string method, WebConnectionData data, CookieContainer container)
  57. {
  58. this.uri = uri;
  59. this.method = method;
  60. webHeaders = data.Headers;
  61. version = data.Version;
  62. statusCode = (HttpStatusCode) data.StatusCode;
  63. statusDescription = data.StatusDescription;
  64. stream = data.stream;
  65. if (container != null) {
  66. this.cookieContainer = container;
  67. FillCookies ();
  68. } else if (webHeaders != null) {
  69. webHeaders.RemoveInternal ("Set-Cookie");
  70. webHeaders.RemoveInternal ("Set-Cookie2");
  71. }
  72. }
  73. protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
  74. {
  75. SerializationInfo info = serializationInfo;
  76. uri = (Uri) info.GetValue ("uri", typeof (Uri));
  77. contentLength = info.GetInt64 ("contentLength");
  78. contentType = info.GetString ("contentType");
  79. method = info.GetString ("method");
  80. statusDescription = info.GetString ("statusDescription");
  81. cookieCollection = (CookieCollection) info.GetValue ("cookieCollection", typeof (CookieCollection));
  82. version = (Version) info.GetValue ("version", typeof (Version));
  83. statusCode = (HttpStatusCode) info.GetValue ("statusCode", typeof (HttpStatusCode));
  84. }
  85. // Properties
  86. public string CharacterSet {
  87. // Content-Type = "Content-Type" ":" media-type
  88. // media-type = type "/" subtype *( ";" parameter )
  89. // parameter = attribute "=" value
  90. // 3.7.1. default is ISO-8859-1
  91. get {
  92. CheckDisposed ();
  93. string contentType = ContentType;
  94. if (contentType == null)
  95. return "ISO-8859-1";
  96. string val = contentType.ToLower ();
  97. int pos = val.IndexOf ("charset=");
  98. if (pos == -1)
  99. return "ISO-8859-1";
  100. pos += 8;
  101. int pos2 = val.IndexOf (';', pos);
  102. return (pos2 == -1)
  103. ? contentType.Substring (pos)
  104. : contentType.Substring (pos, pos2 - pos);
  105. }
  106. }
  107. public string ContentEncoding {
  108. get {
  109. CheckDisposed ();
  110. string h = webHeaders ["Content-Encoding"];
  111. return h != null ? h : "";
  112. }
  113. }
  114. public override long ContentLength {
  115. get {
  116. CheckDisposed ();
  117. if (contentLength != -1)
  118. return contentLength;
  119. try {
  120. contentLength = (long) UInt64.Parse (webHeaders ["Content-Length"]);
  121. } catch (Exception) {
  122. return -1;
  123. }
  124. return contentLength;
  125. }
  126. }
  127. public override string ContentType {
  128. get {
  129. CheckDisposed ();
  130. if (contentType == null)
  131. contentType = webHeaders ["Content-Type"];
  132. return contentType;
  133. }
  134. }
  135. public CookieCollection Cookies {
  136. get {
  137. CheckDisposed ();
  138. if (cookieCollection == null)
  139. cookieCollection = new CookieCollection ();
  140. return cookieCollection;
  141. }
  142. set {
  143. CheckDisposed ();
  144. cookieCollection = value;
  145. }
  146. }
  147. public override WebHeaderCollection Headers {
  148. get {
  149. CheckDisposed ();
  150. return webHeaders;
  151. }
  152. }
  153. public DateTime LastModified {
  154. get {
  155. CheckDisposed ();
  156. try {
  157. string dtStr = webHeaders ["Last-Modified"];
  158. return MonoHttpDate.Parse (dtStr);
  159. } catch (Exception) {
  160. return DateTime.Now;
  161. }
  162. }
  163. }
  164. public string Method {
  165. get {
  166. CheckDisposed ();
  167. return method;
  168. }
  169. }
  170. public Version ProtocolVersion {
  171. get {
  172. CheckDisposed ();
  173. return version;
  174. }
  175. }
  176. public override Uri ResponseUri {
  177. get {
  178. CheckDisposed ();
  179. return uri;
  180. }
  181. }
  182. public string Server {
  183. get {
  184. CheckDisposed ();
  185. return webHeaders ["Server"];
  186. }
  187. }
  188. public HttpStatusCode StatusCode {
  189. get {
  190. CheckDisposed ();
  191. return statusCode;
  192. }
  193. }
  194. public string StatusDescription {
  195. get {
  196. CheckDisposed ();
  197. return statusDescription;
  198. }
  199. }
  200. // Methods
  201. #if !NET_2_0
  202. public override int GetHashCode ()
  203. {
  204. CheckDisposed ();
  205. return base.GetHashCode ();
  206. }
  207. #endif
  208. public string GetResponseHeader (string headerName)
  209. {
  210. CheckDisposed ();
  211. string value = webHeaders [headerName];
  212. return (value != null) ? value : "";
  213. }
  214. internal void ReadAll ()
  215. {
  216. WebConnectionStream wce = stream as WebConnectionStream;
  217. if (wce == null)
  218. return;
  219. try {
  220. wce.ReadAll ();
  221. } catch {}
  222. }
  223. public override Stream GetResponseStream ()
  224. {
  225. CheckDisposed ();
  226. if (stream == null)
  227. return Stream.Null;
  228. if (0 == String.Compare (method, "HEAD", true)) // see par 4.3 & 9.4
  229. return Stream.Null;
  230. return stream;
  231. }
  232. void ISerializable.GetObjectData (SerializationInfo serializationInfo,
  233. StreamingContext streamingContext)
  234. {
  235. SerializationInfo info = serializationInfo;
  236. info.AddValue ("uri", uri);
  237. info.AddValue ("contentLength", contentLength);
  238. info.AddValue ("contentType", contentType);
  239. info.AddValue ("method", method);
  240. info.AddValue ("statusDescription", statusDescription);
  241. info.AddValue ("cookieCollection", cookieCollection);
  242. info.AddValue ("version", version);
  243. info.AddValue ("statusCode", statusCode);
  244. }
  245. // Cleaning up stuff
  246. public override void Close ()
  247. {
  248. ((IDisposable) this).Dispose ();
  249. }
  250. void IDisposable.Dispose ()
  251. {
  252. Dispose (true);
  253. GC.SuppressFinalize (this);
  254. }
  255. #if !NET_2_0
  256. protected virtual
  257. #endif
  258. void Dispose (bool disposing)
  259. {
  260. if (this.disposed)
  261. return;
  262. this.disposed = true;
  263. if (disposing) {
  264. // release managed resources
  265. uri = null;
  266. webHeaders = null;
  267. cookieCollection = null;
  268. method = null;
  269. version = null;
  270. statusDescription = null;
  271. }
  272. // release unmanaged resources
  273. Stream st = stream;
  274. stream = null;
  275. if (st != null)
  276. st.Close ();
  277. }
  278. private void CheckDisposed ()
  279. {
  280. if (disposed)
  281. throw new ObjectDisposedException (GetType ().FullName);
  282. }
  283. void FillCookies ()
  284. {
  285. if (webHeaders == null)
  286. return;
  287. string [] values = webHeaders.GetValues ("Set-Cookie");
  288. if (values != null) {
  289. foreach (string va in values)
  290. SetCookie (va);
  291. }
  292. values = webHeaders.GetValues ("Set-Cookie2");
  293. if (values != null) {
  294. foreach (string va in values)
  295. SetCookie2 (va);
  296. }
  297. }
  298. void SetCookie (string header)
  299. {
  300. string name, val;
  301. Cookie cookie = null;
  302. CookieParser parser = new CookieParser (header);
  303. while (parser.GetNextNameValue (out name, out val)) {
  304. if ((name == null || name == "") && cookie == null)
  305. continue;
  306. if (cookie == null) {
  307. cookie = new Cookie (name, val);
  308. continue;
  309. }
  310. name = name.ToUpper ();
  311. switch (name) {
  312. case "COMMENT":
  313. if (cookie.Comment == null)
  314. cookie.Comment = val;
  315. break;
  316. case "COMMENTURL":
  317. if (cookie.CommentUri == null)
  318. cookie.CommentUri = new Uri (val);
  319. break;
  320. case "DISCARD":
  321. cookie.Discard = true;
  322. break;
  323. case "DOMAIN":
  324. if (cookie.Domain == "")
  325. cookie.Domain = val;
  326. break;
  327. case "MAX-AGE": // RFC Style Set-Cookie2
  328. if (cookie.Expires == DateTime.MinValue) {
  329. try {
  330. cookie.Expires = cookie.TimeStamp.AddSeconds (UInt32.Parse (val));
  331. } catch {}
  332. }
  333. break;
  334. case "EXPIRES": // Netscape Style Set-Cookie
  335. if (cookie.Expires != DateTime.MinValue)
  336. break;
  337. try {
  338. cookie.Expires = DateTime.ParseExact (val, "r", CultureInfo.InvariantCulture);
  339. } catch {
  340. try {
  341. cookie.Expires = DateTime.ParseExact (val,
  342. "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'",
  343. CultureInfo.InvariantCulture);
  344. } catch {
  345. cookie.Expires = DateTime.Now.AddDays (1);
  346. }
  347. }
  348. break;
  349. case "PATH":
  350. cookie.Path = val;
  351. break;
  352. case "PORT":
  353. if (cookie.Port == null)
  354. cookie.Port = val;
  355. break;
  356. case "SECURE":
  357. cookie.Secure = true;
  358. break;
  359. case "VERSION":
  360. try {
  361. cookie.Version = (int) UInt32.Parse (val);
  362. } catch {}
  363. break;
  364. }
  365. }
  366. if (cookieCollection == null)
  367. cookieCollection = new CookieCollection ();
  368. if (cookie.Domain == "")
  369. cookie.Domain = uri.Host;
  370. cookieCollection.Add (cookie);
  371. if (cookieContainer != null)
  372. cookieContainer.Add (uri, cookie);
  373. }
  374. void SetCookie2 (string cookies_str)
  375. {
  376. string [] cookies = cookies_str.Split (',');
  377. foreach (string cookie_str in cookies)
  378. SetCookie (cookie_str);
  379. }
  380. }
  381. class CookieParser {
  382. string header;
  383. int pos;
  384. int length;
  385. public CookieParser (string header) : this (header, 0)
  386. {
  387. }
  388. public CookieParser (string header, int position)
  389. {
  390. this.header = header;
  391. this.pos = position;
  392. this.length = header.Length;
  393. }
  394. public bool GetNextNameValue (out string name, out string val)
  395. {
  396. name = null;
  397. val = null;
  398. if (pos >= length)
  399. return false;
  400. name = GetCookieName ();
  401. if (pos < header.Length && header [pos] == '=') {
  402. pos++;
  403. val = GetCookieValue ();
  404. }
  405. if (pos < length && header [pos] == ';')
  406. pos++;
  407. return true;
  408. }
  409. string GetCookieName ()
  410. {
  411. int k = pos;
  412. while (k < length && Char.IsWhiteSpace (header [k]))
  413. k++;
  414. int begin = k;
  415. while (k < length && header [k] != ';' && header [k] != '=')
  416. k++;
  417. pos = k;
  418. return header.Substring (begin, k - begin).Trim ();
  419. }
  420. string GetCookieValue ()
  421. {
  422. if (pos >= length)
  423. return null;
  424. int k = pos;
  425. while (k < length && Char.IsWhiteSpace (header [k]))
  426. k++;
  427. int begin;
  428. if (header [k] == '"'){
  429. int j;
  430. begin = ++k;
  431. while (k < length && header [k] != '"')
  432. k++;
  433. for (j = k; j < length && header [j] != ';'; j++)
  434. ;
  435. pos = j;
  436. } else {
  437. begin = k;
  438. while (k < length && header [k] != ';')
  439. k++;
  440. pos = k;
  441. }
  442. return header.Substring (begin, k - begin).Trim ();
  443. }
  444. }
  445. }