Removed Consoles and ^Ms
[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 \r
8 using System;\r
9 using System.Collections;\r
10 using System.Collections.Specialized;\r
11 using System.Configuration;\r
12 using System.IO;\r
13 using System.Runtime.Serialization;\r
14 \r
15 namespace System.Net \r
16 {\r
17         [Serializable]\r
18         public abstract class WebRequest : MarshalByRefObject, ISerializable\r
19         {\r
20                 static HybridDictionary prefixes = new HybridDictionary ();\r
21                 \r
22                 // Constructors\r
23                 \r
24                 static WebRequest ()\r
25                 {\r
26                         ConfigurationSettings.GetConfig ("system.net/webRequestModules");\r
27                 }\r
28                 \r
29                 protected WebRequest () { }             \r
30                 \r
31                 protected WebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext) \r
32                 {\r
33                         throw new NotSupportedException ();\r
34                 }\r
35                 \r
36                 // Properties\r
37                 \r
38                 public virtual string ConnectionGroupName { \r
39                         get { throw new NotSupportedException (); }\r
40                         set { throw new NotSupportedException (); }\r
41                 }\r
42                 \r
43                 public virtual long ContentLength { \r
44                         get { throw new NotSupportedException (); }\r
45                         set { throw new NotSupportedException (); }\r
46                 }\r
47                 \r
48                 public virtual string ContentType { \r
49                         get { throw new NotSupportedException (); }\r
50                         set { throw new NotSupportedException (); }\r
51                 }\r
52                 \r
53                 public virtual ICredentials Credentials { \r
54                         get { throw new NotSupportedException (); }\r
55                         set { throw new NotSupportedException (); }\r
56                 }\r
57                 \r
58                 public virtual WebHeaderCollection Headers { \r
59                         get { throw new NotSupportedException (); }\r
60                         set { throw new NotSupportedException (); }\r
61                 }\r
62                 \r
63                 public virtual string Method { \r
64                         get { throw new NotSupportedException (); }\r
65                         set { throw new NotSupportedException (); }\r
66                 }\r
67                 \r
68                 public virtual bool PreAuthenticate { \r
69                         get { throw new NotSupportedException (); }\r
70                         set { throw new NotSupportedException (); }\r
71                 }\r
72                 \r
73                 public virtual IWebProxy Proxy { \r
74                         get { throw new NotSupportedException (); }\r
75                         set { throw new NotSupportedException (); }\r
76                 }\r
77                 \r
78                 public virtual Uri RequestUri { \r
79                         get { throw new NotSupportedException (); }\r
80                 }\r
81                 \r
82                 public virtual int Timeout { \r
83                         get { throw new NotSupportedException (); }\r
84                         set { throw new NotSupportedException (); }\r
85                 }\r
86                 \r
87                 // Methods\r
88                 \r
89                 public virtual void Abort()\r
90                 {\r
91                         throw new NotSupportedException ();\r
92                 }\r
93                 \r
94                 public virtual IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state) \r
95                 {\r
96                         throw new NotSupportedException ();\r
97                 }\r
98                 \r
99                 public virtual IAsyncResult BeginGetResponse (AsyncCallback callback, object state)\r
100                 {\r
101                         throw new NotSupportedException ();\r
102                 }\r
103 \r
104                 public static WebRequest Create (string requestUriString) \r
105                 {\r
106                         if (requestUriString == null)\r
107                                 throw new ArgumentNullException ("requestUriString");\r
108                         return Create (new Uri (requestUriString));\r
109                 }\r
110                                 \r
111                 public static WebRequest Create (Uri requestUri) \r
112                 {\r
113                         if (requestUri == null)\r
114                                 throw new ArgumentNullException ("requestUri");\r
115                         return GetCreator (requestUri.AbsoluteUri).Create (requestUri);\r
116                 }\r
117                 \r
118                 public static WebRequest CreateDefault (Uri requestUri)\r
119                 {\r
120                         if (requestUri == null)\r
121                                 throw new ArgumentNullException ("requestUri");\r
122                         return GetCreator (requestUri.Scheme).Create (requestUri);\r
123                 }\r
124 \r
125                 public virtual Stream EndGetRequestStream (IAsyncResult asyncResult)\r
126                 {\r
127                         throw new NotSupportedException ();\r
128                 }\r
129                 \r
130                 public virtual WebResponse EndGetResponse (IAsyncResult asyncResult)\r
131                 {\r
132                         throw new NotSupportedException ();\r
133                 }\r
134                 \r
135                 public virtual Stream GetRequestStream()\r
136                 {\r
137                         throw new NotSupportedException ();\r
138                 }\r
139                 \r
140                 public virtual WebResponse GetResponse()\r
141                 {\r
142                         throw new NotSupportedException ();\r
143                 }\r
144                 \r
145                 void ISerializable.GetObjectData (SerializationInfo serializationInfo,\r
146                                                   StreamingContext streamingContext)\r
147                 {\r
148                         throw new NotSupportedException ();\r
149                 }\r
150 \r
151                 public static bool RegisterPrefix (string prefix, IWebRequestCreate creator)\r
152                 {\r
153                         if (prefix == null)\r
154                                 throw new ArgumentNullException("prefix");\r
155                         if (creator == null)\r
156                                 throw new ArgumentNullException("creator");                     \r
157                         \r
158                         lock (prefixes.SyncRoot) {\r
159                                 string lowerCasePrefix = prefix.ToLower ();\r
160                                 if (prefixes.Contains (lowerCasePrefix))\r
161                                         return false;\r
162                                 prefixes.Add (lowerCasePrefix, creator);\r
163                         }\r
164                         return true;\r
165                 }\r
166                 \r
167                 private static IWebRequestCreate GetCreator (string prefix)\r
168                 {\r
169                         int longestPrefix = -1;\r
170                         IWebRequestCreate creator = null;\r
171 \r
172                         prefix = prefix.ToLower ();\r
173 \r
174                         IDictionaryEnumerator e = prefixes.GetEnumerator ();\r
175                         while (e.MoveNext ()) {\r
176                                 string key = e.Key as string;\r
177 \r
178                                 if (key.Length <= longestPrefix) \r
179                                         continue;\r
180                                 \r
181                                 if (!prefix.StartsWith (key))\r
182                                         continue;                                       \r
183                                         \r
184                                 longestPrefix = key.Length;\r
185                                 creator = (IWebRequestCreate) e.Value;\r
186                         }\r
187                         \r
188                         if (creator == null) \r
189                                 throw new NotSupportedException (prefix);\r
190                                 \r
191                         return creator;\r
192                 }\r
193 \r
194                 internal static void ClearPrefixes ()\r
195                 {\r
196                         prefixes.Clear ();\r
197                 }\r
198 \r
199                 internal static void RemovePrefix (string prefix)\r
200                 {\r
201                         prefixes.Remove (prefix);\r
202                 }\r
203 \r
204                 internal static void AddPrefix (string prefix, string typeName)\r
205                 {\r
206                         Type type = Type.GetType (typeName);\r
207                         if (type == null)\r
208                                 throw new ConfigurationException (String.Format ("Type {0} not found", typeName));\r
209 \r
210                         object o = Activator.CreateInstance (type, true);\r
211                         prefixes [prefix] = o;\r
212                 }\r
213         }\r
214 }\r
215 \r