Created
April 9, 2016 07:27
-
-
Save chartinger/69a8335e7542484a9e93f4006e14fd87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Text; | |
namespace ConsoleWebServer | |
{ | |
/** | |
* Based on https://msdn.microsoft.com/en-us/library/6y0e13d3%28v=vs.110%29.aspx | |
* | |
* A Simple webserver to demonstrate the HTTP protocol. | |
*/ | |
class Program | |
{ | |
// Incoming data from the client. | |
public static string data = null; | |
public static void StartListening() | |
{ | |
// Data buffer for incoming data. | |
byte[] bytes = new Byte[1024]; | |
// Establish the local endpoint for the socket. | |
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 11000); | |
// Create a TCP/IP socket. | |
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
// Bind the socket to the local endpoint and | |
// listen for incoming connections. | |
try | |
{ | |
listener.Bind(localEndPoint); | |
listener.Listen(10); | |
// Start listening for connections. | |
while (true) | |
{ | |
Console.WriteLine("Waiting for a connection..."); | |
// Program is suspended while waiting for an incoming connection. | |
Socket handler = listener.Accept(); | |
data = null; | |
// An incoming connection needs to be processed. | |
while (true) | |
{ | |
bytes = new byte[1024]; | |
int bytesRec = handler.Receive(bytes); | |
data += Encoding.ASCII.GetString(bytes, 0, bytesRec); | |
if (data.Length > 0) | |
{ | |
break; | |
} | |
} | |
// Show the data on the console. | |
Console.WriteLine("Text received:"); | |
Console.WriteLine("----------------------------"); | |
Console.WriteLine("{0}", data); | |
Console.WriteLine("----------------------------"); | |
string request_url = getRequestUrl(data); | |
string response = getResponseFor(request_url); | |
// Echo the data back to the client. | |
byte[] reponse_bytes = Encoding.ASCII.GetBytes(response); | |
handler.Send(reponse_bytes); | |
handler.Shutdown(SocketShutdown.Both); | |
handler.Close(); | |
} | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e.ToString()); | |
} | |
Console.WriteLine("\nPress ENTER to continue..."); | |
Console.Read(); | |
} | |
private static string getRequestUrl(string data) | |
{ | |
string request_url = ""; | |
using (StringReader sr = new StringReader(data)) | |
{ | |
string request_line; | |
while ((request_line = sr.ReadLine()) != null) | |
{ | |
if (request_line.StartsWith("GET")) | |
{ | |
var request_token = request_line.Split(new[] { ' ' }); | |
if (request_token.Length > 2) | |
{ | |
request_url = request_token[1]; | |
break; | |
} | |
} | |
} | |
} | |
return request_url; | |
} | |
private static string getResponseFor(string request_url) | |
{ | |
StringBuilder response = new StringBuilder(); | |
if (request_url.Equals("/")) | |
{ | |
response.AppendLine("HTTP/1.0 200 OK"); | |
response.AppendLine("Content-Type: text/html"); | |
response.AppendLine(); | |
response.AppendLine("<html><h1>YEY - Response from our ConsoleWebServer!</h1></html>"); | |
} | |
else if (request_url.StartsWith("/hello/")) | |
{ | |
string name = request_url.Split('/')[2]; | |
response.AppendLine("HTTP/1.0 200 OK"); | |
response.AppendLine("Content-Type: text/html"); | |
response.AppendLine(); | |
response.Append("<html><h1>Hello "); | |
response.Append(name); | |
response.AppendLine("</h1></html>"); | |
} | |
else if (request_url.StartsWith("/info.html")) | |
{ | |
string file_content = File.ReadAllText(@"../../info.html"); | |
response.AppendLine("HTTP/1.0 200 OK"); | |
response.AppendLine("Content-Type: text/html"); | |
response.AppendLine(); | |
response.Append(file_content); | |
} | |
else | |
{ | |
response.AppendLine("HTTP/1.0 404 Not Found"); | |
response.AppendLine("Content-Type: text/html"); | |
response.AppendLine(); | |
response.AppendLine("<html>Oh no! I can't find what you are looking for :(</html>"); | |
} | |
return response.ToString(); | |
} | |
static int Main(string[] args) | |
{ | |
StartListening(); | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment