<commit_before>/*
 * Copyright (C) 2013-2014 <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 "context_p.h"

#include "common.h"
#include "request.h"
#include "response.h"
#include "action.h"
#include "dispatcher.h"
#include "controller.h"

#include <QUrl>
#include <QUrlQuery>

using namespace Cutelyst;

Context::Context(ContextPrivate *priv) :
    d_ptr(priv)
{
}

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

bool Context::error() const
{
    Q_D(const Context);
    return !d->error.isEmpty();
}

void Context::error(const QString &error)
{
    Q_D(Context);
    if (!error.isEmpty()) {
        d->error << error;
    }
}

QStringList Context::errors() const
{
    Q_D(const Context);
    return d->error;
}

bool Context::state() const
{
    Q_D(const Context);
    return d->state;
}

void Context::setState(bool state)
{
    Q_D(Context);
    d->state = state;
}

Engine *Context::engine() const
{
    Q_D(const Context);
    return d->engine;
}

Application *Context::app() const
{
    Q_D(const Context);
    return d->app;
}

Response *Context::response() const
{
    Q_D(const Context);
    return d->response;
}

Response *Context::res() const
{
    Q_D(const Context);
    return d->response;
}

Action *Context::action() const
{
    Q_D(const Context);
    return d->action;
}

QByteArray Context::actionName() const
{
    Q_D(const Context);
    return d->action->name();
}

Request *Context::request() const
{
    Q_D(const Context);
    return d->request;
}

Request *Context::req() const
{
    Q_D(const Context);
    return d->request;
}

Dispatcher *Context::dispatcher() const
{
    Q_D(const Context);
    return d->dispatcher;
}

QByteArray Cutelyst::Context::controllerName() const
{
    Q_D(const Context);
    return d->action->controller()->metaObject()->className();
}

Controller *Context::controller(const QByteArray &name) const
{
    Q_D(const Context);
    if (name.isEmpty()) {
        return d->action->controller();
    } else {
        return d->dispatcher->controllers().value(name);
    }
}

QVariantHash &Context::stash()
{
    Q_D(Context);
    return d->stash;
}

QUrl Context::uriFor(const QByteArray &path, const QStringList &args, const QMultiHash<QString, QString> &queryValues) const
{
    Q_D(const Context);

    QUrl ret = d->request->base();

    QStringList encodedArgs;
    encodedArgs.append(path);

    Q_FOREACH (const QString &arg, args) {
        encodedArgs.append(QUrl::toPercentEncoding(arg));
    }
    ret.setPath(encodedArgs.join(QLatin1Char('/')));

    QUrlQuery query;
    QMultiHash<QString, QString>::ConstIterator i = queryValues.constBegin();
    while (i != queryValues.constEnd()) {
        query.addQueryItem(i.key(), i.value());
        ++i;
    }
    ret.setQuery(query);

    return ret;
}

QUrl Context::uriFor(const Action *action, const QStringList &args, const QMultiHash<QString, QString> &queryValues) const
{
    Q_D(const Context);

    QByteArray path;
    if (action) {
        path = d->dispatcher->uriForAction(action, args);
    } else {
        // use the current action if none is provided
        path = d->dispatcher->uriForAction(d->action, args);
    }

    return uriFor(path, args, queryValues);
}
<commit_msg>Don't use args as captures on uriFor<commit_after>/*
 * Copyright (C) 2013-2014 <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 "context_p.h"

#include "common.h"
#include "request.h"
#include "response.h"
#include "action.h"
#include "dispatcher.h"
#include "controller.h"

#include <QUrl>
#include <QUrlQuery>

using namespace Cutelyst;

Context::Context(ContextPrivate *priv) :
    d_ptr(priv)
{
}

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

bool Context::error() const
{
    Q_D(const Context);
    return !d->error.isEmpty();
}

void Context::error(const QString &error)
{
    Q_D(Context);
    if (!error.isEmpty()) {
        d->error << error;
    }
}

QStringList Context::errors() const
{
    Q_D(const Context);
    return d->error;
}

bool Context::state() const
{
    Q_D(const Context);
    return d->state;
}

void Context::setState(bool state)
{
    Q_D(Context);
    d->state = state;
}

Engine *Context::engine() const
{
    Q_D(const Context);
    return d->engine;
}

Application *Context::app() const
{
    Q_D(const Context);
    return d->app;
}

Response *Context::response() const
{
    Q_D(const Context);
    return d->response;
}

Response *Context::res() const
{
    Q_D(const Context);
    return d->response;
}

Action *Context::action() const
{
    Q_D(const Context);
    return d->action;
}

QByteArray Context::actionName() const
{
    Q_D(const Context);
    return d->action->name();
}

Request *Context::request() const
{
    Q_D(const Context);
    return d->request;
}

Request *Context::req() const
{
    Q_D(const Context);
    return d->request;
}

Dispatcher *Context::dispatcher() const
{
    Q_D(const Context);
    return d->dispatcher;
}

QByteArray Cutelyst::Context::controllerName() const
{
    Q_D(const Context);
    return d->action->controller()->metaObject()->className();
}

Controller *Context::controller(const QByteArray &name) const
{
    Q_D(const Context);
    if (name.isEmpty()) {
        return d->action->controller();
    } else {
        return d->dispatcher->controllers().value(name);
    }
}

QVariantHash &Context::stash()
{
    Q_D(Context);
    return d->stash;
}

QUrl Context::uriFor(const QByteArray &path, const QStringList &args, const QMultiHash<QString, QString> &queryValues) const
{
    Q_D(const Context);

    QUrl ret = d->request->base();

    QStringList encodedArgs;
    encodedArgs.append(path);

    Q_FOREACH (const QString &arg, args) {
        encodedArgs.append(QUrl::toPercentEncoding(arg));
    }
    ret.setPath(encodedArgs.join(QLatin1Char('/')));

    QUrlQuery query;
    QMultiHash<QString, QString>::ConstIterator i = queryValues.constBegin();
    while (i != queryValues.constEnd()) {
        query.addQueryItem(i.key(), i.value());
        ++i;
    }
    ret.setQuery(query);

    return ret;
}

QUrl Context::uriFor(const Action *action, const QStringList &args, const QMultiHash<QString, QString> &queryValues) const
{
    Q_UNUSED(args)
    Q_D(const Context);

    QByteArray path;
    if (action) {
        // TODO check action for captures
        path = d->dispatcher->uriForAction(action, QStringList());
    } else {
        // use the current action if none is provided
        path = d->dispatcher->uriForAction(d->action, QStringList());
    }

    return uriFor(path, args, queryValues);
}

{'fraction_non_alphanumeric': 0.13241155954160438, 'fraction_numerical': 0.00523168908819133, 'mean_word_length': 3.59324942791762, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 81, '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': '5132254', 'n_tokens_mistral': 2572, 'n_tokens_neox': 2527, 'n_words': 726}