-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
121 lines (101 loc) · 5.57 KB
/
Copy pathProgram.cs
File metadata and controls
121 lines (101 loc) · 5.57 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
using Serverito;
using System;
namespace ServeritoTest
{
class Program
{
/// <summary>
/// 'Main' to quickly test the lib by building it as console app.
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
{
// create serverito
var host = "http://localhost:8000/";
ServeritoListener server = new ServeritoListener(host);
// hold everything we print to console
System.Text.StringBuilder consoleString = new System.Text.StringBuilder();
// function to dump line to console + store it in consoleString so we can retrieve it view the webpage.
System.Action<string> PrintConsoleLine = (string line) =>
{
// special case: ignore requests to fetch console
if (line.EndsWith("/console/"))
return;
// add to console string and write to console
consoleString.Append(line);
consoleString.Append('\n');
Console.WriteLine(line);
};
// add test print for all callbacks
server.OnException += (ServeritoContext context, Exception exception) => { PrintConsoleLine("Got Exception! " + exception.ToString()); };
server.OnFinishedProcessingView += (ServeritoContext context) => { PrintConsoleLine("Finished processing view: " + context.Context.Request.RawUrl); };
server.OnFinishHandlingRequest += (ServeritoContext context) => { PrintConsoleLine("Finished handling request: " + context.Context.Request.RawUrl); };
server.OnNewRawRequest += (ServeritoContext context) => { PrintConsoleLine("\nNEW RAW REQUEST: " + context.Context.Request.RawUrl); };
server.OnPassingRequestToView += (ServeritoContext context) => { PrintConsoleLine("Before passing to view: " + context.Context.Request.RawUrl); };
server.OnServingFile += (ServeritoContext context) => { PrintConsoleLine("Serving file: '" + context.Context.Request.RawUrl + "' with content-type: " + context.Context.Response.ContentType); };
server.OnUndefinedURL += (ServeritoContext context) => { PrintConsoleLine("Undefined URL: " + context.Context.Request.RawUrl); };
server.OnMissingFile += (ServeritoContext context) => { PrintConsoleLine("Missing file: " + context.Context.Request.RawUrl); };
server.OnUrlMatching += (ServeritoContext context) => { PrintConsoleLine("URL matching: " + context.Context.Request.RawUrl); };
// on undefined URL or missing file dump "404 not found!" on screen.
server.OnMissingFile += server.OnUndefinedURL += (ServeritoContext context) =>
{
Utils.WriteToResponse(context.Context, "404 not found!");
};
// add static files
server.StaticFilesRootUrl = "/static/";
server.StaticFilesPath = "../../test_static_files";
// dump all errors to response
Utils.DumpExceptionsToResponse(server);
// force trailing slash
Utils.ForceTrailingSlash(server);
// add some test views
// root (eg '/') serve a testing html file that shows interesting links
server.AddView(new URL("/"), (ServeritoContext context) =>
{
server.ServeHtmlPage(context, "index.html");
});
// /hello/ just write "hello world"
server.AddView(new URL("/hello/"), (ServeritoContext context) =>
{
Utils.WriteToResponse(context.Context, "Hello World!");
});
// /main/ serve a testing html file
server.AddView(new URL("/index/"), (ServeritoContext context) =>
{
server.ServeHtmlPage(context, "test.html");
});
// /exp/ will throw exception
server.AddView(new URL("/exp/"), (ServeritoContext context) =>
{
throw new Exception("This is a test.");
});
// /anynumber/(+d)/ just check regex urls
server.AddView(new URL(@"/anynumber/\d+/", matchType: UrlMatchingType.RegEx), (ServeritoContext context) =>
{
Utils.WriteToResponse(context.Context, "It Works!");
});
// /console/ return the content of the console string
server.AddView(new URL("/console/"), (ServeritoContext context) =>
{
Utils.WriteToResponse(context.Context, consoleString.ToString());
});
// /echo/ return the input we got from POST requests
server.AddView(new URL("/echo/", HttpMethods.POST), (ServeritoContext context) =>
{
var ret = Utils.ReadRequestInput(context.Context);
Utils.WriteToResponse(context.Context, ret);
});
// /kill/ will stop listening and finish app.
server.AddView(new URL("/kill/"), (ServeritoContext context) =>
{
server.Stop();
});
// open test page in default browser
System.Diagnostics.Process.Start(host);
// start listening
server.Start();
}
}
}
}