FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
uhttpsharp/uhttpsharp/Handlers/GZipHandler.cs~RF51dd35.TMP at master · bonesoul/uhttpsharp · GitHub
bonesoul
/
uhttpsharp
Public
Notifications
You must be signed in to change notification settings
Fork
98
Star
178
Code
Issues
11
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
uhttpsharp
/
uhttpsharp
/
Handlers
/
GZipHandler.cs~RF51dd35.TMP
Copy path
More file actions
More file actions
Latest commit
History
History
History
117 lines (96 loc) · 3.47 KB
Breadcrumbs
uhttpsharp
/
uhttpsharp
/
Handlers
/
GZipHandler.cs~RF51dd35.TMP
Copy path
File metadata and controls
117 lines (96 loc) · 3.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using uhttpsharp.Headers;
namespace uhttpsharp.Handlers
{
public class CompressionHandler : IHttpRequestHandler
{
private readonly IEnumerable<ICompressor> _compressors;
private static readonly char[] Seperator = new [] {','};
public CompressionHandler(IEnumerable<ICompressor> compressors)
{
_compressors = compressors.ToList();
}
public async Task Handle(IHttpContext context, Func<Task> next)
{
await next();
if (context.Response == null)
{
return;
}
var encodings = context.Request.Headers.GetByName("Accept-Encoding")
.Split(Seperator, StringSplitOptions.RemoveEmptyEntries);
}
}
public interface ICompressor
{
string Name { get; }
IHttpResponse Compress(IHttpResponse response);
}
public class DeflateHandler : IHttpRequestHandler
{
public async Task Handle(IHttpContext context, Func<Task> next)
{
await next();
if (context.Response != null)
{
context.Response = await DeflateResponse.Create(context.Response).ConfigureAwait(false);
}
}
}
public class DeflateResponse : IHttpResponse
{
private readonly HttpResponseCode _responseCode;
private readonly IHttpHeaders _headers;
private readonly MemoryStream _memoryStream;
private readonly bool _closeConnection;
public DeflateResponse(IHttpResponse child, MemoryStream memoryStream)
{
_memoryStream = memoryStream;
_responseCode = child.ResponseCode;
_closeConnection = child.CloseConnection;
_headers =
new ListHttpHeaders(
child.Headers.Where(h => !h.Key.Equals("content-length", StringComparison.InvariantCultureIgnoreCase))
.Concat(new[]
{
new KeyValuePair<string, string>("content-length", memoryStream.Length.ToString(CultureInfo.InvariantCulture)),
new KeyValuePair<string, string>("content-encoding", "deflate"),
})
.ToList());
}
public static async Task<DeflateResponse> Create(IHttpResponse child)
{
var memoryStream = new MemoryStream();
using (var deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress, true))
using (var deflateWriter = new StreamWriter(deflateStream))
{
await child.WriteBody(deflateWriter).ConfigureAwait(false);
await deflateWriter.FlushAsync();
}
return new DeflateResponse(child, memoryStream);
}
public async Task WriteBody(StreamWriter writer)
{
_memoryStream.Position = 0;
await _memoryStream.CopyToAsync(writer.BaseStream).ConfigureAwait(false);
}
public HttpResponseCode ResponseCode
{
get { return _responseCode; }
}
public IHttpHeaders Headers
{
get { return _headers; }
}
public bool CloseConnection
{
get { return _closeConnection; }
}
}
}
Back
|
FazBrowse Home
|
New Git URL