-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.cs
186 lines (159 loc) · 6.96 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
namespace tinykestrel
{
class Program
{
static async Task Main(string[] args)
{
NullLoggerFactory nullLoggerFactory = new NullLoggerFactory();
SocketTransportOptions socketTransportOptions = new SocketTransportOptions();
SocketTransportFactory socketTransportFactory = new SocketTransportFactory(
new OptionsWrapper<SocketTransportOptions>(socketTransportOptions), nullLoggerFactory);
KestrelServerOptions kestrelServerOptions = new KestrelServerOptions();
//kestrelServerOptions.AllowSynchronousIO = true;
kestrelServerOptions.ListenLocalhost(5000);
kestrelServerOptions.ApplicationServices = new ServiceProvider();
kestrelServerOptions.ListenLocalhost(5001, listenOptions =>
{
X509Certificate2 serverCertificate = new X509Certificate2("certificate.pfx", "xxxx");
listenOptions.UseHttps(serverCertificate);
});
using (KestrelServer kestrelServer =
new KestrelServer(new OptionsWrapper<KestrelServerOptions>(kestrelServerOptions),
socketTransportFactory,
nullLoggerFactory
)
)
{
await kestrelServer.StartAsync(new HttpApplication(), CancellationToken.None);
Console.ReadLine();
}
}
private class ServiceProvider : ISupportRequiredService, IServiceProvider
{
public object GetRequiredService(Type serviceType)
{
return GetService(serviceType);
}
public object GetService(Type serviceType)
{
if (serviceType == typeof(ILoggerFactory))
{
return NullLoggerFactory.Instance;
}
return null;
}
}
}
public class HttpApplication : IHttpApplication<IFeatureCollection>
{
public IFeatureCollection CreateContext(IFeatureCollection contextFeatures)
{
return contextFeatures;
}
public void DisposeContext(IFeatureCollection context, Exception exception)
{
}
public async Task ProcessRequestAsync(IFeatureCollection features)
{
bool emptyBody = false;
IHttpRequestFeature request = (IHttpRequestFeature)features[typeof(IHttpRequestFeature)];
IHttpResponseFeature response = (IHttpResponseFeature)features[typeof(IHttpResponseFeature)];
IHttpResponseBodyFeature responseBody = (IHttpResponseBodyFeature)features[typeof(IHttpResponseBodyFeature)];
Dictionary<string, object> jsonObjects = new Dictionary<string, object>();
// This uses reflection
// jsonObjects = await JsonSerializer.DeserializeAsync<Dictionary<string, object>>(request.Body);
// This has internal Assembly call
// JsonSerializerOptions serializerOptions = new JsonSerializerOptions();
// serializerOptions.Converters.Add(new NativeJson());
// jsonObjects = await JsonSerializer.DeserializeAsync<Dictionary<string, object>>(request.Body, serializerOptions);
// More complex
// NativeJson.Read(request.Body);
try
{
using (StreamReader streamReader = new StreamReader(request.Body))
{
//string requestBodyString = await streamReader.ReadToEndAsync();
//byte[] utf8JsonBytes = Encoding.UTF8.GetBytes(requestBodyString);
//Utf8JsonReader utf8JsonReader = new Utf8JsonReader(utf8JsonBytes);
using (JsonDocument document = await JsonDocument.ParseAsync(streamReader.BaseStream))
{
JsonElement root = document.RootElement;
if (root.ValueKind == JsonValueKind.Object)
{
foreach (JsonProperty property in root.EnumerateObject())
{
jsonObjects.Add(property.Name, property.Value.ToString());
}
jsonObjects.Add("feedback", "your json has just gone through a native kestrel");
}
else if (root.ValueKind == JsonValueKind.Array)
{
foreach (JsonElement element in root.EnumerateArray())
{
}
}
}
}
}
catch
{
emptyBody = true;
}
if (emptyBody)
{
jsonObjects.Add("firstname", "Natty");
jsonObjects.Add("lastname", "de Balancet");
jsonObjects.Add("info", "copy this json and post it back");
}
JsonWriterOptions jsonWriterOptions = new JsonWriterOptions
{
Indented = true
};
MemoryStream memoryStream = new MemoryStream();
using (Utf8JsonWriter utf8JsonWriter = new Utf8JsonWriter(memoryStream, jsonWriterOptions))
{
utf8JsonWriter.WriteStartObject();
foreach (KeyValuePair<string, object> keyValuePair in jsonObjects)
{
utf8JsonWriter.WriteString(keyValuePair.Key, keyValuePair.Value.ToString());
//utf8JsonWriter.WriteNumber("temp", 42);
}
utf8JsonWriter.WriteEndObject();
utf8JsonWriter.Flush();
//string responseString = Encoding.UTF8.GetString(memoryStream.ToArray());
}
/* Database call test
string names = string.Empty;
//Odbc pool test
for (int i = 0; i < 2; i++)
{
using (IDbConnection dbConnection = new NativeConnection())
{
names = OdbcPerformance.ReadContacts(dbConnection);
}
}
jsonObjects.Add("names", names);
*/
//byte[] responseBodyBytes = Encoding.UTF8.GetBytes(responseString);
response.Headers.ContentLength = memoryStream.Length; //responseBodyBytes.Length;
response.Headers.Add("Content-Type", "application/json");
await responseBody.Stream.WriteAsync(memoryStream.ToArray(), 0, (int)memoryStream.Length);
}
}
}