WebRequest.cs 8.3 KB

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