HttpProvider.jvm.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. using System;
  2. using System.Security.Cryptography.X509Certificates;
  3. using System.IO;
  4. namespace System.Net
  5. {
  6. [Serializable]
  7. internal abstract class HttpProvider
  8. {
  9. #region Fields
  10. protected static int _defaultMaxResponseHeadersLength;
  11. protected static int _defaultMaxRedirectsNum = 50;
  12. protected Uri _originalUri;
  13. protected WebHeaderCollection _headers;
  14. protected bool _allowAutoRedirect;
  15. protected bool _allowWriteStreamBuffering = true;
  16. protected X509CertificateCollection _certificates;
  17. protected string _connectionGroupName;
  18. protected HttpContinueDelegate _continueDelegate;
  19. protected CookieContainer _cookieContainer;
  20. protected ICredentials _credentials;
  21. protected bool _keepAlive = true;
  22. protected int _maxResponseHeadersLength = _defaultMaxResponseHeadersLength;
  23. protected int _maxAutoRedirections = _defaultMaxRedirectsNum;
  24. protected int _readWriteTimeout = 300000;
  25. protected string _mediaType = string.Empty;
  26. protected string _methodName = "GET";
  27. protected bool _pipelined = true;
  28. protected bool _preAuthenticate;
  29. protected Version _version = HttpVersion.Version11;
  30. protected IWebProxy _proxy;
  31. protected bool _sendChunked;
  32. protected ServicePoint _servicePoint;
  33. protected int _timeout = 100000;
  34. protected bool _isAborted;
  35. protected long _contentLength = -1L;
  36. #endregion /* Fields */
  37. #region Constructors and Factory Methods
  38. protected HttpProvider(Uri uri)
  39. {
  40. _originalUri = uri;
  41. _headers = new WebHeaderCollection(true);
  42. _allowAutoRedirect = true;
  43. }
  44. public static HttpProvider GetHttpProvider(Uri uri)
  45. {
  46. return new VMWHttpProvider(uri);
  47. }
  48. public static HttpProvider GetHttpProvider(string provider, Uri uri)
  49. {
  50. Type type = Type.GetType(provider, true);
  51. if(type != null)
  52. return GetHttpProvider(type, uri);
  53. //log it as an error
  54. return new VMWHttpProvider(uri);
  55. }
  56. public static HttpProvider GetHttpProvider(Type provider, Uri uri)
  57. {
  58. try
  59. {
  60. return (HttpProvider)Activator.CreateInstance(provider,
  61. new object[]{uri});
  62. }
  63. catch
  64. {
  65. //log it as an error
  66. return new VMWHttpProvider(uri);
  67. }
  68. }
  69. #endregion
  70. #region Properties
  71. internal virtual WebHeaderCollection Headers
  72. {
  73. get{return _headers;}
  74. set
  75. {
  76. if(IsRequestStarted ())
  77. throw new InvalidOperationException("Connection already opened");
  78. WebHeaderCollection newHeaders = new WebHeaderCollection (true);
  79. int count = value.Count;
  80. for (int i = 0; i < count; i++)
  81. newHeaders.Add (value.GetKey (i), value.Get (i));
  82. _headers = newHeaders;
  83. }
  84. }
  85. internal virtual bool AllowAutoRedirect
  86. {
  87. get{return _allowAutoRedirect;}
  88. set{_allowAutoRedirect = value;}
  89. }
  90. internal virtual bool AllowWriteStreamBuffering
  91. {
  92. get{return _allowWriteStreamBuffering;}
  93. set{_allowWriteStreamBuffering = value;}
  94. }
  95. internal virtual string ConnectionGroupName
  96. {
  97. get{return _connectionGroupName;}
  98. set{_connectionGroupName = value;}
  99. }
  100. internal virtual HttpContinueDelegate ContinueDelegate
  101. {
  102. get{return _continueDelegate;}
  103. set{_continueDelegate = value;}
  104. }
  105. internal virtual CookieContainer CookieContainer
  106. {
  107. get{return _cookieContainer;}
  108. set{_cookieContainer = value;}
  109. }
  110. internal virtual ICredentials Credentials
  111. {
  112. get{return _credentials;}
  113. set{_credentials = value;}
  114. }
  115. internal static int DefaultMaxResponseHeadersLength
  116. {
  117. get{return _defaultMaxResponseHeadersLength;}
  118. set
  119. {
  120. if (value < 0 && value != -1)
  121. throw new ArgumentOutOfRangeException("Argument should be positive");
  122. _defaultMaxResponseHeadersLength = value;
  123. }
  124. }
  125. internal virtual bool KeepAlive
  126. {
  127. get{return _keepAlive;}
  128. set{_keepAlive = value;}
  129. }
  130. internal virtual int MaxAutoRedirections
  131. {
  132. get{return _maxAutoRedirections;}
  133. set
  134. {
  135. if (value <= 0)
  136. throw new ArgumentException("Must be > 0", "value");
  137. _maxAutoRedirections = value;
  138. }
  139. }
  140. internal virtual int MaximumResponseHeadersLength
  141. {
  142. get{return _maxResponseHeadersLength;}
  143. set
  144. {
  145. if (IsRequestStarted())
  146. {
  147. throw new InvalidOperationException("Request has been already submitted.");
  148. }
  149. if (value < 0 && value != -1)
  150. throw new ArgumentOutOfRangeException("The argument must be positive or -1");
  151. _maxResponseHeadersLength = value;
  152. }
  153. }
  154. internal virtual string MediaType
  155. {
  156. get{return _mediaType;}
  157. set{_mediaType = value;}
  158. }
  159. internal virtual string MethodName
  160. {
  161. get{return _methodName;}
  162. set
  163. {
  164. if (value == null || value.Trim () == "")
  165. throw new ArgumentException ("not a valid method");
  166. _methodName = value;
  167. }
  168. }
  169. internal virtual bool Pipelined
  170. {
  171. get{return _pipelined;}
  172. set{_pipelined = value;}
  173. }
  174. internal virtual bool PreAuthenticate
  175. {
  176. get { return _preAuthenticate; }
  177. set { _preAuthenticate = value; }
  178. }
  179. internal virtual Version ProtocolVersion
  180. {
  181. get{return _version;}
  182. set
  183. {
  184. if (value != HttpVersion.Version10 && value != HttpVersion.Version11)
  185. throw new ArgumentException ("value");
  186. _version = value;
  187. }
  188. }
  189. internal virtual IWebProxy Proxy
  190. {
  191. get{return _proxy;}
  192. set
  193. {
  194. if(IsRequestStarted())
  195. throw new InvalidOperationException("Request already has been submitted");
  196. if(value == null)
  197. throw new ArgumentNullException("value");
  198. if(!(value is WebProxy))
  199. throw new NotImplementedException("The supported proxy objects only of type System.Net.WebProxy");
  200. _proxy = value;
  201. }
  202. }
  203. internal virtual int ReadWriteTimeout
  204. {
  205. get{return _readWriteTimeout;}
  206. set
  207. {
  208. if (IsRequestStarted())
  209. throw new InvalidOperationException("Request has been submitted.");
  210. if (value < 0 && value != -1)
  211. throw new ArgumentOutOfRangeException("value");
  212. _readWriteTimeout = value;
  213. }
  214. }
  215. internal virtual bool SendChunked
  216. {
  217. get{return _sendChunked;}
  218. set
  219. {
  220. if(IsRequestStarted ())
  221. throw new InvalidOperationException("Request has been submitted.");
  222. _sendChunked = value;
  223. }
  224. }
  225. internal virtual ServicePoint ServicePoint
  226. {
  227. get{return _servicePoint;}
  228. }
  229. internal virtual int Timeout
  230. {
  231. get{return _timeout;}
  232. set
  233. {
  234. if (value < -1)
  235. throw new ArgumentOutOfRangeException ("value");
  236. _timeout = value;
  237. }
  238. }
  239. internal virtual long ContentLength
  240. {
  241. get{return _contentLength;}
  242. set
  243. {
  244. if(value < 0)
  245. throw new ArgumentOutOfRangeException("value", "The Content-Length property value must be positive");
  246. _contentLength = value;
  247. }
  248. }
  249. #endregion
  250. #region Methods
  251. public virtual Uri GetOriginalAddress()
  252. {
  253. return _originalUri;
  254. }
  255. public virtual X509CertificateCollection GetX509Certificates()
  256. {
  257. if(_certificates == null)
  258. _certificates = new X509CertificateCollection();
  259. return _certificates;
  260. }
  261. public abstract bool IsRequestStarted();
  262. public abstract Uri GetAddress();
  263. public abstract bool IsHaveResponse();
  264. public abstract void Abort();
  265. public abstract Stream GetRequestStream();
  266. public abstract WebResponse GetResponse();
  267. public abstract IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state);
  268. public abstract Stream EndGetRequestStream(IAsyncResult asyncResult);
  269. public abstract IAsyncResult BeginGetResponse(AsyncCallback callback, object state);
  270. public abstract WebResponse EndGetResponse(IAsyncResult asyncResult);
  271. #endregion
  272. }
  273. }