<commit_before>﻿using Grapevine.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace GameState4Net
{
    /// <summary>
    /// POST request handler which receives all the post requests which
    /// were send to the gamestate server
    /// </summary>
    public sealed class PostRequestHandler : RESTResource
    {
        [RESTRoute(Method = Grapevine.HttpMethod.POST)]
        public void HandleAllPostsRequests(HttpListenerContext context)
        {
            var request = context.Request;

            // Get the server and the content
            var server = (Server as GameStateServer);
            var content = request.GetContent();

            if (!string.IsNullOrEmpty(content))
            {
                server.AddGameState(content);
            }

            this.SendTextResponse(context, "Success");
        }<commit_msg>Improved POST request handling<commit_after>﻿using Grapevine;
using Grapevine.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace GameState4Net
{
    /// <summary>
    /// POST request handler which receives all the post requests which
    /// were send to the gamestate server
    /// </summary>
    public sealed class PostRequestHandler : RESTResource
    {
        [RESTRoute(Method = Grapevine.HttpMethod.POST)]
        public void HandleAllPostRequests(HttpListenerContext context)
        {
            var request = context.Request;

            // Get the server and the content
            var server = (Server as GameStateServer);
            var content = request.GetContent();

            // Make sure this handler is used by a gamestate server only
            if(server == null)
            {
                InternalServerError(context);
            }

            if (!string.IsNullOrEmpty(content))
            {   
                // At this point the content could still be no valid json/gamestate
                // so actually we should return BadRequest...well but we don't :x


                server.AddGameState(content);

                this.SendTextResponse(context, "Success");
            }
            else
            {
                // If no content was sent, it is a bad request
                BadRequest(context);
            }
        }

        private void BadRequest(HttpListenerContext context, string payload = "<h1>Bad Request</h1>", ContentType contentType = ContentType.HTML)
        {
            var buffer = Encoding.UTF8.GetBytes(payload);
            var length = buffer.Length;

            context.Response.StatusCode = 400;
            context.Response.StatusDescription = "Bad Request";
            context.Response.ContentType = ContentType.HTML.ToValue();

            FlushResponse(context, buffer, length);
        }
{'fraction_non_alphanumeric': 0.08341880341880342, 'fraction_numerical': 0.002735042735042735, 'mean_word_length': 2.0352697095435683, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7372476', 'n_tokens_mistral': 735, 'n_tokens_neox': 706, 'n_words': 213}