HTTP2Stream.cs 31 KB

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