<commit_before>#include <cstdio>
#include <cstdlib>
#include <sstream>

#include "pixelboost/data/json/writer.h"
#include "pixelboost/debug/debugVariable.h"
#include "pixelboost/debug/debugVariableManager.h"
#include "pixelboost/file/fileHelpers.h"
#include "pixelboost/network/networkMessage.h"

#include "command/manager.h"
#include "http/httpInterface.h"
#include "project/entity.h"
#include "project/project.h"
#include "project/record.h"
#include "project/schema.h"
#include "core.h"

using namespace pixelboost;
using namespace pixeleditor;

HttpInterface::HttpInterface(const std::string& htmlLocation)
    : pixelboost::HttpServer()
{
    Start(9090, pixelboost::FileHelpers::GetRootPath()+"/"+htmlLocation);
}

HttpInterface::~HttpInterface()
{
    
}

bool HttpInterface::OnHttpRequest(HttpServer::RequestType type, const std::string& uri, const std::string& query, const std::string& data, pixelboost::HttpConnection& connection)
{
    ssize_t split = uri.find('/', 1);
    
    std::string command;
    std::string argumentString;
    std::string queryString = query;
    std::vector<std::string> urlArguments;
    std::map<std::string, std::string> queryArguments;
    
    if (split > 0)
    {
        command = uri.substr(1, split-1);
        argumentString = uri.substr(split);
    } else {
        command = uri.substr(1);
    }
        
    while (argumentString.length() > 1)
    {
        ssize_t split = argumentString.find('/', 1);
        
        std::string argument;
        
        if (split > 0)
        {
            argument = argumentString.substr(1, split-1);
            argumentString = argumentString.substr(split);
        } else {
            argument = argumentString.substr(1);
            argumentString = "";
        }
        
        if (argument.length() > 0)
            urlArguments.push_back(argument);
        else
            break;
    }
    
    while (queryString.length() > 1)
    {
        ssize_t split = queryString.find('&', 1);
        
        std::string argument;
        
        if (split > 0)
        {
            argument = queryString.substr(0, split);
            queryString = queryString.substr(split+1);
        } else {
            argument = queryString.substr(0);
            queryString = "";
        }
        
        if (argument.length() > 0)
        {
            std::string field;
            std::string value;
            
            int op = argument.find('=');
            if (op > 0)
            {
                field = argument.substr(0, op);
                value = argument.substr(op+1);
            }
            queryArguments[field] = value;
        }
        else
            break;
    }
    
    connection.AddHeader("Content-Type", "application/json;charset=utf-8");
    
    bool replied = false;
    
    if (command == "records" && type == kRequestTypeGet)
    {
        replied = OnGetRecords(connection);
    } else if (command == "record" && type == kRequestTypeGet)
    {
        if (urlArguments.size() == 2 && urlArguments[1] == "entities")
        {
            std::string record = urlArguments[0];
            replied = OnGetEntities(connection, atoi(record.c_str()));
        }
    } else if (command == "schema" && type == kRequestTypeGet)
    {
        if (urlArguments.size() == 0)
        {
            replied = OnGetSchema(connection);
        } else if (urlArguments.size() == 1)
        {
            replied = OnGetSchema(connection, urlArguments[0]);
        }
    } else if (command == "record" && type == kRequestTypePost)
    {
        if (urlArguments.size() == 2 && urlArguments[1] == "entities")
        {
            std::string record = urlArguments[0];
            replied = OnCreateEntity(connection, atoi(record.c_str()), queryArguments["type"]);
        }
    } else if (command == "records" && type == kRequestTypePost)
    {
        if (urlArguments.size() == 0)
        {
            replied = OnCreateRecord(connection, queryArguments["name"], queryArguments["type"]);
        }
    } else if (command == "save" && type == kRequestTypePut)
    {
        OnSave(connection);
    }
    
    if (replied == true)
    {
        connection.Send();
        return true;
    }
    
    if (type == kRequestTypeGet)
        return false; // No over-ride, default to finding html
    else
        return true;
}

bool HttpInterface::OnSave(pixelboost::HttpConnection& connection)
{
    Core::Instance()->GetCommandManager()->Exec("save");
    return true;
}

