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-05-27 00:22:50 -04:00
|
|
|
|
Manager.GetInstance().Logger.Write("Web service has encountered an error - " + e.Message);
|
2015-08-21 21:14:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnStop(IScheduler scheduler)
|
|
|
|
|
{
|
2017-05-26 18:49:27 -04:00
|
|
|
|
Manager.GetInstance().Logger.Write("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-27 00:22:50 -04:00
|
|
|
|
// Program.getManager().mainLog.Write("HTTP request received", SharedLibrary.Log.Level.Debug);
|
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)
|
|
|
|
|
querySet = System.Web.HttpUtility.ParseQueryString(SharedLibrary.Utilities.removeNastyChars(request.QueryString));
|
|
|
|
|
|
2017-05-27 00:22:50 -04:00
|
|
|
|
querySet.Set("IP", IP);
|
2017-05-26 18:49:27 -04:00
|
|
|
|
SharedLibrary.HttpResponse requestedPage = WebService.getPage(request.Path, querySet, request.Headers);
|
|
|
|
|
|
2015-08-21 21:14:02 -04:00
|
|
|
|
var headers = new HttpResponseHead()
|
2017-05-27 00:22:50 -04:00
|
|
|
|
{
|
|
|
|
|
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-05-27 00:22:50 -04:00
|
|
|
|
};
|
2015-08-21 21:14:02 -04:00
|
|
|
|
|
2017-05-26 18:49:27 -04:00
|
|
|
|
foreach (var key in requestedPage.additionalHeaders.Keys)
|
|
|
|
|
headers.Headers.Add(key, requestedPage.additionalHeaders[key]);
|
|
|
|
|
|
|
|
|
|
response.OnResponse(headers, new BufferedProducer(requestedPage.content));
|
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)
|
|
|
|
|
{
|
|
|
|
|
channel.OnData(data, null);
|
|
|
|
|
channel.OnEnd();
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
buffer.Add(data);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnError(Exception error)
|
|
|
|
|
{
|
|
|
|
|
errorCallback(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|