protected override async Task ProcessRequestCoreAsync(HttpContext ctx, string viewer, string world, long startTime)
{
var req = ctx.Request;
var resp = ctx.Response;
if(!req.IsAuthenticated) {
resp.StatusCode = 403;
return;
}
var currentTs = Utils.UnixTimestamp.CurrentMillisecondTimestamp;
var start2Ts = start2Timestamp;
if((currentTs - startTime < Start2ValidityPeriod) && (currentTs - start2Ts > Start2ValidityPeriod)) {
string referer = null;
if(req.Headers["Referer"] != null) {
var hdrLine = req.Headers["Referer"];
var schema = hdrLine.IndexOf("//") + 2;
var kcsUrl = hdrLine.IndexOf("/kcs", schema);
if(kcsUrl > 0) {
referer = world + hdrLine.Substring(kcsUrl + 1);
}
}
var proxyResponse = await Utils.Forwarder.ForwardRequest(req, world + "kcsapi/api_start2", referer);
if(proxyResponse != null) {
using(proxyResponse) {
if(proxyResponse.StatusCode == HttpStatusCode.OK) {
string newStart2;
using(StreamReader rdr = new StreamReader(proxyResponse.GetResponseStream())) {
newStart2 = (await rdr.ReadToEndAsync());
}
JsonReader json = new JsonTextReader(new StringReader(newStart2.Substring(7)));
while(json.Read()) {
if(json.TokenType == JsonToken.PropertyName && json.Value.ToString() == "api_result") {
if(json.ReadAsInt32() == 1) {
if(System.Threading.Interlocked.CompareExchange(ref start2Timestamp, currentTs, start2Ts) == start2Ts) {
start2Content = newStart2;
File.WriteAllText(ctx.Server.MapPath("~/App_Data/api_start2.json"), start2Content);
}
}
break;
}
}
}
}
}
}
if(start2Content == null) {
ctx.Response.StatusCode = 503;
return;
}
ctx.Response.ContentType = "text/plain";
ctx.Response.Write(start2Content);
return;
}