[system] Fix valid sorted set operations which could cause underlying tree modification
[mono.git] / mcs / class / Mono.Http / Mono.Http / GZipWebRequest.cs
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 //
9
10 //
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.
29 //
30
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;
37
38 namespace Mono.Http
39 {
40         [Serializable]
41         public class GZipWebRequest : WebRequest, ISerializable
42         {
43                 WebRequest request;
44                 bool enabled;
45
46                 public GZipWebRequest (WebRequest request)
47                 {
48                         if (request == null)
49                                 throw new ArgumentNullException ("request");
50
51                         this.request = request;
52                         enabled = true;
53                 }
54
55                 protected GZipWebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext) 
56                 {
57                         SerializationInfo info = serializationInfo;
58                         request = (WebRequest) info.GetValue ("request", typeof (WebRequest));
59                         enabled = info.GetBoolean ("enabled");
60                 }
61
62                 public override string ConnectionGroupName { 
63                         get { return request.ConnectionGroupName; }
64                         set { request.ConnectionGroupName = value; }
65                 }
66                 
67                 public override long ContentLength { 
68                         get { return request.ContentLength; }
69                         set { request.ContentLength = value; }
70                 }
71                 
72                 public override string ContentType { 
73                         get { return request.ContentType; }
74                         set { request.ContentType = value; }
75                 }
76                 
77                 public override ICredentials Credentials { 
78                         get { return request.Credentials; }
79                         set { request.Credentials = value; }
80                 }
81                 
82                 public override WebHeaderCollection Headers { 
83                         get { return request.Headers; }
84                         set { request.Headers = value; }
85                 }
86                 
87                 public override string Method { 
88                         get { return request.Method; }
89                         set { request.Method = value; }
90                 }
91                 
92                 public override bool PreAuthenticate { 
93                         get { return request.PreAuthenticate; }
94                         set { request.PreAuthenticate = value; }
95                 }
96                 
97                 public override IWebProxy Proxy { 
98                         get { return request.Proxy; }
99                         set { request.Proxy = value; }
100                 }
101                 
102                 public override Uri RequestUri { 
103                         get { return request.RequestUri; }
104                 }
105                 
106                 public override int Timeout { 
107                         get { return request.Timeout; }
108                         set { request.Timeout = value; }
109                 }
110                 
111                 public WebRequest RealRequest {
112                         get { return request; }
113                 }
114
115                 public bool EnableCompression {
116                         get { return enabled; }
117                         set { enabled = value; }
118                 }
119
120                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context) 
121                 {
122                         info.AddValue ("request", request, typeof (WebRequest));
123                         info.AddValue ("enabled", enabled);
124                 }
125                 
126                 public override void Abort ()
127                 {
128                         request.Abort ();
129                 }
130                 
131                 public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state) 
132                 {
133                         CheckHeader ();
134                         return request.BeginGetRequestStream (callback, state);
135                 }
136                 
137                 public override Stream EndGetRequestStream (IAsyncResult asyncResult)
138                 {
139                         return request.EndGetRequestStream (asyncResult);
140                 }
141                 
142                 public override Stream GetRequestStream ()
143                 {
144                         CheckHeader ();
145                         return request.GetRequestStream ();
146                 }
147                 
148                 void CheckHeader ()
149                 {
150                         if (!enabled)
151                                 return;
152
153                         string accept = request.Headers ["Accept-Encoding"];
154                         if (accept == null || accept == "")
155                                 request.Headers ["Accept-Encoding"] = "gzip; q=1.0, identity";
156                 }
157
158                 public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
159                 {
160                         CheckHeader ();
161                         return request.BeginGetResponse (callback, state);
162                 }
163
164                 public override WebResponse EndGetResponse (IAsyncResult asyncResult)
165                 {
166                         WebResponse response = request.EndGetResponse (asyncResult);
167                         bool compressed = (String.Compare (response.Headers ["Content-Encoding"], "gzip", true) == 0);
168                         if (!compressed)
169                                 return response;
170
171                         return new GZipWebResponse (response, compressed);
172                 }
173                 
174                 public override WebResponse GetResponse ()
175                 {
176                         IAsyncResult result = BeginGetResponse (null, null);
177                         return EndGetResponse (result);
178                 }
179         }
180 }
181