<commit_before>#include "engineuwsgi.h"

#include <Cutelyst/application.h>
#include <Cutelyst/context.h>
#include <Cutelyst/response.h>

#include <QPluginLoader>
#include <QUrl>

extern struct uwsgi_server uwsgi;

using namespace Cutelyst;

CutelystEngineUwsgi::CutelystEngineUwsgi(const QString &app, QObject *parent) :
    Engine(parent),
    m_app(0)
{
    m_loader = new QPluginLoader(app, this);
    if (m_loader->load()) {
        QObject *instance = m_loader->instance();
        if (instance) {
            m_app = qobject_cast<Application *>(instance);
            if (m_app) {
                m_app->setup(this);
            } else {
                qCritical() << "Could not create an instance of the application:" << instance;
            }
        }
    } else {
        qWarning() << "Failed to open app:" << m_loader->errorString();
    }
}

struct wsgi_request *_wsgi_req;

void CutelystEngineUwsgi::finalizeBody(Context *ctx)
{
    QByteArray body = ctx->res()->body();
    qDebug() << "BODY" << body;
    uwsgi_response_prepare_headers(_wsgi_req, (char *)"200 OK", 6);
    uwsgi_response_add_content_type(_wsgi_req, (char *)"text/html", 9);
    uwsgi_response_write_body_do(_wsgi_req, body.data(), body.size());
}

void CutelystEngineUwsgi::processRequest(struct wsgi_request *req)
{
    char *url;
    uint16_t url_len;
    url = uwsgi_get_var(req, (char *) "PATH_INFO", 9, &url_len);

    QUrl urlParsed = QString(url).trimmed();
    qDebug() << url << urlParsed;
    createRequest(0,
                  urlParsed,
                  "GET",
                  QString(),
                  QHash<QString, QByteArray>(),
                  QByteArray());
}

void CutelystEngineUwsgi::finalizeHeaders(Context *ctx)
{

}

bool CutelystEngineUwsgi::init()
{

}<commit_msg>Improve the creation of the Request by uWSGI<commit_after>#include "engineuwsgi.h"

#include <Cutelyst/application.h>
#include <Cutelyst/context.h>
#include <Cutelyst/response.h>

#include <QPluginLoader>
#include <QUrl>
#include <QHostInfo>
#include <QStringBuilder>

extern struct uwsgi_server uwsgi;

using namespace Cutelyst;

CutelystEngineUwsgi::CutelystEngineUwsgi(const QString &app, QObject *parent) :
    Engine(parent),
    m_app(0)
{
    m_loader = new QPluginLoader(app, this);
    if (m_loader->load()) {
        QObject *instance = m_loader->instance();
        if (instance) {
            m_app = qobject_cast<Application *>(instance);
            if (m_app) {
                m_app->setup(this);
            } else {
                qCritical() << "Could not create an instance of the application:" << instance;
            }
        }
    } else {
        qWarning() << "Failed to open app:" << m_loader->errorString();
    }
}

struct wsgi_request *_wsgi_req;

void CutelystEngineUwsgi::finalizeBody(Context *ctx)
{
    QByteArray body = ctx->res()->body();
    qDebug() << "BODY" << body;
    uwsgi_response_prepare_headers(_wsgi_req, (char *)"200 OK", 6);
    uwsgi_response_add_content_type(_wsgi_req, (char *)"text/html", 9);
    uwsgi_response_write_body_do(_wsgi_req, body.data(), body.size());
}

void CutelystEngineUwsgi::processRequest(struct wsgi_request *req)
{
    uint16_t path_info_len;
    char *path_info = uwsgi_get_var(req, (char *) "PATH_INFO", 9, &path_info_len);

    uint16_t remote_addr_len = 0;
    char *remote_addr = uwsgi_get_var(req, (char *) "REMOTE_ADDR", 11, &remote_addr_len);

    uint16_t request_method_len;
    char *request_method = uwsgi_get_var(req, (char *) "REQUEST_METHOD", 14, &request_method_len);

    uint16_t server_protocol_len;
    char *server_protocol = uwsgi_get_var(req, (char *) "SERVER_PROTOCOL", 15, &server_protocol_len);


    QString path = QByteArray(path_info, path_info_len);
    QByteArray remote(remote_addr, remote_addr_len);
    QByteArray method(request_method, request_method_len);
    QByteArray protocol(server_protocol, server_protocol_len);
    QHash<QString, QByteArray> headers;

    QUrl url;
    if (!remote.isEmpty()) {
        // TODO I think SERVER_NAME:SERVER_PORT is what we want here
        url = QLatin1String("http://") % remote % path;
    } else {
        // This is a hack just in case remote is not set
        url = QLatin1String("http://") % QHostInfo::localHostName() % path;
    }

    qDebug() << url << method << remote << path << protocol;
    createRequest(0,
                  url,
                  method,
                  protocol,
                  headers,
                  QByteArray());
}

void CutelystEngineUwsgi::finalizeHeaders(Context *ctx)
{

}

bool CutelystEngineUwsgi::init()
{

}
{'fraction_non_alphanumeric': 0.12788168769030014, 'fraction_numerical': 0.007612005219660722, 'mean_word_length': 3.1395139513951396, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 31, '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': '3716127', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1539, 'n_words': 320}