bool HttpInterface::OnCreateRecord(pixelboost::HttpConnection& connection, const std::string& name, const std::string& type)
{
    Project* project = Core::Instance()->GetProject();
    
    project->AddRecord(name, type);
    <commit_msg>Check for empty record name<commit_after>#include <cstdio>
#include <cstdlib>
#include <sstream>

#include "pixelboost/data/json/writer.h"
#include "pixelboost/debug/debugVariable.h"
#include "pixelboost/debug/debugVariableManager.h"
#include "pixelboost/file/fileHelpers.h"
#include "pixelboost/network/networkMessage.h"

#include "command/manager.h"
#include "http/httpInterface.h"
#include "project/entity.h"
#include "project/project.h"
#include "project/record.h"
#include "project/schema.h"
#include "core.h"

using namespace pixelboost;
using namespace pixeleditor;

HttpInterface::HttpInterface(const std::string& htmlLocation)
    : pixelboost::HttpServer()
{
    Start(9090, pixelboost::FileHelpers::GetRootPath()+"/"+htmlLocation);
}

HttpInterface::~HttpInterface()
{
    
}

bool HttpInterface::OnHttpRequest(HttpServer::RequestType type, const std::string& uri, const std::string& query, const std::string& data, pixelboost::HttpConnection& connection)
{
    ssize_t split = uri.find('/', 1);
    
    std::string command;
    std::string argumentString;
    std::string queryString = query;
    std::vector<std::string> urlArguments;
    std::map<std::string, std::string> queryArguments;
    
    if (split > 0)
    {
        command = uri.substr(1, split-1);
        argumentString = uri.substr(split);
    } else {
        command = uri.substr(1);
    }
        
    while (argumentString.length() > 1)
    {
        ssize_t split = argumentString.find('/', 1);
        
        std::string argument;
        
        if (split > 0)
        {
            argument = argumentString.substr(1, split-1);
            argumentString = argumentString.substr(split);
        } else {
            argument = argumentString.substr(1);
            argumentString = "";
        }
        
        if (argument.length() > 0)
            urlArguments.push_back(argument);
        else
            break;
    }
    
    while (queryString.length() > 1)
    {
        ssize_t split = queryString.find('&', 1);
        
        std::string argument;
        
        if (split > 0)
        {
            argument = queryString.substr(0, split);
            queryString = queryString.substr(split+1);
        } else {
            argument = queryString.substr(0);
            queryString = "";
        }
        
        if (argument.length() > 0)
        {
            std::string field;
            std::string value;
            
            int op = argument.find('=');
            if (op > 0)
            {
                field = argument.substr(0, op);
                value = argument.substr(op+1);
            }
            queryArguments[field] = value;
        }
        else
            break;
    }
    
    connection.AddHeader("Content-Type", "application/json;charset=utf-8");
    
    bool replied = false;
    
    if (command == "records" && type == kRequestTypeGet)
    {
        replied = OnGetRecords(connection);
    } else if (command == "record" && type == kRequestTypeGet)
    {
        if (urlArguments.size() == 2 && urlArguments[1] == "entities")
        {
            std::string record = urlArguments[0];
            replied = OnGetEntities(connection, atoi(record.c_str()));
        }
    } else if (command == "schema" && type == kRequestTypeGet)
    {
        if (urlArguments.size() == 0)
        {
            replied = OnGetSchema(connection);
        } else if (urlArguments.size() == 1)
        {
            replied = OnGetSchema(connection, urlArguments[0]);
        }
    } else if (command == "record" && type == kRequestTypePost)
    {
        if (urlArguments.size() == 2 && urlArguments[1] == "entities")
        {
            std::string record = urlArguments[0];
            replied = OnCreateEntity(connection, atoi(record.c_str()), queryArguments["type"]);
        }
    } else if (command == "records" && type == kRequestTypePost)
    {
        if (urlArguments.size() == 0)
        {
            replied = OnCreateRecord(connection, queryArguments["name"], queryArguments["type"]);
        }
    } else if (command == "save" && type == kRequestTypePut)
    {
        OnSave(connection);
    }
    
    if (replied == true)
    {
        connection.Send();
        return true;
    }
    
    if (type == kRequestTypeGet)
        return false; // No over-ride, default to finding html
    else
        return true;
}

bool HttpInterface::OnSave(pixelboost::HttpConnection& connection)
{
    Core::Instance()->GetCommandManager()->Exec("save");
    return true;
}

bool HttpInterface::OnCreateRecord(pixelboost::HttpConnection& connection, const std::string& name, const std::string& type)
{
    if (name == "")
        return false;
    
    Project* project = Core::Instance()->GetProject();
    
    project->AddRecord(name, type);
    
{'fraction_non_alphanumeric': 0.13667402038029206, 'fraction_numerical': 0.0077739258325454355, 'mean_word_length': 2.2714776632302405, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 37, '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': '2620223', 'n_tokens_mistral': 2771, 'n_tokens_neox': 2762, 'n_words': 566}