/// <summary> /// Copies the streams chunked /// </summary> /// <param name="reader"></param> /// <param name="onCopy"></param> /// <param name="cancellationToken"></param> /// <returns></returns> private async Task copyBodyChunkedAsync(ICustomStreamReader reader, Action <byte[], int, int> onCopy, CancellationToken cancellationToken) { while (true) { string chunkHead = await reader.ReadLineAsync(cancellationToken); int idx = chunkHead.IndexOf(";"); if (idx >= 0) { chunkHead = chunkHead.Substring(0, idx); } int chunkSize = int.Parse(chunkHead, NumberStyles.HexNumber); await WriteLineAsync(chunkHead, cancellationToken); if (chunkSize != 0) { await copyBytesFromStream(reader, chunkSize, onCopy, cancellationToken); } await WriteLineAsync(cancellationToken); // chunk trail await reader.ReadLineAsync(cancellationToken); if (chunkSize == 0) { break; } } }
private void getNextChunk() { if (readChunkTrail) { // read the chunk trail of the previous chunk string s = baseStream.ReadLineAsync().Result; } readChunkTrail = true; string chunkHead = baseStream.ReadLineAsync().Result; int idx = chunkHead.IndexOf(";", StringComparison.Ordinal); if (idx >= 0) { chunkHead = chunkHead.Substring(0, idx); } int chunkSize = int.Parse(chunkHead, NumberStyles.HexNumber); bytesRemaining = chunkSize; if (chunkSize == 0) { bytesRemaining = -1; // chunk trail baseStream.ReadLineAsync().Wait(); } }
internal static async Task ReadHeaders(ICustomStreamReader reader, HeaderCollection headerCollection, CancellationToken cancellationToken) { string tmpLine; while (!string.IsNullOrEmpty(tmpLine = await reader.ReadLineAsync(cancellationToken))) { var header = tmpLine.Split(ProxyConstants.ColonSplit, 2); headerCollection.AddHeader(header[0], header[1]); } }
internal static async Task ReadHeaders(ICustomStreamReader reader, HeaderCollection headerCollection, CancellationToken cancellationToken) { string?tmpLine; while (!string.IsNullOrEmpty(tmpLine = await reader.ReadLineAsync(cancellationToken))) { int colonIndex = tmpLine !.IndexOf(':'); if (colonIndex == -1) { throw new Exception("Header line should contain a colon character."); } string headerName = tmpLine.AsSpan(0, colonIndex).ToString(); string headerValue = tmpLine.AsSpan(colonIndex + 1).ToString(); headerCollection.AddHeader(headerName, headerValue); } }