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 webHeaders.Remove (HttpRequestHeader.ContentEncoding);
93 else if (content_encoding == "deflate" && (data.request.AutomaticDecompression & DecompressionMethods.Deflate) != 0) {
94 stream = new DeflateStream (stream, CompressionMode.Decompress);
95 webHeaders.Remove (HttpRequestHeader.ContentEncoding);
99 [Obsolete ("Serialization is obsoleted for this type", false)]
100 protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
102 SerializationInfo info = serializationInfo;
104 uri = (Uri) info.GetValue ("uri", typeof (Uri));
105 contentLength = info.GetInt64 ("contentLength");
106 contentType = info.GetString ("contentType");
107 method = info.GetString ("method");
108 statusDescription = info.GetString ("statusDescription");
109 cookieCollection = (CookieCollection) info.GetValue ("cookieCollection", typeof (CookieCollection));
110 version = (Version) info.GetValue ("version", typeof (Version));
111 statusCode = (HttpStatusCode) info.GetValue ("statusCode", typeof (HttpStatusCode));
116 public string CharacterSet {
117 // Content-Type = "Content-Type" ":" media-type
118 // media-type = type "/" subtype *( ";" parameter )
119 // parameter = attribute "=" value
120 // 3.7.1. default is ISO-8859-1
122 string contentType = ContentType;
123 if (contentType == null)
125 string val = contentType.ToLower ();
126 int pos = val.IndexOf ("charset=", StringComparison.Ordinal);
130 int pos2 = val.IndexOf (';', pos);
132 ? contentType.Substring (pos)
133 : contentType.Substring (pos, pos2 - pos);
137 public string ContentEncoding {
140 string h = webHeaders ["Content-Encoding"];
141 return h != null ? h : "";
145 public override long ContentLength {
147 return contentLength;
151 public override string ContentType {
155 if (contentType == null)
156 contentType = webHeaders ["Content-Type"];
163 public CookieCollection Cookies {
166 if (cookieCollection == null)
167 cookieCollection = new CookieCollection ();
168 return cookieCollection;
172 cookieCollection = value;
176 public override WebHeaderCollection Headers {
182 static Exception GetMustImplement ()
184 return new NotImplementedException ();
188 public override bool IsMutuallyAuthenticated
191 throw GetMustImplement ();
195 public DateTime LastModified {
199 string dtStr = webHeaders ["Last-Modified"];
200 return MonoHttpDate.Parse (dtStr);
201 } catch (Exception) {
208 public string Method {
215 public Version ProtocolVersion {
222 public override Uri ResponseUri {
229 public string Server {
232 return webHeaders ["Server"] ?? "";
237 public HttpStatusCode StatusCode {
244 public string StatusDescription {
247 return statusDescription;
251 public override bool SupportsHeaders {
259 public string GetResponseHeader (string headerName)
262 string value = webHeaders [headerName];
263 return (value != null) ? value : "";
266 internal void ReadAll ()
268 WebConnectionStream wce = stream as WebConnectionStream;
277 public override Stream GetResponseStream ()
282 if (string.Equals (method, "HEAD", StringComparison.OrdinalIgnoreCase)) // see par 4.3 & 9.4
288 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
289 StreamingContext streamingContext)
291 GetObjectData (serializationInfo, streamingContext);
294 protected override void GetObjectData (SerializationInfo serializationInfo,
295 StreamingContext streamingContext)
297 SerializationInfo info = serializationInfo;
299 info.AddValue ("uri", uri);
300 info.AddValue ("contentLength", contentLength);
301 info.AddValue ("contentType", contentType);
302 info.AddValue ("method", method);
303 info.AddValue ("statusDescription", statusDescription);
304 info.AddValue ("cookieCollection", cookieCollection);
305 info.AddValue ("version", version);
306 info.AddValue ("statusCode", statusCode);
311 public override void Close ()
313 if (stream != null) {
321 void IDisposable.Dispose ()
326 protected override void Dispose (bool disposing)
328 this.disposed = true;
332 private void CheckDisposed ()
335 throw new ObjectDisposedException (GetType ().FullName);
340 if (webHeaders == null)
344 // Don't terminate response reading on bad cookie value
347 CookieCollection cookies = null;
349 value = webHeaders.Get ("Set-Cookie");
351 cookies = cookie_container.CookieCutter (uri, HttpKnownHeaderNames.SetCookie, value, false);
356 value = webHeaders.Get ("Set-Cookie2");
358 var cookies2 = cookie_container.CookieCutter (uri, HttpKnownHeaderNames.SetCookie2, value, false);
360 if (cookies != null && cookies.Count != 0) {
361 cookies.Add (cookies2);
369 this.cookieCollection = cookies;