Skip to content

Instantly share code, notes, and snippets.

@teyc
Last active November 9, 2020 20:44
Show Gist options
  • Save teyc/20e0a35ac84b6e3f5fd2a7af42c939e0 to your computer and use it in GitHub Desktop.
Save teyc/20e0a35ac84b6e3f5fd2a7af42c939e0 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace MyDankyWebServer
{
public class DankyWebServer
{
UTF8Encoding utf8Encoding = new UTF8Encoding(false);
StringBuilder sb = new StringBuilder();
void WriteLog(string msg) {
sb.Append(msg);
}
string GetLog()
{
return sb.ToString();
}
public void Listen(int port, bool send100Continue)
{
var listener = new TcpListener(new IPAddress(new byte[] {127, 0, 0, 1}), port);
listener.Start();
new Thread(() => ServeOneRequest(listener.AcceptSocket(), send100Continue)).Start();
//listener.Stop();
}
private long ServeOneRequest(Socket socket, bool send100Continue)
{
var buffer = new byte[4096];
var sw = new Stopwatch();
var receivedBytes = socket.Receive(buffer);
var receivedString = utf8Encoding.GetString(buffer,0, receivedBytes);
var receivedHeaderMilliseconds = sw.ElapsedMilliseconds;
WriteLog($"\n{receivedHeaderMilliseconds} S: Received Header");
WriteLog(receivedString);
if (send100Continue) Send100Continue(socket, utf8Encoding);
do
{
receivedBytes = socket.Receive(buffer);
receivedString = utf8Encoding.GetString(buffer, 0, receivedBytes);
} while (receivedBytes == 0);
var timeToBodyReceivedMilliseconds = sw.ElapsedMilliseconds - receivedHeaderMilliseconds;
WriteLog($"\n{sw.ElapsedMilliseconds} S: Received Request Body " + receivedBytes + $" bytes {timeToBodyReceivedMilliseconds} ms");
WriteLog(receivedString);
WriteLog($"\n{sw.ElapsedMilliseconds} S: Sending Response");
socket.Send(utf8Encoding.GetBytes(@"HTTP/1.1 200 OK
Content-Type: application/json
"));
var response = JsonConvert.SerializeObject(new {
Success = true,
TimeToBodyReceivedMilliseconds = timeToBodyReceivedMilliseconds,
Logs = GetLog()
});
socket.Send(utf8Encoding.GetBytes(response));
WriteLog($"{sw.ElapsedMilliseconds} S: Sent Response");
WriteLog(receivedString);
socket.Close();
return timeToBodyReceivedMilliseconds;
}
private void Send100Continue(Socket socket, UTF8Encoding utf8Encoding)
{
var text = @"HTTP/1.1 100 Continue
";
socket.Send(utf8Encoding.GetBytes(text));
WriteLog("S: " + text);
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<PublishTrimmed>true</PublishTrimmed>
<PublishReadyToRun>true</PublishReadyToRun>
<PublishSingleFile>true</PublishSingleFile>
<CrossGenDuringPublish>false</CrossGenDuringPublish>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NewtonSoft.Json" Version="12.0.3" />
</ItemGroup>
</Project>
using System;
namespace MyDankyWebServer
{
class Program
{
static void Main(string[] args)
{
var ws = new DankyWebServer();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment