<commit_before>/*
 * Copyright (C) 2010-2012 <NAME>
 * Contact: http://code.google.com/p/qdjango/
 *
 * This file is part of the QDjango Library.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QCoreApplication>
#include <QDateTime>
#include <QFile>
#include <QFileInfo>
#include <QRegExp>
#include <QStringList>
#include <QUrl>

#include "QDjangoHttpController.h"
#include "QDjangoHttpRequest.h"
#include "QDjangoHttpResponse.h"

/** Extract basic credentials from an HTTP \a request.
 *
 * Returns \b true if credentials were provider, \b false otherwise.
 */
bool QDjangoHttpController::getBasicAuth(const QDjangoHttpRequest &request, QString &username, QString &password)
{
    QRegExp authRx("^Basic (.+)$");
    const QString authHeader = request.meta("HTTP_AUTHORIZATION");
    if (authRx.exactMatch(authHeader)) {
        const QString authValue = QString::fromUtf8(QByteArray::fromBase64(authRx.cap(1).toAscii()));
        const QStringList bits = authValue.split(":");
        if (bits.size() == 2 && !bits[0].isEmpty() && !bits[1].isEmpty()) {
            username = bits[0];
            password = bits[1];
            return true;
        }
    }
    return false;
}

QString QDjangoHttpController::httpDateTime(const QDateTime &dt)
{
    if (dt.isValid())
        return dt.toUTC().toString("ddd, dd MMM yyyy HH:mm:ss") + " GMT";
    return QString();
}

QDateTime QDjangoHttpController::httpDateTime(const QString &str)
{
    QDateTime dt = QDateTime::fromString(str.left(25), "ddd, dd MMM yyyy HH:mm:ss");
    dt.setTimeSpec(Qt::UTC);
    return dt;
}


QDjangoHttpResponse *QDjangoHttpController::serveError(const QDjangoHttpRequest &request, int code, const QString &text)
{
    Q_UNUSED(request);

    QDjangoHttpResponse *response = new QDjangoHttpResponse;
    response->setHeader("Content-Type", "text/html; charset=utf-8");
    response->setStatusCode(code);<commit_msg>add some documentation<commit_after>/*
 * Copyright (C) 2010-2012 <NAME>
 * Contact: http://code.google.com/p/qdjango/
 *
 * This file is part of the QDjango Library.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QCoreApplication>
#include <QDateTime>
#include <QFile>
#include <QFileInfo>
#include <QRegExp>
#include <QStringList>
#include <QUrl>

#include "QDjangoHttpController.h"
#include "QDjangoHttpRequest.h"
#include "QDjangoHttpResponse.h"

/** Extract basic credentials from an HTTP \a request.
 *
 * Returns \b true if credentials were provider, \b false otherwise.
 */
bool QDjangoHttpController::getBasicAuth(const QDjangoHttpRequest &request, QString &username, QString &password)
{
    QRegExp authRx("^Basic (.+)$");
    const QString authHeader = request.meta("HTTP_AUTHORIZATION");
    if (authRx.exactMatch(authHeader)) {
        const QString authValue = QString::fromUtf8(QByteArray::fromBase64(authRx.cap(1).toAscii()));
        const QStringList bits = authValue.split(":");
        if (bits.size() == 2 && !bits[0].isEmpty() && !bits[1].isEmpty()) {
            username = bits[0];
            password = bits[1];
            return true;
        }
    }
    return false;
}

/** Converts a QDateTime to an HTTP datetime string.
 */
QString QDjangoHttpController::httpDateTime(const QDateTime &dt)
{
    if (dt.isValid())
        return dt.toUTC().toString("ddd, dd MMM yyyy HH:mm:ss") + " GMT";
    return QString();
}

/** Converts an HTTP datetime string to a QDateTime.
 */
QDateTime QDjangoHttpController::httpDateTime(const QString &str)
{
    QDateTime dt = QDateTime::fromString(str.left(25), "ddd, dd MMM yyyy HH:mm:ss");
    dt.setTimeSpec(Qt::UTC);
    return dt;
}

QDjangoHttpResponse *QDjangoHttpController::serveError(const QDjangoHttpRequest &request, int code, const QString &text)
{
    Q_UNUSED(request);

    QDjangoHttpResponse *response = new QDjangoHttpResponse;
    response->setHeader("Content-Type", "text/html; charset=utf-8");
    response->setStatusCode(code);
{'fraction_non_alphanumeric': 0.11764705882352941, 'fraction_numerical': 0.008126934984520124, 'mean_word_length': 4.028210116731517, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3933447', 'n_tokens_mistral': 1574, 'n_tokens_neox': 1532, 'n_words': 495}