2015-08-21 21:14:02 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
using System.Collections.Specialized;
|
2015-08-21 21:14:02 -04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Kayak.Http;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
using Kayak;
|
2015-08-21 21:14:02 -04:00
|
|
|
|
|
2015-08-23 17:58:48 -04:00
|
|
|
|
|
2017-05-26 18:49:27 -04:00
|
|
|
|
namespace IW4MAdmin
|
|
|
|
|
{
|
2017-05-27 00:22:50 -04:00
|
|
|
|
class Scheduler : ISchedulerDelegate
|
2015-08-21 21:14:02 -04:00
|
|
|
|
{
|
|
|
|
|
public void OnException(IScheduler scheduler, Exception e)
|
|
|
|
|
{
|
2017-06-12 13:50:00 -04:00
|
|
|
|
// it looks like there's a library error in
|
|
|
|
|
// Kayak.Http.HttpServerTransactionDelegate.OnError
|
|
|
|
|
if (e.GetType() == typeof(NullReferenceException))
|
|
|
|
|
return;
|
|
|
|
|
|
2017-06-19 13:58:01 -04:00
|
|
|
|
ApplicationManager.GetInstance().Logger.WriteWarning("Web service has encountered an error - " + e.Message);
|
|
|
|
|
ApplicationManager.GetInstance().Logger.WriteDebug($"Stack Trace: {e.StackTrace}");
|
2017-05-30 17:23:31 -04:00
|
|
|
|
|
|
|
|
|
if (e.InnerException != null)
|
|
|
|
|
{
|
2017-06-19 13:58:01 -04:00
|
|
|
|
ApplicationManager.GetInstance().Logger.WriteDebug($"Inner Exception: {e.InnerException.Message}");
|
|
|
|
|
ApplicationManager.GetInstance().Logger.WriteDebug($"Inner Stack Trace: {e.InnerException.StackTrace}");
|
2017-05-30 17:23:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-21 21:14:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnStop(IScheduler scheduler)
|
|
|
|
|
{
|
2017-06-19 13:58:01 -04:00
|
|
|
|
ApplicationManager.GetInstance().Logger.WriteInfo("Web service has been stopped...");
|
2015-08-21 21:14:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-26 18:49:27 -04:00
|
|
|
|
class Request : IHttpRequestDelegate
|
2015-08-21 21:14:02 -04:00
|
|
|
|
{
|
2017-05-27 00:22:50 -04:00
|
|
|
|
public void OnRequest(HttpRequestHead request, IDataProducer requestBody, IHttpResponseDelegate response, string IP)
|
2015-08-21 21:14:02 -04:00
|
|
|
|
{
|
2017-05-26 18:49:27 -04:00
|
|
|
|
NameValueCollection querySet = new NameValueCollection();
|
2017-05-27 00:22:50 -04:00
|
|
|
|
|
2017-05-26 18:49:27 -04:00
|
|
|
|
if (request.QueryString != null)
|
2017-06-12 17:47:31 -04:00
|
|
|
|
querySet = System.Web.HttpUtility.ParseQueryString(SharedLibrary.Utilities.StripIllegalCharacters(request.QueryString));
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2017-05-27 00:22:50 -04:00
|
|
|
|
querySet.Set("IP", IP);
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2017-08-17 19:28:08 -04:00
|
|
|
|
try
|
2017-05-27 00:22:50 -04:00
|
|
|
|
{
|
2017-08-17 19:28:08 -04:00
|
|
|
|
SharedLibrary.HttpResponse requestedPage = WebService.GetPage(request.Path, querySet, request.Headers);
|
|
|
|
|
|
|
|
|
|
var headers = new HttpResponseHead()
|
|
|
|
|
{
|
|
|
|
|
Status = "200 OK",
|
|
|
|
|
Headers = new Dictionary<string, string>()
|
2015-08-21 21:14:02 -04:00
|
|
|
|
{
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{ "Content-Type", requestedPage.contentType },
|
|
|
|
|
{ "Content-Length", requestedPage.content.Length.ToString() },
|
|
|
|
|
{ "Access-Control-Allow-Origin", "*" },
|
2015-08-21 21:14:02 -04:00
|
|
|
|
}
|
2017-08-17 19:28:08 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
foreach (var key in requestedPage.additionalHeaders.Keys)
|
|
|
|
|
headers.Headers.Add(key, requestedPage.additionalHeaders[key]);
|
2015-08-21 21:14:02 -04:00
|
|
|
|
|
2017-08-17 19:28:08 -04:00
|
|
|
|
response.OnResponse(headers, new BufferedProducer(requestedPage.content));
|
|
|
|
|
}
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2017-08-17 19:28:08 -04:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
ApplicationManager.GetInstance().Logger.WriteError($"Webfront error during request: {e.Message}");
|
|
|
|
|
response.OnResponse(new HttpResponseHead()
|
|
|
|
|
{
|
|
|
|
|
Status = "500 Internal Server Error",
|
|
|
|
|
Headers = new Dictionary<string, string>()
|
|
|
|
|
{
|
|
|
|
|
{ "Content-Type", "text/html" },
|
|
|
|
|
{ "Content-Length", "0"},
|
|
|
|
|
}
|
|
|
|
|
}, new BufferedProducer(""));
|
|
|
|
|
}
|
2015-08-21 21:14:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class BufferedProducer : IDataProducer
|
|
|
|
|
{
|
|
|
|
|
ArraySegment<byte> data;
|
|
|
|
|
|
|
|
|
|
public BufferedProducer(string data) : this(data, Encoding.UTF8) { }
|
|
|
|
|
public BufferedProducer(string data, Encoding encoding) : this(encoding.GetBytes(data)) { }
|
|
|
|
|
public BufferedProducer(byte[] data) : this(new ArraySegment<byte>(data)) { }
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2015-08-21 21:14:02 -04:00
|
|
|
|
public BufferedProducer(ArraySegment<byte> data)
|
|
|
|
|
{
|
|
|
|
|
this.data = data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IDisposable Connect(IDataConsumer channel)
|
|
|
|
|
{
|
2017-06-12 08:28:08 -04:00
|
|
|
|
channel?.OnData(data, null);
|
|
|
|
|
channel?.OnEnd();
|
2015-08-21 21:14:02 -04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class BufferedConsumer : IDataConsumer
|
|
|
|
|
{
|
|
|
|
|
List<ArraySegment<byte>> buffer = new List<ArraySegment<byte>>();
|
|
|
|
|
Action<string> resultCallback;
|
|
|
|
|
Action<Exception> errorCallback;
|
|
|
|
|
|
|
|
|
|
public BufferedConsumer(Action<string> resultCallback, Action<Exception> errorCallback)
|
|
|
|
|
{
|
|
|
|
|
this.resultCallback = resultCallback;
|
|
|
|
|
this.errorCallback = errorCallback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool OnData(ArraySegment<byte> data, Action continuation)
|
|
|
|
|
{
|
2017-06-12 08:28:08 -04:00
|
|
|
|
buffer?.Add(data);
|
2015-08-21 21:14:02 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnError(Exception error)
|
|
|
|
|
{
|
2017-06-06 23:45:21 -04:00
|
|
|
|
errorCallback?.Invoke(error);
|
2015-08-21 21:14:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnEnd()
|
|
|
|
|
{
|
|
|
|
|
var str = buffer
|
|
|
|
|
.Select(b => Encoding.UTF8.GetString(b.Array, b.Offset, b.Count))
|
|
|
|
|
.Aggregate((result, next) => result + next);
|
|
|
|
|
|
|
|
|
|
resultCallback(str);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|