-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttp.cs
More file actions
261 lines (206 loc) · 7.58 KB
/
Http.cs
File metadata and controls
261 lines (206 loc) · 7.58 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
namespace Samicpp.Http;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
public enum HttpVersion
{
Test = -3,
Unknown = -2,
Http09 = -1,
Http10 = 0,
Http11 = 1,
Http2 = 2,
Http3 = 3,
}
public abstract class ADualSocket : IDualSocket
{
abstract protected Stream Stream { get; }
abstract public bool IsSecure { get; }
// abstract public EndPoint? EndPoint { get; }
public int Read(Span<byte> bytes) => Stream.Read(bytes);
public int Read(byte[] bytes, int offset, int size) => Stream.Read(bytes, offset, size);
public void Write(Span<byte> bytes) => Stream.Write(bytes);
public void Write(byte[] bytes, int offset, int size) => Stream.Write(bytes, offset, size);
public void Write(Stream stream) => stream.CopyTo(Stream);
public async Task<int> ReadAsync(Memory<byte> bytes) => await Stream.ReadAsync(bytes);
public async Task<int> ReadAsync(byte[] bytes, int offset, int size) => await Stream.ReadAsync(bytes, offset, size);
public async Task WriteAsync(Memory<byte> bytes) => await Stream.WriteAsync(bytes);
public async Task WriteAsync(Stream stream) => await stream.CopyToAsync(Stream);
public async Task WriteAsync(byte[] bytes, int offset, int size) => await Stream.WriteAsync(bytes, offset, size);
public void Flush() => Stream.Flush();
public async Task FlushAsync() => await Stream.FlushAsync();
public void Close() => Stream.Close();
public void Dispose() => Stream.Dispose();
public async ValueTask DisposeAsync() => await Stream.DisposeAsync();
public bool CanRead { get { return Stream.CanRead; } }
public bool CanWrite { get { return Stream.CanWrite; } }
public bool CanSeek { get { return Stream.CanSeek; } }
public bool CanTimeout { get { return Stream.CanTimeout; } }
public List<byte> ReadAll()
{
List<byte> all = [];
byte[] buff = new byte[4096];
while (true)
{
int s = Read(buff);
all.AddRange(buff[..s]);
if (s < buff.Length) break;
}
return all;
}
public async Task<List<byte>> ReadAllAsync()
{
List<byte> all = [];
byte[] buff = new byte[4096];
while (true)
{
int s = await ReadAsync(buff);
all.AddRange(buff[..s]);
if (s < buff.Length) break;
}
return all;
}
public byte[] ReadCertain(int size)
{
byte[] bytes = new byte[size];
int last = 0;
while (last < bytes.Length)
{
int s = Read(bytes, last, bytes.Length - last);
if (s <= 0) throw new HttpException.ConnectionClosed(null);
last += s;
}
return bytes;
}
public async Task<byte[]> ReadCertainAsync(int size)
{
byte[] bytes = new byte[size];
int last = 0;
while (last < bytes.Length)
{
int s = await ReadAsync(bytes, last, bytes.Length - last);
if (s <= 0) throw new HttpException.ConnectionClosed(null);
last += s;
}
return bytes;
}
private static bool EndsWith(List<byte> source, byte[] stop)
{
int start = source.Count - stop.Length;
if (start < 0) return false;
for (int i = 0; i < stop.Length; i++)
{
if (source[start + i] != stop[i]) return false;
}
return true;
}
public List<byte> ReadUntil(byte stop)
{
List<byte> total = [];
byte[] buff = new byte[1];
while (true)
{
int s = Read(buff);
if (s <= 0) throw new HttpException.ConnectionClosed(null);
total.Add(buff[0]);
if (buff[0] == stop) break;
}
return total;
}
public List<byte> ReadUntil(byte[] stop)
{
List<byte> total = [];
byte[] buff = new byte[1];
while (true)
{
int s = Read(buff);
if (s <= 0) throw new HttpException.ConnectionClosed(null);
total.Add(buff[0]);
// total.AddRange(buff[..s]);
if (total.Count < stop.Length) continue;
if (EndsWith(total, stop)) break;
}
return total;
}
public List<byte> ReadUntil(params byte[][] stops)
{
List<byte> total = [];
byte[] buff = new byte[1];
while (true)
{
int s = Read(buff);
if (s <= 0) throw new HttpException.ConnectionClosed(null);
total.Add(buff[0]);
// total.AddRange(buff[..s]);
foreach (var stop in stops) if (total.Count < stop.Length) goto cont;
foreach (var stop in stops) if (EndsWith(total, stop)) goto end;
cont: continue; // continue not nessecary
}
end: return total;
}
public async Task<List<byte>> ReadUntilAsync(byte stop)
{
List<byte> total = [];
byte[] buff = new byte[1];
while (true)
{
int s = await ReadAsync(buff);
if (s <= 0) throw new HttpException.ConnectionClosed(null);
total.Add(buff[0]);
if (buff[0] == stop) break;
}
return total;
}
public async Task<List<byte>> ReadUntilAsync(byte[] stop)
{
List<byte> total = [];
byte[] buff = new byte[1];
while (true)
{
int s = await ReadAsync(buff);
if (s <= 0) throw new HttpException.ConnectionClosed(null);
total.Add(buff[0]);
// total.AddRange(buff[..s]);
if (total.Count < stop.Length) continue;
if (EndsWith(total, stop)) break;
}
return total;
}
public async Task<List<byte>> ReadUntilAsync(params byte[][] stops)
{
List<byte> total = [];
byte[] buff = new byte[1];
while (true)
{
int s = await ReadAsync(buff);
if (s <= 0) throw new HttpException.ConnectionClosed(null);
total.Add(buff[0]);
// total.AddRange(buff[..s]);
foreach (var stop in stops) if (total.Count < stop.Length) goto cont;
foreach (var stop in stops) if (EndsWith(total, stop)) goto end;
cont: continue;
}
end: return total;
}
}
public class HttpClient : IHttpClient
{
public bool IsValid { get; set; } = false;
public Dictionary<string, List<string>> Headers { get; set; } = new(StringComparer.OrdinalIgnoreCase);
public string Host { get; set; } = "about:blank";
public string Method { get; set; } = "NILL";
public string Path { get; set; } = "/";
public HttpVersion Version { get; set; } = HttpVersion.Unknown;
public string VersionString { get => Version switch { HttpVersion.Test => "Test", HttpVersion.Unknown => "Unknown", HttpVersion.Http09 => "HTTP/0.9", HttpVersion.Http10 => "HTTP/1.0", HttpVersion.Http11 => "HTTP/1.1", HttpVersion.Http2 => "HTTP/2", HttpVersion.Http3 => "HTTP/3", _ => "Impossible" }; }
public List<byte> Body { get; set; } = [];
public bool HeadersComplete { get; set; } = false;
public bool BodyComplete { get; set; } = false;
}
public class HttpException(string? message = null, Exception? source = null) : Exception(message)
{
public readonly Exception? source = source;
public sealed class ConnectionClosed(string? message) : HttpException(message);
public sealed class HeadersSent(string? message) : HttpException(message);
public sealed class MalformedRequest(string? message) : HttpException(message);
public sealed class UnsupportedVersion(string? message) : HttpException(message);
}