Skip to content

Commit

Permalink
iterating over http header is simplified
Browse files Browse the repository at this point in the history
by adding helper function: THeaderHash::forEach<>()
  • Loading branch information
azadkuh committed Jun 30, 2016
1 parent 286ced0 commit a6ce9c5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
8 changes: 3 additions & 5 deletions example/helloworld/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void runServer(const QString& portOrPath) {
qPrintable(req->url().toString().toUtf8())
);
qDebug("[Headers (%d)]", h.size());
qhttp::for_each(h.constBegin(), h.constEnd(), [](auto iter) {
h.forEach([](auto iter) {
qDebug(" %s : %s",
iter.key().constData(),
iter.value().constData()
Expand Down Expand Up @@ -101,8 +101,7 @@ void runClient(QString url) {

// just for fun! print headers:
qDebug("\n[Headers:]");
const auto& hs = res->headers();
qhttp::for_each(hs.constBegin(), hs.constEnd(), [](auto cit) {
res->headers().forEach([](auto cit) {
qDebug("%s : %s", cit.key().constData(), cit.value().constData());
});
});
Expand Down Expand Up @@ -145,8 +144,7 @@ void runWeatherClient(const QString& cityName) {

// just for fun! print headers:
qDebug("\n[Headers:]");
const auto& hs = res->headers();
qhttp::for_each(hs.constBegin(), hs.constEnd(), [](auto cit){
res->headers().forEach([](auto cit) {
qDebug("%s : %s", cit.key().constData(), cit.value().constData());
});
});
Expand Down
8 changes: 2 additions & 6 deletions src/private/httpwriter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,8 @@ public:
TImpl* me = static_cast<TImpl*>(this);
me->prepareHeadersToWrite();

qhttp::for_each(TBase::iheaders.constBegin(), TBase::iheaders.constEnd(),
[this](const auto& cit) {
const QByteArray& field = cit.key();
const QByteArray& value = cit.value();

this->writeHeader(field, value);
TBase::iheaders.forEach([this](const auto& cit) {
this->writeHeader(cit.key(), cit.value());
});

isocket.writeRaw("\r\n");
Expand Down
25 changes: 15 additions & 10 deletions src/qhttpfwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,38 @@ struct http_parser;
namespace qhttp {
///////////////////////////////////////////////////////////////////////////////

/// QHash/QMap iterators are incompatibility with range for
template<class Iterator, class Func>
void for_each(Iterator first, Iterator last, Func&& f) {
while ( first != last ) {
f( first );
++first;
}
}

/** A map of request or response headers. */
class THeaderHash : public QHash<QByteArray, QByteArray>
{
public:
/** checks for a header item, regardless of the case of the characters. */
bool has(const QByteArray& key) const {
bool has(const QByteArray& key) const {
return contains(key.toLower());
}

/** checks if a header has the specified value ignoring the case of the characters. */
bool keyHasValue(const QByteArray& key, const QByteArray& value) const {
bool keyHasValue(const QByteArray& key, const QByteArray& value) const {
if ( !contains(key) )
return false;

const QByteArray& v = QHash<QByteArray, QByteArray>::value(key);
return qstrnicmp(value.constData(), v.constData(), v.size()) == 0;
}
};

/// QHash/QMap iterators are incompatibility with range for
template<class Iterator, class Func>
void for_each(Iterator first, Iterator last, Func&& f) {
while ( first != last ) {
f( first );
++first;
template<class Func>
void forEach(Func&& f) const {
for_each(constBegin(), constEnd(), f);
}
}
};


/** Request method enumeration.
Expand Down

0 comments on commit a6ce9c5

Please sign in to comment.