// TODO: Implement cache and use a tree to improve speed bool HttpServerRequestRouter::handleRequest(HttpServerRequest &request, HttpServerResponse &response) { const QString path{request.url().path()}; for (const auto &mapping: priv->mappings) { QRegularExpressionMatch match{mapping.path.match(path)}; if (match.hasMatch()) { if (mapping.method.size() && request.method() != mapping.method) continue; QStringList args{match.capturedTexts().mid(1)}; QVariant backup{request.customData()}; if (args.size()) { QVariantMap options{backup.toMap()}; options["args"] = options["args"].toStringList() + args; request.setCustomData(options); } if (mapping.handler(request, response)) return true; if (args.size()) request.setCustomData(backup); } } return false; }
void SessionStore::resetSession(HttpServerRequest &request) const { // init variables QList<QByteArray> headers(request.headers().values("Cookie")); QByteArray newValue; // remove old cookies request.headers().remove("Cookie"); // find cookies that don't match this store's settings for (int i = 0;i != headers.size();++i) { QList<QNetworkCookie> cookies(QNetworkCookie::parseCookies(headers[i])); for (int i = 0;i != cookies.size();++i) { if (cookies[i].name() != settings.name) { newValue += cookies[i].toRawForm(QNetworkCookie::NameAndValueOnly) + "; "; } } } if (!newValue.isEmpty()) { // removes the final "; " newValue.remove(newValue.size() - 2, 2); } // update the request headers request.headers().insert("Cookie", newValue); }
void Engine::createPredefinedObjects(JSContext *cx,JSObject *obj,HttpServerRequest &httpRequest) { JSObject *predefinedObj = NULL; predefinedObj = JS_DefineObject(cx,obj,"response",JsHttpServerResponse::getJsClass(),NULL,JSPROP_PERMANENT|JSPROP_READONLY|JSPROP_ENUMERATE); if ( predefinedObj==NULL ) { LogManager::getInstance()->warning(LOGGER_CLASSNAME,"Failed to create predefined \"response\" object"); } predefinedObj = JS_DefineObject(cx,obj,"request",JsHttpServerRequest::getJsClass(),NULL,JSPROP_PERMANENT|JSPROP_READONLY|JSPROP_ENUMERATE); if ( predefinedObj==NULL ) { LogManager::getInstance()->warning(LOGGER_CLASSNAME,"Failed to create predefined \"request\" object"); } predefinedObj = JS_DefineObject(cx,obj,"server",JsServer::getJsClass(),NULL,JSPROP_PERMANENT|JSPROP_READONLY|JSPROP_ENUMERATE); if ( predefinedObj==NULL ) { LogManager::getInstance()->warning(LOGGER_CLASSNAME,"Failed to create predefined \"server\" object"); } if ( httpRequest.getSession()!=NULL ) { predefinedObj = JS_DefineObject(cx,obj,"session",JsHttpSession::getJsClass(),NULL,JSPROP_PERMANENT|JSPROP_READONLY|JSPROP_ENUMERATE); if ( predefinedObj==NULL ) { LogManager::getInstance()->warning(LOGGER_CLASSNAME,"Failed to create predefined \"session\" object"); } else { JS_SetPrivate(cx,predefinedObj,new HttpSession::Ptr(httpRequest.getSession())); } } else { if ( JS_DefineProperty(cx,obj,"session",JSVAL_NULL,NULL,NULL,JSPROP_PERMANENT|JSPROP_READONLY|JSPROP_ENUMERATE)==JS_FALSE ) { LogManager::getInstance()->warning(LOGGER_CLASSNAME,"Failed to create predefined \"session\" object"); } } if ( httpRequest.getSite()!=NULL ) { predefinedObj = JS_DefineObject(cx,obj,"site",JsSite::getJsClass(),NULL,JSPROP_PERMANENT|JSPROP_READONLY|JSPROP_ENUMERATE); if ( predefinedObj==NULL ) { LogManager::getInstance()->warning(LOGGER_CLASSNAME,"Failed to create predefined \"site\" object"); } else { JS_SetPrivate(cx,predefinedObj,httpRequest.getSite()); } } else { if ( JS_DefineProperty(cx,obj,"site",JSVAL_NULL,NULL,NULL,JSPROP_PERMANENT|JSPROP_READONLY|JSPROP_ENUMERATE)==JS_FALSE ) { LogManager::getInstance()->warning(LOGGER_CLASSNAME,"Failed to create predefined \"site\" object"); } } }
void Worker::onRequestReady() { HttpServerRequest *request = qobject_cast<HttpServerRequest *>(sender()); QAbstractSocket *socket = request->socket(); HttpServerResponse *response = new HttpServerResponse(socket, request->responseOptions(), this); connect(socket, SIGNAL(disconnected()), response, SLOT(deleteLater())); connect(response, SIGNAL(finished()), response, SLOT(deleteLater())); if (request->headers().contains("Expect", "100-continue")) response->writeContinue(); handler->handleRequest(request, response); }
bool Handler::handleRequest(HttpServerRequest &request, HttpServerResponse &response) { // ... QStringList args = request.customData().toMap()["args"].toStringList(); // ... }
bool Engine::executeDirective(std::string directive,HttpServerRequest &httpRequest, std::string &output) { std::string name; std::map<std::string,std::string> attributes; if ( !parseDirective(directive,name,attributes) ) { return false; } if ( name=="include" ) { std::string fileUri = attributes["file"]; if ( httpRequest.getSite()!=NULL && Util::UriUtil::isValid(fileUri) ) { if ( !Util::UriUtil::isAbsolute(fileUri) ) { fileUri = Util::UriUtil::getParentSegment(httpRequest.getUri()) + "/" + fileUri; } std::string filePath = httpRequest.getSite()->getRealPath(fileUri); FILE *file = fopen(filePath.c_str(),"rb"); if ( file!=NULL ) { std::string script; if ( parseFile(file,httpRequest,script) ) { output = script; } fclose(file); } return true; } } return false; }
QByteArray SessionStore::session(const HttpServerRequest &request) const { // init variables QList<QByteArray> headers(request.headers().values("Cookie")); // try to find a compatible cookie... // ...and returns the first for (int i = 0;i != headers.size();++i) { QList<QByteArray> cookiePairs = headers[i].split(';'); QList<QNetworkCookie> cookies; for (int i = 0;i != cookiePairs.size();++i) cookies.append(QNetworkCookie::parseCookies(cookiePairs[i])); for (int i = 0;i != cookies.size();++i) { if (cookies[i].name() == settings.name) return unsignSession(cookies[i].value()); } } // cannot find a compatible cookie... // ...returns an empty value return QByteArray(); }