2010-06-03 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Mono.Http / Mono.Http / GZipWebRequest.cs
blobd5a745591368786e5b170a16a226f8343ccf2b70
1 //
2 // Mono.Http.GzipWebRequest
3 //
4 // Authors:
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
8 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System;
32 using System.Net;
33 using System.Collections;
34 using System.IO;
35 using System.Runtime.Serialization;
36 using ICSharpCode.SharpZipLib.GZip;
38 namespace Mono.Http
40 [Serializable]
41 public class GZipWebRequest : WebRequest, ISerializable
43 WebRequest request;
44 bool enabled;
46 public GZipWebRequest (WebRequest request)
48 if (request == null)
49 throw new ArgumentNullException ("request");
51 this.request = request;
52 enabled = true;
55 protected GZipWebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext)
57 SerializationInfo info = serializationInfo;
58 request = (WebRequest) info.GetValue ("request", typeof (WebRequest));
59 enabled = info.GetBoolean ("enabled");
62 public override string ConnectionGroupName {
63 get { return request.ConnectionGroupName; }
64 set { request.ConnectionGroupName = value; }
67 public override long ContentLength {
68 get { return request.ContentLength; }
69 set { request.ContentLength = value; }
72 public override string ContentType {
73 get { return request.ContentType; }
74 set { request.ContentType = value; }
77 public override ICredentials Credentials {
78 get { return request.Credentials; }
79 set { request.Credentials = value; }
82 public override WebHeaderCollection Headers {
83 get { return request.Headers; }
84 set { request.Headers = value; }
87 public override string Method {
88 get { return request.Method; }
89 set { request.Method = value; }
92 public override bool PreAuthenticate {
93 get { return request.PreAuthenticate; }
94 set { request.PreAuthenticate = value; }
97 public override IWebProxy Proxy {
98 get { return request.Proxy; }
99 set { request.Proxy = value; }
102 public override Uri RequestUri {
103 get { return request.RequestUri; }
106 public override int Timeout {
107 get { return request.Timeout; }
108 set { request.Timeout = value; }
111 public WebRequest RealRequest {
112 get { return request; }
115 public bool EnableCompression {
116 get { return enabled; }
117 set { enabled = value; }
120 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
122 info.AddValue ("request", request, typeof (WebRequest));
123 info.AddValue ("enabled", enabled);
126 public override void Abort ()
128 request.Abort ();
131 public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state)
133 CheckHeader ();
134 return request.BeginGetRequestStream (callback, state);
137 public override Stream EndGetRequestStream (IAsyncResult asyncResult)
139 return request.EndGetRequestStream (asyncResult);
142 public override Stream GetRequestStream ()
144 CheckHeader ();
145 return request.GetRequestStream ();
148 void CheckHeader ()
150 if (!enabled)
151 return;
153 string accept = request.Headers ["Accept-Encoding"];
154 if (accept == null || accept == "")
155 request.Headers ["Accept-Encoding"] = "gzip; q=1.0, identity";
158 public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
160 CheckHeader ();
161 return request.BeginGetResponse (callback, state);
164 public override WebResponse EndGetResponse (IAsyncResult asyncResult)
166 WebResponse response = request.EndGetResponse (asyncResult);
167 bool compressed = (String.Compare (response.Headers ["Content-Encoding"], "gzip", true) == 0);
168 if (!compressed)
169 return response;
171 return new GZipWebResponse (response, compressed);
174 public override WebResponse GetResponse ()
176 IAsyncResult result = BeginGetResponse (null, null);
177 return EndGetResponse (result);