-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStreamExtensions.cs
More file actions
148 lines (134 loc) · 4.79 KB
/
StreamExtensions.cs
File metadata and controls
148 lines (134 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace SharpProxy
{
public static class StreamExtensions
{
public static string ReadLine(this Stream stream)
{
string line = "";
int allowedBadChars = 1;
while (true)
{
int b = stream.ReadByte();
if (b < 0)
{
if (allowedBadChars == 0)
break;
allowedBadChars++;
continue;
}
if (b == '\r')
continue;
if (b == '\n')
break;
line += Convert.ToChar(b);
}
return line;
}
public static void WriteLine(this Stream stream, string line = null)
{
line = line ?? "";
var lineBytes = Encoding.UTF8.GetBytes(line + "\r\n");
stream.Write(lineBytes, 0, lineBytes.Length);
}
async public static Task CopyToAsync(this Stream source, Stream target, long length)
{
var totalToRead = length;
var buffer = new byte[0x1000];
//Debug.WriteLine("Copying " + totalToRead + " ...");
while (totalToRead > 0)
{
var read = await source.ReadAsync(buffer, 0, buffer.Length);
await target.WriteAsync(buffer, 0, read);
totalToRead -= read;
//Debug.Write(Encoding.UTF8.GetString(buffer, 0, read));
//Debug.WriteLine("TotalToRead: " + totalToRead);
}
//Debug.Write("\r\n");
//Debug.WriteLine("Copying DONE (" + length + ")");
}
async public static Task CopyHttpMessageToAsync(this Stream source, Socket sourceSocket, Stream target, long contentLength = -1)
{
if (sourceSocket.Available == 0)
{
//Debug.WriteLine("DataAvailable: 0");
return;
}
var isChunked = contentLength == -1;
var remaining = contentLength;
int read = 0;
var bufferLength = 0x1000;
byte[] buffer = new byte[bufferLength];
string chunkHeader = null;
do
{
if (isChunked)
{
bufferLength = GetChunkLength(source, out chunkHeader);
//Debug.WriteLine("Expecting Chunk: " + bufferLength + " (" + chunkHeader + ")");
target.WriteLine(chunkHeader);
if (bufferLength == 0)
{
source.ReadLine(); //eat last \r\n
target.WriteLine(); //echo last \r\n to target
bufferLength = -1;
}
if (bufferLength == -1)
return;
buffer = new byte[bufferLength];
remaining = bufferLength;
}
do
{
read = await source.ReadAsync(buffer, 0, (int) Math.Min(buffer.Length, remaining));
//Debug.WriteLine("Read: " + read);
remaining -= read;
await target.WriteAsync(buffer, 0, read);
} while (read > 0 && remaining > 0);
if (isChunked)
{
var line = source.ReadLine(); //eat \r\n
if (line != "")
throw new InvalidDataException("Unexpected line content");
target.WriteLine(null); //end chunk
remaining = 1; //continue to next chunk
}
else
{
remaining -= read;
}
} while (remaining > 0);
}
private static int GetChunkLength(Stream source, out string chunkHeader)
{
chunkHeader = null;
try
{
chunkHeader = source.ReadLine();
var parts = chunkHeader.Split(new char[] {':'}, 2, StringSplitOptions.None);
var sizeString = parts[0].Trim();
var chunkSize = int.Parse(sizeString, NumberStyles.AllowHexSpecifier);
return chunkSize;
}
catch (Exception ex)
{
Debug.WriteLine("CopyAllToAsync error: " + ex.Message);
return -1;
}
}
}
//public class CustomNetworkStream : NetworkStream
//{
// public CustomNetworkStream(Socket socket) : base(socket)
// {
// }
// override
//}
}