Commit daddec29 authored by Mickaël Turnel's avatar Mickaël Turnel Committed by Ghislain MARY
Browse files

Changed Cpim message to be able to return headers from a specific namespace

parent 55909fbe
......@@ -18,6 +18,7 @@
*/
#include <algorithm>
#include <map>
#include "linphone/utils/utils.h"
......@@ -36,9 +37,10 @@ LINPHONE_BEGIN_NAMESPACE
class Cpim::MessagePrivate : public ObjectPrivate {
public:
typedef list<shared_ptr<const Header> > PrivHeaderList;
using PrivHeaderList = list<shared_ptr<const Header>>;
using PrivHeaderMap = map<string, shared_ptr<PrivHeaderList>>;
shared_ptr<PrivHeaderList> messageHeaders = make_shared<PrivHeaderList>();
PrivHeaderMap messageHeaders;
shared_ptr<PrivHeaderList> contentHeaders = make_shared<PrivHeaderList>();
string content;
};
......@@ -47,21 +49,47 @@ Cpim::Message::Message () : Object(*new MessagePrivate) {}
// -----------------------------------------------------------------------------
Cpim::Message::HeaderList Cpim::Message::getMessageHeaders () const {
Cpim::Message::HeaderList Cpim::Message::getMessageHeaders (const string &ns) const {
L_D();
return d->messageHeaders;
if (d->messageHeaders.find(ns) == d->messageHeaders.end())
return nullptr;
return d->messageHeaders.at(ns);
}
void Cpim::Message::addMessageHeader (const Header &messageHeader) {
void Cpim::Message::addMessageHeader (const Header &messageHeader, const string &ns) {
L_D();
d->messageHeaders->push_back(Parser::getInstance()->cloneHeader(messageHeader));
if (d->messageHeaders.find(ns) == d->messageHeaders.end())
d->messageHeaders[ns] = make_shared<Cpim::MessagePrivate::PrivHeaderList>();
auto list = d->messageHeaders.at(ns);
list->push_back(Parser::getInstance()->cloneHeader(messageHeader));
}
void Cpim::Message::removeMessageHeader (const Header &messageHeader) {
void Cpim::Message::removeMessageHeader (const Header &messageHeader, const string &ns) {
L_D();
d->messageHeaders->remove_if([&messageHeader](const shared_ptr<const Header> &header) {
return messageHeader.getName() == header->getName() && messageHeader.getValue() == header->getValue();
});
if (d->messageHeaders.find(ns) != d->messageHeaders.end())
d->messageHeaders.at(ns)->remove_if([&messageHeader](const shared_ptr<const Header> &header) {
return messageHeader.getName() == header->getName() && messageHeader.getValue() == header->getValue();
});
}
shared_ptr<const Cpim::Header> Cpim::Message::getMessageHeader (const string &name, const string &ns) {
L_D();
if (d->messageHeaders.find(ns) == d->messageHeaders.end())
return nullptr;
auto list = d->messageHeaders.at(ns);
for (const auto &messageHeader : *list) {
if (messageHeader->getName() == name)
return messageHeader;
}
return nullptr;
}
// -----------------------------------------------------------------------------
......@@ -83,6 +111,17 @@ void Cpim::Message::removeContentHeader (const Header &contentHeader) {
});
}
shared_ptr<const Cpim::Header> Cpim::Message::getContentHeader(const string &name) {
L_D();
for (const auto &contentHeader : *d->contentHeaders) {
if (contentHeader->getName() == name)
return contentHeader;
}
return nullptr;
}
// -----------------------------------------------------------------------------
string Cpim::Message::getContent () const {
......@@ -102,9 +141,15 @@ string Cpim::Message::asString () const {
L_D();
string output;
if (d->messageHeaders->size() > 0) {
for (const auto &messageHeader : *d->messageHeaders)
output += messageHeader->asString();
if (d->messageHeaders.size() > 0) {
for (const auto &entry : d->messageHeaders) {
auto list = entry.second;
for (const auto &messageHeader : *list) {
if (entry.first != "")
output += entry.first + ".";
output += messageHeader->asString();
}
}
output += "\r\n";
}
......
......@@ -34,15 +34,17 @@ namespace Cpim {
public:
Message ();
typedef std::shared_ptr<std::list<std::shared_ptr<const Cpim::Header> > > HeaderList;
typedef std::shared_ptr<std::list<std::shared_ptr<const Cpim::Header>>> HeaderList;
HeaderList getMessageHeaders () const;
void addMessageHeader (const Header &messageHeader);
void removeMessageHeader (const Header &messageHeader);
HeaderList getMessageHeaders (const std::string &ns = "") const;
void addMessageHeader (const Header &messageHeader, const std::string &ns = "");
void removeMessageHeader (const Header &messageHeader, const std::string &ns = "");
std::shared_ptr<const Cpim::Header> getMessageHeader (const std::string &name, const std::string &ns = "");
HeaderList getContentHeaders () const;
void addContentHeader (const Header &contentHeader);
void removeContentHeader (const Header &contentHeader);
std::shared_ptr<const Cpim::Header> getContentHeader (const std::string &name);
std::string getContent () const;
bool setContent (const std::string &content);
......
......@@ -18,7 +18,6 @@
*/
#include <set>
#include <unordered_map>
#include <belr/abnf.h>
#include <belr/grammarbuilder.h>
......@@ -542,11 +541,19 @@ namespace Cpim {
// Add message headers.
for (const auto &headerNode : mMessageHeaders) {
string ns = "";
string::size_type n = headerNode->getName().find(".");
if (n != string::npos) {
ns = headerNode->getName().substr(0, n);
headerNode->setName(headerNode->getName().substr(n + 1));
}
const shared_ptr<const Header> header = headerNode->createHeader();
if (!header)
return nullptr;
message->addMessageHeader(*header);
message->addMessageHeader(*header, ns);
}
// Add content headers.
......
......@@ -116,6 +116,14 @@ static void parse_rfc_example () {
string content = message->getContent();
BC_ASSERT_STRING_EQUAL(content.c_str(), body.c_str());
Cpim::Message::HeaderList list = message->getMessageHeaders();
if (!BC_ASSERT_PTR_NOT_NULL(list)) return;
BC_ASSERT_EQUAL(list->size(), 7, int, "%d");
list = message->getMessageHeaders("MyFeatures");
if (!BC_ASSERT_PTR_NOT_NULL(list)) return;
BC_ASSERT_EQUAL(list->size(), 2, int, "%d");
}
static void parse_message_with_generic_header_parameters () {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment