<commit_before>/*
 * Handler for HTTP requests.
 *
 * author: <NAME> <<EMAIL>>
 */

#include "connection.h"
#include "handler.h"
#include "config.h"
#include "translate.h"

#include <assert.h>
#include <stdio.h>
#include <string.h>

static void
translate_callback(const struct translate_response *response,
                   void *ctx)
{
    struct http_server_request *request = ctx;
    struct translated *translated;
    int ret;

    if (response->status < 0 ||
        (response->path == NULL && response->proxy == NULL)) {
        http_server_send_message(request,
                                 HTTP_STATUS_INTERNAL_SERVER_ERROR,
                                 "Internal server error");
        return;
    }

    translated = p_malloc(request->pool, sizeof(*translated));

    ret = uri_parse(request->pool, &translated->uri, request->uri);
    if (ret < 0) {
        http_server_send_message(request,
                                 HTTP_STATUS_INTERNAL_SERVER_ERROR,
                                 "Internal server error");
        return;
    }

    if (response->path != NULL) {
        translated->path = response->path;
        file_callback(request, translated);
    } else if (response->proxy != NULL) {
        translated->path = response->proxy;
        proxy_callback(request, translated);
    } else {
        assert(0);
    }
}

static struct translated *
translate2(struct http_server_request *request,
           const struct config *config)
{
    struct translated *translated;
    int ret;

    if (config->translation_socket != NULL) {
        struct translate_request tr = {
            .host = strmap_get(request->headers, "host"),
            .uri = request->uri,
            /* XXX .session */
        };

        translate(request->pool, config, &tr, translate_callback, request);
        return NULL;
    }

    translated = p_malloc(request->pool, sizeof(*translated));

    ret = uri_parse(request->pool, &translated->uri, request->uri);
    if (ret < 0) {
        http_server_send_message(request,
                                 HTTP_STATUS_INTERNAL_SERVER_ERROR,
                                 "Internal server error");
        return NULL;
    }

    assert(translated->uri.base_length > 0);
    assert(translated->uri.base[0] == '/');

    translated->path = p_strncat(request->pool,
                                 config->document_root,
                                 strlen(config->document_root),
                                 translated->uri.base,
                                 translated->uri.base_length,
                                 NULL);

    return translated;
}

static void
my_http_server_connection_request(struct http_server_request *request,
                                  void *ctx)
{
    struct client_connection *connection = ctx;
    struct translated *translated;

    assert(request != NULL);

    (void)request;
    (void)connection;

    translated = translate2(request, connection->config);
    if (translated == NULL)
        return;

    if (translated == NULL || translated->path == NULL) {
        http_server_send_message(request,
                                 HTTP_STATUS_NOT_FOUND,
                                 "The requested resource does not exist.");
        return;
    }

    if (memcmp(translated->path, "http://", 7) == 0)
        proxy_callback(request, translated);
    else
        file_callback(request, translated);
}

static void
my_http_server_connection_free(void *ctx)
{
    struct client_connection *connection = ctx;

    /* since remove_connection() might recurse here, we check if<commit_msg>removed the hard-coded proxy locations II<commit_after>/*
 * Handler for HTTP requests.
 *
 * author: <NAME> <<EMAIL>>
 */

#include "connection.h"
#include "handler.h"
#include "config.h"
#include "translate.h"

#include <assert.h>
#include <stdio.h>
#include <string.h>

static void
translate_callback(const struct translate_response *response,
                   void *ctx)
{
    struct http_server_request *request = ctx;
    struct translated *translated;
    int ret;

    if (response->status < 0 ||
        (response->path == NULL && response->proxy == NULL)) {
        http_server_send_message(request,
                                 HTTP_STATUS_INTERNAL_SERVER_ERROR,
                                 "Internal server error");
        return;
    }

    translated = p_malloc(request->pool, sizeof(*translated));

    ret = uri_parse(request->pool, &translated->uri, request->uri);
    if (ret < 0) {
        http_server_send_message(request,
                                 HTTP_STATUS_INTERNAL_SERVER_ERROR,
                                 "Internal server error");
        return;
    }

    if (response->path != NULL) {
        translated->path = response->path;
        file_callback(request, translated);
    } else if (response->proxy != NULL) {
        translated->path = response->proxy;
        proxy_callback(request, translated);
    } else {
        assert(0);
    }
}

static struct translated *
translate2(struct http_server_request *request,
           const struct config *config)
{
    struct translated *translated;
    int ret;

    if (config->translation_socket != NULL) {
        struct translate_request tr = {
            .host = strmap_get(request->headers, "host"),
            .uri = request->uri,
            /* XXX .session */
        };

        translate(request->pool, config, &tr, translate_callback, request);
        return NULL;
    }

    translated = p_malloc(request->pool, sizeof(*translated));

    ret = uri_parse(request->pool, &translated->uri, request->uri);
    if (ret < 0) {
        http_server_send_message(request,
                                 HTTP_STATUS_INTERNAL_SERVER_ERROR,
                                 "Internal server error");
        return NULL;
    }

    assert(translated->uri.base_length > 0);
    assert(translated->uri.base[0] == '/');

    translated->path = p_strncat(request->pool,
                                 config->document_root,
                                 strlen(config->document_root),
                                 translated->uri.base,
                                 translated->uri.base_length,
                                 NULL);

    return translated;
}

static void
my_http_server_connection_request(struct http_server_request *request,
                                  void *ctx)
{
    struct client_connection *connection = ctx;
    struct translated *translated;

    assert(request != NULL);

    (void)request;
    (void)connection;

    translated = translate2(request, connection->config);
    if (translated == NULL)
        return;

    if (translated == NULL || translated->path == NULL) {
        http_server_send_message(request,
                                 HTTP_STATUS_NOT_FOUND,
                                 "The requested resource does not exist.");
        return;
    }

    file_callback(request, translated);
}

static void
my_http_server_connection_free(void *ctx)
{
    struct client_connection *connection = ctx;

    /* since remove_connection() might recurse here, we check if
{'fraction_non_alphanumeric': 0.10711788561040414, 'fraction_numerical': 0.0025171304712627604, 'mean_word_length': 1.983729662077597, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 80, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '451915', 'n_tokens_mistral': 1915, 'n_tokens_neox': 1843, 'n_words': 424}