forked from atauenis/webone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpServer1.cs
134 lines (120 loc) · 3.76 KB
/
HttpServer1.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
using System;
using System.Net;
using static WebOne.Program;
namespace WebOne
{
/// <summary>
/// HTTP/1.1 Listener and Server (HttpListener-based)
/// </summary>
class HttpServer1 : HttpServer
{
/* This is the old version of HTTP Server, used from WebOne 0.8.5 to 0.15.3.
* Pluses: very stable, very fast, very compatible, professional-made, easy to maintain.
* Minuses: doesn't accept CONNECT method, doesn't accept non-HTTP URIs (CERN-style requests), wants 'Host:' header, rely on Microsoft-HTTPAPI.
* In v0.16.0 and up, kept for troubleshooting purposes.
*/
private int Port;
private static HttpListener _listener = new();
private LogWriter Log = new();
/// <summary>
/// Status of this HTTP Listener & Server
/// </summary>
public override bool Working { get; set; }
/// <summary>
/// Initizlize a HTTP Listener & Server (HttpListener-based).
/// </summary>
/// <param name="port">TCP Port to listen on</param>
public HttpServer1(int port) : base(port)
{
Port = port;
Working = false;
}
/// <summary>
/// Start this HTTP Listener & Server
/// </summary>
public override void Start()
{
//if (_listener == null) _listener = new HttpListener();
try { int test = _listener.Prefixes.Count; }
catch { _listener = new HttpListener(); /*initialize HttpListener if it is not ready*/ }
_listener.Prefixes.Add("http://*:" + Port + "/");
_listener.Start();
_listener.BeginGetContext(ProcessRequest, null);
Working = true;
Log.WriteLine(true, false, "The proxy is running in HTTP-only mode.");
UpdateStatistics();
}
/// <summary>
/// Gracefully stop this HTTP Listener & Server
/// </summary>
public override void Stop()
{
Working = false;
Log.BeginTime = DateTime.Now;
if (_listener != null)
{
if (_listener.IsListening) _listener.Stop();
_listener.Prefixes.Clear();
}
Log.WriteLine(true, true, "HTTP Server stopped.");
}
/// <summary>
/// Process a HTTP request (callback for HttpListener)
/// </summary>
/// <param name="ar">Something from HttpListener</param>
private void ProcessRequest(IAsyncResult ar)
{
if (!Working) return;
Load++;
UpdateStatistics();
LogWriter Logger = new();
#if DEBUG
Logger.WriteLine("Got a request.");
#endif
string RawUrl = null;
try
{
HttpListenerContext ctx = _listener.EndGetContext(ar);
_listener.BeginGetContext(ProcessRequest, null);
HttpListenerRequest req = ctx.Request;
RawUrl = req.RawUrl;
HttpRequest Request = new()
{
HttpMethod = req.HttpMethod,
RawUrl = req.RawUrl,
Url = req.Url,
ProtocolVersion = req.ProtocolVersion,
Headers = req.Headers,
HasEntityBody = req.HasEntityBody,
InputStream = req.InputStream,
RemoteEndPoint = req.RemoteEndPoint,
LocalEndPoint = req.LocalEndPoint,
IsSecureConnection = false,
KeepAlive = req.KeepAlive,
Kind = HttpUtil.GetKindOfRequest(req.RawUrl, req.Headers["Host"])
};
HttpResponse Response = new(ctx.Response);
HttpTransit Transit = new(Request, Response, Logger);
Logger.WriteLine(">{0} {1} ({2})", Request.HttpMethod, Request.RawUrl, Transit.GetClientIdString());
Transit.ProcessTransit();
Logger.WriteLine("<Done.");
}
catch (Exception ex)
{
Logger.WriteLine("Broken request ({0}): {1}. Aborted.", RawUrl ?? "unknown URL", ex.Message);
}
Load--;
UpdateStatistics();
}
/// <summary>
/// Display count of open requests in app's titlebar
/// </summary>
private void UpdateStatistics()
{
if (DaemonMode)
Console.Title = string.Format("WebOne (silent) @ {0}:{1} [{2}]", ConfigFile.DefaultHostName, Port, Load);
else
Console.Title = string.Format("WebOne @ {0}:{1} [{2}]", ConfigFile.DefaultHostName, Port, Load);
}
}
}