Beispiel #1
0
void Test4::testRequest(Tufao::HttpServerRequest *request,
                        Tufao::HttpServerResponse *response)
{
    Q_ASSERT(request->method() == "GET");
    Q_ASSERT(request->url() == "/a/b/c/x/y/z");
    Q_ASSERT(request->httpVersion() == Tufao::HttpServerRequest::HTTP_1_1);

    Tufao::Headers headers = request->headers();
    for (Tufao::Headers::iterator i = headers.begin();i != headers.end();++i) {
        if (i.key() == Tufao::IByteArray("Host")) {
            Q_ASSERT(i.value() == "localhost:8080");
        } else if (i.key() == Tufao::IByteArray("UserDefined")) {
            Q_ASSERT(i.value() == "test4");
        } else if (i.key() == Tufao::IByteArray("Connection")) {
            Q_ASSERT(i.value() == "close");
        } else {
            qFatal("Unexpected header");
        }
    }

    Tufao::HttpServerResponse::Options options = response->options();
    Q_ASSERT(!options.testFlag(Tufao::HttpServerResponse::KEEP_ALIVE));
    Q_ASSERT(!options.testFlag(Tufao::HttpServerResponse::HTTP_1_0));
    Q_ASSERT(options.testFlag(Tufao::HttpServerResponse::HTTP_1_1));

    response->writeHead(Tufao::HttpServerResponse::OK);
    response->end("42\n");
}
Beispiel #2
0
ranges(const Tufao::Headers &headers, qulonglong fileSize)
{
    if (!headers.contains("Range"))
        return QList< QPair<qulonglong, qulonglong> >();

    QList< QPair<qulonglong, qulonglong> > ranges;

    QList<QByteArray> rangeHeaders(headers.values("Range"));
    foreach (QByteArray rangesSpecifier, rangeHeaders) {
        static const QByteArray bytesUnit("bytes=");
        if (!rangesSpecifier.startsWith(bytesUnit))
            continue;

        QList<QByteArray> rangeSet(rangesSpecifier.mid(bytesUnit.size())
                                   .split(','));
        foreach (QByteArray rangeSpec, rangeSet) {
            rangeSpec = rangeSpec.trimmed();

            if (rangeSpec.startsWith('-')) {
                bool ok;
                qulonglong nbytes = rangeSpec.mid(1).toULongLong(&ok);
                if (!ok || nbytes == 0)
                    continue;

                if (nbytes >= fileSize) {
                    ranges.push_back(QPair<qulonglong, qulonglong>
                                     (0, fileSize - 1));
                } else {
                    ranges.push_back(QPair<qulonglong, qulonglong>
                                     (fileSize - nbytes, fileSize - 1));
                }
            } else {
                int i = rangeSpec.indexOf('-');
                if (i == -1)
                    continue;

                bool ok[2];
                QPair<qulonglong, qulonglong>
                        range(rangeSpec.toULongLong(ok),
                              rangeSpec.mid(1 + i).toULongLong());

                if (!ok[1])
                    range.second = fileSize - 1;

                if (!ok[0] || range.second < range.first
                        || range.first >= fileSize)
                    continue;

                if (range.second >= fileSize)
                    range.second = fileSize;

                ranges.push_back(range);
            }
        }
    }
bool MyRequestHandler::handleRequest(Tufao::HttpServerRequest& request, Tufao::HttpServerResponse& response)
{

    QPair<QString, QString> userAuth = DBHandler::instance()->getPassword(QString("asanka"));

    QString requestPath = request.url().path();
    QStringList operations = requestPath.split("/");
    if (operations.size() > 1)
    {
        QString app = operations.at(1);
        if (mAppsMap.contains(app))
        {
            QString requestMethod = QString(request.method());
            operations.removeAt(0);
            operations.removeAt(0);
            QByteArray requestData = request.readBody();
            QMap<QString,QString> queryMap,headerMap;
            QUrlQuery query(request.url().query());
            QList<QPair<QString,QString> > queryItems = query.queryItems();
            for (int i=0; i<queryItems.size(); i++)
            {
                QPair<QString, QString> item = queryItems[i];
                queryMap.insert(item.first,item.second);
            }
            Tufao::Headers headers = request.headers();
            Tufao::Headers::iterator i;
            for (i=headers.begin(); i != headers.end(); ++i)
                headerMap[QString(i.key())] = QString(i.value());
            AbstractAppsHander *handler = mAppsMap[app];
            handler->handleRequest(operations,requestMethod,requestData,queryMap,headerMap,response);
        }
        else
        {
            response.writeHead(Tufao::HttpResponseStatus::NOT_IMPLEMENTED);
            response.end(JSONHandler::encodeError(INVALID_APPLICATION));
        }
    }
    else
    {
        response.writeHead(Tufao::HttpResponseStatus::BAD_REQUEST);
        response.end(JSONHandler::encodeError(INVALID_REQUEST));
    }


}