Esempio n. 1
0
        private Task Pull(HttpContext arg)
        {
            try
            {
                var req = arg.PostData <CowidPlusPullRequest>();

                arg.Response.SendChunk("{");

                var items = _dbs[req.N].GetBatch(req.Items);
                var last  = items.Length - 1;

                for (var i = 0; i < items.Length; i++)
                {
                    arg.Response
                    .SendChunk($"\"{HttpUtility.JavaScriptStringEncode(items[i].Key)}\":{(string.IsNullOrWhiteSpace(items[i].Value) ? "null" : items[i].Value)}}}");
                    if (i < last)
                    {
                        arg.Response.SendChunk(",");
                    }
                }

                return(arg.Response.SendFinalChunk("}"));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);

                return(arg.Response.Send(HttpStatusCode.InternalServerError));
            }
        }
Esempio n. 2
0
        private Task Update(HttpContext arg)
        {
            try
            {
                var req = arg.PostData <CowidPlusUpdateRequest>();

                using (var sha = SHA512.Create())
                {
                    var dataHash = Convert.ToBase64String(sha.ComputeHash(Encoding.ASCII.GetBytes(req.Data)));

                    var token = $"{dataHash}-{_lastUpdateToken}-{_secureUpdateToken}";
                    if (req.SessionKey != Convert.ToBase64String(sha.ComputeHash(Encoding.ASCII.GetBytes(token))))
                    {
                        return(arg.Response.Send(HttpStatusCode.Forbidden));
                    }
                }
                _lastUpdateToken = Guid.NewGuid().ToString("N");

                Database.SetupElasticIndex(ref _es);

                Database.InsertFromWebRequest(req.Data, new DateTime(req.Year, req.Month, req.Day), _es, _dbs);

                return(arg.Response.Send(HttpStatusCode.OK));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);

                return(arg.Response.Send(HttpStatusCode.InternalServerError));
            }
        }
Esempio n. 3
0
        private Task Down(HttpContext arg)
        {
            try
            {
                if (arg.Request.ContentLength > _maxPostSize)
                {
                    return(arg.Response.Send(HttpStatusCode.RequestUriTooLong));
                }

                var request = arg.PostData <CowidPlusDownRequest>();
                foreach (var e in _exporter)
                {
                    if (e.Id == request.Format.ToUpper())
                    {
                        var db = _dbs[request.N];
                        return(e.Convert(arg, ref db, request.N, request.Requests));
                    }
                }
                return(arg.Response.Send(HttpStatusCode.NotFound));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);

                return(arg.Response.Send(HttpStatusCode.InternalServerError));
            }
        }
Esempio n. 4
0
        private Task FindCall(HttpContext arg, int max)
        {
            try
            {
                var post = arg.PostDataAsString;
                if (_enableLogging && arg.Request.Headers.ContainsKey("sessionKey"))
                {
                    var dir = $"log/{arg.Request.Headers["sessionKey"]}/";
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    File.WriteAllText($"{dir}{Directory.GetFiles(dir).Length:D4}.json", post, Encoding.UTF8);
                }

                Console.WriteLine(post);
                var req = JsonConvert.DeserializeObject <CowidPlusFindRequest>(post);

                var subq = new List <QueryContainer>
                {
                    new TermQuery
                    {
                        Field = "n",
                        Value = req.N
                    }
                };
                // ReSharper disable once LoopCanBeConvertedToQuery
                foreach (var x in req.Items)
                {
                    subq.Add(new QueryStringQuery
                    {
                        Query  = x.Query,
                        Fields = x.Fields
                    });
                }

                var container = new QueryContainer(new BoolQuery {
                    Must = subq
                });

                var items = _es.Search <EsEntry>(s => s.Query(q => container).Source(src => src.Includes(i => i.Field("key")))
                                                 .Size(max));
                Console.WriteLine(items.Hits.Count);
                return(arg.Response.Send(new CowidPlusPullRequest
                {
                    N = req.N,
                    Items = items.Hits.Select(x => x.Source.Key).ToArray()
                }));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);

                return(arg.Response.Send(HttpStatusCode.InternalServerError));
            }
        }
Esempio n. 5
0
        private Task Init(HttpContext arg)
        {
            try
            {
                return(arg.Response.Send(Guid.NewGuid().ToString("N")));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);

                return(arg.Response.Send(HttpStatusCode.InternalServerError));
            }
        }
Esempio n. 6
0
        private Task Norm(HttpContext arg)
        {
            try
            {
                var stb = new StringBuilder();
                stb.Append("[");
                for (byte i = 1; i <= _n; i++)
                {
                    stb.Append($"{_dbs[i].Get($">{i}")}}}{(i != _n ? "," : "")}");
                }
                stb.Append("]");
                return(arg.Response.Send(stb.ToString()));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);

                return(arg.Response.Send(HttpStatusCode.InternalServerError));
            }
        }
Esempio n. 7
0
 private Task Find(HttpContext arg)
 {
     return(FindCall(arg, 10000));
 }