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 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); }
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(); }