Esempio n. 1
0
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.HttpMethod.Equals("POST", StringComparison.OrdinalIgnoreCase))
            {
                context.Response.Clear();
                context.Response.ContentType = "application/json";

                try
                {
                    string practice = context.Request["Practice"];

                    if (practice != null)
                    {
                        string code = ReadFile(context.Request.Files["Code"]);
                        string output = ReadFile(context.Request.Files["Output"]);

                        var response = new InterviewService().Practice(new PracticeRequest
                        {
                            Code = code,
                            Output = output
                        });

                        var serializer = new DataContractJsonSerializer(typeof(PracticeResponse));
                        serializer.WriteObject(context.Response.OutputStream, response);
                    }
                    else
                    {
                        string token = context.Request["Token"];
                        string authToken = context.Request["AuthToken"];
                        string antiForgeryToken = context.Request["AntiForgeryToken"];
                        string attemptToken = context.Request["AttemptToken"];
                        string questionIdString = context.Request["QuestionID"];

                        Guid questionId;

                        if (!Guid.TryParse(questionIdString, out questionId))
                        {
                            throw new Exception("Invalid Question ID");
                        }

                        if (token != null)
                        {
                            string code = ReadFile(context.Request.Files["Code"]);
                            string output = ReadFile(context.Request.Files["Output"]);

                            var response = new InterviewService().SubmitAnswer(new SubmitAnswerRequest
                            {
                                Token = token,
                                ID = questionId,
                                Code = code,
                                Output = output,
                                AttemptToken = attemptToken
                            });

                            var serializer = new DataContractJsonSerializer(typeof(SubmitAnswerResponse));
                            serializer.WriteObject(context.Response.OutputStream, response);
                        }
                        else if (authToken != null)
                        {
                            string output = ReadFile(context.Request.Files["Output"]);

                            var response = new QuestionService().RunTests(new RunTestsRequest
                            {
                                AuthToken = authToken,
                                AntiForgeryToken = antiForgeryToken,
                                QuestionID = questionId,
                                Output = output
                            });

                            var serializer = new DataContractJsonSerializer(typeof(RunTestsResponse));
                            serializer.WriteObject(context.Response.OutputStream, response);
                        }
                    }
                }
                catch (Exception ex)
                {
                    var serializer = new DataContractJsonSerializer(typeof(Message));

                    string errorMessage;

                    if (ex is WebFaultException<string>)
                    {
                        errorMessage = (ex as WebFaultException<string>).Detail;
                    }
                    else
                    {
                        errorMessage = ex.Message;
                    }

                    serializer.WriteObject(context.Response.OutputStream, new Message { ErrorMessage = errorMessage });
                }
                finally
                {
                    context.Response.End();
                }
            }
        }
Esempio n. 2
0
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.HttpMethod.Equals("GET", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    string practice = context.Request["Practice"];

                    string input;

                    if (practice != null)
                    {
                        input = PracticeController.InputString;
                    }
                    else
                    {
                        string token = context.Request["Token"];
                        string authToken = context.Request["AuthToken"];
                        string attemptToken = context.Request["AttemptToken"];
                        string questionIdString = context.Request["QuestionID"];

                        Guid questionId;

                        if (!Guid.TryParse(questionIdString, out questionId))
                        {
                            throw new Exception("Invalid Question ID");
                        }

                        if (token != null)
                        {
                            var response = new InterviewService().DownloadInput(new Classes.InterviewService.DownloadInputRequest
                            {
                                Token = token,
                                QuestionID = questionId,
                                AttemptToken = attemptToken
                            });

                            input = response.Input;
                        }
                        else
                        {
                            var response = new QuestionService().DownloadInput(new DownloadInputRequest
                            {
                                AuthToken = authToken,
                                QuestionID = questionId
                            });

                            input = response.Input;
                        }
                    }

                    context.Response.Clear();
                    context.Response.AddHeader("content-disposition", "attachment;filename=input.txt");
                    context.Response.ContentType = "text/plain";
                    context.Response.Write(input);
                }
                catch (Exception)
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                }
                finally
                {
                    context.Response.End();
                }
            }
        }