-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebServer.cs
More file actions
180 lines (147 loc) · 5.96 KB
/
WebServer.cs
File metadata and controls
180 lines (147 loc) · 5.96 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
using System;
using System.Net;
using System.Threading;
using System.Linq;
using System.Text;
using System.Collections.Generic;
namespace HttpServer
{
public class WebServerMethod
{
public static int GET = 0;
public static int POST = 1;
}
public class WebServerResponse
{
private HttpListenerResponse Response;
public WebServerResponse (HttpListenerContext context)
{
this.Response = context.Response;
}
public void send(string message)
{
try
{
byte[] buf = Encoding.UTF8.GetBytes(message);
this.Response.ContentLength64 = buf.Length;
this.Response.OutputStream.Write(buf, 0, buf.Length);
Console.WriteLine(string.Format("[OUT] Sending {0}b", this.Response.ContentLength64));
}
catch { } // suppress any exceptions
finally
{
this.Response.OutputStream.Close(); // always close the stream
}
}
public void setMIMEtype(string MIMEtype)
{
this.Response.ContentType = MIMEtype;
}
public HttpListenerResponse getMethods()
{
return this.Response;
}
}
public class WebServerRequest
{
private HttpListenerRequest Request;
public WebServerRequest (HttpListenerContext context)
{
this.Request = context.Request;
}
public string param(string key)
{
return this.Request.QueryString[key];
}
public HttpListenerRequest getMethods()
{
return this.Request;
}
}
public class WebServer
{
private HttpListener httpSocket = new HttpListener();
private Dictionary<string, Action<WebServerRequest, WebServerResponse>>[] ActionRoutes = new Dictionary<string, Action<WebServerRequest, WebServerResponse>>[2];
private string prefix;
public WebServer() : this(8080) { }
public WebServer(int port)
{
if (!HttpListener.IsSupported)
throw new NotSupportedException("Needs Windows XP SP2, Server 2003 or later.");
// URI prefixes are required, for example "http://localhost:8080/index/".
this.prefix = string.Format("http://*:{0}/", port);
httpSocket.Prefixes.Add(prefix);
}
public void get(string path, Action<WebServerRequest, WebServerResponse> method)
{
this.AddRoute(WebServerMethod.GET, path, method);
}
public void post(string url, Action<WebServerRequest, WebServerResponse> method)
{
this.AddRoute(WebServerMethod.POST, url, method);
}
public void Start()
{
httpSocket.Start();
Console.WriteLine(string.Format("Server listenting on {0}", this.prefix));
ThreadPool.QueueUserWorkItem((o) =>
{
try
{
while (httpSocket.IsListening)
{
ThreadPool.QueueUserWorkItem((context) =>
{
var httpContext = context as HttpListenerContext;
Console.WriteLine(string.Format("[IN] {0} {1} ({2})", httpContext.Request.HttpMethod, httpContext.Request.Url, httpContext.Request.ContentLength64));
Action<WebServerRequest, WebServerResponse> activeMethod = null;
switch (httpContext.Request.HttpMethod)
{
case "GET":
activeMethod = this.FindRouteFunction(WebServerMethod.GET, httpContext.Request.Url.AbsolutePath);
break;
case "POST":
activeMethod = this.FindRouteFunction(WebServerMethod.POST, httpContext.Request.Url.AbsolutePath);
break;
default:
activeMethod = MethodNotImplemented;
throw new Exception();
}
// Call the active method
activeMethod(new WebServerRequest(httpContext), new WebServerResponse(httpContext));
}, httpSocket.GetContext());
}
}
catch { } // suppress any exceptions
});
}
public void Stop()
{
httpSocket.Stop();
httpSocket.Close();
}
private void AddRoute(int httpMethod, string route, Action<WebServerRequest, WebServerResponse> method)
{
if (this.ActionRoutes[httpMethod] == null)
{
this.ActionRoutes[httpMethod] = new Dictionary<string, Action<WebServerRequest, WebServerResponse>>();
}
this.ActionRoutes[httpMethod].Add(route, method);
}
private Action<WebServerRequest, WebServerResponse> FindRouteFunction(int httpMethod, string uri)
{
Dictionary<string, Action<WebServerRequest, WebServerResponse>> _routes = this.ActionRoutes[httpMethod];
if (_routes.ContainsKey(uri))
return _routes[uri];
return NotFoundResponse;
}
private static void NotFoundResponse(WebServerRequest request, WebServerResponse response)
{
response.send(string.Format("<HTML><BODY><h1>404 URL Not Found!</h1><hr/><i>{0}</i></BODY></HTML>", request.getMethods().Url.OriginalString));
}
private static void MethodNotImplemented(WebServerRequest request, WebServerResponse response)
{
response.send(string.Format("<HTML><BODY><h1>401 Not Implemented!</h1><hr/><i>{0}</i></BODY></HTML>", request.getMethods().Url.OriginalString));
}
}
}