ln.http/HTTPServer.cs

169 lines
4.9 KiB
C#
Raw Normal View History

2019-02-14 09:14:50 +01:00
using System;
using System.Collections.Generic;
2019-03-13 14:17:46 +01:00
using ln.logging;
using ln.types.threads;
using ln.types;
2019-11-04 10:00:33 +01:00
using ln.http.listener;
using ln.http.connections;
using ln.types.net;
using ln.http.cert;
using System.Globalization;
2019-11-04 10:00:33 +01:00
using System.Net.Sockets;
2019-11-15 13:47:01 +01:00
using ln.http.session;
2019-11-26 12:20:50 +01:00
using ln.http.exceptions;
2019-02-14 09:14:50 +01:00
namespace ln.http
{
public class HTTPServer
{
public static int backlog = 5;
public static int defaultPort = 8080;
public static bool exclusivePortListener = false;
2019-11-26 12:20:50 +01:00
public HttpRouter Router { get; set; }
2019-03-13 14:17:46 +01:00
2019-11-04 10:00:33 +01:00
public bool IsRunning => !shutdown && (threadPool.CurrentPoolSize > 0);
public Logger Logger { get; set; }
2019-11-26 12:20:50 +01:00
2019-02-14 09:14:50 +01:00
bool shutdown = false;
2019-11-04 10:00:33 +01:00
List<Listener> listeners = new List<Listener>();
public Listener[] Listeners => listeners.ToArray();
DynamicPool threadPool;
public DynamicPool ThreadPool => threadPool;
2019-02-26 22:00:37 +01:00
2019-02-14 09:14:50 +01:00
public HTTPServer()
{
Logger = Logger.Default;
threadPool = new DynamicPool(1024);
2019-02-14 09:14:50 +01:00
}
2019-11-26 12:20:50 +01:00
public HTTPServer(HttpRouter router)
: this()
{
Router = router;
}
public HTTPServer(Listener listener, HttpRouter router)
: this(router)
{
AddListener(listener);
}
public HTTPServer(Endpoint endpoint, HttpRouter router)
: this(new HttpListener(endpoint), router) { }
2019-02-14 09:14:50 +01:00
2019-11-04 10:00:33 +01:00
public void AddListener(Listener listener)
2019-02-14 09:14:50 +01:00
{
2019-11-04 10:00:33 +01:00
listeners.Add(listener);
if (IsRunning)
StartListener(listener);
}
2019-02-14 09:14:50 +01:00
2019-11-04 10:00:33 +01:00
public void StartListener(Listener listener)
{
listener.Open();
threadPool.Enqueue(
() => listener.AcceptMany(
(connection) => threadPool.Enqueue(
() => this.HandleConnection(connection)
)
)
);
2019-02-14 09:14:50 +01:00
}
2019-11-04 10:00:33 +01:00
public void StopListener(Listener listener)
2019-02-14 09:14:50 +01:00
{
2019-11-04 10:00:33 +01:00
listener.Close();
}
public void AddEndpoint(Endpoint endpoint)
{
AddListener(new HttpListener(endpoint));
2019-02-14 09:14:50 +01:00
}
public void Start()
{
threadPool.Start();
2019-11-04 10:00:33 +01:00
foreach (Listener listener in listeners)
StartListener(listener);
2019-02-14 09:14:50 +01:00
}
public void Stop()
{
lock (this)
{
this.shutdown = true;
2019-03-14 08:35:54 +01:00
}
2019-11-04 10:00:33 +01:00
foreach (Listener listener in listeners)
StopListener(listener);
2019-03-13 14:17:46 +01:00
2019-11-04 10:00:33 +01:00
threadPool.Stop(true);
2019-02-14 09:14:50 +01:00
}
2019-11-04 10:00:33 +01:00
private void HandleConnection(Connection connection)
2019-02-14 09:14:50 +01:00
{
2019-11-04 10:00:33 +01:00
HttpRequest httpRequest = null;
bool keepAlive = true;
try
2019-02-14 09:14:50 +01:00
{
2019-11-04 10:00:33 +01:00
do
2019-02-14 09:14:50 +01:00
{
2019-11-04 10:00:33 +01:00
httpRequest = connection.ReadRequest(this);
if (httpRequest == null)
break;
2019-03-13 14:17:46 +01:00
2019-11-26 12:20:50 +01:00
HttpResponse response;
try
{
response = Router.Route(httpRequest);
} catch (HttpException httpExc)
{
response = new HttpResponse(httpRequest);
response.StatusCode = httpExc.StatusCode;
response.StatusMessage = httpExc.Message;
response.ContentWriter.WriteLine(httpExc.Message);
}
2019-11-04 10:00:33 +01:00
2019-11-15 13:47:01 +01:00
if (response != null)
{
keepAlive = httpRequest.GetRequestHeader("connection", "keep-alive").Equals("keep-alive") && response.GetHeader("connection", "keep-alive").Equals("keep-alive");
response.SetHeader("connection", keepAlive ? "keep-alive" : "close");
connection.SendResponse(response);
}
else
{
keepAlive = false;
}
2019-11-04 10:00:33 +01:00
} while (keepAlive);
2019-08-03 12:56:33 +02:00
}
catch (Exception e)
2019-03-13 14:17:46 +01:00
{
Logging.Log(e);
2019-11-04 10:00:33 +01:00
if (httpRequest != null)
{
HttpResponse httpResponse = new HttpResponse(httpRequest);
httpResponse.StatusCode = 500;
httpResponse.ContentWriter.WriteLine("500 Internal Server Error");
2019-11-26 12:20:50 +01:00
httpResponse.ContentWriter.Flush();
2019-03-13 14:17:46 +01:00
2019-11-04 10:00:33 +01:00
connection.SendResponse(httpResponse);
}
2019-02-14 09:14:50 +01:00
}
2019-11-15 13:47:01 +01:00
HttpRequest.ClearCurrent();
2019-11-04 10:00:33 +01:00
connection.GetStream().Close();
2019-08-03 12:56:33 +02:00
}
2019-02-26 22:00:37 +01:00
public void Log(DateTime startTime,double duration,HttpRequest httpRequest,HttpResponse httpResponse)
{
Logger.Log(LogLevel.INFO, "{0} {1} {2} {3}",startTime.ToString("yyyyMMdd-HH:mm:ss"),duration.ToString(CultureInfo.InvariantCulture),httpRequest.Hostname,httpRequest.RequestURL);
}
2019-02-14 09:14:50 +01:00
}
}