/
JustServe.cs
284 lines (247 loc) · 9.37 KB
/
JustServe.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
namespace just_serve
{
public class JustServe
{
public const string SERVERNAME = "justserve 1.0";
Dictionary<string, string> MimeTypes = new Dictionary<string, string>()
{
{ "atom", "application/atom+xml" },
{ "json", "application/json" },
{ "js", "application/javascript" },
{ "xhtml", "application/xhtml+xml" },
{ "gif", "image/gif" },
{ "jpeg", "image/jpeg" },
{ "jpg", "image/jpeg" },
{ "png", "image/png" },
{ "svg", "image/svg+xml" },
{ "tiff", "image/tiff" },
{ "css", "text/css" },
{ "htm", "text/html; charset=utf-8" },
{ "html", "text/html; charset=utf-8" },
{ "xml", "text/xml" },
};
public delegate void Callback(Request request, Response response);
#region Private variables
private List<Responder> responders = new List<Responder>();
private TcpListener listener;
private Callback NotFoundCallBack;
#endregion
public string LocalPath { get; set; }
public JustServe(string LocalPath)
{
this.LocalPath = LocalPath.TrimEnd('\\', '/');
NotFoundCallBack = (req, resp) =>
{
resp.End("404 Not Found");
};
}
public void Get(string expr, Callback callback)
{
responders.Add(new Responder(Verb.GET, expr, callback));
}
public void Post(string expr, Callback callback)
{
responders.Add(new Responder(Verb.POST, expr, callback));
}
public void Put(string expr, Callback callback)
{
responders.Add(new Responder(Verb.PUT, expr, callback));
}
public void Delete(string expr, Callback callback)
{
responders.Add(new Responder(Verb.DELETE, expr, callback));
}
public void NotFound(Callback Callback)
{
NotFoundCallBack = Callback;
}
public void Listen(string Address, int Port)
{
if (Address.ToLower() == "localhost") { Address = "127.0.0.1"; }
listener = new TcpListener(IPAddress.Parse(Address), Port);
listener.Start();
new Thread(() =>
{
while (true)
{
using (Socket socket = listener.AcceptSocket())
{
if (socket.Connected)
{
byte[] bytes = new byte[1024];
int received = socket.Receive(bytes, bytes.Length, 0);
string buffer = Encoding.UTF8.GetString(bytes);
string[] sections = buffer.Split(System.Environment.NewLine.ToCharArray());
Request req = new Request();
for (int i = 0; i < sections.Length; i++)
{
string section = sections[i].Trim();
Match matchKey = Regex.Match(section, "^(.+?)(?=([ :]))");
if (matchKey.Success)
{
string key = matchKey.Value;
string value = section.Replace(key, "").TrimStart(' ', ':');
SetRequestHeader(key, value, ref req);
}
}
// Find the callback.
Responder responder = responders.FirstOrDefault(r => r.Verb == req.Verb && Regex.IsMatch(req.Path, r.Expression));
if (!responder.IsEmpty())
responder.Callback(req, new Response(socket, 200));
else if (FileExists(req.Path))
ServeFile(req, new Response(socket, 200));
else
NotFoundCallBack(req, new Response(socket, 404));
}
}
}
}).Start();
}
public void ServeFile(string RelativePath, Request request, Response response)
{
string FullPath = LocalPath + RelativePath.Replace('/', '\\');
FileStream fs = new FileStream(FullPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(FullPath).Length;
byte[] buffer = br.ReadBytes((int)numBytes);
string ext = FullPath.Substring(FullPath.LastIndexOf('.') + 1);
response.ContentType = MimeTypes.FirstOrDefault(a => a.Key == ext).Value;
response.End(buffer);
}
private void ServeFile(Request request, Response response)
{
ServeFile(request.Path, request, response);
}
private bool FileExists(string Path)
{
Path = LocalPath + Path.Replace('/', '\\');
return File.Exists(Path);
}
private void SetRequestHeader(string key, string value, ref Request req)
{
switch(key.ToLower())
{
case "get":
req.Verb = Verb.GET;
goto Path;
case "post":
req.Verb = Verb.POST;
goto Path;
case "put":
req.Verb = Verb.PUT;
goto Path;
case "delete":
req.Verb = Verb.DELETE;
goto Path;
case "accept":
req.Accept = value;
return;
case "accept-language":
req.AcceptLanguage = value;
return;
case "user-agent":
req.UserAgent = value;
return;
case "accept-encoding":
req.AcceptEncoding = value;
return;
case "host":
req.Host = value;
return;
case "connection":
req.Connection = value;
return;
case "accept-charset":
req.AcceptCharset = value;
return;
default: return;
}
Path:
string[] vals = value.Split(' ');
req.Path = vals.Length > 0 ? vals[0] : value;
}
private struct Responder
{
public Callback Callback;
public string Expression;
public Verb Verb;
public Responder(Verb verb, string expression, Callback callback)
{
Verb = verb;
Expression = expression;
Callback = callback;
}
public bool IsEmpty() { return String.IsNullOrEmpty(Expression); }
}
}
public enum Verb
{
GET,
POST,
PUT,
DELETE
}
public class Request
{
public Verb Verb { get; set; }
public string Path { get; set; }
public string Accept { get; set; }
public string AcceptLanguage { get; set; }
public string UserAgent { get; set; }
public string AcceptEncoding { get; set; }
public string Host { get; set; }
public string Connection { get; set; }
public string AcceptCharset { get; set; }
}
public class Response
{
private Socket socket { get; set; }
public string ServerName { get; set; }
public byte[] Body { get; set; }
public int StatusCode { get; set; }
public string HttpVersion { get; set; }
public string ContentType { get; set; }
public Response()
{
ServerName = JustServe.SERVERNAME;
HttpVersion = "HTTP/1.1";
}
public Response(Socket Socket) : this()
{
this.socket = Socket;
}
public Response(Socket Socket, int StatusCode) : this(Socket)
{
this.StatusCode = StatusCode;
}
public void End(string body)
{
if (String.IsNullOrEmpty(ContentType))
ContentType = Regex.IsMatch(body, "<(.)+>") ? "text/html" : "text/plain";
Body = Encoding.UTF8.GetBytes(body);
End(Body);
}
public void End(byte[] buffer)
{
if (String.IsNullOrEmpty(ContentType))
ContentType = "text/html";
string header = "{0}{1}\r\n"
+ "Server: {2}\r\n"
+ "Content-Type: {3}\r\n"
+ "Accept-Ranges: bytes\r\n"
+ "Content-Length: {4}\r\n\r\n";
header = String.Format(header, HttpVersion, StatusCode, ServerName, ContentType, buffer.Length);
socket.Send(Encoding.UTF8.GetBytes(header));
socket.Send(buffer);
}
}
}