Commit 307e27e6 authored by Ghislain MARY's avatar Ghislain MARY
Browse files

Allow receiving and parsing of CPIM messages without buggy Content-Type header.

parent 18696f2a
......@@ -38,7 +38,7 @@ class Cpim::MessagePrivate : public ObjectPrivate {
public:
typedef list<shared_ptr<const Header> > PrivHeaderList;
shared_ptr<PrivHeaderList> cpimHeaders = make_shared<PrivHeaderList>();
shared_ptr<PrivHeaderList> cpimHeaders = make_shared<PrivHeaderList>(); // TODO: Remove this useless variable
shared_ptr<PrivHeaderList> messageHeaders = make_shared<PrivHeaderList>();
shared_ptr<PrivHeaderList> contentHeaders = make_shared<PrivHeaderList>();
string content;
......@@ -148,10 +148,11 @@ string Cpim::Message::asString () const {
L_D();
string output;
// TODO: Remove cpimHeaders
for (const auto &cpimHeader : *d->cpimHeaders)
output += cpimHeader->asString();
output += "\r\n";
// TODO Remove cpimHeaders
if (d->messageHeaders->size() > 0) {
for (const auto &messageHeader : *d->messageHeaders)
......
......@@ -36,9 +36,11 @@ namespace Cpim {
typedef std::shared_ptr<std::list<std::shared_ptr<const Cpim::Header> > > HeaderList;
// TODO: Remove these useless methods
HeaderList getCpimHeaders () const;
bool addCpimHeader (const Header &cpimHeader);
void removeCpimHeader (const Header &cpimHeader);
// TODO: Remove these useless methods
HeaderList getMessageHeaders () const;
bool addMessageHeader (const Header &messageHeader);
......@@ -51,7 +53,7 @@ namespace Cpim {
std::string getContent () const;
bool setContent (const std::string &content);
bool isValid () const;
bool isValid () const; // TODO: Remove this useless method
std::string asString () const;
......
......@@ -171,37 +171,47 @@ namespace Cpim {
// Warning: Call this function one time!
shared_ptr<Message> createMessage () const {
size_t size = mHeaders->size();
if (size < 2 || size > 3) {
if (size < 2 || size > 3) { // TODO: Check that size is == 2
lWarning() << "Bad headers lists size.";
return nullptr;
}
const shared_ptr<Message> message = make_shared<Message>();
const shared_ptr<ListHeaderNode> cpimHeaders = mHeaders->front();
if (find_if(cpimHeaders->cbegin(), cpimHeaders->cend(),
[](const shared_ptr<const HeaderNode> &headerNode) {
return Utils::iequals(headerNode->getName(), "content-type") && (ContentType(headerNode->getValue()) == ContentType::Cpim);
}) == cpimHeaders->cend()) {
lWarning() << "No MIME `Content-Type` found!";
return nullptr;
}
// Add MIME headers.
for (const auto &headerNode : *cpimHeaders) {
const shared_ptr<const Header> header = headerNode->createHeader();
if (!header || !message->addCpimHeader(*header))
// TODO: To remove
if (size == 3) {
const shared_ptr<ListHeaderNode> cpimHeaders = mHeaders->front();
if (find_if(cpimHeaders->cbegin(), cpimHeaders->cend(),
[](const shared_ptr<const HeaderNode> &headerNode) {
return Utils::iequals(headerNode->getName(), "content-type") && (ContentType(headerNode->getValue()) == ContentType::Cpim);
}) == cpimHeaders->cend()) {
lWarning() << "No MIME `Content-Type` found!";
return nullptr;
}
}
// Add message headers.
if (mHeaders->size() > 2) {
// Add MIME headers.
for (const auto &headerNode : *cpimHeaders) {
const shared_ptr<const Header> header = headerNode->createHeader();
if (!header || !message->addCpimHeader(*header))
return nullptr;
}
// Add message headers.
for (const auto &headerNode : **(++mHeaders->cbegin())) {
const shared_ptr<const Header> header = headerNode->createHeader();
if (!header || !message->addMessageHeader(*header))
return nullptr;
}
}
// TODO: To remove
else {
// Add message headers.
for (const auto &headerNode : *mHeaders->front()) {
const shared_ptr<const Header> header = headerNode->createHeader();
if (!header || !message->addMessageHeader(*header))
return nullptr;
}
}
// Add content headers.
for (const auto &headerNode : *mHeaders->back()) {
......
......@@ -34,10 +34,13 @@ LINPHONE_BEGIN_NAMESPACE
ChatMessageModifier::Result CpimChatMessageModifier::encode (const shared_ptr<ChatMessage> &message, int &errorCode) {
Cpim::Message cpimMessage;
// TODO: Remove this buggy Content-Type header
Cpim::GenericHeader cpimContentTypeHeader;
cpimContentTypeHeader.setName("Content-Type");
cpimContentTypeHeader.setValue(ContentType::Cpim.asString());
cpimMessage.addCpimHeader(cpimContentTypeHeader);
// TODO: Remove this buggy Content-Type header
Cpim::FromHeader cpimFromHeader;
cpimFromHeader.setValue(cpimAddressAsString(message->getFromAddress()));
......@@ -71,11 +74,13 @@ ChatMessageModifier::Result CpimChatMessageModifier::encode (const shared_ptr<Ch
const string contentBody = content->getBodyAsString();
cpimMessage.setContent(contentBody);
// TODO: Remove this check because of buggy Content-Type header
if (!cpimMessage.isValid()) {
lError() << "[CPIM] Message is invalid: " << contentBody;
errorCode = 500;
return ChatMessageModifier::Result::Error;
}
// TODO: Remove this check because of buggy Content-Type header
Content newContent;
newContent.setContentType(ContentType::Cpim);
......@@ -99,7 +104,7 @@ ChatMessageModifier::Result CpimChatMessageModifier::decode (const shared_ptr<Ch
const string contentBody = content->getBodyAsString();
const shared_ptr<const Cpim::Message> cpimMessage = Cpim::Message::createFromString(contentBody);
if (!cpimMessage || !cpimMessage->isValid()) {
if (!cpimMessage) {
lError() << "[CPIM] Message is invalid: " << contentBody;
errorCode = 500;
return ChatMessageModifier::Result::Error;
......
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