void QNetworkAccessHttpBackend::replyHeaderChanged() { // reconstruct the HTTP header QList<QPair<QByteArray, QByteArray> > headerMap = httpReply->header(); QList<QPair<QByteArray, QByteArray> >::ConstIterator it = headerMap.constBegin(), end = headerMap.constEnd(); QByteArray header; for (; it != end; ++it) { QByteArray value = rawHeader(it->first); if (!value.isEmpty()) value += ", "; value += it->second; setRawHeader(it->first, value); } setAttribute(QNetworkRequest::HttpStatusCodeAttribute, httpReply->statusCode()); setAttribute(QNetworkRequest::HttpReasonPhraseAttribute, httpReply->reasonPhrase()); // is it a redirection? switch (httpReply->statusCode()) { case 301: // Moved Permanently case 302: // Found case 303: // See Other case 307: // Temporary Redirect // What do we do about the caching of the HTML note? // The response to a 303 MUST NOT be cached, while the response to // all of the others is cacheable if the headers indicate it to be redirectionRequested(QUrl::fromEncoded(rawHeader("location"))); } metaDataChanged(); }
void Message::parse() { Content::parse(); QCString raw; if(!(raw = rawHeader(s_ubject.type())).isEmpty()) s_ubject.from7BitString(raw); if(!(raw = rawHeader(d_ate.type())).isEmpty()) d_ate.from7BitString(raw); }
QJsonDocument GetPocketApi::PreparsingReply(QObject *sender, bool& ok) { QJsonDocument doc; auto reply = qobject_cast<QNetworkReply*> (sender); if (!reply) { qWarning() << "Invalid reply"; emit error(tr("General error"), 503, ETGeneral); ok = false; return doc; } reply->deleteLater(); if (reply->error() != QNetworkReply::NoError && reply->error() != QNetworkReply::UnknownContentError && reply->error() != QNetworkReply::UnknownNetworkError && reply->error() != m_InvalidRequestError && reply->error() != m_AuthError && reply->error() != m_PermissionsRateError && reply->error() != m_MaintenanceError && reply->error() != QNetworkReply::ContentOperationNotPermittedError) { qWarning() << Q_FUNC_INFO << "There is network error: " << reply->error() << reply->errorString(); emit error(tr("Network error: %1").arg(reply->errorString()), reply->error(), ETGeneral); ok = false; return doc; } else if (reply->error() != QNetworkReply::NoError) { const int errorCode = reply->rawHeader("X-Error-Code").toInt(); const QString errorString = reply->rawHeader("X-Error"); qWarning() << Q_FUNC_INFO << "There is getpocket error: " << errorCode << errorString; emit error(errorString, errorCode, ETGetPocket); ok = false; return doc; } ok = false; QJsonParseError err; doc = QJsonDocument::fromJson(reply->readAll(), &err); if (err.error != QJsonParseError::NoError) { qWarning() << "Unable to generate json from reply"; emit error(tr("Reply data is corrupted"), 503, ETGetPocket); return doc; } ok = true; return doc; }
void GetPocketApi::handleRequestAccessToken() { emit requestFinished(true); auto reply = qobject_cast<QNetworkReply*> (sender()); if (!reply) { qWarning() << "Invalid reply"; emit error(tr("General error"), 503, ETGeneral); return; } reply->deleteLater(); bool result = false; if (reply->error() != QNetworkReply::NoError && reply->error() != QNetworkReply::UnknownContentError && reply->error() != QNetworkReply::UnknownNetworkError && reply->error() != m_InvalidRequestError && reply->error() != m_AuthError && reply->error() != m_PermissionsRateError && reply->error() != m_MaintenanceError && reply->error() != QNetworkReply::ContentOperationNotPermittedError) { qWarning() << Q_FUNC_INFO << "There is network error: " << reply->error() << reply->errorString(); emit error(tr("Network error: %1").arg(reply->errorString()), reply->error(), ETGeneral); } else if (reply->error() != QNetworkReply::NoError) { const int errorCode = reply->rawHeader("X-Error-Code").toInt(); const QString errorString = reply->rawHeader("X-Error"); qWarning() << Q_FUNC_INFO << "There is getpocket error: " << errorCode << errorString; emit error(errorString, errorCode, ETGetPocket); emit logged(result, QString(), QString()); return; } QJsonParseError err; QJsonDocument doc = QJsonDocument::fromJson(reply->readAll(), &err); if (err.error != QJsonParseError::NoError) { qWarning() << "Unable to generate json from reply"; emit error(tr("Reply data is corrupted"), 503, ETGetPocket); } const auto& accessToken = doc.object()["access_token"].toString(); const auto& userName = doc.object()["username"].toString(); result = !accessToken.isEmpty() && !userName.isEmpty(); emit logged(result, accessToken, userName); }
void HandleRead(boost::system::error_code const &error, std::size_t bytes) { if (error) return; std::vector<boost::asio::const_buffer> Buffers; //Raw request std::string rawHeader( Buffer.data() ); parseHttpHeader(rawHeader); formAnswer(); std::cout<<"Raw Request:\n"<<rawHeader<<std::endl; std::cout<<"Formed answer:\n"<<httpMessage.message; Buffers.push_back(boost::asio::const_buffer(httpMessage.message.c_str(), httpMessage.message.size() )); boost::asio::async_write(Socket, Buffers, Strand.wrap( boost::bind(&Connection::HandleWrite, shared_from_this(), boost::asio::placeholders::error) )); }
void NewsArticle::parse() { Message::parse(); QCString raw; if(!(raw = rawHeader(l_ines.type())).isEmpty()) l_ines.from7BitString(raw); }
void QNetworkAccessHttpBackend::checkForRedirect(const int statusCode) { switch (statusCode) { case 301: // Moved Permanently case 302: // Found case 303: // See Other case 307: // Temporary Redirect // What do we do about the caching of the HTML note? // The response to a 303 MUST NOT be cached, while the response to // all of the others is cacheable if the headers indicate it to be QByteArray header = rawHeader("location"); QUrl url = QUrl::fromEncoded(header); if (!url.isValid()) url = QUrl(QLatin1String(header)); redirectionRequested(url); } }
void MTcpSocket::readData() { qDebug("readData"); static int headerLength = _packetHeaderBytes.length(); switch(_readState) { case ReadReady: { qDebug("ReadReady"); _nextPacketSize = -1; } case WaitingForHeader: { qDebug("WaitingForHeader"); if(bytesAvailable() > headerLength) { qDebug("WaitingForHeader1"); QByteArray rawHeader(read(headerLength)); QString header(rawHeader); if(header != _packetHeader) { qDebug("WaitingForHeader->Error"); _readState = Error; qWarning("Encountered invalid packet header \"%s\".", qPrintable(header)); qDebug("Invalid header hex: [%s]", rawHeader.toHex().data()); Mara::MLogger::log(Mara::LogLevel::Error, QString("Invalid packet header: %1.").arg(qPrintable(header))); emit readyRead(); break; } _socketStream >> _nextPacketSize; _socketStream >> _nextPacketType; _readState = ReadingData; _nextPacketBytesRead = 0; _nextPacketBuffer.clear(); } else { qDebug("WaitingForHeader2"); _readState = WaitingForHeader; break; } }
QByteArray HTTPRequest::request() const { QByteArray r; r += m_method + " " + url().toEncoded( QUrl::RemoveScheme|QUrl::RemoveAuthority ) + " HTTP/" + m_ver + "\r\n"; r += "Host: " + url().host() + "\r\n"; r += "User-Agent: " + QString( "%1/%2 (%3)\r\n" ) .arg( qApp->applicationName(), qApp->applicationVersion(), Common::applicationOs() ).toUtf8(); foreach( const QByteArray &header, rawHeaderList() ) r += header + ": " + rawHeader( header ) + "\r\n"; if( !m_data.isEmpty() ) { r += "Content-Length: " + QByteArray::number( m_data.size() ) + "\r\n\r\n"; r += m_data; } else r += "\r\n"; return r; }
QList<QByteArray> TMailMessage::addresses(const QByteArray &field) const { QList<QByteArray> addrList; const QList<QByteArray> lst = rawHeader(field).split(','); for (const auto &ba : lst) { QByteArray addr; int i = ba.indexOf('<'); if (i >= 0) { int j = ba.indexOf('>', ++i); if (j > i) { addr = ba.mid(i, j - i); } } else { addr = ba.trimmed(); } if (!addr.isEmpty() && !addrList.contains(addr)) addrList << addr; } return addrList; }
void TMailMessage::addAddress(const QByteArray &field, const QByteArray &address, const QString &friendlyName) { QByteArray addr = rawHeader(field); if (!addr.isEmpty()) { addr += ", "; } if (!friendlyName.isEmpty()) { QByteArray uname = friendlyName.toUtf8(); if (uname.length() == friendlyName.length()) { addr += uname; } else { // multibyte char addr += THttpUtility::toMimeEncoded(friendlyName, textCodec); } addr += ' '; } addr += '<'; addr += address.trimmed(); addr += '>'; setRawHeader(field, addr); }
void QNetworkAccessHttpBackend::replyHeaderChanged() { // reconstruct the HTTP header QList<QPair<QByteArray, QByteArray> > headerMap = httpReply->header(); QList<QPair<QByteArray, QByteArray> >::ConstIterator it = headerMap.constBegin(), end = headerMap.constEnd(); QByteArray header; for (; it != end; ++it) { QByteArray value = rawHeader(it->first); if (!value.isEmpty()) { if (it->first.toLower() == "set-cookie") value += "\n"; else value += ", "; } value += it->second; setRawHeader(it->first, value); } setAttribute(QNetworkRequest::HttpStatusCodeAttribute, httpReply->statusCode()); setAttribute(QNetworkRequest::HttpReasonPhraseAttribute, httpReply->reasonPhrase()); // is it a redirection? const int statusCode = httpReply->statusCode(); checkForRedirect(statusCode); if (statusCode >= 500 && statusCode < 600) { QAbstractNetworkCache *nc = networkCache(); if (nc) { QNetworkCacheMetaData metaData = nc->metaData(url()); QNetworkHeadersPrivate cacheHeaders; cacheHeaders.setAllRawHeaders(metaData.rawHeaders()); QNetworkHeadersPrivate::RawHeadersList::ConstIterator it; it = cacheHeaders.findRawHeader("Cache-Control"); bool mustReValidate = false; if (it != cacheHeaders.rawHeaders.constEnd()) { QHash<QByteArray, QByteArray> cacheControl = parseHttpOptionHeader(it->second); if (cacheControl.contains("must-revalidate")) mustReValidate = true; } if (!mustReValidate && sendCacheContents(metaData)) return; } } if (statusCode == 304) { #if defined(QNETWORKACCESSHTTPBACKEND_DEBUG) qDebug() << "Received a 304 from" << url(); #endif QAbstractNetworkCache *nc = networkCache(); if (nc) { QNetworkCacheMetaData oldMetaData = nc->metaData(url()); QNetworkCacheMetaData metaData = fetchCacheMetaData(oldMetaData); if (oldMetaData != metaData) nc->updateMetaData(metaData); if (sendCacheContents(metaData)) return; } } if (statusCode != 304 && statusCode != 303) { if (!isCachingEnabled()) setCachingEnabled(true); } metaDataChanged(); }
/*! Returns the value of the header field content-type. */ QByteArray TInternetMessageHeader::contentType() const { return rawHeader("Content-Type"); }
/*! Returns the value of the header field content-length. */ uint TInternetMessageHeader::contentLength() const { return rawHeader("Content-Length").toUInt(); }
QByteArray TMailMessage::bcc() const { return rawHeader("Bcc"); }
QByteArray TMailMessage::to() const { return rawHeader("To"); }
QByteArray TMailMessage::from() const { return rawHeader("From"); }
QString TMailMessage::subject() const { return THttpUtility::fromMimeEncoded(rawHeader("Subject")); }
QNetworkCacheMetaData QNetworkAccessHttpBackend::fetchCacheMetaData(const QNetworkCacheMetaData &oldMetaData) const { QNetworkCacheMetaData metaData = oldMetaData; QNetworkHeadersPrivate cacheHeaders; cacheHeaders.setAllRawHeaders(metaData.rawHeaders()); QNetworkHeadersPrivate::RawHeadersList::ConstIterator it; QList<QByteArray> newHeaders = rawHeaderList(); foreach (QByteArray header, newHeaders) { QByteArray originalHeader = header; header = header.toLower(); bool hop_by_hop = (header == "connection" || header == "keep-alive" || header == "proxy-authenticate" || header == "proxy-authorization" || header == "te" || header == "trailers" || header == "transfer-encoding" || header == "upgrade"); if (hop_by_hop) continue; // Don't store Warning 1xx headers if (header == "warning") { QByteArray v = rawHeader(header); if (v.length() == 3 && v[0] == '1' && v[1] >= '0' && v[1] <= '9' && v[2] >= '0' && v[2] <= '9') continue; } it = cacheHeaders.findRawHeader(header); if (it != cacheHeaders.rawHeaders.constEnd()) { // Match the behavior of Firefox and assume Cache-Control: "no-transform" if (header == "content-encoding" || header == "content-range" || header == "content-type") continue; // For MS servers that send "Content-Length: 0" on 304 responses // ignore this too if (header == "content-length") continue; } #if defined(QNETWORKACCESSHTTPBACKEND_DEBUG) QByteArray n = rawHeader(header); QByteArray o; if (it != cacheHeaders.rawHeaders.constEnd()) o = (*it).second; if (n != o && header != "date") { qDebug() << "replacing" << header; qDebug() << "new" << n; qDebug() << "old" << o; } #endif cacheHeaders.setRawHeader(originalHeader, rawHeader(header)); }
/*! Returns the value of the header field date. */ QByteArray TInternetMessageHeader::date() const { return rawHeader("Date"); }
/*! Returns true if the Internet message header has an entry with the given \a key; otherwise returns false. */ bool TInternetMessageHeader::hasRawHeader(const QByteArray &key) const { return !rawHeader(key).isNull(); }