Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

HTTPRequest.cs 51KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. namespace BestHTTP
  7. {
  8. using BestHTTP.Authentication;
  9. using BestHTTP.Extensions;
  10. using BestHTTP.Forms;
  11. #if !BESTHTTP_DISABLE_COOKIES && (!UNITY_WEBGL || UNITY_EDITOR)
  12. using BestHTTP.Cookies;
  13. #endif
  14. /// <summary>
  15. /// Possible logical states of a HTTTPRequest object.
  16. /// </summary>
  17. public enum HTTPRequestStates
  18. {
  19. /// <summary>
  20. /// Initial status of a request. No callback will be called with this status.
  21. /// </summary>
  22. Initial,
  23. /// <summary>
  24. /// Waiting in a queue to be processed. No callback will be called with this status.
  25. /// </summary>
  26. Queued,
  27. /// <summary>
  28. /// Processing of the request started. In this state the client will send the request, and parse the response. No callback will be called with this status.
  29. /// </summary>
  30. Processing,
  31. /// <summary>
  32. /// The request finished without problem. Parsing the response done, the result can be used. The user defined callback will be called with a valid response object. The request’s Exception property will be null.
  33. /// </summary>
  34. Finished,
  35. /// <summary>
  36. /// The request finished with an unexpected error. The user defined callback will be called with a null response object. The request's Exception property may contain more info about the error, but it can be null.
  37. /// </summary>
  38. Error,
  39. /// <summary>
  40. /// The request aborted by the client(HTTPRequest’s Abort() function). The user defined callback will be called with a null response. The request’s Exception property will be null.
  41. /// </summary>
  42. Aborted,
  43. /// <summary>
  44. /// Connecting to the server timed out. The user defined callback will be called with a null response. The request’s Exception property will be null.
  45. /// </summary>
  46. ConnectionTimedOut,
  47. /// <summary>
  48. /// The request didn't finished in the given time. The user defined callback will be called with a null response. The request’s Exception property will be null.
  49. /// </summary>
  50. TimedOut
  51. }
  52. public delegate void OnRequestFinishedDelegate(HTTPRequest originalRequest, HTTPResponse response);
  53. public delegate void OnDownloadProgressDelegate(HTTPRequest originalRequest, int downloaded, int downloadLength);
  54. public delegate void OnUploadProgressDelegate(HTTPRequest originalRequest, long uploaded, long uploadLength);
  55. public delegate bool OnBeforeRedirectionDelegate(HTTPRequest originalRequest, HTTPResponse response, Uri redirectUri);
  56. public delegate void OnHeaderEnumerationDelegate(string header, List<string> values);
  57. public delegate void OnBeforeHeaderSendDelegate(HTTPRequest req);
  58. /// <summary>
  59. ///
  60. /// </summary>
  61. public sealed class HTTPRequest : IEnumerator, IEnumerator<HTTPRequest>
  62. {
  63. #region Statics
  64. public static readonly byte[] EOL = { HTTPResponse.CR, HTTPResponse.LF };
  65. /// <summary>
  66. /// Cached uppercase values to save some cpu cycles and GC alloc per request.
  67. /// </summary>
  68. public static readonly string[] MethodNames = {
  69. HTTPMethods.Get.ToString().ToUpper(),
  70. HTTPMethods.Head.ToString().ToUpper(),
  71. HTTPMethods.Post.ToString().ToUpper(),
  72. HTTPMethods.Put.ToString().ToUpper(),
  73. HTTPMethods.Delete.ToString().ToUpper(),
  74. HTTPMethods.Patch.ToString().ToUpper()
  75. };
  76. /// <summary>
  77. /// Size of the internal buffer, and upload progress will be fired when this size of data sent to the wire. It's default value is 2 KiB.
  78. /// </summary>
  79. public static int UploadChunkSize = 2 * 1024;
  80. #endregion
  81. #region Properties
  82. /// <summary>
  83. /// The original request's Uri.
  84. /// </summary>
  85. public Uri Uri { get; private set; }
  86. /// <summary>
  87. /// The method that how we want to process our request the server.
  88. /// </summary>
  89. public HTTPMethods MethodType { get; set; }
  90. /// <summary>
  91. /// The raw data to send in a POST request. If it set all other fields that added to this request will be ignored.
  92. /// </summary>
  93. public byte[] RawData { get; set; }
  94. /// <summary>
  95. /// The stream that the plugin will use to get the data to send out the server. When this property is set, no forms or the RawData property will be used
  96. /// </summary>
  97. public Stream UploadStream { get; set; }
  98. /// <summary>
  99. /// When set to true(its default value) the plugin will call the UploadStream's Dispose() function when finished uploading the data from it. Default value is true.
  100. /// </summary>
  101. public bool DisposeUploadStream { get; set; }
  102. /// <summary>
  103. /// If it's true, the plugin will use the Stream's Length property. Otherwise the plugin will send the data chunked. Default value is true.
  104. /// </summary>
  105. public bool UseUploadStreamLength { get; set; }
  106. /// <summary>
  107. /// Called after data sent out to the wire.
  108. /// </summary>
  109. public OnUploadProgressDelegate OnUploadProgress;
  110. /// <summary>
  111. /// Indicates that the connection should be open after the response received. If its true, then the internal TCP connections will be reused if it's possible. Default value is true.
  112. /// The default value can be changed in the HTTPManager class. If you make rare request to the server it's should be changed to false.
  113. /// </summary>
  114. public bool IsKeepAlive
  115. {
  116. get { return isKeepAlive; }
  117. set
  118. {
  119. if (State == HTTPRequestStates.Processing)
  120. throw new NotSupportedException("Changing the IsKeepAlive property while processing the request is not supported.");
  121. isKeepAlive = value;
  122. }
  123. }
  124. #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
  125. /// <summary>
  126. /// With this property caching can be enabled/disabled on a per-request basis.
  127. /// </summary>
  128. public bool DisableCache
  129. {
  130. get { return disableCache; }
  131. set
  132. {
  133. if (State == HTTPRequestStates.Processing)
  134. throw new NotSupportedException("Changing the DisableCache property while processing the request is not supported.");
  135. disableCache = value;
  136. }
  137. }
  138. #endif
  139. /// <summary>
  140. /// If it's true, the Callback will be called every time if we can send out at least one fragment.
  141. /// </summary>
  142. public bool UseStreaming
  143. {
  144. get { return useStreaming; }
  145. set
  146. {
  147. if (State == HTTPRequestStates.Processing)
  148. throw new NotSupportedException("Changing the UseStreaming property while processing the request is not supported.");
  149. useStreaming = value;
  150. }
  151. }
  152. /// <summary>
  153. /// Maximum size of a data chunk that we want to receive when streaming is set.
  154. /// </summary>
  155. public int StreamFragmentSize
  156. {
  157. get{ return streamFragmentSize; }
  158. set
  159. {
  160. if (State == HTTPRequestStates.Processing)
  161. throw new NotSupportedException("Changing the StreamFragmentSize property while processing the request is not supported.");
  162. if (value < 1)
  163. throw new System.ArgumentException("StreamFragmentSize must be at least 1.");
  164. streamFragmentSize = value;
  165. }
  166. }
  167. /// <summary>
  168. /// The callback function that will be called when a request is fully processed or when any downloaded fragment is available if UseStreaming is true. Can be null for fire-and-forget requests.
  169. /// </summary>
  170. public OnRequestFinishedDelegate Callback { get; set; }
  171. /// <summary>
  172. /// Called when new data downloaded from the server.
  173. /// The first parameter is the original HTTTPRequest object itself, the second parameter is the downloaded bytes while the third parameter is the content length.
  174. /// <remarks>There are download modes where we can't figure out the exact length of the final content. In these cases we just guarantee that the third parameter will be at least the size of the second one.</remarks>
  175. /// </summary>
  176. public OnDownloadProgressDelegate OnProgress;
  177. /// <summary>
  178. /// Called when the current protocol is upgraded to an other. (HTTP => WebSocket for example)
  179. /// </summary>
  180. public OnRequestFinishedDelegate OnUpgraded;
  181. /// <summary>
  182. /// With this option if reading back the server's response fails, the request will fail and any exceptions can be checked through the Exception property. The default value is True for POST requests, otherwise false.
  183. /// </summary>
  184. public bool DisableRetry { get; set; }
  185. /// <summary>
  186. /// Indicates that the request is redirected. If a request is redirected, the connection that served it will be closed regardless of the value of IsKeepAlive.
  187. /// </summary>
  188. public bool IsRedirected { get; internal set; }
  189. /// <summary>
  190. /// The Uri that the request redirected to.
  191. /// </summary>
  192. public Uri RedirectUri { get; internal set; }
  193. /// <summary>
  194. /// If redirected it contains the RedirectUri.
  195. /// </summary>
  196. public Uri CurrentUri { get { return IsRedirected ? RedirectUri : Uri; } }
  197. /// <summary>
  198. /// The response to the query.
  199. /// <remarks>If an exception occurred during reading of the response stream or can't connect to the server, this will be null!</remarks>
  200. /// </summary>
  201. public HTTPResponse Response { get; internal set; }
  202. #if !BESTHTTP_DISABLE_PROXY
  203. /// <summary>
  204. /// Response from the Proxy server. It's null with transparent proxies.
  205. /// </summary>
  206. public HTTPResponse ProxyResponse { get; internal set; }
  207. #endif
  208. /// <summary>
  209. /// It there is an exception while processing the request or response the Response property will be null, and the Exception will be stored in this property.
  210. /// </summary>
  211. public Exception Exception { get; internal set; }
  212. /// <summary>
  213. /// Any object can be passed with the request with this property. (eq. it can be identified, etc.)
  214. /// </summary>
  215. public object Tag { get; set; }
  216. /// <summary>
  217. /// The UserName, Password pair that the plugin will use to authenticate to the remote server.
  218. /// </summary>
  219. public Credentials Credentials { get; set; }
  220. #if !BESTHTTP_DISABLE_PROXY
  221. /// <summary>
  222. /// True, if there is a Proxy object.
  223. /// </summary>
  224. public bool HasProxy { get { return Proxy != null; } }
  225. /// <summary>
  226. /// A web proxy's properties where the request must pass through.
  227. /// </summary>
  228. public HTTPProxy Proxy { get; set; }
  229. #endif
  230. /// <summary>
  231. /// How many redirection supported for this request. The default is int.MaxValue. 0 or a negative value means no redirection supported.
  232. /// </summary>
  233. public int MaxRedirects { get; set; }
  234. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  235. /// <summary>
  236. /// Use Bouncy Castle's code to handle the secure protocol instead of Mono's. You can try to set it true if you receive a "System.Security.Cryptography.CryptographicException: Unsupported hash algorithm" exception.
  237. /// </summary>
  238. public bool UseAlternateSSL { get; set; }
  239. #endif
  240. #if !BESTHTTP_DISABLE_COOKIES && (!UNITY_WEBGL || UNITY_EDITOR)
  241. /// <summary>
  242. /// If true cookies will be added to the headers (if any), and parsed from the response. If false, all cookie operations will be ignored. It's default value is HTTPManager's IsCookiesEnabled.
  243. /// </summary>
  244. public bool IsCookiesEnabled { get; set; }
  245. /// <summary>
  246. /// Cookies that are added to this list will be sent to the server alongside withe the server sent ones. If cookies are disabled only these cookies will be sent.
  247. /// </summary>
  248. public List<Cookie> Cookies
  249. {
  250. get
  251. {
  252. if (customCookies == null)
  253. customCookies = new List<Cookie>();
  254. return customCookies;
  255. }
  256. set { customCookies = value; }
  257. }
  258. private List<Cookie> customCookies;
  259. #endif
  260. /// <summary>
  261. /// What form should used. Default to Automatic.
  262. /// </summary>
  263. public HTTPFormUsage FormUsage { get; set; }
  264. /// <summary>
  265. /// Current state of this request.
  266. /// </summary>
  267. public HTTPRequestStates State { get; internal set; }
  268. /// <summary>
  269. /// How many times redirected.
  270. /// </summary>
  271. public int RedirectCount { get; internal set; }
  272. #if !NETFX_CORE && !UNITY_WP8
  273. /// <summary>
  274. /// Custom validator for an SslStream. This event will receive the original HTTPRequest, an X509Certificate and an X509Chain objects. It must return true if the certificate valid, false otherwise.
  275. /// <remarks>It's called in a thread! Not available on Windows Phone!</remarks>
  276. /// </summary>
  277. public event System.Func<HTTPRequest, System.Security.Cryptography.X509Certificates.X509Certificate, System.Security.Cryptography.X509Certificates.X509Chain, bool> CustomCertificationValidator;
  278. #endif
  279. /// <summary>
  280. /// Maximum time we wait to establish the connection to the target server. If set to TimeSpan.Zero or lower, no connect timeout logic is executed. Default value is 20 seconds.
  281. /// </summary>
  282. public TimeSpan ConnectTimeout { get; set; }
  283. /// <summary>
  284. /// Maximum time we want to wait to the request to finish after the connection is established. Default value is 60 seconds.
  285. /// <remarks>It's disabled for streaming requests! See <see cref="EnableTimoutForStreaming"/>.</remarks>
  286. /// </summary>
  287. public TimeSpan Timeout { get; set; }
  288. /// <summary>
  289. /// Set to true to enable Timeouts on streaming request. Default value is false.
  290. /// </summary>
  291. public bool EnableTimoutForStreaming { get; set; }
  292. /// <summary>
  293. /// Enables safe read method when the response's length of the content is unknown. Its default value is enabled (true).
  294. /// </summary>
  295. public bool EnableSafeReadOnUnknownContentLength { get; set; }
  296. /// <summary>
  297. /// The priority of the request. Higher priority requests will be picked from the request queue sooner than lower priority ones.
  298. /// </summary>
  299. public int Priority { get; set; }
  300. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  301. /// <summary>
  302. /// The ICertificateVerifyer implementation that the plugin will use to verify the server certificates when the request's UseAlternateSSL property is set to true.
  303. /// </summary>
  304. public Org.BouncyCastle.Crypto.Tls.ICertificateVerifyer CustomCertificateVerifyer { get; set; }
  305. /// <summary>
  306. /// The IClientCredentialsProvider implementation that the plugin will use to send client certificates when the request's UseAlternateSSL property is set to true.
  307. /// </summary>
  308. public Org.BouncyCastle.Crypto.Tls.IClientCredentialsProvider CustomClientCredentialsProvider { get; set; }
  309. #endif
  310. /// <summary>
  311. ///
  312. /// </summary>
  313. public SupportedProtocols ProtocolHandler { get; set; }
  314. /// <summary>
  315. /// It's called before the plugin will do a new request to the new uri. The return value of this function will control the redirection: if it's false the redirection is aborted.
  316. /// This function is called on a thread other than the main Unity thread!
  317. /// </summary>
  318. public event OnBeforeRedirectionDelegate OnBeforeRedirection
  319. {
  320. add { onBeforeRedirection += value; }
  321. remove { onBeforeRedirection -= value; }
  322. }
  323. private OnBeforeRedirectionDelegate onBeforeRedirection;
  324. /// <summary>
  325. /// This event will be fired before the plugin will write headers to the wire. New headers can be added in this callback. This event is called on a non-Unity thread!
  326. /// </summary>
  327. public event OnBeforeHeaderSendDelegate OnBeforeHeaderSend
  328. {
  329. add { _onBeforeHeaderSend += value; }
  330. remove { _onBeforeHeaderSend -= value; }
  331. }
  332. private OnBeforeHeaderSendDelegate _onBeforeHeaderSend;
  333. #region Internal Properties For Progress Report Support
  334. /// <summary>
  335. /// How many bytes downloaded so far.
  336. /// </summary>
  337. internal int Downloaded { get; set; }
  338. /// <summary>
  339. /// The length of the content that we are currently downloading.
  340. /// If chunked encoding is used, then it is the size of the sum of all previous chunks plus the current one.
  341. /// When no Content-Length present and no chunked encoding is used then its size is the currently downloaded size.
  342. /// </summary>
  343. internal int DownloadLength { get; set; }
  344. /// <summary>
  345. /// Set to true when the downloaded bytes are changed, and set to false when the OnProgress event called.
  346. /// </summary>
  347. internal bool DownloadProgressChanged { get; set; }
  348. /// <summary>
  349. /// Will return the length of the UploadStream, or -1 if it's not supported.
  350. /// </summary>
  351. internal long UploadStreamLength
  352. {
  353. get
  354. {
  355. if (UploadStream == null || !UseUploadStreamLength)
  356. return -1;
  357. try
  358. {
  359. // This may will throw a NotSupportedException
  360. return UploadStream.Length;
  361. }
  362. catch
  363. {
  364. // We will fall back to chunked
  365. return -1;
  366. }
  367. }
  368. }
  369. /// <summary>
  370. /// How many bytes are sent to the wire
  371. /// </summary>
  372. internal long Uploaded { get; set; }
  373. /// <summary>
  374. /// How many bytes are expected we are sending. If we are don't know, then it will be -1.
  375. /// </summary>
  376. internal long UploadLength { get; set; }
  377. /// <summary>
  378. /// Set to true when the uploaded bytes are changed, and set to false when the OnUploadProgress event called.
  379. /// </summary>
  380. internal bool UploadProgressChanged { get; set; }
  381. #endregion
  382. #endregion
  383. #region Privates
  384. private bool isKeepAlive;
  385. #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
  386. private bool disableCache;
  387. #endif
  388. private int streamFragmentSize;
  389. private bool useStreaming;
  390. private Dictionary<string, List<string>> Headers { get; set; }
  391. /// <summary>
  392. /// We will collect the fields and values to the FieldCollector through the AddField and AddBinaryData functions.
  393. /// </summary>
  394. private HTTPFormBase FieldCollector;
  395. /// <summary>
  396. /// When the request about to send the request we will create a specialized form implementation(url-encoded, multipart, or the legacy WWWForm based).
  397. /// And we will use this instance to create the data that we will send to the server.
  398. /// </summary>
  399. private HTTPFormBase FormImpl;
  400. #endregion
  401. #region Constructors
  402. #region Default Get Constructors
  403. public HTTPRequest(Uri uri)
  404. : this(uri, HTTPMethods.Get, HTTPManager.KeepAliveDefaultValue,
  405. #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
  406. HTTPManager.IsCachingDisabled
  407. #else
  408. true
  409. #endif
  410. , null)
  411. {
  412. }
  413. public HTTPRequest(Uri uri, OnRequestFinishedDelegate callback)
  414. : this(uri, HTTPMethods.Get, HTTPManager.KeepAliveDefaultValue,
  415. #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
  416. HTTPManager.IsCachingDisabled
  417. #else
  418. true
  419. #endif
  420. , callback)
  421. {
  422. }
  423. public HTTPRequest(Uri uri, bool isKeepAlive, OnRequestFinishedDelegate callback)
  424. : this(uri, HTTPMethods.Get, isKeepAlive,
  425. #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
  426. HTTPManager.IsCachingDisabled
  427. #else
  428. true
  429. #endif
  430. , callback)
  431. {
  432. }
  433. public HTTPRequest(Uri uri, bool isKeepAlive, bool disableCache, OnRequestFinishedDelegate callback)
  434. : this(uri, HTTPMethods.Get, isKeepAlive, disableCache, callback)
  435. {
  436. }
  437. #endregion
  438. public HTTPRequest(Uri uri, HTTPMethods methodType)
  439. : this(uri, methodType, HTTPManager.KeepAliveDefaultValue,
  440. #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
  441. HTTPManager.IsCachingDisabled || methodType != HTTPMethods.Get
  442. #else
  443. true
  444. #endif
  445. , null)
  446. {
  447. }
  448. public HTTPRequest(Uri uri, HTTPMethods methodType, OnRequestFinishedDelegate callback)
  449. : this(uri, methodType, HTTPManager.KeepAliveDefaultValue,
  450. #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
  451. HTTPManager.IsCachingDisabled || methodType != HTTPMethods.Get
  452. #else
  453. true
  454. #endif
  455. , callback)
  456. {
  457. }
  458. public HTTPRequest(Uri uri, HTTPMethods methodType, bool isKeepAlive, OnRequestFinishedDelegate callback)
  459. : this(uri, methodType, isKeepAlive,
  460. #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
  461. HTTPManager.IsCachingDisabled || methodType != HTTPMethods.Get
  462. #else
  463. true
  464. #endif
  465. , callback)
  466. {
  467. }
  468. public HTTPRequest(Uri uri, HTTPMethods methodType, bool isKeepAlive, bool disableCache, OnRequestFinishedDelegate callback)
  469. {
  470. this.Uri = uri;
  471. this.MethodType = methodType;
  472. this.IsKeepAlive = isKeepAlive;
  473. #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
  474. this.DisableCache = disableCache;
  475. #endif
  476. this.Callback = callback;
  477. this.StreamFragmentSize = 4 * 1024;
  478. this.DisableRetry = !(methodType == HTTPMethods.Get);
  479. this.MaxRedirects = int.MaxValue;
  480. this.RedirectCount = 0;
  481. #if !BESTHTTP_DISABLE_COOKIES && (!UNITY_WEBGL || UNITY_EDITOR)
  482. this.IsCookiesEnabled = HTTPManager.IsCookiesEnabled;
  483. #endif
  484. this.Downloaded = DownloadLength = 0;
  485. this.DownloadProgressChanged = false;
  486. this.State = HTTPRequestStates.Initial;
  487. this.ConnectTimeout = HTTPManager.ConnectTimeout;
  488. this.Timeout = HTTPManager.RequestTimeout;
  489. this.EnableTimoutForStreaming = false;
  490. this.EnableSafeReadOnUnknownContentLength = true;
  491. #if !BESTHTTP_DISABLE_PROXY
  492. this.Proxy = HTTPManager.Proxy;
  493. #endif
  494. this.UseUploadStreamLength = true;
  495. this.DisposeUploadStream = true;
  496. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  497. this.CustomCertificateVerifyer = HTTPManager.DefaultCertificateVerifyer;
  498. this.CustomClientCredentialsProvider = HTTPManager.DefaultClientCredentialsProvider;
  499. this.UseAlternateSSL = HTTPManager.UseAlternateSSLDefaultValue;
  500. #endif
  501. }
  502. #endregion
  503. #region Public Field Functions
  504. /// <summary>
  505. /// Add a field with a given string value.
  506. /// </summary>
  507. public void AddField(string fieldName, string value)
  508. {
  509. AddField(fieldName, value, System.Text.Encoding.UTF8);
  510. }
  511. /// <summary>
  512. /// Add a field with a given string value.
  513. /// </summary>
  514. public void AddField(string fieldName, string value, System.Text.Encoding e)
  515. {
  516. if (FieldCollector == null)
  517. FieldCollector = new HTTPFormBase();
  518. FieldCollector.AddField(fieldName, value, e);
  519. }
  520. /// <summary>
  521. /// Add a field with binary content to the form.
  522. /// </summary>
  523. public void AddBinaryData(string fieldName, byte[] content)
  524. {
  525. AddBinaryData(fieldName, content, null, null);
  526. }
  527. /// <summary>
  528. /// Add a field with binary content to the form.
  529. /// </summary>
  530. public void AddBinaryData(string fieldName, byte[] content, string fileName)
  531. {
  532. AddBinaryData(fieldName, content, fileName, null);
  533. }
  534. /// <summary>
  535. /// Add a field with binary content to the form.
  536. /// </summary>
  537. public void AddBinaryData(string fieldName, byte[] content, string fileName, string mimeType)
  538. {
  539. if (FieldCollector == null)
  540. FieldCollector = new HTTPFormBase();
  541. FieldCollector.AddBinaryData(fieldName, content, fileName, mimeType);
  542. }
  543. /// <summary>
  544. /// Set or overwrite the internal form. Remarks: on WP8 it doesn't supported!
  545. /// </summary>
  546. public void SetFields(UnityEngine.WWWForm wwwForm)
  547. {
  548. #if !BESTHTTP_DISABLE_UNITY_FORM
  549. FormUsage = HTTPFormUsage.Unity;
  550. FormImpl = new UnityForm(wwwForm);
  551. #endif
  552. }
  553. /// <summary>
  554. /// Manually set a HTTP Form.
  555. /// </summary>
  556. public void SetForm(HTTPFormBase form)
  557. {
  558. FormImpl = form;
  559. }
  560. /// <summary>
  561. /// Clears all data from the form.
  562. /// </summary>
  563. public void ClearForm()
  564. {
  565. FormImpl = null;
  566. FieldCollector = null;
  567. }
  568. /// <summary>
  569. /// Will create the form implementation based on the value of the FormUsage property.
  570. /// </summary>
  571. private HTTPFormBase SelectFormImplementation()
  572. {
  573. // Our form already created with a previous
  574. if (FormImpl != null)
  575. return FormImpl;
  576. // No field added to this request yet
  577. if (FieldCollector == null)
  578. return null;
  579. switch (FormUsage)
  580. {
  581. case HTTPFormUsage.Automatic:
  582. // A really simple decision making: if there are at least one field with binary data, or a 'long' string value then we will choose a Multipart form.
  583. // Otherwise Url Encoded form will be used.
  584. if (FieldCollector.HasBinary || FieldCollector.HasLongValue)
  585. goto case HTTPFormUsage.Multipart;
  586. else
  587. goto case HTTPFormUsage.UrlEncoded;
  588. case HTTPFormUsage.UrlEncoded: FormImpl = new HTTPUrlEncodedForm(); break;
  589. case HTTPFormUsage.Multipart: FormImpl = new HTTPMultiPartForm(); break;
  590. #if !BESTHTTP_DISABLE_UNITY_FORM
  591. case HTTPFormUsage.Unity: FormImpl = new UnityForm(); break;
  592. #endif
  593. }
  594. // Copy the fields, and other properties to the new implementation
  595. FormImpl.CopyFrom(FieldCollector);
  596. return FormImpl;
  597. }
  598. #endregion
  599. #region Header Management
  600. #region General Management
  601. /// <summary>
  602. /// Adds a header and value pair to the Headers. Use it to add custom headers to the request.
  603. /// </summary>
  604. /// <example>AddHeader("User-Agent', "FooBar 1.0")</example>
  605. public void AddHeader(string name, string value)
  606. {
  607. if (Headers == null)
  608. Headers = new Dictionary<string, List<string>>();
  609. List<string> values;
  610. if (!Headers.TryGetValue(name, out values))
  611. Headers.Add(name, values = new List<string>(1));
  612. values.Add(value);
  613. }
  614. /// <summary>
  615. /// Removes any previously added values, and sets the given one.
  616. /// </summary>
  617. public void SetHeader(string name, string value)
  618. {
  619. if (Headers == null)
  620. Headers = new Dictionary<string, List<string>>();
  621. List<string> values;
  622. if (!Headers.TryGetValue(name, out values))
  623. Headers.Add(name, values = new List<string>(1));
  624. values.Clear();
  625. values.Add(value);
  626. }
  627. /// <summary>
  628. /// Removes the specified header. Returns true, if the header found and succesfully removed.
  629. /// </summary>
  630. /// <param name="name"></param>
  631. /// <returns></returns>
  632. public bool RemoveHeader(string name)
  633. {
  634. if (Headers == null)
  635. return false;
  636. return Headers.Remove(name);
  637. }
  638. /// <summary>
  639. /// Returns true if the given head name is already in the Headers.
  640. /// </summary>
  641. public bool HasHeader(string name)
  642. {
  643. return Headers != null && Headers.ContainsKey(name);
  644. }
  645. /// <summary>
  646. /// Returns the first header or null for the given header name.
  647. /// </summary>
  648. public string GetFirstHeaderValue(string name)
  649. {
  650. if (Headers == null)
  651. return null;
  652. List<string> headers = null;
  653. if (Headers.TryGetValue(name, out headers) && headers.Count > 0)
  654. return headers[0];
  655. return null;
  656. }
  657. /// <summary>
  658. /// Returns all header values for the given header or null.
  659. /// </summary>
  660. public List<string> GetHeaderValues(string name)
  661. {
  662. if (Headers == null)
  663. return null;
  664. List<string> headers = null;
  665. if (Headers.TryGetValue(name, out headers) && headers.Count > 0)
  666. return headers;
  667. return null;
  668. }
  669. public void RemoveHeaders()
  670. {
  671. if (Headers == null)
  672. return;
  673. Headers.Clear();
  674. }
  675. #endregion
  676. #region Range Headers
  677. /// <summary>
  678. /// Sets the Range header to download the content from the given byte position. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35
  679. /// </summary>
  680. /// <param name="firstBytePos">Start position of the download.</param>
  681. public void SetRangeHeader(int firstBytePos)
  682. {
  683. SetHeader("Range", string.Format("bytes={0}-", firstBytePos));
  684. }
  685. /// <summary>
  686. /// Sets the Range header to download the content from the given byte position to the given last position. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35
  687. /// </summary>
  688. /// <param name="firstBytePos">Start position of the download.</param>
  689. /// <param name="lastBytePos">The end position of the download.</param>
  690. public void SetRangeHeader(int firstBytePos, int lastBytePos)
  691. {
  692. SetHeader("Range", string.Format("bytes={0}-{1}", firstBytePos, lastBytePos));
  693. }
  694. #endregion
  695. public void EnumerateHeaders(OnHeaderEnumerationDelegate callback)
  696. {
  697. EnumerateHeaders(callback, false);
  698. }
  699. public void EnumerateHeaders(OnHeaderEnumerationDelegate callback, bool callBeforeSendCallback)
  700. {
  701. #if !UNITY_WEBGL || UNITY_EDITOR
  702. if (!HasHeader("Host"))
  703. SetHeader("Host", CurrentUri.Authority);
  704. if (IsRedirected && !HasHeader("Referer"))
  705. AddHeader("Referer", Uri.ToString());
  706. if (!HasHeader("Accept-Encoding"))
  707. AddHeader("Accept-Encoding", "gzip, identity");
  708. #if !BESTHTTP_DISABLE_PROXY
  709. if (HasProxy && !HasHeader("Proxy-Connection"))
  710. AddHeader("Proxy-Connection", IsKeepAlive ? "Keep-Alive" : "Close");
  711. #endif
  712. if (!HasHeader("Connection"))
  713. AddHeader("Connection", IsKeepAlive ? "Keep-Alive, TE" : "Close, TE");
  714. if (!HasHeader("TE"))
  715. AddHeader("TE", "identity");
  716. if (!HasHeader("User-Agent"))
  717. AddHeader("User-Agent", "BestHTTP");
  718. #endif
  719. long contentLength = -1;
  720. if (UploadStream == null)
  721. {
  722. byte[] entityBody = GetEntityBody();
  723. contentLength = entityBody != null ? entityBody.Length : 0;
  724. if (RawData == null && (FormImpl != null || (FieldCollector != null && !FieldCollector.IsEmpty)))
  725. {
  726. SelectFormImplementation();
  727. if (FormImpl != null)
  728. FormImpl.PrepareRequest(this);
  729. }
  730. }
  731. else
  732. {
  733. contentLength = UploadStreamLength;
  734. if (contentLength == -1)
  735. SetHeader("Transfer-Encoding", "Chunked");
  736. if (!HasHeader("Content-Type"))
  737. SetHeader("Content-Type", "application/octet-stream");
  738. }
  739. // Always set the Content-Length header if possible
  740. // http://tools.ietf.org/html/rfc2616#section-4.4 : For compatibility with HTTP/1.0 applications, HTTP/1.1 requests containing a message-body MUST include a valid Content-Length header field unless the server is known to be HTTP/1.1 compliant.
  741. if (contentLength != -1 && !HasHeader("Content-Length"))
  742. SetHeader("Content-Length", contentLength.ToString());
  743. #if !UNITY_WEBGL || UNITY_EDITOR
  744. #if !BESTHTTP_DISABLE_PROXY
  745. // Proxy Authentication
  746. if (HasProxy && Proxy.Credentials != null)
  747. {
  748. switch (Proxy.Credentials.Type)
  749. {
  750. case AuthenticationTypes.Basic:
  751. // With Basic authentication we don't want to wait for a challenge, we will send the hash with the first request
  752. SetHeader("Proxy-Authorization", string.Concat("Basic ", Convert.ToBase64String(Encoding.UTF8.GetBytes(Proxy.Credentials.UserName + ":" + Proxy.Credentials.Password))));
  753. break;
  754. case AuthenticationTypes.Unknown:
  755. case AuthenticationTypes.Digest:
  756. var digest = DigestStore.Get(Proxy.Address);
  757. if (digest != null)
  758. {
  759. string authentication = digest.GenerateResponseHeader(this, Proxy.Credentials);
  760. if (!string.IsNullOrEmpty(authentication))
  761. SetHeader("Proxy-Authorization", authentication);
  762. }
  763. break;
  764. }
  765. }
  766. #endif
  767. #endif
  768. // Server authentication
  769. if (Credentials != null)
  770. {
  771. switch (Credentials.Type)
  772. {
  773. case AuthenticationTypes.Basic:
  774. // With Basic authentication we don't want to wait for a challenge, we will send the hash with the first request
  775. SetHeader("Authorization", string.Concat("Basic ", Convert.ToBase64String(Encoding.UTF8.GetBytes(Credentials.UserName + ":" + Credentials.Password))));
  776. break;
  777. case AuthenticationTypes.Unknown:
  778. case AuthenticationTypes.Digest:
  779. var digest = DigestStore.Get(this.CurrentUri);
  780. if (digest != null)
  781. {
  782. string authentication = digest.GenerateResponseHeader(this, Credentials);
  783. if (!string.IsNullOrEmpty(authentication))
  784. SetHeader("Authorization", authentication);
  785. }
  786. break;
  787. }
  788. }
  789. // Cookies.
  790. #if !BESTHTTP_DISABLE_COOKIES && (!UNITY_WEBGL || UNITY_EDITOR)
  791. // User added cookies are sent even when IsCookiesEnabled is set to false
  792. List<Cookie> cookies = IsCookiesEnabled ? CookieJar.Get(CurrentUri) : null;
  793. // Merge server sent cookies with user-set cookies
  794. if (cookies == null || cookies.Count == 0)
  795. cookies = this.customCookies;
  796. else if (this.customCookies != null)
  797. {
  798. // Merge
  799. int idx = 0;
  800. while (idx < this.customCookies.Count)
  801. {
  802. Cookie customCookie = customCookies[idx];
  803. int foundIdx = cookies.FindIndex(c => c.Name.Equals(customCookie.Name));
  804. if (foundIdx >= 0)
  805. cookies[foundIdx] = customCookie;
  806. else
  807. cookies.Add(customCookie);
  808. idx++;
  809. }
  810. }
  811. // http://tools.ietf.org/html/rfc6265#section-5.4
  812. // -When the user agent generates an HTTP request, the user agent MUST NOT attach more than one Cookie header field.
  813. if (cookies != null && cookies.Count > 0)
  814. {
  815. // TODO:
  816. // 2. The user agent SHOULD sort the cookie-list in the following order:
  817. // * Cookies with longer paths are listed before cookies with shorter paths.
  818. // * Among cookies that have equal-length path fields, cookies with earlier creation-times are listed before cookies with later creation-times.
  819. bool first = true;
  820. string cookieStr = string.Empty;
  821. bool isSecureProtocolInUse = HTTPProtocolFactory.IsSecureProtocol(CurrentUri);
  822. foreach (var cookie in cookies)
  823. if (!cookie.IsSecure || (cookie.IsSecure && isSecureProtocolInUse))
  824. {
  825. if (!first)
  826. cookieStr += "; ";
  827. else
  828. first = false;
  829. cookieStr += cookie.ToString();
  830. // 3. Update the last-access-time of each cookie in the cookie-list to the current date and time.
  831. cookie.LastAccess = DateTime.UtcNow;
  832. }
  833. if (!string.IsNullOrEmpty(cookieStr))
  834. SetHeader("Cookie", cookieStr);
  835. }
  836. #endif
  837. if (callBeforeSendCallback && _onBeforeHeaderSend != null)
  838. {
  839. try
  840. {
  841. _onBeforeHeaderSend(this);
  842. }
  843. catch(Exception ex)
  844. {
  845. HTTPManager.Logger.Exception("HTTPRequest", "OnBeforeHeaderSend", ex);
  846. }
  847. }
  848. // Write out the headers to the stream
  849. if (callback != null)
  850. foreach (var kvp in Headers)
  851. callback(kvp.Key, kvp.Value);
  852. }
  853. /// <summary>
  854. /// Writes out the Headers to the stream.
  855. /// </summary>
  856. private void SendHeaders(Stream stream)
  857. {
  858. EnumerateHeaders((header, values) =>
  859. {
  860. if (string.IsNullOrEmpty(header) || values == null)
  861. return;
  862. byte[] headerName = string.Concat(header, ": ").GetASCIIBytes();
  863. for (int i = 0; i < values.Count; ++i)
  864. {
  865. if (string.IsNullOrEmpty(values[i]))
  866. {
  867. HTTPManager.Logger.Warning("HTTPRequest", string.Format("Null/empty value for header: {0}", header));
  868. continue;
  869. }
  870. stream.WriteArray(headerName);
  871. stream.WriteArray(values[i].GetASCIIBytes());
  872. stream.WriteArray(EOL);
  873. }
  874. }, /*callBeforeSendCallback:*/ true);
  875. }
  876. /// <summary>
  877. /// Returns a string representation of the headers.
  878. /// </summary>
  879. public string DumpHeaders()
  880. {
  881. using (var ms = new MemoryStream())
  882. {
  883. SendHeaders(ms);
  884. return ms.ToArray().AsciiToString();
  885. }
  886. }
  887. #endregion
  888. #region Internal Helper Functions
  889. internal byte[] GetEntityBody()
  890. {
  891. if (RawData != null)
  892. return RawData;
  893. if (FormImpl != null || (FieldCollector != null && !FieldCollector.IsEmpty))
  894. {
  895. SelectFormImplementation();
  896. if (FormImpl != null)
  897. return FormImpl.GetData();
  898. }
  899. return null;
  900. }
  901. internal void SendOutTo(Stream stream)
  902. {
  903. try
  904. {
  905. #if !UNITY_WEBGL || UNITY_EDITOR
  906. string requestPathAndQuery =
  907. #if !BESTHTTP_DISABLE_PROXY
  908. HasProxy && Proxy.SendWholeUri ? CurrentUri.OriginalString :
  909. #endif
  910. CurrentUri.GetRequestPathAndQueryURL();
  911. string requestLine = string.Format("{0} {1} HTTP/1.1", MethodNames[(byte)MethodType], requestPathAndQuery);
  912. if (HTTPManager.Logger.Level <= Logger.Loglevels.Information)
  913. HTTPManager.Logger.Information("HTTPRequest", string.Format("Sending request: '{0}'", requestLine));
  914. stream.WriteArray(requestLine.GetASCIIBytes());
  915. stream.WriteArray(EOL);
  916. SendHeaders(stream);
  917. stream.WriteArray(EOL);
  918. // Send headers to the wire
  919. if (UploadStream != null)
  920. stream.Flush();
  921. #endif
  922. byte[] data = RawData;
  923. // We are sending forms? Then convert the form to a byte array
  924. if (data == null && FormImpl != null)
  925. data = FormImpl.GetData();
  926. if (data != null || UploadStream != null)
  927. {
  928. // Make a new reference, as we will check the UploadStream property in the HTTPManager
  929. Stream uploadStream = UploadStream;
  930. if (uploadStream == null)
  931. {
  932. // Make stream from the data
  933. uploadStream = new MemoryStream(data, 0, data.Length);
  934. // Initialize progress report variable
  935. UploadLength = data.Length;
  936. }
  937. else
  938. UploadLength = UseUploadStreamLength ? UploadStreamLength : -1;
  939. // Initialize the progress report variables
  940. Uploaded = 0;
  941. // Upload buffer. First we will read the data into this buffer from the UploadStream, then write this buffer to our outStream
  942. byte[] buffer = new byte[UploadChunkSize];
  943. // How many bytes was read from the UploadStream
  944. int count = 0;
  945. while ((count = uploadStream.Read(buffer, 0, buffer.Length)) > 0)
  946. {
  947. // If we don't know the size, send as chunked
  948. if (!UseUploadStreamLength)
  949. {
  950. stream.WriteArray(count.ToString("X").GetASCIIBytes());
  951. stream.WriteArray(EOL);
  952. }
  953. // write out the buffer to the wire
  954. stream.Write(buffer, 0, count);
  955. // chunk trailing EOL
  956. if (!UseUploadStreamLength)
  957. stream.WriteArray(EOL);
  958. // Make sure that the system sends the buffer
  959. stream.Flush();
  960. // update how many bytes are uploaded
  961. Uploaded += count;
  962. // let the callback fire
  963. UploadProgressChanged = true;
  964. }
  965. // All data from the stream are sent, write the 'end' chunk if necessary
  966. if (!UseUploadStreamLength)
  967. {
  968. stream.WriteArray("0".GetASCIIBytes());
  969. stream.WriteArray(EOL);
  970. stream.WriteArray(EOL);
  971. }
  972. // Make sure all remaining data will be on the wire
  973. stream.Flush();
  974. // Dispose the MemoryStream
  975. if (UploadStream == null && uploadStream != null)
  976. uploadStream.Dispose();
  977. }
  978. else
  979. stream.Flush();
  980. #if !UNITY_WEBGL || UNITY_EDITOR
  981. HTTPManager.Logger.Information("HTTPRequest", "'" + requestLine + "' sent out");
  982. #endif
  983. }
  984. finally
  985. {
  986. if (UploadStream != null && DisposeUploadStream)
  987. UploadStream.Dispose();
  988. }
  989. }
  990. internal void UpgradeCallback()
  991. {
  992. if (Response == null || !Response.IsUpgraded)
  993. return;
  994. try
  995. {
  996. if (OnUpgraded != null)
  997. OnUpgraded(this, Response);
  998. }
  999. catch (Exception ex)
  1000. {
  1001. HTTPManager.Logger.Exception("HTTPRequest", "UpgradeCallback", ex);
  1002. }
  1003. }
  1004. internal void CallCallback()
  1005. {
  1006. try
  1007. {
  1008. if (this.Callback != null)
  1009. this.Callback(this, Response);
  1010. }
  1011. catch (Exception ex)
  1012. {
  1013. HTTPManager.Logger.Exception("HTTPRequest", "CallCallback", ex);
  1014. }
  1015. }
  1016. internal bool CallOnBeforeRedirection(Uri redirectUri)
  1017. {
  1018. if (onBeforeRedirection != null)
  1019. return onBeforeRedirection(this, this.Response, redirectUri);
  1020. return true;
  1021. }
  1022. internal void FinishStreaming()
  1023. {
  1024. if (Response != null && UseStreaming)
  1025. Response.FinishStreaming();
  1026. }
  1027. /// <summary>
  1028. /// Called on Unity's main thread just before processing it.
  1029. /// </summary>
  1030. internal void Prepare()
  1031. {
  1032. #if !BESTHTTP_DISABLE_UNITY_FORM
  1033. if (FormUsage == HTTPFormUsage.Unity)
  1034. SelectFormImplementation();
  1035. #endif
  1036. }
  1037. #if !NETFX_CORE && !UNITY_WP8
  1038. internal bool CallCustomCertificationValidator(System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Security.Cryptography.X509Certificates.X509Chain chain)
  1039. {
  1040. if (CustomCertificationValidator != null)
  1041. return CustomCertificationValidator(this, cert, chain);
  1042. return true;
  1043. }
  1044. #endif
  1045. #endregion
  1046. /// <summary>
  1047. /// Starts processing the request.
  1048. /// </summary>
  1049. public HTTPRequest Send()
  1050. {
  1051. return HTTPManager.SendRequest(this);
  1052. }
  1053. /// <summary>
  1054. /// Aborts an already established connection, so no further download or upload are done.
  1055. /// </summary>
  1056. public void Abort()
  1057. {
  1058. if (System.Threading.Monitor.TryEnter(HTTPManager.Locker, TimeSpan.FromMilliseconds(100)))
  1059. {
  1060. try
  1061. {
  1062. if (this.State >= HTTPRequestStates.Finished)
  1063. {
  1064. HTTPManager.Logger.Warning("HTTPRequest", string.Format("Abort - Already in a state({0}) where no Abort required!", this.State.ToString()));
  1065. return;
  1066. }
  1067. // Get the parent connection
  1068. var connection = HTTPManager.GetConnectionWith(this);
  1069. // No Connection found for this request, maybe not even started
  1070. if (connection == null)
  1071. {
  1072. // so try to remove from the request queue
  1073. if (!HTTPManager.RemoveFromQueue(this))
  1074. HTTPManager.Logger.Warning("HTTPRequest", "Abort - No active connection found with this request! (The request may already finished?)");
  1075. this.State = HTTPRequestStates.Aborted;
  1076. this.CallCallback();
  1077. }
  1078. else
  1079. {
  1080. // destroy the incomplete response
  1081. if (Response != null && Response.IsStreamed)
  1082. Response.Dispose();
  1083. // send an abort request to the connection
  1084. connection.Abort(HTTPConnectionStates.AbortRequested);
  1085. }
  1086. }
  1087. finally
  1088. {
  1089. System.Threading.Monitor.Exit(HTTPManager.Locker);
  1090. }
  1091. }
  1092. else
  1093. throw new Exception("Wasn't able to acquire a thread lock. Abort failed!");
  1094. }
  1095. /// <summary>
  1096. /// Resets the request for a state where switching MethodType is possible.
  1097. /// </summary>
  1098. public void Clear()
  1099. {
  1100. ClearForm();
  1101. RemoveHeaders();
  1102. }
  1103. #region System.Collections.IEnumerator implementation
  1104. public object Current { get { return null; } }
  1105. public bool MoveNext()
  1106. {
  1107. return this.State < HTTPRequestStates.Finished;
  1108. }
  1109. public void Reset()
  1110. {
  1111. throw new NotImplementedException();
  1112. }
  1113. #endregion
  1114. HTTPRequest IEnumerator<HTTPRequest>.Current
  1115. {
  1116. get { return this; }
  1117. }
  1118. public void Dispose()
  1119. {
  1120. }
  1121. }
  1122. }