-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupersimpleserver.cs
More file actions
122 lines (104 loc) · 4.72 KB
/
supersimpleserver.cs
File metadata and controls
122 lines (104 loc) · 4.72 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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
namespace SuperSimpleServer
{
class Program
{
// Page Builder defines how the file is "read" and "returned" to the browser/requestor
static void pageBuilder(string page, HttpListenerResponse res)
{
byte[] buffer = Encoding.UTF8.GetBytes(page);
res.ContentLength64 = buffer.Length;
Stream st = res.OutputStream;
st.Write(buffer, 0, buffer.Length);
}
// Main - collection for CL arguements that will NOT be analyzed.
static void Main(string[] args)
{
// Check if HttpListener is NOT supported
if (!HttpListener.IsSupported)
{
Console.WriteLine("\nWindows XP SP2 or Server 2003 is required to use this Http Listener.");
return;
}
// Get domain input
Console.WriteLine("Enter domain to listen at: ");
string domain = Console.ReadLine();
// Get port input
Console.WriteLine("\nEnter port to listen at: ");
string port = Console.ReadLine();
// Define address to listen at
string prefix = "http://" + domain + ":" + port + "/";
HttpListener server = new HttpListener();
server.Prefixes.Add(prefix);
// Start listener
try
{
server.Start();
Console.WriteLine("\nSimple Listener is listening at " + prefix);
// Handle requests
while (true)
{
// Set "context" for listening
HttpListenerContext context = server.GetContext();
// Set "response" for serving
HttpListenerResponse response = context.Response;
// Defining local directory files outside of request
string localDir = Directory.GetCurrentDirectory();
string localFile = context.Request.Url.LocalPath;
string file = localDir + localFile + ".html";
// Successful Response (server)
try
{
// Read static file into string
TextReader tr = new StreamReader(file);
string msg = tr.ReadToEnd();
// Build and send page
Program.pageBuilder(msg, response);
// Log success
Console.WriteLine("\nSuccess! 200 - OK");
// Log sucess message
Console.WriteLine("Page for " + localFile + " done got served!");
}
// Handle File Not Found exceptions
catch (FileNotFoundException)
{
string notFound = "<html><header></header><body><h1>404! Page Not Found</h1><p>The page you requested could not be found</p></body></html>";
Program.pageBuilder(notFound, response);
// Log error
Console.WriteLine("\nError! 404 - Not Found");
// Log error message
Console.WriteLine("Request not fullfilled as the file requested (" + localFile + ") was not found");
}
// Handle any other exceptions
catch (Exception e)
{
string badReq = "<html><body><h1>400! Bad Request</h1><p>The server didn't recognize your request, sorry!</p></body></html>";
Program.pageBuilder(badReq, response);
// Log error
Console.WriteLine("\nError! 400 - Bad Request");
// Log error message
Console.WriteLine("Page could not be loaded. Server returned the following:\n" + e);
}
// For having Super Simple Server listener stop listener after a single request.
// context.Response.Close();
// Console.WriteLine("Simple Listener shutting down");
// break;
}
}
// Handle exceptions arising from starting listener
catch (Exception e)
{
// Log error
Console.WriteLine("\nError!");
// Log error message
Console.WriteLine("Listener could not be started at " + prefix + ". The following error was returned:\n" + e);
}
}
}
}