-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
122 lines (105 loc) · 4.35 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
using System.Text;
using RazorEngine.Templating;
namespace HttpListener1
{
class Program
{
static int SwitchDomain(AppDomain primary)
{
var setup = new AppDomainSetup();
setup.ApplicationBase = primary.SetupInformation.ApplicationBase;
var secondary = AppDomain.CreateDomain(
"SecondaryDomain",
null,
primary.SetupInformation,
new PermissionSet(PermissionState.Unrestricted),
new StrongName[0]
);
var result = secondary.ExecuteAssembly(Assembly.GetExecutingAssembly().Location);
AppDomain.Unload(secondary);
return result;
}
static int Main(string[] args)
{
var domain = AppDomain.CurrentDomain;
if (domain.IsDefaultAppDomain())
{
Console.WriteLine("Switching to secondary AppDomain...");
return SwitchDomain(domain);
}
Console.WriteLine("Now running in secondary AppDomain.\n");
var addresses = new List<IPAddress>();
addresses.Add(IPAddress.Loopback);
addresses.AddRange(Dns.GetHostAddresses("").Where(x => x.AddressFamily == AddressFamily.InterNetwork));
using (var listener = new HttpListener())
{
foreach (var address in addresses)
{
var url = String.Format("http://{0}:12345/", address);
listener.Prefixes.Add(url);
Console.WriteLine("{0}", url);
}
listener.Start();
Console.WriteLine();
while (true)
{
var context = listener.GetContext();
using (var response = context.Response)
{
var request = context.Request;
Console.WriteLine("{0}", request.RawUrl);
if (request.RawUrl == "/")
{
response.ContentEncoding = Encoding.UTF8;
response.ContentType = "text/html";
using (var reader = new StreamReader(File.OpenRead("index.cshtml")))
{
using (var writer = new StreamWriter(response.OutputStream, response.ContentEncoding))
{
writer.Write(
RazorEngine.Engine.Razor.RunCompile(
reader.ReadToEnd(),
"templateKey",
null,
new
{
Title = "HttpListener1"
}
)
);
}
}
}
else if (request.RawUrl == "/sse")
{
response.ContentEncoding = Encoding.UTF8;
response.ContentType = "text/event-stream";
var writer = new StreamWriter(response.OutputStream, response.ContentEncoding);
writer.Write("data: Testing!\n\n");
writer.Flush();
}
else
{
response.ContentEncoding = Encoding.UTF8;
response.ContentType = "text/html";
response.StatusCode = 404;
using (var writer = new StreamWriter(response.OutputStream, response.ContentEncoding))
writer.Write("404 Not Found");
}
}
}
}
return 0;
}
}
}