<commit_before>// Copyright 2015 Cutehacks AS. All rights reserved.
// License can be found in the LICENSE file.

#include <QtCore/QRegExp>
#include <QtCore/QTextCodec>
#include <QtNetwork/QNetworkReply>
#include <QtQml/QQmlEngine>

#include "response.h"
#include "serialization.h"

namespace com { namespace cutehacks { namespace duperagent {

ResponsePrototype::ResponsePrototype(QQmlEngine *engine, QNetworkReply *reply) : QObject(0),
    m_engine(engine),
    m_reply(reply)
{
    QString type = m_reply->header(QNetworkRequest::ContentTypeHeader).toString();

    QRegExp charsetRegexp(".*charset=(.*)[\\s]*", Qt::CaseInsensitive, QRegExp::RegExp2);
    if (charsetRegexp.exactMatch(type)) {
        m_charset = charsetRegexp.capturedTexts().at(1);
    }

    if (m_reply->isReadable()) {
        QByteArray data = m_reply->readAll();

        QTextCodec *text = QTextCodec::codecForName(m_charset.toLatin1());
        m_text = text ? text->makeDecoder()->toUnicode(data) : QString::fromUtf8(data);

        if (type.contains("application/json")) {
            // TODO: add error handling
            JsonCodec json(m_engine);
            m_body = json.parse(data);
        } else if (type.contains("application/x-www-form-urlencoded")) {
            // TODO: Implement parsing of form-urlencoded
        } else if (type.contains("multipart/form-data")) {
            // TODO: Implement parsing of form-data
        }
    }

    m_header = m_engine->newObject();
    QList<QNetworkReply::RawHeaderPair>::const_iterator it = m_reply->rawHeaderPairs().cbegin();
    for(; it != m_reply->rawHeaderPairs().cend(); it++) {
        m_header.setProperty(
                    QString::fromUtf8((*it).first).toLower(),
                    QString::fromUtf8((*it).second));
    }

    m_engine->setObjectOwnership(this, QQmlEngine::JavaScriptOwnership);
}

ResponsePrototype::~ResponsePrototype()
{
    delete m_reply;
}

int ResponsePrototype::statusType() const {<commit_msg>Fallback to text if we don't have a valid parser for the mime type.<commit_after>// Copyright 2015 Cutehacks AS. All rights reserved.
// License can be found in the LICENSE file.

#include <QtCore/QRegExp>
#include <QtCore/QTextCodec>
#include <QtNetwork/QNetworkReply>
#include <QtQml/QQmlEngine>

#include "response.h"
#include "serialization.h"

namespace com { namespace cutehacks { namespace duperagent {

ResponsePrototype::ResponsePrototype(QQmlEngine *engine, QNetworkReply *reply) : QObject(0),
    m_engine(engine),
    m_reply(reply)
{
    QString type = m_reply->header(QNetworkRequest::ContentTypeHeader).toString();

    QRegExp charsetRegexp(".*charset=(.*)[\\s]*", Qt::CaseInsensitive, QRegExp::RegExp2);
    if (charsetRegexp.exactMatch(type)) {
        m_charset = charsetRegexp.capturedTexts().at(1);
    }

    if (m_reply->isReadable()) {
        QByteArray data = m_reply->readAll();

        QTextCodec *text = QTextCodec::codecForName(m_charset.toLatin1());
        m_text = text ? text->makeDecoder()->toUnicode(data) : QString::fromUtf8(data);

        if (type.contains("application/json")) {
            // TODO: add error handling
            JsonCodec json(m_engine);
            m_body = json.parse(data);
//        } else if (type.contains("application/x-www-form-urlencoded")) {
            // TODO: Implement parsing of form-urlencoded
//        } else if (type.contains("multipart/form-data")) {
            // TODO: Implement parsing of form-data
        } else {
            m_body = QJSValue(m_text);
        }
    }

    m_header = m_engine->newObject();
    QList<QNetworkReply::RawHeaderPair>::const_iterator it = m_reply->rawHeaderPairs().cbegin();
    for(; it != m_reply->rawHeaderPairs().cend(); it++) {
        m_header.setProperty(
                    QString::fromUtf8((*it).first).toLower(),
                    QString::fromUtf8((*it).second));
    }

    m_engine->setObjectOwnership(this, QQmlEngine::JavaScriptOwnership);
}

ResponsePrototype::~ResponsePrototype()
{
    delete m_reply;
}

int ResponsePrototype::statusType() const {
{'fraction_non_alphanumeric': 0.1409561360275998, 'fraction_numerical': 0.005421389847215377, 'mean_word_length': 3.378640776699029, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 31, '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': '6558794', 'n_tokens_mistral': 1287, 'n_tokens_neox': 1265, 'n_words': 236}