HTTPRequest.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Best.HTTP.Cookies;
  5. using Best.HTTP.Hosts.Connections;
  6. using Best.HTTP.HostSetting;
  7. using Best.HTTP.Request.Authenticators;
  8. using Best.HTTP.Request.Settings;
  9. using Best.HTTP.Request.Timings;
  10. using Best.HTTP.Request.Upload;
  11. using Best.HTTP.Response.Decompression;
  12. using Best.HTTP.Shared;
  13. using Best.HTTP.Shared.Extensions;
  14. using Best.HTTP.Shared.Logger;
  15. namespace Best.HTTP
  16. {
  17. /// <summary>
  18. /// Delegate for a callback function that is called after the request is fully processed.
  19. /// </summary>
  20. public delegate void OnRequestFinishedDelegate(HTTPRequest req, HTTPResponse resp);
  21. /// <summary>
  22. /// Delegate for enumerating headers during request preparation.
  23. /// </summary>
  24. /// <param name="header">The header name.</param>
  25. /// <param name="values">A list of header values.</param>
  26. public delegate void OnHeaderEnumerationDelegate(string header, List<string> values);
  27. /// <summary>
  28. /// Represents an HTTP request that allows you to send HTTP requests to remote servers and receive responses asynchronously.
  29. /// </summary>
  30. /// <remarks>
  31. /// <list type="bullet">
  32. /// <item><term>Asynchronous HTTP requests</term><description>Utilize a Task-based API for performing HTTP requests asynchronously.</description></item>
  33. /// <item><term>Unity coroutine support</term><description>Seamlessly integrate with Unity's coroutine system for coroutine-based request handling.</description></item>
  34. /// <item><term>HTTP method support</term><description>Support for various HTTP methods including GET, POST, PUT, DELETE, and more.</description></item>
  35. /// <item><term>Compression and decompression</term><description>Automatic request and response compression and decompression for efficient data transfer.</description></item>
  36. /// <item><term>Timing information</term><description>Collect detailed timing information about the request for performance analysis.</description></item>
  37. /// <item><term>Upload and download support</term><description>Support for uploading and downloading files with progress tracking.</description></item>
  38. /// <item><term>Customizable</term><description>Extensive options for customizing request headers, handling cookies, and more.</description></item>
  39. /// <item><term>Redirection handling</term><description>Automatic handling of request redirections for a seamless experience.</description></item>
  40. /// <item><term>Proxy server support</term><description>Ability to route requests through proxy servers for enhanced privacy and security.</description></item>
  41. /// <item><term>Authentication</term><description>Automatic authentication handling using authenticators for secure communication.</description></item>
  42. /// <item><term>Cancellation support</term><description>Ability to cancel requests to prevent further processing and release resources.</description></item>
  43. /// </list>
  44. /// </remarks>
  45. public sealed class HTTPRequest : IEnumerator
  46. {
  47. /// <summary>
  48. /// Creates an <see cref="HTTPMethods.Get">HTTP GET</see> request with the specified URL.
  49. /// </summary>
  50. /// <param name="url">The URL of the request.</param>
  51. /// <returns>An HTTPRequest instance for the GET request.</returns>
  52. public static HTTPRequest CreateGet(string url) => new HTTPRequest(url);
  53. /// <summary>
  54. /// Creates an <see cref="HTTPMethods.Get">HTTP GET</see> request with the specified URI.
  55. /// </summary>
  56. /// <param name="uri">The URI of the request.</param>
  57. /// <returns>An HTTPRequest instance for the GET request.</returns>
  58. public static HTTPRequest CreateGet(Uri uri) => new HTTPRequest(uri);
  59. /// <summary>
  60. /// Creates an <see cref="HTTPMethods.Get">HTTP GET</see> request with the specified URL and registers a callback function to be called
  61. /// when the request is fully processed.
  62. /// </summary>
  63. /// <param name="url">The URL of the request.</param>
  64. /// <param name="callback">A callback function to be called when the request is finished.</param>
  65. /// <returns>An HTTPRequest instance for the GET request.</returns>
  66. public static HTTPRequest CreateGet(string url, OnRequestFinishedDelegate callback) => new HTTPRequest(url, callback);
  67. /// <summary>
  68. /// Creates an <see cref="HTTPMethods.Get">HTTP GET</see> request with the specified URI and registers a callback function to be called
  69. /// when the request is fully processed.
  70. /// </summary>
  71. /// <param name="uri">The URI of the request.</param>
  72. /// <param name="callback">A callback function to be called when the request is finished.</param>
  73. /// <returns>An HTTPRequest instance for the GET request.</returns>
  74. public static HTTPRequest CreateGet(Uri uri, OnRequestFinishedDelegate callback) => new HTTPRequest(uri, callback);
  75. /// <summary>
  76. /// Creates an <see cref="HTTPMethods.Post">HTTP POST</see> request with the specified URL.
  77. /// </summary>
  78. /// <param name="url">The URL of the request.</param>
  79. /// <returns>An HTTPRequest instance for the POST request.</returns>
  80. public static HTTPRequest CreatePost(string url) => new HTTPRequest(url, HTTPMethods.Post);
  81. /// <summary>
  82. /// Creates an <see cref="HTTPMethods.Post">HTTP POST</see> request with the specified URI.
  83. /// </summary>
  84. /// <param name="uri">The URI of the request.</param>
  85. /// <returns>An HTTPRequest instance for the POST request.</returns>
  86. public static HTTPRequest CreatePost(Uri uri) => new HTTPRequest(uri, HTTPMethods.Post);
  87. /// <summary>
  88. /// Creates an <see cref="HTTPMethods.Post">HTTP POST</see> request with the specified URL and registers a callback function to be called
  89. /// when the request is fully processed.
  90. /// </summary>
  91. /// <param name="url">The URL of the request.</param>
  92. /// <param name="callback">A callback function to be called when the request is finished.</param>
  93. /// <returns>An HTTPRequest instance for the POST request.</returns>
  94. public static HTTPRequest CreatePost(string url, OnRequestFinishedDelegate callback) => new HTTPRequest(url, HTTPMethods.Post, callback);
  95. /// <summary>
  96. /// Creates an <see cref="HTTPMethods.Post">HTTP POST</see> request with the specified URI and registers a callback function to be called
  97. /// when the request is fully processed.
  98. /// </summary>
  99. /// <param name="uri">The URI of the request.</param>
  100. /// <param name="callback">A callback function to be called when the request is finished.</param>
  101. /// <returns>An HTTPRequest instance for the POST request.</returns>
  102. public static HTTPRequest CreatePost(Uri uri, OnRequestFinishedDelegate callback) => new HTTPRequest(uri, HTTPMethods.Post, callback);
  103. /// <summary>
  104. /// Creates an <see cref="HTTPMethods.Put">HTTP PUT</see> request with the specified URL.
  105. /// </summary>
  106. /// <param name="url">The URL of the request.</param>
  107. /// <returns>An HTTPRequest instance for the PUT request.</returns>
  108. public static HTTPRequest CreatePut(string url) => new HTTPRequest(url, HTTPMethods.Put);
  109. /// <summary>
  110. /// Creates an <see cref="HTTPMethods.Put">HTTP PUT</see> request with the specified URI.
  111. /// </summary>
  112. /// <param name="uri">The URI of the request.</param>
  113. /// <returns>An HTTPRequest instance for the PUT request.</returns>
  114. public static HTTPRequest CreatePut(Uri uri) => new HTTPRequest(uri, HTTPMethods.Put);
  115. /// <summary>
  116. /// Creates an <see cref="HTTPMethods.Put">HTTP PUT</see> request with the specified URL and registers a callback function to be called
  117. /// when the request is fully processed.
  118. /// </summary>
  119. /// <param name="url">The URL of the request.</param>
  120. /// <param name="callback">A callback function to be called when the request is finished.</param>
  121. /// <returns>An HTTPRequest instance for the PUT request.</returns>
  122. public static HTTPRequest CreatePut(string url, OnRequestFinishedDelegate callback) => new HTTPRequest(url, HTTPMethods.Put, callback);
  123. /// <summary>
  124. /// Creates an <see cref="HTTPMethods.Put">HTTP PUT</see> request with the specified URI and registers a callback function to be called
  125. /// when the request is fully processed.
  126. /// </summary>
  127. /// <param name="uri">The URI of the request.</param>
  128. /// <param name="callback">A callback function to be called when the request is finished.</param>
  129. /// <returns>An HTTPRequest instance for the PUT request.</returns>
  130. public static HTTPRequest CreatePut(Uri uri, OnRequestFinishedDelegate callback) => new HTTPRequest(uri, HTTPMethods.Put, callback);
  131. /// <summary>
  132. /// Cached uppercase values to save some cpu cycles and GC alloc per request.
  133. /// </summary>
  134. public static readonly string[] MethodNames = {
  135. HTTPMethods.Get.ToString().ToUpper(),
  136. HTTPMethods.Head.ToString().ToUpper(),
  137. HTTPMethods.Post.ToString().ToUpper(),
  138. HTTPMethods.Put.ToString().ToUpper(),
  139. HTTPMethods.Delete.ToString().ToUpper(),
  140. HTTPMethods.Patch.ToString().ToUpper(),
  141. HTTPMethods.Merge.ToString().ToUpper(),
  142. HTTPMethods.Options.ToString().ToUpper(),
  143. HTTPMethods.Connect.ToString().ToUpper(),
  144. HTTPMethods.Query.ToString().ToUpper()
  145. };
  146. /// <summary>
  147. /// The method that how we want to process our request the server.
  148. /// </summary>
  149. public HTTPMethods MethodType { get; set; }
  150. /// <summary>
  151. /// The original request's Uri.
  152. /// </summary>
  153. public Uri Uri { get; set; }
  154. /// <summary>
  155. /// If redirected it contains the RedirectUri.
  156. /// </summary>
  157. public Uri CurrentUri { get { return this.RedirectSettings.IsRedirected ? this.RedirectSettings.RedirectUri : Uri; } }
  158. /// <summary>
  159. /// A host-key that can be used to find the right host-variant for the request.
  160. /// </summary>
  161. public HostKey CurrentHostKey { get => HostKey.From(this); }
  162. /// <summary>
  163. /// The response received from the server.
  164. /// </summary>
  165. /// <remarks>If an exception occurred during reading of the response stream or can't connect to the server, this will be null!</remarks>
  166. public HTTPResponse Response { get; set; }
  167. /// <summary>
  168. /// Download related options and settings.
  169. /// </summary>
  170. public DownloadSettings DownloadSettings = new DownloadSettings();
  171. /// <summary>
  172. /// Upload related options and settings.
  173. /// </summary>
  174. public UploadSettings UploadSettings = new UploadSettings();
  175. /// <summary>
  176. /// Timeout settings for the request.
  177. /// </summary>
  178. public TimeoutSettings TimeoutSettings;
  179. /// <summary>
  180. /// Retry settings for the request.
  181. /// </summary>
  182. public RetrySettings RetrySettings;
  183. /// <summary>
  184. /// Proxy settings for the request.
  185. /// </summary>
  186. public ProxySettings ProxySettings;
  187. /// <summary>
  188. /// Redirect settings for the request.
  189. /// </summary>
  190. public RedirectSettings RedirectSettings { get; private set; } = new RedirectSettings(10);
  191. /// <summary>
  192. /// The callback function that will be called after the request is fully processed.
  193. /// </summary>
  194. public OnRequestFinishedDelegate Callback { get; set; }
  195. /// <summary>
  196. /// Indicates if <see cref="Abort"/> is called on this request.
  197. /// </summary>
  198. public bool IsCancellationRequested { get => this.CancellationTokenSource != null ? this.CancellationTokenSource.IsCancellationRequested : true; }
  199. /// <summary>
  200. /// Gets the cancellation token source for this request.
  201. /// </summary>
  202. internal System.Threading.CancellationTokenSource CancellationTokenSource { get; private set; }
  203. /// <summary>
  204. /// Action called when <see cref="Abort"/> function is invoked.
  205. /// </summary>
  206. public Action<HTTPRequest> OnCancellationRequested;
  207. /// <summary>
  208. /// Stores any exception that occurs during processing of the request or response.
  209. /// </summary>
  210. /// <remarks>This property if for debugging purposes as <see href="https://github.com/Benedicht/BestHTTP-Issues/issues/174">seen here</see>!</remarks>
  211. public Exception Exception { get; internal set; }
  212. /// <summary>
  213. /// Any user-object that can be passed with the request.
  214. /// </summary>
  215. public object Tag { get; set; }
  216. /// <summary>
  217. /// Current state of this request.
  218. /// </summary>
  219. public HTTPRequestStates State {
  220. get { return this._state; }
  221. internal set {
  222. if (!HTTPUpdateDelegator.Instance.IsMainThread() && HTTPUpdateDelegator.Instance.CurrentThreadingMode == ThreadingMode.UnityUpdate)
  223. HTTPManager.Logger.Error(nameof(HTTPRequest), $"State.Set({this._state} => {value}) isn't called on the main thread({HTTPUpdateDelegator.Instance.MainThreadId})!", this.Context);
  224. // In a case where the request is aborted its state is set to a >= Finished state then,
  225. // on another thread the reqest processing will fail too queuing up a >= Finished state again.
  226. if (this._state >= HTTPRequestStates.Finished && value >= HTTPRequestStates.Finished)
  227. {
  228. HTTPManager.Logger.Warning(nameof(HTTPRequest), $"State.Set({this._state} => {value})", this.Context);
  229. return;
  230. }
  231. if (HTTPManager.Logger.IsDiagnostic)
  232. HTTPManager.Logger.Verbose(nameof(HTTPRequest), $"State.Set({this._state} => {value})", this.Context);
  233. this._state = value;
  234. }
  235. }
  236. private volatile HTTPRequestStates _state;
  237. /// <summary>
  238. /// Timing information about the request.
  239. /// </summary>
  240. public TimingCollector Timing { get; private set; }
  241. /// <summary>
  242. /// An IAuthenticator implementation that can be used to authenticate the request.
  243. /// </summary>
  244. /// <remarks>Out-of-the-box included authenticators are <see cref="CredentialAuthenticator"/> and <see cref="BearerTokenAuthenticator"/>.</remarks>
  245. public IAuthenticator Authenticator;
  246. #if UNITY_WEBGL
  247. /// <summary>
  248. /// Its value will be set to the XmlHTTPRequest's withCredentials field, required to send 3rd party cookies with the request.
  249. /// </summary>
  250. /// <remarks>
  251. /// More details can be found here:
  252. /// <list type="bullet">
  253. /// <item><description><see href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials">Mozilla Developer Networks - XMLHttpRequest.withCredentials</see></description></item>
  254. /// </list>
  255. /// </remarks>
  256. public bool WithCredentials { get; set; }
  257. #endif
  258. /// <summary>
  259. /// Logging context of the request.
  260. /// </summary>
  261. public LoggingContext Context { get; private set; }
  262. private Dictionary<string, List<string>> Headers { get; set; }
  263. /// <summary>
  264. /// Creates an HTTP GET request with the specified URL.
  265. /// </summary>
  266. /// <param name="url">The URL of the request.</param>
  267. public HTTPRequest(string url)
  268. :this(new Uri(url)) {}
  269. /// <summary>
  270. /// Creates an HTTP GET request with the specified URL and registers a callback function to be called
  271. /// when the request is fully processed.
  272. /// </summary>
  273. /// <param name="url">The URL of the request.</param>
  274. /// <param name="callback">A callback function to be called when the request is finished.</param>
  275. public HTTPRequest(string url, OnRequestFinishedDelegate callback)
  276. : this(new Uri(url), callback) { }
  277. /// <summary>
  278. /// Creates an HTTP GET request with the specified URL and HTTP method type.
  279. /// </summary>
  280. /// <param name="url">The URL of the request.</param>
  281. /// <param name="methodType">The HTTP method type for the request (e.g., GET, POST, PUT).</param>
  282. public HTTPRequest(string url, HTTPMethods methodType)
  283. : this(new Uri(url), methodType) { }
  284. /// <summary>
  285. /// Creates an HTTP request with the specified URL, HTTP method type, and registers a callback function to be called
  286. /// when the request is fully processed.
  287. /// </summary>
  288. /// <param name="url">The URL of the request.</param>
  289. /// <param name="methodType">The HTTP method type for the request (e.g., GET, POST, PUT).</param>
  290. /// <param name="callback">A callback function to be called when the request is finished.</param>
  291. public HTTPRequest(string url, HTTPMethods methodType, OnRequestFinishedDelegate callback)
  292. : this(new Uri(url), methodType, callback) { }
  293. /// <summary>
  294. /// Creates an HTTP GET request with the specified URI.
  295. /// </summary>
  296. /// <param name="uri">The URI of the request.</param>
  297. public HTTPRequest(Uri uri)
  298. : this(uri, HTTPMethods.Get, null)
  299. {
  300. }
  301. /// <summary>
  302. /// Creates an HTTP GET request with the specified URI and registers a callback function to be called
  303. /// when the request is fully processed.
  304. /// </summary>
  305. /// <param name="uri">The URI of the request.</param>
  306. /// <param name="callback">A callback function to be called when the request is finished.</param>
  307. public HTTPRequest(Uri uri, OnRequestFinishedDelegate callback)
  308. : this(uri, HTTPMethods.Get, callback)
  309. {
  310. }
  311. /// <summary>
  312. /// Creates an HTTP request with the specified URI and HTTP method type.
  313. /// </summary>
  314. /// <param name="uri">The URI of the request.</param>
  315. /// <param name="methodType">The HTTP method type for the request (e.g., GET, POST, PUT).</param>
  316. public HTTPRequest(Uri uri, HTTPMethods methodType)
  317. : this(uri, methodType, null)
  318. {
  319. }
  320. /// <summary>
  321. /// Creates an HTTP request with the specified URI, HTTP method type, and registers a callback function
  322. /// to be called when the request is fully processed.
  323. /// </summary>
  324. /// <param name="uri">The URI of the request.</param>
  325. /// <param name="methodType">The HTTP method type for the request (e.g., GET, POST, PUT).</param>
  326. /// <param name="callback">A callback function to be called when the request is finished.</param>
  327. public HTTPRequest(Uri uri, HTTPMethods methodType, OnRequestFinishedDelegate callback)
  328. {
  329. this.Uri = uri;
  330. this.MethodType = methodType;
  331. this.TimeoutSettings = new TimeoutSettings(this);
  332. this.ProxySettings = new ProxySettings() { Proxy = HTTPManager.Proxy };
  333. this.RetrySettings = new RetrySettings(methodType == HTTPMethods.Get ? 1 : 0);
  334. this.Callback = callback;
  335. #if UNITY_WEBGL && !UNITY_EDITOR
  336. // Just because cookies are enabled, it doesn't justify creating XHR with WithCredentials == 1.
  337. //this.WithCredentials = this.CookieSettings.IsCookiesEnabled;
  338. #endif
  339. this.Context = new LoggingContext(this);
  340. this.Timing = new TimingCollector(this);
  341. this.CancellationTokenSource = new System.Threading.CancellationTokenSource();
  342. }
  343. /// <summary>
  344. /// Adds a header-value pair to the Headers. Use it to add custom headers to the request.
  345. /// </summary>
  346. /// <example>AddHeader("User-Agent', "FooBar 1.0")</example>
  347. public void AddHeader(string name, string value) => this.Headers = Headers.AddHeader(name, value);
  348. /// <summary>
  349. /// For the given header name, removes any previously added values and sets the given one.
  350. /// </summary>
  351. public void SetHeader(string name, string value) => this.Headers = this.Headers.SetHeader(name, value);
  352. /// <summary>
  353. /// Removes the specified header and all of its associated values. Returns <c>true</c>, if the header found and succesfully removed.
  354. /// </summary>
  355. public bool RemoveHeader(string name) => Headers.RemoveHeader(name);
  356. /// <summary>
  357. /// Returns <c>true</c> if the given head name is already in the <see cref="Headers"/>.
  358. /// </summary>
  359. public bool HasHeader(string name) => Headers.HasHeader(name);
  360. /// <summary>
  361. /// Returns the first header or <c>null</c> for the given header name.
  362. /// </summary>
  363. public string GetFirstHeaderValue(string name) => Headers.GetFirstHeaderValue(name);
  364. /// <summary>
  365. /// Returns all header values for the given header or <c>null</c>.
  366. /// </summary>
  367. public List<string> GetHeaderValues(string name) => Headers.GetHeaderValues(name);
  368. /// <summary>
  369. /// Removes all headers.
  370. /// </summary>
  371. public void RemoveHeaders() => Headers.RemoveHeaders();
  372. /// <summary>
  373. /// 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
  374. /// </summary>
  375. /// <param name="firstBytePos">Start position of the download.</param>
  376. public void SetRangeHeader(long firstBytePos)
  377. {
  378. SetHeader("Range", string.Format("bytes={0}-", firstBytePos));
  379. }
  380. /// <summary>
  381. /// 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
  382. /// </summary>
  383. /// <param name="firstBytePos">Start position of the download.</param>
  384. /// <param name="lastBytePos">The end position of the download.</param>
  385. public void SetRangeHeader(long firstBytePos, long lastBytePos)
  386. {
  387. SetHeader("Range", string.Format("bytes={0}-{1}", firstBytePos, lastBytePos));
  388. }
  389. internal void RemoveUnsafeHeaders()
  390. {
  391. // https://www.rfc-editor.org/rfc/rfc9110.html#name-redirection-3xx
  392. /* 2. Remove header fields that were automatically generated by the implementation, replacing them with updated values as appropriate to the new request. This includes:
  393. 1. Connection-specific header fields (see Section 7.6.1),
  394. 2. Header fields specific to the client's proxy configuration, including (but not limited to) Proxy-Authorization,
  395. 3. Origin-specific header fields (if any), including (but not limited to) Host,
  396. 4. Validating header fields that were added by the implementation's cache (e.g., If-None-Match, If-Modified-Since), and
  397. 5. Resource-specific header fields, including (but not limited to) Referer, Origin, Authorization, and Cookie.
  398. 3. Consider removing header fields that were not automatically generated by the implementation
  399. (i.e., those present in the request because they were added by the calling context) where there are security implications;
  400. this includes but is not limited to Authorization and Cookie.
  401. * */
  402. // 2.1
  403. RemoveHeader("Connection");
  404. RemoveHeader("Proxy-Connection");
  405. RemoveHeader("Keep-Alive");
  406. RemoveHeader("TE");
  407. RemoveHeader("Transfer-Encoding");
  408. RemoveHeader("Upgrade");
  409. // 2.2
  410. RemoveHeader("Proxy-Authorization");
  411. // 2.3
  412. RemoveHeader("Host");
  413. // 2.4
  414. RemoveHeader("If-None-Match");
  415. RemoveHeader("If-Modified-Since");
  416. // 2.5 & 3
  417. RemoveHeader("Referer");
  418. RemoveHeader("Origin");
  419. RemoveHeader("Authorization");
  420. RemoveHeader("Cookie");
  421. RemoveHeader("Accept-Encoding");
  422. RemoveHeader("Content-Length");
  423. }
  424. internal void Prepare()
  425. {
  426. // Upload settings
  427. this.UploadSettings?.SetupRequest(this, true);
  428. }
  429. public void EnumerateHeaders(OnHeaderEnumerationDelegate callback, bool callBeforeSendCallback)
  430. {
  431. #if !UNITY_WEBGL || UNITY_EDITOR
  432. if (!HasHeader("Host"))
  433. {
  434. if (CurrentUri.Port == 80 || CurrentUri.Port == 443)
  435. SetHeader("Host", CurrentUri.Host);
  436. else
  437. SetHeader("Host", CurrentUri.Authority);
  438. }
  439. DecompressorFactory.SetupHeaders(this);
  440. if (!DownloadSettings.DisableCache)
  441. HTTPManager.LocalCache?.SetupValidationHeaders(this);
  442. var hostSettings = HTTPManager.PerHostSettings.Get(this.CurrentUri.Host);
  443. // Websocket would be very, very sad if its "connection: upgrade" header would be overwritten!
  444. if (!HasHeader("Connection"))
  445. AddHeader("Connection", hostSettings.HTTP1ConnectionSettings.TryToReuseConnections ? "Keep-Alive, TE" : "Close, TE");
  446. if (hostSettings.HTTP1ConnectionSettings.TryToReuseConnections /*&& !HasHeader("Keep-Alive")*/)
  447. {
  448. // Send the server a slightly larger value to make sure it's not going to close sooner than the client
  449. int seconds = (int)Math.Ceiling(hostSettings.HTTP1ConnectionSettings.MaxConnectionIdleTime.TotalSeconds + 1);
  450. AddHeader("Keep-Alive", "timeout=" + seconds);
  451. }
  452. if (!HasHeader("TE"))
  453. AddHeader("TE", "identity");
  454. if (!string.IsNullOrEmpty(HTTPManager.UserAgent) && !HasHeader("User-Agent"))
  455. AddHeader("User-Agent", HTTPManager.UserAgent);
  456. #endif
  457. long contentLength = -1;
  458. if (this.UploadSettings.UploadStream == null)
  459. {
  460. contentLength = 0;
  461. }
  462. else
  463. {
  464. contentLength = this.UploadSettings.UploadStream.Length;
  465. if (contentLength == BodyLengths.UnknownWithChunkedTransferEncoding)
  466. SetHeader("Transfer-Encoding", "chunked");
  467. if (!HasHeader("Content-Type"))
  468. SetHeader("Content-Type", "application/octet-stream");
  469. }
  470. // Always set the Content-Length header if possible
  471. // 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.
  472. // 2018.06.03: Changed the condition so that content-length header will be included for zero length too.
  473. // 2022.05.25: Don't send a Content-Length (: 0) header if there's an Upgrade header. Upgrade is set for websocket, and it might be not true that the client doesn't send any bytes.
  474. if (contentLength >= BodyLengths.NoBody && !HasHeader("Content-Length") && !HasHeader("Upgrade"))
  475. SetHeader("Content-Length", contentLength.ToString());
  476. // Server authentication
  477. this.Authenticator?.SetupRequest(this);
  478. // Cookies
  479. //this.CookieSettings?.SetupRequest(this);
  480. CookieJar.SetupRequest(this);
  481. // Write out the headers to the stream
  482. if (callback != null && this.Headers != null)
  483. foreach (var kvp in this.Headers)
  484. callback(kvp.Key, kvp.Value);
  485. }
  486. /// <summary>
  487. /// Starts processing the request.
  488. /// </summary>
  489. public HTTPRequest Send()
  490. {
  491. // TODO: Are we really want to 'reset' the token source? Two problems i see:
  492. // 1.) User code will not know about this change
  493. // 2.) We might dispose the source while the DNS and TCP queries are running and checking the source request's Token.
  494. //if (this.IsRedirected)
  495. //{
  496. // this.CancellationTokenSource?.Dispose();
  497. // this.CancellationTokenSource = new System.Threading.CancellationTokenSource();
  498. //}
  499. this.Exception = null;
  500. return HTTPManager.SendRequest(this);
  501. }
  502. /// <summary>
  503. /// Cancels any further processing of the HTTP request.
  504. /// </summary>
  505. public void Abort()
  506. {
  507. HTTPManager.Logger.Verbose("HTTPRequest", $"Abort({this.State})", this.Context);
  508. if (this.State >= HTTPRequestStates.Finished || this.CancellationTokenSource == null)
  509. return;
  510. //this.IsCancellationRequested = true;
  511. this.CancellationTokenSource.Cancel();
  512. // There's a race-condition here too, another thread might set it too.
  513. // In this case, both state going to be queued up that we have to handle in RequestEvents.cs.
  514. if (this.TimeoutSettings.IsTimedOut(HTTPManager.CurrentFrameDateTime))
  515. {
  516. RequestEventHelper.EnqueueRequestEvent(new RequestEventInfo(this, this.TimeoutSettings.IsConnectTimedOut(HTTPManager.CurrentFrameDateTime) ? HTTPRequestStates.ConnectionTimedOut : HTTPRequestStates.TimedOut, null));
  517. }
  518. else
  519. RequestEventHelper.EnqueueRequestEvent(new RequestEventInfo(this, HTTPRequestStates.Aborted, null));
  520. if (this.OnCancellationRequested != null)
  521. {
  522. try
  523. {
  524. this.OnCancellationRequested(this);
  525. }
  526. catch { }
  527. }
  528. }
  529. /// <summary>
  530. /// Resets the request for a state where switching MethodType is possible.
  531. /// </summary>
  532. public void Clear()
  533. {
  534. RemoveHeaders();
  535. this.RedirectSettings.Reset();
  536. this.Exception = null;
  537. this.CancellationTokenSource?.Dispose();
  538. this.CancellationTokenSource = new System.Threading.CancellationTokenSource();
  539. this.UploadSettings?.Dispose();
  540. }
  541. #region System.Collections.IEnumerator implementation
  542. /// <summary>
  543. /// <see cref="IEnumerator.Current"/> implementation, required for <see cref="UnityEngine.Coroutine"/> support.
  544. /// </summary>
  545. public object Current { get { return null; } }
  546. /// <summary>
  547. /// <see cref="IEnumerator.MoveNext"/> implementation, required for <see cref="UnityEngine.Coroutine"/> support.
  548. /// </summary>
  549. /// <returns><c>true</c> if the request isn't finished yet.</returns>
  550. public bool MoveNext() => this.State < HTTPRequestStates.Finished;
  551. /// <summary>
  552. /// <see cref="IEnumerator.MoveNext"/> implementation throwing <see cref="NotImplementedException"/>, required for <see cref="UnityEngine.Coroutine"/> support.
  553. /// </summary>
  554. /// <exception cref="NotImplementedException"></exception>
  555. public void Reset() => throw new NotImplementedException();
  556. #endregion
  557. /// <summary>
  558. /// Disposes of resources used by the HTTPRequest instance.
  559. /// </summary>
  560. internal void Dispose()
  561. {
  562. this.UploadSettings?.Dispose();
  563. this.Response?.Dispose();
  564. this.CancellationTokenSource?.Dispose();
  565. this.CancellationTokenSource = null;
  566. }
  567. public override string ToString()
  568. {
  569. return $"[HTTPRequest {this.State}, {this.Context.Hash}, {this.CurrentUri}, {this.CurrentHostKey}]";
  570. }
  571. }
  572. }