HTTPRequest.cs 54 KB

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