* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System / System.Net / WebRequest.cs
1 //\r
2 // System.Net.WebRequest\r
3 //\r
4 // Authors:\r
5 //   Lawrence Pit (loz@cable.a2000.nl)\r
6 //\r
7
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 \r
29 using System;\r
30 using System.Collections;\r
31 using System.Collections.Specialized;\r
32 using System.Configuration;\r
33 using System.IO;\r
34 using System.Runtime.Serialization;\r
35 #if NET_2_0\r
36 using System.Net.Configuration;\r
37 #endif\r
38 \r
39 namespace System.Net \r
40 {\r
41         [Serializable]\r
42         public abstract class WebRequest : MarshalByRefObject, ISerializable\r
43         {\r
44                 static HybridDictionary prefixes = new HybridDictionary ();\r
45                 \r
46                 // Constructors\r
47                 \r
48                 static WebRequest ()\r
49                 {\r
50 #if NET_2_0 && CONFIGURATION_DEP\r
51                         object cfg = ConfigurationManager.GetSection ("system.net/webRequestModules");\r
52                         WebRequestModulesSection s = cfg as WebRequestModulesSection;\r
53                         if (s != null) {\r
54                                 foreach (WebRequestModuleElement el in\r
55                                          s.WebRequestModules)\r
56                                         AddPrefix (el.Prefix, el.Type);\r
57                                 return;\r
58                         }\r
59 #endif\r
60                         ConfigurationSettings.GetConfig ("system.net/webRequestModules");\r
61                 }\r
62                 \r
63                 protected WebRequest () 
64                 {
65                 }\r
66                 \r
67                 protected WebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext) \r
68                 {\r
69                 }\r
70                 \r
71                 // Properties\r
72                 \r
73                 public virtual string ConnectionGroupName { \r
74                         get { throw new NotImplementedException (); }\r
75                         set { throw new NotImplementedException (); }\r
76                 }\r
77                 \r
78                 public virtual long ContentLength { \r
79                         get { throw new NotImplementedException (); }\r
80                         set { throw new NotImplementedException (); }\r
81                 }\r
82                 \r
83                 public virtual string ContentType { \r
84                         get { throw new NotImplementedException (); }\r
85                         set { throw new NotImplementedException (); }\r
86                 }\r
87                 \r
88                 public virtual ICredentials Credentials { \r
89                         get { throw new NotImplementedException (); }\r
90                         set { throw new NotImplementedException (); }\r
91                 }\r
92                 \r
93                 public virtual WebHeaderCollection Headers { \r
94                         get { throw new NotImplementedException (); }\r
95                         set { throw new NotImplementedException (); }\r
96                 }\r
97                 \r
98                 public virtual string Method { \r
99                         get { throw new NotImplementedException (); }\r
100                         set { throw new NotImplementedException (); }\r
101                 }\r
102                 \r
103                 public virtual bool PreAuthenticate { \r
104                         get { throw new NotImplementedException (); }\r
105                         set { throw new NotImplementedException (); }\r
106                 }\r
107                 \r
108                 public virtual IWebProxy Proxy { \r
109                         get { throw new NotImplementedException (); }\r
110                         set { throw new NotImplementedException (); }\r
111                 }\r
112                 \r
113                 public virtual Uri RequestUri { \r
114                         get { throw new NotImplementedException (); }\r
115                 }\r
116                 \r
117                 public virtual int Timeout { \r
118                         get { throw new NotImplementedException (); }\r
119                         set { throw new NotImplementedException (); }\r
120                 }\r
121                 \r
122                 // Methods\r
123                 \r
124                 public virtual void Abort()\r
125                 {\r
126                         throw new NotImplementedException ();\r
127                 }\r
128                 \r
129                 public virtual IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state) \r
130                 {\r
131                         throw new NotImplementedException ();\r
132                 }\r
133                 \r
134                 public virtual IAsyncResult BeginGetResponse (AsyncCallback callback, object state)\r
135                 {\r
136                         throw new NotImplementedException ();\r
137                 }\r
138 \r
139                 public static WebRequest Create (string requestUriString) \r
140                 {\r
141                         if (requestUriString == null)\r
142                                 throw new ArgumentNullException ("requestUriString");\r
143                         return Create (new Uri (requestUriString));\r
144                 }\r
145                                 \r
146                 public static WebRequest Create (Uri requestUri) \r
147                 {\r
148                         if (requestUri == null)\r
149                                 throw new ArgumentNullException ("requestUri");\r
150                         return GetCreator (requestUri.AbsoluteUri).Create (requestUri);\r
151                 }\r
152                 \r
153                 public static WebRequest CreateDefault (Uri requestUri)\r
154                 {\r
155                         if (requestUri == null)\r
156                                 throw new ArgumentNullException ("requestUri");\r
157                         return GetCreator (requestUri.Scheme).Create (requestUri);\r
158                 }\r
159 \r
160                 public virtual Stream EndGetRequestStream (IAsyncResult asyncResult)\r
161                 {\r
162                         throw new NotImplementedException ();\r
163                 }\r
164                 \r
165                 public virtual WebResponse EndGetResponse (IAsyncResult asyncResult)\r
166                 {\r
167                         throw new NotImplementedException ();\r
168                 }\r
169                 \r
170                 public virtual Stream GetRequestStream()\r
171                 {\r
172                         throw new NotImplementedException ();\r
173                 }\r
174                 \r
175                 public virtual WebResponse GetResponse()\r
176                 {\r
177                         throw new NotImplementedException ();\r
178                 }\r
179                 \r
180                 void ISerializable.GetObjectData (SerializationInfo serializationInfo,\r
181                                                   StreamingContext streamingContext)\r
182                 {\r
183                         throw new NotSupportedException ();\r
184                 }\r
185 \r
186                 public static bool RegisterPrefix (string prefix, IWebRequestCreate creator)\r
187                 {\r
188                         if (prefix == null)\r
189                                 throw new ArgumentNullException("prefix");\r
190                         if (creator == null)\r
191                                 throw new ArgumentNullException("creator");                     \r
192                         \r
193                         lock (prefixes.SyncRoot) {\r
194                                 string lowerCasePrefix = prefix.ToLower ();\r
195                                 if (prefixes.Contains (lowerCasePrefix))\r
196                                         return false;\r
197                                 prefixes.Add (lowerCasePrefix, creator);\r
198                         }\r
199                         return true;\r
200                 }\r
201                 \r
202                 private static IWebRequestCreate GetCreator (string prefix)\r
203                 {\r
204                         int longestPrefix = -1;\r
205                         IWebRequestCreate creator = null;\r
206 \r
207                         prefix = prefix.ToLower ();\r
208 \r
209                         IDictionaryEnumerator e = prefixes.GetEnumerator ();\r
210                         while (e.MoveNext ()) {\r
211                                 string key = e.Key as string;\r
212 \r
213                                 if (key.Length <= longestPrefix) \r
214                                         continue;\r
215                                 \r
216                                 if (!prefix.StartsWith (key))\r
217                                         continue;                                       \r
218                                         \r
219                                 longestPrefix = key.Length;\r
220                                 creator = (IWebRequestCreate) e.Value;\r
221                         }\r
222                         \r
223                         if (creator == null) \r
224                                 throw new NotSupportedException (prefix);\r
225                                 \r
226                         return creator;\r
227                 }\r
228 \r
229                 internal static void ClearPrefixes ()\r
230                 {\r
231                         prefixes.Clear ();\r
232                 }\r
233 \r
234                 internal static void RemovePrefix (string prefix)\r
235                 {\r
236                         prefixes.Remove (prefix);\r
237                 }\r
238 \r
239                 internal static void AddPrefix (string prefix, string typeName)\r
240                 {\r
241                         Type type = Type.GetType (typeName);\r
242                         if (type == null)\r
243                                 throw new ConfigurationException (String.Format ("Type {0} not found", typeName));\r
244                         AddPrefix (prefix, type);\r
245                 }\r
246 \r
247                 internal static void AddPrefix (string prefix, Type type)\r
248                 {\r
249                         object o = Activator.CreateInstance (type, true);\r
250                         prefixes [prefix] = o;\r
251                 }\r
252         }\r
253 }\r
254 \r