2 // System.Net.HttpWebResponse
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Daniel Nauck (dna(at)mono-project(dot)de)
9 // (c) 2002 Lawrence Pit
10 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
11 // (c) 2008 Daniel Nauck
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Collections;
37 using System.Globalization;
39 using System.IO.Compression;
40 using System.Net.Sockets;
41 using System.Runtime.Serialization;
47 public class HttpWebResponse : WebResponse, ISerializable, IDisposable {
49 WebHeaderCollection webHeaders;
50 CookieCollection cookieCollection;
53 HttpStatusCode statusCode;
54 string statusDescription;
57 CookieContainer cookie_container;
64 internal HttpWebResponse (Uri uri, string method, WebConnectionData data, CookieContainer container)
68 webHeaders = data.Headers;
69 version = data.Version;
70 statusCode = (HttpStatusCode) data.StatusCode;
71 statusDescription = data.StatusDescription;
76 string cl = webHeaders ["Content-Length"];
77 if (String.IsNullOrEmpty (cl) || !Int64.TryParse (cl, out contentLength))
83 if (container != null) {
84 this.cookie_container = container;
88 string content_encoding = webHeaders ["Content-Encoding"];
89 if (content_encoding == "gzip" && (data.request.AutomaticDecompression & DecompressionMethods.GZip) != 0)
90 stream = new GZipStream (stream, CompressionMode.Decompress);
91 else if (content_encoding == "deflate" && (data.request.AutomaticDecompression & DecompressionMethods.Deflate) != 0)
92 stream = new DeflateStream (stream, CompressionMode.Decompress);
95 [Obsolete ("Serialization is obsoleted for this type", false)]
96 protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
98 SerializationInfo info = serializationInfo;
100 uri = (Uri) info.GetValue ("uri", typeof (Uri));
101 contentLength = info.GetInt64 ("contentLength");
102 contentType = info.GetString ("contentType");
103 method = info.GetString ("method");
104 statusDescription = info.GetString ("statusDescription");
105 cookieCollection = (CookieCollection) info.GetValue ("cookieCollection", typeof (CookieCollection));
106 version = (Version) info.GetValue ("version", typeof (Version));
107 statusCode = (HttpStatusCode) info.GetValue ("statusCode", typeof (HttpStatusCode));
112 public string CharacterSet {
113 // Content-Type = "Content-Type" ":" media-type
114 // media-type = type "/" subtype *( ";" parameter )
115 // parameter = attribute "=" value
116 // 3.7.1. default is ISO-8859-1
118 string contentType = ContentType;
119 if (contentType == null)
121 string val = contentType.ToLower ();
122 int pos = val.IndexOf ("charset=", StringComparison.Ordinal);
126 int pos2 = val.IndexOf (';', pos);
128 ? contentType.Substring (pos)
129 : contentType.Substring (pos, pos2 - pos);
133 public string ContentEncoding {
136 string h = webHeaders ["Content-Encoding"];
137 return h != null ? h : "";
141 public override long ContentLength {
143 return contentLength;
147 public override string ContentType {
151 if (contentType == null)
152 contentType = webHeaders ["Content-Type"];
158 public CookieCollection Cookies {
161 if (cookieCollection == null)
162 cookieCollection = new CookieCollection ();
163 return cookieCollection;
167 cookieCollection = value;
171 public override WebHeaderCollection Headers {
177 static Exception GetMustImplement ()
179 return new NotImplementedException ();
183 public override bool IsMutuallyAuthenticated
186 throw GetMustImplement ();
190 public DateTime LastModified {
194 string dtStr = webHeaders ["Last-Modified"];
195 return MonoHttpDate.Parse (dtStr);
196 } catch (Exception) {
202 public string Method {
209 public Version ProtocolVersion {
216 public override Uri ResponseUri {
223 public string Server {
226 return webHeaders ["Server"];
230 public HttpStatusCode StatusCode {
236 public string StatusDescription {
239 return statusDescription;
245 public string GetResponseHeader (string headerName)
248 string value = webHeaders [headerName];
249 return (value != null) ? value : "";
252 internal void ReadAll ()
254 WebConnectionStream wce = stream as WebConnectionStream;
263 public override Stream GetResponseStream ()
268 if (0 == String.Compare (method, "HEAD", true)) // see par 4.3 & 9.4
274 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
275 StreamingContext streamingContext)
277 GetObjectData (serializationInfo, streamingContext);
280 protected override void GetObjectData (SerializationInfo serializationInfo,
281 StreamingContext streamingContext)
283 SerializationInfo info = serializationInfo;
285 info.AddValue ("uri", uri);
286 info.AddValue ("contentLength", contentLength);
287 info.AddValue ("contentType", contentType);
288 info.AddValue ("method", method);
289 info.AddValue ("statusDescription", statusDescription);
290 info.AddValue ("cookieCollection", cookieCollection);
291 info.AddValue ("version", version);
292 info.AddValue ("statusCode", statusCode);
297 public override void Close ()
299 if (stream != null) {
307 void IDisposable.Dispose ()
310 GC.SuppressFinalize (this);
313 void Dispose (bool disposing)
315 this.disposed = true;
320 private void CheckDisposed ()
323 throw new ObjectDisposedException (GetType ().FullName);
328 if (webHeaders == null)
331 string value = webHeaders.Get ("Set-Cookie");
336 value = webHeaders.Get ("Set-Cookie2");
342 void SetCookie (string header)
344 if (cookieCollection == null)
345 cookieCollection = new CookieCollection ();
347 var parser = new CookieParser (header);
348 foreach (var cookie in parser.Parse ()) {
349 if (cookie.Domain == "") {
350 cookie.Domain = uri.Host;
351 cookie.HasDomain = false;
354 if (cookie.HasDomain &&
355 !CookieContainer.CheckSameOrigin (uri, cookie.Domain))
358 cookieCollection.Add (cookie);
359 if (cookie_container != null)
360 cookie_container.Add (uri, cookie);