private void SaveDataFrame(Http2Stream stream, DataFrame dataFrame) { lock (_writeLock) { string originalPath = stream.Headers.GetValue(":path".ToLower()); //If user sets the empty file in get command we return notFound webpage string fileName = string.IsNullOrEmpty(Path.GetFileName(originalPath)) ? NotFound : Path.GetFileName(originalPath); string path = Path.Combine(AssemblyPath, fileName); try { _fileHelper.SaveToFile(dataFrame.Data.Array, dataFrame.Data.Offset, dataFrame.Data.Count, path, stream.ReceivedDataAmount != 0); } catch (IOException) { Http2Logger.LogError("File is still downloading. Repeat request later"); stream.WriteDataFrame(new byte[0], true); stream.Dispose(); } stream.ReceivedDataAmount += dataFrame.FrameLength; if (dataFrame.IsEndStream) { if (!stream.EndStreamSent) { //send terminator stream.WriteDataFrame(new byte[0], true); Http2Logger.LogConsole("Terminator was sent"); } _fileHelper.RemoveStream(path); Http2Logger.LogConsole("Bytes received " + stream.ReceivedDataAmount); #if DEBUG const string wayToServerRoot1 = @"..\..\..\..\..\Drop\Root"; const string wayToServerRoot2 = @".\Root"; var areFilesEqual = _fileHelper.CompareFiles(path, wayToServerRoot1 + originalPath) || _fileHelper.CompareFiles(path, wayToServerRoot2 + originalPath); if (!areFilesEqual) { Console.ForegroundColor = ConsoleColor.Red; Http2Logger.LogError("Files are NOT EQUAL!"); } else { Console.ForegroundColor = ConsoleColor.Green; Http2Logger.LogConsole("Files are EQUAL!"); } Console.ForegroundColor = ConsoleColor.Gray; #endif } } }
//Method for future usage in server push private void SendDataTo(Http2Stream stream, byte[] binaryData) { int i = 0; Http2Logger.LogConsole("Transfer begin"); do { bool isLastData = binaryData.Length - i < Constants.MaxDataFrameContentSize; int chunkSize = stream.WindowSize > 0 ? MathEx.Min(binaryData.Length - i, Constants.MaxDataFrameContentSize, stream.WindowSize) : MathEx.Min(binaryData.Length - i, Constants.MaxDataFrameContentSize); var chunk = new byte[chunkSize]; Buffer.BlockCopy(binaryData, i, chunk, 0, chunk.Length); stream.WriteDataFrame(chunk, isLastData); i += chunkSize; } while (binaryData.Length > i); //It was not send exactly. Some of the data frames could be pushed to the unshipped frames collection Http2Logger.LogConsole("File sent: " + stream.Headers.GetValue(":path")); }
private void SaveDataFrame(Http2Stream stream, DataFrame dataFrame) { string originalPath = stream.Headers.GetValue(CommonHeaders.Path.ToLower()); //If user sets the empty file in get command we return notFound webpage string fileName = string.IsNullOrEmpty(Path.GetFileName(originalPath)) ? Index : Path.GetFileName(originalPath); string path = Path.Combine(AssemblyPath, fileName); try { _fileHelper.SaveToFile(dataFrame.Data.Array, dataFrame.Data.Offset, dataFrame.Data.Count, path, stream.ReceivedDataAmount != 0); } catch (IOException) { Http2Logger.LogError("File is still downloading. Repeat request later"); stream.Close(ResetStatusCode.InternalError); return; } stream.ReceivedDataAmount += dataFrame.Data.Count; if (dataFrame.IsEndStream) { if (stream.HalfClosedRemote) { //send terminator stream.WriteDataFrame(new ArraySegment <byte>(new byte[0]), true); Http2Logger.LogConsole("Terminator was sent"); } _fileHelper.RemoveStream(path); Http2Logger.LogConsole("Bytes received: " + stream.ReceivedDataAmount); } }
private void SaveDataFrame(Http2Stream stream, DataFrame dataFrame) { lock (_writeLock) { string path = stream.Headers.GetValue(":path".ToLower()); try { string pathToSave = AssemblyPath + Root + path; if (!Directory.Exists(Path.GetDirectoryName(pathToSave))) { throw new DirectoryNotFoundException("Access denied"); } _fileHelper.SaveToFile(dataFrame.Data.Array, dataFrame.Data.Offset, dataFrame.Data.Count, pathToSave, stream.ReceivedDataAmount != 0); } catch (Exception ex) { Http2Logger.LogError(ex.Message); stream.WriteDataFrame(new byte[0], true); stream.Dispose(); } stream.ReceivedDataAmount += dataFrame.FrameLength; if (dataFrame.IsEndStream) { if (!stream.EndStreamSent) { //send terminator stream.WriteDataFrame(new byte[0], true); Http2Logger.LogDebug("Terminator was sent"); } _fileHelper.RemoveStream(AssemblyPath + Root + path); } } }
public override void Write(byte[] buffer, int offset, int count) { FirstWrite(); // TODO: Validate buffer, offset, count int sent = 0; while (sent < count) { // maximum chunk length of the file to be read int chunkSize = MathEx.Min(count - sent, Constants.MaxFramePayloadSize - Constants.MaxFramePaddingSize); var segment = new ArraySegment <byte>(buffer, offset + sent, chunkSize); _stream.WriteDataFrame(segment, isEndStream: false); sent += chunkSize; } }
// If no response headers have been sent yet, send them. internal void FinishResponse() { if (!_responseStarted) { Http2Logger.LogDebug("Transfer begin"); SendHeaders(final: true); Http2Logger.LogDebug("Transfer end"); } else { // End the data stream. _protocolStream.WriteDataFrame(new ArraySegment <byte>(new byte[0]), isEndStream: true); } }
private void SendDataTo(Http2Stream stream, byte[] binaryData) { int i = 0; Http2Logger.LogDebug("Transfer begin"); do { bool isLastData = binaryData.Length - i < Constants.MaxDataFrameContentSize; int chunkSize = stream.WindowSize > 0 ? MathEx.Min(binaryData.Length - i, Constants.MaxDataFrameContentSize, stream.WindowSize) : MathEx.Min(binaryData.Length - i, Constants.MaxDataFrameContentSize); var chunk = new byte[chunkSize]; Buffer.BlockCopy(binaryData, i, chunk, 0, chunk.Length); stream.WriteDataFrame(chunk, isLastData); i += chunkSize; } while (binaryData.Length > i); }