<commit_before>/*
 * Copyright (C) 2013-2015 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB. If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "engine_p.h"

#include "common.h"
#include "request_p.h"
#include "application.h"
#include "response_p.h"
#include "context_p.h"

#include <QUrl>
#include <QSettings>
#include <QDir>
#include <QtCore/QByteArray>
#include <QDebug>

using namespace Cutelyst;

Engine::Engine(const QVariantMap &opts, QObject *parent) :
    QObject(parent),
    d_ptr(new EnginePrivate)
{
    Q_D(Engine);

    // Debug messages should be disabled by default
    QLoggingCategory::setFilterRules(QLatin1String("cutelyst.*.debug=false"));

    // Load application configuration
    if (qEnvironmentVariableIsSet("CUTELYST_CONFIG")) {
        const QByteArray config = qgetenv("CUTELYST_CONFIG");
        qCDebug(CUTELYST_CORE) << "Reading config file:" << config;
        QSettings settings(QString::fromLatin1(config), QSettings::IniFormat);
        Q_FOREACH (const QString &group, settings.childGroups()) {
            settings.beginGroup(group);
            Q_FOREACH (const QString &key, settings.childKeys()) {
                d->config[group].insert(key, settings.value(key));
            }
            settings.endGroup();
        }
        qCDebug(CUTELYST_CORE) << "Configuration:" << d->config;
    }

    d->opts = opts;
}

Engine::~Engine()
{
    delete d_ptr;
}

void Engine::finalizeCookies(Context *c)
{
    Response *res = c->response();
    Headers &headers = res->headers();
    Q_FOREACH (const QNetworkCookie &cookie, res->cookies()) {
        headers.pushHeader(QStringLiteral("Set-Cookie"), QString::fromLatin1(cookie.toRawForm()));
    }
}

bool Engine::finalizeHeaders(Context *c)
{
    Response *response = c->response();

    // Check if we already finalized headers
    if (response->d_ptr->finalizedHeaders) {
        return true;
    }

    QIODevice *body = response->bodyDevice();

    // Fix missing content length
    if (body && !response->contentLength()) {
        response->setContentLength(body->size());
    } else {
        const auto bodyByteArray = response->body();
        response->setContentLength(bodyByteArray.length());
    }

    const QString protocol = c->request()->protocol();
    if (protocol == QLatin1String("HTTP/1.1")) {
        if (!response->contentLength()) {
            quint16 status = response->status();
            // if status is not 1xx or 204 NoContent or 304 NotModified
            if (!(status >= 100 && status <= 199) && status != 204 && status != 304) {
                qCDebug(CUTELYST_ENGINE, "Using chunked transfer-encoding to send unknown length body");
                response->setHeader(QStringLiteral("Transfer-Encoding"), QStringLiteral("chunked"));
                c->d_ptr->chunked = true;
            }
        } else if (response->header(QStringLiteral("Transfer-Encoding")) == QLatin1String("chunked")) {
            qCDebug(CUTELYST_ENGINE, "Chunked transfer-encoding set for response");
            c->d_ptr->chunked = true;
        }
    }

    // Handle redirects
    const QUrl &location = response->location();
    if (!location.isEmpty()) {
        qCDebug(CUTELYST_ENGINE, "Redirecting to \"%s\"", location.toEncoded().constData());
        response->headers().setHeader(QStringLiteral("Location"), QString::fromLatin1(location.toEncoded()));
    }

    finalizeCookies(c);

    // Done
    response->d_ptr->finalizedHeaders = true;<commit_msg>Fix engine setting content-length when no body is set<commit_after>/*
 * Copyright (C) 2013-2015 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB. If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "engine_p.h"

#include "common.h"
#include "request_p.h"
#include "application.h"
#include "response_p.h"
#include "context_p.h"

#include <QUrl>
#include <QSettings>
#include <QDir>
#include <QtCore/QByteArray>
#include <QDebug>

using namespace Cutelyst;

Engine::Engine(const QVariantMap &opts, QObject *parent) :
    QObject(parent),
    d_ptr(new EnginePrivate)
{
    Q_D(Engine);

    // Debug messages should be disabled by default
    QLoggingCategory::setFilterRules(QLatin1String("cutelyst.*.debug=false"));

    // Load application configuration
    if (qEnvironmentVariableIsSet("CUTELYST_CONFIG")) {
        const QByteArray config = qgetenv("CUTELYST_CONFIG");
        qCDebug(CUTELYST_CORE) << "Reading config file:" << config;
        QSettings settings(QString::fromLatin1(config), QSettings::IniFormat);
        Q_FOREACH (const QString &group, settings.childGroups()) {
            settings.beginGroup(group);
            Q_FOREACH (const QString &key, settings.childKeys()) {
                d->config[group].insert(key, settings.value(key));
            }
            settings.endGroup();
        }
        qCDebug(CUTELYST_CORE) << "Configuration:" << d->config;
    }

    d->opts = opts;
}

Engine::~Engine()
{
    delete d_ptr;
}

void Engine::finalizeCookies(Context *c)
{
    Response *res = c->response();
    Headers &headers = res->headers();
    Q_FOREACH (const QNetworkCookie &cookie, res->cookies()) {
        headers.pushHeader(QStringLiteral("Set-Cookie"), QString::fromLatin1(cookie.toRawForm()));
    }
}

bool Engine::finalizeHeaders(Context *c)
{
    Response *response = c->response();

    // Check if we already finalized headers
    if (response->d_ptr->finalizedHeaders) {
        return true;
    }

    // Fix missing content length
    if (response->hasBody() && !response->contentLength()) {
        QIODevice *body = response->bodyDevice();
        if (body) {
            response->setContentLength(body->size());
        } else {
            response->setContentLength(response->body().size());
        }
    }

    const QString protocol = c->request()->protocol();
    if (protocol == QLatin1String("HTTP/1.1")) {
        if (!response->contentLength()) {
            quint16 status = response->status();
            // if status is not 1xx or 204 NoContent or 304 NotModified
            if (!(status >= 100 && status <= 199) && status != 204 && status != 304) {
                qCDebug(CUTELYST_ENGINE, "Using chunked transfer-encoding to send unknown length body");
                response->setHeader(QStringLiteral("Transfer-Encoding"), QStringLiteral("chunked"));
                c->d_ptr->chunked = true;
            }
        } else if (response->header(QStringLiteral("Transfer-Encoding")) == QLatin1String("chunked")) {
            qCDebug(CUTELYST_ENGINE, "Chunked transfer-encoding set for response");
            c->d_ptr->chunked = true;
        }
    }

    // Handle redirects
    const QUrl &location = response->location();
    if (!location.isEmpty()) {
        qCDebug(CUTELYST_ENGINE, "Redirecting to \"%s\"", location.toEncoded().constData());
        response->headers().setHeader(QStringLiteral("Location"), QString::fromLatin1(location.toEncoded()));
    }

    finalizeCookies(c);

    // Done
    response->d_ptr->finalizedHeaders = true;
{'fraction_non_alphanumeric': 0.12735287109840362, 'fraction_numerical': 0.011675005956635693, 'mean_word_length': 3.208020050125313, 'pattern_counts': {'":': 0, '<': 37, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2927252', 'n_tokens_mistral': 2506, 'n_tokens_neox': 2375, 'n_words': 669}