HTTP2Stream.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. #if (!UNITY_WEBGL || UNITY_EDITOR) && !BESTHTTP_DISABLE_ALTERNATE_SSL
  2. #define ENABLE_LOGGING
  3. using System;
  4. using System.Collections.Generic;
  5. using Best.HTTP.Request.Upload;
  6. using Best.HTTP.Request.Timings;
  7. using Best.HTTP.Response;
  8. using Best.HTTP.Shared;
  9. using Best.HTTP.Shared.Extensions;
  10. using Best.HTTP.Shared.Logger;
  11. using Best.HTTP.Shared.PlatformSupport.Memory;
  12. namespace Best.HTTP.Hosts.Connections.HTTP2
  13. {
  14. // https://httpwg.org/specs/rfc7540.html#StreamStates
  15. //
  16. // Idle
  17. // |
  18. // V
  19. // Open
  20. // Receive END_STREAM / | \ Send END_STREAM
  21. // v |R V
  22. // Half Closed Remote |S Half Closed Locale
  23. // \ |T /
  24. // Send END_STREAM | RST_STREAM \ | / Receive END_STREAM | RST_STREAM
  25. // Receive RST_STREAM \ | / Send RST_STREAM
  26. // V
  27. // Closed
  28. //
  29. // IDLE -> send headers -> OPEN -> send data -> HALF CLOSED - LOCAL -> receive headers -> receive Data -> CLOSED
  30. // | ^ | ^
  31. // +-------------------------------------+ +-----------------------------+
  32. // END_STREAM flag present? END_STREAM flag present?
  33. //
  34. public enum HTTP2StreamStates
  35. {
  36. Idle,
  37. //ReservedLocale,
  38. //ReservedRemote,
  39. Open,
  40. HalfClosedLocal,
  41. HalfClosedRemote,
  42. Closed
  43. }
  44. /// <summary>
  45. /// Implements an HTTP/2 logical stream.
  46. /// </summary>
  47. public class HTTP2Stream : IDownloadContentBufferAvailable
  48. {
  49. public UInt32 Id { get; private set; }
  50. public HTTP2StreamStates State {
  51. get { return this._state; }
  52. protected set {
  53. var oldState = this._state;
  54. this._state = value;
  55. #if ENABLE_LOGGING
  56. if (oldState != this._state && HTTPManager.Logger.IsDiagnostic)
  57. HTTPManager.Logger.Information("HTTP2Stream", string.Format("[{0}] State changed from {1} to {2}", this.Id, oldState, this._state), this.Context);
  58. #endif
  59. }
  60. }
  61. private HTTP2StreamStates _state;
  62. /// <summary>
  63. /// This flag is checked by the connection to decide whether to do a new processing-frame sending round before sleeping until new data arrives
  64. /// </summary>
  65. public virtual bool HasFrameToSend
  66. {
  67. get
  68. {
  69. // Don't let the connection sleep until
  70. return this.outgoing.Count > 0 || // we already booked at least one frame in advance
  71. (this.State == HTTP2StreamStates.Open && this.remoteWindow > 0 && this.lastReadCount > 0); // we are in the middle of sending request data
  72. }
  73. }
  74. /// <summary>
  75. /// Next interaction scheduled by the stream relative to *now*. Its default is TimeSpan.MaxValue == no interaction.
  76. /// </summary>
  77. public virtual TimeSpan NextInteraction { get; } = TimeSpan.MaxValue;
  78. public HTTPRequest AssignedRequest { get; protected set; }
  79. public LoggingContext Context { get; protected set; }
  80. protected uint downloaded;
  81. protected HTTP2SettingsManager settings;
  82. protected HPACKEncoder encoder;
  83. // Outgoing frames. The stream will send one frame per Process call, but because one step might be able to
  84. // generate more than one frames, we use a list.
  85. protected Queue<HTTP2FrameHeaderAndPayload> outgoing = new Queue<HTTP2FrameHeaderAndPayload>();
  86. protected Queue<HTTP2FrameHeaderAndPayload> incomingFrames = new Queue<HTTP2FrameHeaderAndPayload>();
  87. protected FramesAsStreamView headerView;
  88. protected Int64 localWindow;
  89. protected Int64 remoteWindow;
  90. protected uint windowUpdateThreshold;
  91. protected UInt32 assignDataLength;
  92. protected long sentData;
  93. protected long uploadLength;
  94. protected bool isRSTFrameSent;
  95. protected bool isEndSTRReceived;
  96. protected HTTP2Response response;
  97. protected int lastReadCount;
  98. protected HTTP2ContentConsumer _parentHandler;
  99. /// <summary>
  100. /// Constructor to create a client stream.
  101. /// </summary>
  102. public HTTP2Stream(UInt32 id, HTTP2ContentConsumer parentHandler, HTTP2SettingsManager registry, HPACKEncoder hpackEncoder)
  103. {
  104. this.Id = id;
  105. this._parentHandler = parentHandler;
  106. this.settings = registry;
  107. this.encoder = hpackEncoder;
  108. this.Context = new LoggingContext(this);
  109. this.Context.Add("id", id);
  110. this.Context.Add("Parent", parentHandler.Context);
  111. this.remoteWindow = this.settings.RemoteSettings[HTTP2Settings.INITIAL_WINDOW_SIZE];
  112. this.settings.RemoteSettings.OnSettingChangedEvent += OnRemoteSettingChanged;
  113. // Room for improvement: If INITIAL_WINDOW_SIZE is small (what we can consider a 'small' value?), threshold must be higher
  114. this.windowUpdateThreshold = (uint)(this.remoteWindow / 2);
  115. }
  116. public virtual void Assign(HTTPRequest request)
  117. {
  118. this.Context.Add("Request", request.Context);
  119. request.Timing.StartNext(TimingEventNames.Request_Sent);
  120. #if ENABLE_LOGGING
  121. HTTPManager.Logger.Information("HTTP2Stream", string.Format("[{0}] Request assigned to stream. Remote Window: {1:N0}. Uri: {2}", this.Id, this.remoteWindow, request.CurrentUri.ToString()), this.Context);
  122. #endif
  123. this.AssignedRequest = request;
  124. this.downloaded = 0;
  125. }
  126. public void Process(List<HTTP2FrameHeaderAndPayload> outgoingFrames)
  127. {
  128. if (this.AssignedRequest.IsCancellationRequested && !this.isRSTFrameSent)
  129. {
  130. #if ENABLE_LOGGING
  131. HTTPManager.Logger.Information("HTTP2Stream", $"[{this.Id}] Process({this.State}) - IsCancellationRequested", this.Context);
  132. #endif
  133. // These two are already set in HTTPRequest's Abort().
  134. //this.AssignedRequest.Response = null;
  135. //this.AssignedRequest.State = this.AssignedRequest.IsTimedOut ? HTTPRequestStates.TimedOut : HTTPRequestStates.Aborted;
  136. this.outgoing.Clear();
  137. if (this.State != HTTP2StreamStates.Idle)
  138. this.outgoing.Enqueue(HTTP2FrameHelper.CreateRSTFrame(this.Id, HTTP2ErrorCodes.CANCEL, this.Context));
  139. // We can close the stream if already received headers, or not even sent one
  140. if (this.State == HTTP2StreamStates.HalfClosedRemote || this.State == HTTP2StreamStates.HalfClosedLocal || this.State == HTTP2StreamStates.Idle)
  141. this.State = HTTP2StreamStates.Closed;
  142. this.isRSTFrameSent = true;
  143. }
  144. // 1.) Go through incoming frames
  145. ProcessIncomingFrames(outgoingFrames);
  146. // 2.) Create outgoing frames based on the stream's state and the request processing state.
  147. ProcessState(outgoingFrames);
  148. // 3.) Send one frame per Process call
  149. if (this.outgoing.Count > 0)
  150. {
  151. HTTP2FrameHeaderAndPayload frame = this.outgoing.Dequeue();
  152. outgoingFrames.Add(frame);
  153. // If END_Stream in header or data frame is present => half closed local
  154. if ((frame.Type == HTTP2FrameTypes.HEADERS && (frame.Flags & (byte)HTTP2HeadersFlags.END_STREAM) != 0) ||
  155. (frame.Type == HTTP2FrameTypes.DATA && (frame.Flags & (byte)HTTP2DataFlags.END_STREAM) != 0))
  156. {
  157. this.State = HTTP2StreamStates.HalfClosedLocal;
  158. }
  159. }
  160. }
  161. public void AddFrame(HTTP2FrameHeaderAndPayload frame, List<HTTP2FrameHeaderAndPayload> outgoingFrames)
  162. {
  163. // Room for improvement: error check for forbidden frames (like settings) and stream state
  164. this.incomingFrames.Enqueue(frame);
  165. ProcessIncomingFrames(outgoingFrames);
  166. }
  167. public void Abort(string msg)
  168. {
  169. #if ENABLE_LOGGING
  170. HTTPManager.Logger.Information("HTTP2Stream", $"[{this.Id}] Abort(\"{msg}\", {this.State}, {this.AssignedRequest.State})", this.Context);
  171. #endif
  172. if (this.State != HTTP2StreamStates.Closed)
  173. {
  174. // TODO: Remove AssignedRequest.State checks. If the main thread has delays processing queued up state change requests,
  175. // the request's State can contain old information!
  176. if (this.AssignedRequest.State != HTTPRequestStates.Processing)
  177. {
  178. // do nothing, its state is already set.
  179. }
  180. else if (this.AssignedRequest.IsCancellationRequested)
  181. {
  182. // These two are already set in HTTPRequest's Abort().
  183. //this.AssignedRequest.Response = null;
  184. //this.AssignedRequest.State = this.AssignedRequest.IsTimedOut ? HTTPRequestStates.TimedOut : HTTPRequestStates.Aborted;
  185. this.State = HTTP2StreamStates.Closed;
  186. }
  187. else if (this.AssignedRequest.RetrySettings.Retries >= this.AssignedRequest.RetrySettings.MaxRetries)
  188. {
  189. this.AssignedRequest.Timing.StartNext(TimingEventNames.Queued_For_Disptach);
  190. RequestEventHelper.EnqueueRequestEvent(new RequestEventInfo(this.AssignedRequest, HTTPRequestStates.Error, new Exception(msg)));
  191. this.State = HTTP2StreamStates.Closed;
  192. }
  193. else
  194. {
  195. this.AssignedRequest.RetrySettings.Retries++;
  196. this.AssignedRequest.Response?.Dispose();
  197. this.AssignedRequest.Response = null;
  198. RequestEventHelper.EnqueueRequestEvent(new RequestEventInfo(this.AssignedRequest, RequestEvents.Resend));
  199. }
  200. }
  201. this.Removed();
  202. }
  203. protected void ProcessIncomingFrames(List<HTTP2FrameHeaderAndPayload> outgoingFrames)
  204. {
  205. while (this.incomingFrames.Count > 0)
  206. {
  207. HTTP2FrameHeaderAndPayload frame = this.incomingFrames.Dequeue();
  208. if ((this.isRSTFrameSent || this.AssignedRequest.IsCancellationRequested) && frame.Type != HTTP2FrameTypes.HEADERS && frame.Type != HTTP2FrameTypes.CONTINUATION)
  209. {
  210. BufferPool.Release(frame.Payload);
  211. continue;
  212. }
  213. #if ENABLE_LOGGING
  214. if (/*HTTPManager.Logger.Level == Logger.Loglevels.All && */frame.Type != HTTP2FrameTypes.DATA && frame.Type != HTTP2FrameTypes.WINDOW_UPDATE)
  215. HTTPManager.Logger.Information("HTTP2Stream", string.Format("[{0}] Process - processing frame: {1}", this.Id, frame.ToString()), this.Context);
  216. #endif
  217. switch (frame.Type)
  218. {
  219. case HTTP2FrameTypes.HEADERS:
  220. case HTTP2FrameTypes.CONTINUATION:
  221. if (this.State != HTTP2StreamStates.HalfClosedLocal && this.State != HTTP2StreamStates.Open && this.State != HTTP2StreamStates.Idle)
  222. {
  223. // ERROR!
  224. continue;
  225. }
  226. // payload will be released by the view
  227. frame.DontUseMemPool = true;
  228. if (this.headerView == null)
  229. {
  230. this.AssignedRequest.Timing.StartNext(TimingEventNames.Headers);
  231. this.headerView = new FramesAsStreamView(new HeaderFrameView());
  232. }
  233. this.headerView.AddFrame(frame);
  234. // END_STREAM may arrive sooner than an END_HEADERS, so we have to store that we already received it
  235. if ((frame.Flags & (byte)HTTP2HeadersFlags.END_STREAM) != 0)
  236. this.isEndSTRReceived = true;
  237. if ((frame.Flags & (byte)HTTP2HeadersFlags.END_HEADERS) != 0)
  238. {
  239. List<KeyValuePair<string, string>> headers = new List<KeyValuePair<string, string>>();
  240. try
  241. {
  242. this.encoder.Decode(this, this.headerView, headers);
  243. }
  244. catch(Exception ex)
  245. {
  246. HTTPManager.Logger.Exception("HTTP2Stream", string.Format("[{0}] ProcessIncomingFrames - Header Frames: {1}, Encoder: {2}", this.Id, this.headerView.ToString(), this.encoder.ToString()), ex, this.Context);
  247. }
  248. this.headerView.Close();
  249. this.headerView = null;
  250. this.AssignedRequest.Timing.StartNext(TimingEventNames.Response_Received);
  251. if (this.isRSTFrameSent)
  252. {
  253. this.State = HTTP2StreamStates.Closed;
  254. break;
  255. }
  256. if (this.response == null)
  257. this.AssignedRequest.Response = this.response = new HTTP2Response(this.AssignedRequest, false);
  258. this.response.AddHeaders(headers);
  259. if (this.isEndSTRReceived)
  260. {
  261. // If there's any trailing header, no data frame has an END_STREAM flag
  262. this.response.FinishProcessData();
  263. FinishRequest();
  264. if (this.State == HTTP2StreamStates.HalfClosedLocal)
  265. this.State = HTTP2StreamStates.Closed;
  266. else
  267. this.State = HTTP2StreamStates.HalfClosedRemote;
  268. }
  269. }
  270. break;
  271. case HTTP2FrameTypes.DATA:
  272. ProcessIncomingDATAFrame(ref frame);
  273. break;
  274. case HTTP2FrameTypes.WINDOW_UPDATE:
  275. HTTP2WindowUpdateFrame windowUpdateFrame = HTTP2FrameHelper.ReadWindowUpdateFrame(frame);
  276. #if ENABLE_LOGGING
  277. if (HTTPManager.Logger.IsDiagnostic)
  278. HTTPManager.Logger.Information("HTTP2Stream", string.Format("[{0}] Received Window Update: {1:N0}, new remoteWindow: {2:N0}, initial remote window: {3:N0}, total data sent: {4:N0}", this.Id, windowUpdateFrame.WindowSizeIncrement, this.remoteWindow + windowUpdateFrame.WindowSizeIncrement, this.settings.RemoteSettings[HTTP2Settings.INITIAL_WINDOW_SIZE], this.sentData), this.Context);
  279. #endif
  280. this.remoteWindow += windowUpdateFrame.WindowSizeIncrement;
  281. break;
  282. case HTTP2FrameTypes.RST_STREAM:
  283. // https://httpwg.org/specs/rfc7540.html#RST_STREAM
  284. // It's possible to receive an RST_STREAM on a closed stream. In this case, we have to ignore it.
  285. if (this.State == HTTP2StreamStates.Closed)
  286. break;
  287. var rstStreamFrame = HTTP2FrameHelper.ReadRST_StreamFrame(frame);
  288. //HTTPManager.Logger.Error("HTTP2Stream", string.Format("[{0}] RST Stream frame ({1}) received in state {2}!", this.Id, rstStreamFrame, this.State), this.Context);
  289. Abort(string.Format("RST_STREAM frame received! Error code: {0}({1})", rstStreamFrame.Error.ToString(), rstStreamFrame.ErrorCode));
  290. break;
  291. default:
  292. HTTPManager.Logger.Warning("HTTP2Stream", string.Format("[{0}] Unexpected frame ({1}, Payload: {2}) in state {3}!", this.Id, frame, frame.PayloadAsHex(), this.State), this.Context);
  293. break;
  294. }
  295. if (!frame.DontUseMemPool)
  296. BufferPool.Release(frame.Payload);
  297. }
  298. }
  299. void IDownloadContentBufferAvailable.BufferAvailable(DownloadContentStream stream)
  300. {
  301. // Signal the http2 thread, window update will be sent out in ProcessOpenState.
  302. this._parentHandler.SignalThread();
  303. }
  304. protected virtual void ProcessIncomingDATAFrame(ref HTTP2FrameHeaderAndPayload frame)
  305. {
  306. if (this.State != HTTP2StreamStates.HalfClosedLocal && this.State != HTTP2StreamStates.Open)
  307. {
  308. // ERROR!
  309. return;
  310. }
  311. HTTP2DataFrame dataFrame = HTTP2FrameHelper.ReadDataFrame(frame);
  312. this.downloaded += (uint)dataFrame.Data.Count;
  313. this.response.Prepare(this);
  314. this.response.ProcessData(dataFrame.Data);
  315. frame.DontUseMemPool = true;
  316. // Because of padding, frame.Payload.Count can be larger than dataFrame.Data.Count!
  317. // "The entire DATA frame payload is included in flow control, including the Pad Length and Padding fields if present."
  318. this.localWindow -= frame.Payload.Count;
  319. this.isEndSTRReceived = (frame.Flags & (byte)HTTP2DataFlags.END_STREAM) != 0;
  320. if (this.isEndSTRReceived)
  321. {
  322. this.response.FinishProcessData();
  323. #if ENABLE_LOGGING
  324. HTTPManager.Logger.Information("HTTP2Stream", string.Format("[{0}] All data arrived, data length: {1:N0}", this.Id, this.downloaded), this.Context);
  325. #endif
  326. FinishRequest();
  327. if (this.State == HTTP2StreamStates.HalfClosedLocal)
  328. this.State = HTTP2StreamStates.Closed;
  329. else
  330. this.State = HTTP2StreamStates.HalfClosedRemote;
  331. }
  332. else if (this.AssignedRequest.DownloadSettings.OnDownloadProgress != null)
  333. RequestEventHelper.EnqueueRequestEvent(new RequestEventInfo(this.AssignedRequest,
  334. RequestEvents.DownloadProgress,
  335. downloaded,
  336. this.response.ExpectedContentLength));
  337. }
  338. protected void ProcessState(List<HTTP2FrameHeaderAndPayload> outgoingFrames)
  339. {
  340. switch (this.State)
  341. {
  342. case HTTP2StreamStates.Idle:
  343. // hpack encode the request's headers
  344. this.encoder.Encode(this, this.AssignedRequest, this.outgoing, this.Id);
  345. // HTTP/2 uses DATA frames to carry message payloads.
  346. // The chunked transfer encoding defined in Section 4.1 of [RFC7230] MUST NOT be used in HTTP/2.
  347. if (this.AssignedRequest.UploadSettings.UploadStream == null)
  348. {
  349. this.State = HTTP2StreamStates.HalfClosedLocal;
  350. //this.AssignedRequest.Timing.Finish(TimingEventNames.Request_Sent);
  351. this.AssignedRequest.Timing.StartNext(TimingEventNames.Waiting_TTFB);
  352. }
  353. else
  354. {
  355. this.State = HTTP2StreamStates.Open;
  356. this.lastReadCount = 1;
  357. if (this.AssignedRequest.UploadSettings.UploadStream is UploadStreamBase upStream)
  358. upStream.BeforeSendBody(this.AssignedRequest, this._parentHandler);
  359. if (this.AssignedRequest.UploadSettings != null && this.AssignedRequest.UploadSettings.UploadStream != null)
  360. this.uploadLength = this.AssignedRequest.UploadSettings.UploadStream.Length;
  361. }
  362. // Change the initial window size to the request's DownloadSettings.ContentStreamMaxBuffered and send it to the server.
  363. // After this initial setup the sending out window_update frames faces two problems:
  364. // 1.) The local window should be bound to the Down-stream's MaxBuffered (ContentStreamMaxBuffered) and how its current length.
  365. // 2.) Even while the its bound to the stream's current values, when the download finishes, we still have to update the global window.
  366. // So, there's two options to follow:
  367. // 1.) Update the local window based on the stream's usage
  368. // 2.a) Send global window_update for every DATA frame processed/received
  369. UInt32 initiatedInitialWindowSize = this.settings.InitiatedMySettings[HTTP2Settings.INITIAL_WINDOW_SIZE];
  370. this.localWindow = initiatedInitialWindowSize;
  371. // Maximize max buffered to HTTP/2's limit
  372. if (this.AssignedRequest.DownloadSettings.ContentStreamMaxBuffered > HTTP2ContentConsumer.MaxValueFor31Bits)
  373. this.AssignedRequest.DownloadSettings.ContentStreamMaxBuffered = HTTP2ContentConsumer.MaxValueFor31Bits;
  374. long localWindowDiff = this.AssignedRequest.DownloadSettings.ContentStreamMaxBuffered - this.localWindow;
  375. if (localWindowDiff > 0)
  376. {
  377. this.localWindow += localWindowDiff;
  378. this.outgoing.Enqueue(HTTP2FrameHelper.CreateWindowUpdateFrame(this.Id, (UInt32)localWindowDiff, this.Context));
  379. }
  380. break;
  381. case HTTP2StreamStates.Open:
  382. ProcessOpenState(outgoingFrames);
  383. //HTTPManager.Logger.Information("HTTP2Stream", string.Format("[{0}] New DATA frame created! remoteWindow: {1:N0}", this.Id, this.remoteWindow), this.Context);
  384. break;
  385. case HTTP2StreamStates.HalfClosedLocal:
  386. if (this.response?.DownStream != null)
  387. {
  388. var windowIncrement = this.response.DownStream.MaxBuffered - this.localWindow - this.response.DownStream.Length;
  389. if (windowIncrement > 0)
  390. {
  391. this.localWindow += windowIncrement;
  392. outgoingFrames.Add(HTTP2FrameHelper.CreateWindowUpdateFrame(this.Id, (UInt32)windowIncrement, this.Context));
  393. #if ENABLE_LOGGING
  394. HTTPManager.Logger.Information("HTTP2Stream", $"[{this.Id}] Sending window inc. update: {windowIncrement:N0}, {this.localWindow:N0}/{this.response.DownStream.MaxBuffered:N0}", this.Context);
  395. #endif
  396. }
  397. }
  398. break;
  399. case HTTP2StreamStates.HalfClosedRemote:
  400. break;
  401. case HTTP2StreamStates.Closed:
  402. break;
  403. }
  404. }
  405. protected virtual void ProcessOpenState(List<HTTP2FrameHeaderAndPayload> outgoingFrames)
  406. {
  407. // remote Window can be negative! See https://httpwg.org/specs/rfc7540.html#InitialWindowSize
  408. if (this.remoteWindow <= 0)
  409. {
  410. #if ENABLE_LOGGING
  411. HTTPManager.Logger.Information("HTTP2Stream", string.Format("[{0}] Skipping data sending as remote Window is {1}!", this.Id, this.remoteWindow), this.Context);
  412. #endif
  413. return;
  414. }
  415. // This step will send one frame per ProcessOpenState call.
  416. Int64 maxFrameSize = Math.Min(this.AssignedRequest.UploadSettings.UploadChunkSize, Math.Min(this.remoteWindow, this.settings.RemoteSettings[HTTP2Settings.MAX_FRAME_SIZE]));
  417. HTTP2FrameHeaderAndPayload frame = new HTTP2FrameHeaderAndPayload();
  418. frame.Type = HTTP2FrameTypes.DATA;
  419. frame.StreamId = this.Id;
  420. frame.Payload = BufferPool.Get(maxFrameSize, true)
  421. .AsBuffer((int)maxFrameSize);
  422. // Expect a readCount of zero if it's end of the stream. But, to enable non-blocking scenario to wait for data, going to treat a negative value as no data.
  423. this.lastReadCount = this.AssignedRequest.UploadSettings.UploadStream.Read(frame.Payload.Data, 0, (int)Math.Min(maxFrameSize, int.MaxValue));
  424. if (this.lastReadCount <= 0)
  425. {
  426. BufferPool.Release(frame.Payload);
  427. frame.Payload = BufferSegment.Empty;
  428. if (this.lastReadCount < 0)
  429. return;
  430. }
  431. else
  432. frame.Payload = frame.Payload.Slice(0, this.lastReadCount);
  433. frame.DontUseMemPool = false;
  434. if (this.lastReadCount <= 0)
  435. {
  436. this.AssignedRequest.UploadSettings.Dispose();
  437. frame.Flags = (byte)(HTTP2DataFlags.END_STREAM);
  438. this.State = HTTP2StreamStates.HalfClosedLocal;
  439. this.AssignedRequest.Timing.StartNext(TimingEventNames.Waiting_TTFB);
  440. }
  441. this.outgoing.Enqueue(frame);
  442. this.remoteWindow -= frame.Payload.Count;
  443. this.sentData += (uint)frame.Payload.Count;
  444. if (this.AssignedRequest.UploadSettings.OnUploadProgress != null)
  445. RequestEventHelper.EnqueueRequestEvent(new RequestEventInfo(this.AssignedRequest, RequestEvents.UploadProgress, this.sentData, uploadLength));
  446. }
  447. protected void OnRemoteSettingChanged(HTTP2SettingsRegistry registry, HTTP2Settings setting, uint oldValue, uint newValue)
  448. {
  449. switch (setting)
  450. {
  451. case HTTP2Settings.INITIAL_WINDOW_SIZE:
  452. // https://httpwg.org/specs/rfc7540.html#InitialWindowSize
  453. // "Prior to receiving a SETTINGS frame that sets a value for SETTINGS_INITIAL_WINDOW_SIZE,
  454. // an endpoint can only use the default initial window size when sending flow-controlled frames."
  455. // "In addition to changing the flow-control window for streams that are not yet active,
  456. // a SETTINGS frame can alter the initial flow-control window size for streams with active flow-control windows
  457. // (that is, streams in the "open" or "half-closed (remote)" state). When the value of SETTINGS_INITIAL_WINDOW_SIZE changes,
  458. // a receiver MUST adjust the size of all stream flow-control windows that it maintains by the difference between the new value and the old value."
  459. // So, if we created a stream before the remote peer's initial settings frame is received, we
  460. // will adjust the window size. For example: initial window size by default is 65535, if we later
  461. // receive a change to 1048576 (1 MB) we will increase the current remoteWindow by (1 048 576 - 65 535 =) 983 041
  462. // But because initial window size in a setting frame can be smaller then the default 65535 bytes,
  463. // the difference can be negative:
  464. // "A change to SETTINGS_INITIAL_WINDOW_SIZE can cause the available space in a flow-control window to become negative.
  465. // A sender MUST track the negative flow-control window and MUST NOT send new flow-controlled frames
  466. // until it receives WINDOW_UPDATE frames that cause the flow-control window to become positive.
  467. // For example, if the client sends 60 KB immediately on connection establishment
  468. // and the server sets the initial window size to be 16 KB, the client will recalculate
  469. // the available flow - control window to be - 44 KB on receipt of the SETTINGS frame.
  470. // The client retains a negative flow-control window until WINDOW_UPDATE frames restore the
  471. // window to being positive, after which the client can resume sending."
  472. this.remoteWindow += newValue - oldValue;
  473. #if ENABLE_LOGGING
  474. HTTPManager.Logger.Information("HTTP2Stream", string.Format("[{0}] Remote Setting's Initial Window Updated from {1:N0} to {2:N0}, diff: {3:N0}, new remoteWindow: {4:N0}, total data sent: {5:N0}", this.Id, oldValue, newValue, newValue - oldValue, this.remoteWindow, this.sentData), this.Context);
  475. #endif
  476. break;
  477. }
  478. }
  479. protected void FinishRequest()
  480. {
  481. #if ENABLE_LOGGING
  482. HTTPManager.Logger.Information("HTTP2Stream", $"FinishRequest({this.Id})", this.Context);
  483. #endif
  484. try
  485. {
  486. this.AssignedRequest.Timing.StartNext(TimingEventNames.Queued);
  487. bool resendRequest;
  488. HTTPConnectionStates proposedConnectionStates; // ignored
  489. KeepAliveHeader keepAliveHeader = null; // ignored
  490. ConnectionHelper.HandleResponse(this.AssignedRequest, out resendRequest, out proposedConnectionStates, ref keepAliveHeader, this.Context);
  491. if (resendRequest && !this.AssignedRequest.IsCancellationRequested)
  492. RequestEventHelper.EnqueueRequestEvent(new RequestEventInfo(this.AssignedRequest, RequestEvents.Resend));
  493. else
  494. {
  495. RequestEventHelper.EnqueueRequestEvent(new RequestEventInfo(this.AssignedRequest, HTTPRequestStates.Finished, null));
  496. }
  497. }
  498. catch(Exception ex)
  499. {
  500. HTTPManager.Logger.Exception("HTTP2Stream", "FinishRequest", ex, this.Context);
  501. }
  502. }
  503. public void Removed()
  504. {
  505. this.AssignedRequest.UploadSettings.Dispose();
  506. // After receiving a RST_STREAM on a stream, the receiver MUST NOT send additional frames for that stream, with the exception of PRIORITY.
  507. this.outgoing.Clear();
  508. // https://github.com/Benedicht/BestHTTP-Issues/issues/77
  509. // Unsubscribe from OnSettingChangedEvent to remove reference to this instance.
  510. this.settings.RemoteSettings.OnSettingChangedEvent -= OnRemoteSettingChanged;
  511. this.headerView?.Close();
  512. #if ENABLE_LOGGING
  513. HTTPManager.Logger.Information("HTTP2Stream", "Stream removed: " + this.Id.ToString(), this.Context);
  514. #endif
  515. }
  516. }
  517. }
  518. #endif