<commit_before>/*
 * author: <NAME> <<EMAIL>>
 */

#include "cgi_address.hxx"
#include "pool.h"
#include "uri_base.hxx"
#include "uri-relative.h"
#include "uri_escape.hxx"
#include "uri-extract.h"
#include "regex.hxx"

#include <string.h>

void
cgi_address_init(struct cgi_address *cgi, const char *path,
                 bool have_address_list)
{
    assert(path != nullptr);

    memset(cgi, 0, sizeof(*cgi));
    cgi->path = path;

    cgi->args.Init();
    cgi->env.Init();
    cgi->options.Init();

    if (have_address_list)
        address_list_init(&cgi->address_list);
}

struct cgi_address *
cgi_address_new(struct pool *pool, const char *path, bool have_address_list)
{
    auto cgi = NewFromPool<struct cgi_address>(pool);
    cgi_address_init(cgi, path, have_address_list);
    return cgi;
}

const char *
cgi_address::GetURI(struct pool *pool) const
{
    if (uri != nullptr)
        return uri;

    const char *p = script_name;
    if (p == nullptr)
        p = "";

    if (path_info != nullptr)
        p = p_strcat(pool, p, path_info, nullptr);

    if (query_string != nullptr)
        p = p_strcat(pool, p, "?", query_string, nullptr);

    return p;
}

const char *
cgi_address::GetId(struct pool *pool) const
{
    char child_options_buffer[4096];
    *options.MakeId(child_options_buffer) = 0;

    const char *p = p_strcat(pool, path,
                             child_options_buffer,
                             nullptr);

    if (document_root != nullptr)
        p = p_strcat(pool, p, ";d=", document_root, nullptr);

    if (interpreter != nullptr)
        p = p_strcat(pool, p, ";i=", interpreter, nullptr);

    if (action != nullptr)
        p = p_strcat(pool, p, ";a=", action, nullptr);

    for (unsigned i = 0; i < args.n; ++i)
        p = p_strcat(pool, p, "!", args.values[i], nullptr);

    for (unsigned i = 0; i < env.n; ++i)
        p = p_strcat(pool, p, "$", env.values[i], nullptr);

    if (uri != nullptr)<commit_msg>cgi_address: optimize GetURI()<commit_after>/*
 * author: <NAME> <<EMAIL>>
 */

#include "cgi_address.hxx"
#include "pool.h"
#include "uri_base.hxx"
#include "uri-relative.h"
#include "uri_escape.hxx"
#include "uri-extract.h"
#include "regex.hxx"

#include <string.h>

void
cgi_address_init(struct cgi_address *cgi, const char *path,
                 bool have_address_list)
{
    assert(path != nullptr);

    memset(cgi, 0, sizeof(*cgi));
    cgi->path = path;

    cgi->args.Init();
    cgi->env.Init();
    cgi->options.Init();

    if (have_address_list)
        address_list_init(&cgi->address_list);
}

struct cgi_address *
cgi_address_new(struct pool *pool, const char *path, bool have_address_list)
{
    auto cgi = NewFromPool<struct cgi_address>(pool);
    cgi_address_init(cgi, path, have_address_list);
    return cgi;
}

const char *
cgi_address::GetURI(struct pool *pool) const
{
    if (uri != nullptr)
        return uri;

    const char *sn = script_name;
    if (sn == nullptr)
        sn = "";

    const char *pi = path_info;
    const char *qm = nullptr;
    const char *qs = query_string;

    if (pi == nullptr) {
        if (qs == nullptr)
            return sn;

        pi = "";
    }

    if (qs != nullptr)
        qm = "?";

    return p_strcat(pool, sn, pi, qm, qs, nullptr);
}

const char *
cgi_address::GetId(struct pool *pool) const
{
    char child_options_buffer[4096];
    *options.MakeId(child_options_buffer) = 0;

    const char *p = p_strcat(pool, path,
                             child_options_buffer,
                             nullptr);

    if (document_root != nullptr)
        p = p_strcat(pool, p, ";d=", document_root, nullptr);

    if (interpreter != nullptr)
        p = p_strcat(pool, p, ";i=", interpreter, nullptr);

    if (action != nullptr)
        p = p_strcat(pool, p, ";a=", action, nullptr);

    for (unsigned i = 0; i < args.n; ++i)
        p = p_strcat(pool, p, "!", args.values[i], nullptr);

    for (unsigned i = 0; i < env.n; ++i)
        p = p_strcat(pool, p, "$", env.values[i], nullptr);

    if (uri != nullptr)
{'fraction_non_alphanumeric': 0.14927214409079695, 'fraction_numerical': 0.003947693066864051, 'mean_word_length': 2.7329650092081033, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3906212', 'n_tokens_mistral': 1482, 'n_tokens_neox': 1407, 'n_words': 353}