Exemplo n.º 1
0
        private void OnStart(HttpListenerRequest req, HttpListenerResponse resp)
        {
            if (req.HttpMethod != "POST")
            {
                resp.Write405();
                return;
            }

            if (!req.HasEntityBody)
            {
                resp.Write400();
                return;
            }

            CnCStartRequest args;

            using (var ist = new StreamReader(req.InputStream))
            {
                var body = ist.ReadToEnd();
                args = body.FromJson<CnCStartRequest>();
            }
            if (args.job == Guid.Empty)
            {
                args.job = Guid.NewGuid();
            }

            if (args.job == Guid.Empty || jobs.ContainsKey(args.job))
            {
                resp.Write400();
                return;
            }

            if (args.type == "http")
            {
                if (args.http == null)
                {
                    resp.Write400();
                    return;
                }

                args.http.Tag = args.job;

                var http = new HttpLoader(args.http);
                jobs[args.job] = http;

                lock (jobQueue)
                {
                    jobQueue.Enqueue(http);
                }

                workerSignal.Set();

                //speculatively wait a few seconds, in case the job is done real quick like
                Thread.Sleep(2000);

                resp.WriteJson(new
                {
                    Location = "/status/" + args.job,
                    Data = new Result(http)
                }, http.Status == Status.Complete ? 200 : 202);

            }
            else
            {
                resp.Write400();
                return;
            }
        }