예제 #1
0
파일: test4.cpp 프로젝트: knopkem/nordwind
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");
}
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));
    }


}