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

Change setters from bool to void and fix crash when a specific header is malformed

parent 17d19644
......@@ -53,14 +53,9 @@ string Cpim::ContactHeader::getUri () const {
return d->uri;
}
bool Cpim::ContactHeader::setUri (const string &uri) {
if (uri.empty())
return false;
void Cpim::ContactHeader::setUri (const string &uri) {
L_D();
d->uri = uri;
return true;
}
string Cpim::ContactHeader::getFormalName () const {
......@@ -68,10 +63,7 @@ string Cpim::ContactHeader::getFormalName () const {
return d->formalName;
}
bool Cpim::ContactHeader::setFormalName (const string &formalName) {
if (formalName.empty())
return false;
void Cpim::ContactHeader::setFormalName (const string &formalName) {
L_D();
if (formalName.front() == '\"' && formalName.back() == '\"')
d->formalName = formalName.substr(1, formalName.size() - 2);
......@@ -79,8 +71,6 @@ bool Cpim::ContactHeader::setFormalName (const string &formalName) {
d->formalName = formalName.substr(0, formalName.size() - 1);
else
d->formalName = formalName;
return true;
}
string Cpim::ContactHeader::getValue () const {
......@@ -102,8 +92,7 @@ public:
Cpim::MessageIdHeader::MessageIdHeader () : Header(*new MessageIdHeaderPrivate) {}
Cpim::MessageIdHeader::MessageIdHeader (const string &token) : MessageIdHeader() {
L_D();
d->token = token;
setToken(token);
}
string Cpim::MessageIdHeader::getToken () const {
......@@ -111,14 +100,9 @@ string Cpim::MessageIdHeader::getToken () const {
return d->token;
}
bool Cpim::MessageIdHeader::setToken (string token) {
if (token.empty())
return false;
void Cpim::MessageIdHeader::setToken (string token) {
L_D();
d->token = token;
return true;
}
string Cpim::MessageIdHeader::getValue () const {
......@@ -177,24 +161,20 @@ time_t Cpim::DateTimeHeader::getTime () const {
return Utils::getTmAsTimeT(result);
}
bool Cpim::DateTimeHeader::setTime (const time_t time) {
void Cpim::DateTimeHeader::setTime (const time_t time) {
L_D();
d->signOffset = "Z";
d->dateTime = Utils::getTimeTAsTm(time);
d->dateTime.tm_year += 1900;
return true;
}
bool Cpim::DateTimeHeader::setTime (const tm &time, const tm &timeOffset, const string &signOffset) {
void Cpim::DateTimeHeader::setTime (const tm &time, const tm &timeOffset, const string &signOffset) {
L_D();
d->dateTime = time;
d->dateTimeOffset = timeOffset;
d->signOffset = signOffset;
return true;
}
string Cpim::DateTimeHeader::getValue () const {
......@@ -246,9 +226,8 @@ public:
Cpim::NsHeader::NsHeader () : Header(*new NsHeaderPrivate) {}
Cpim::NsHeader::NsHeader (const string &uri, const string &prefixName) : NsHeader() {
L_D();
d->uri = uri;
d->prefixName = prefixName;
setUri(uri);
setPrefixName(prefixName);
}
string Cpim::NsHeader::getUri () const {
......@@ -256,14 +235,9 @@ string Cpim::NsHeader::getUri () const {
return d->uri;
}
bool Cpim::NsHeader::setUri (const string &uri) {
if (uri.empty())
return false;
void Cpim::NsHeader::setUri (const string &uri) {
L_D();
d->uri = uri;
return true;
}
string Cpim::NsHeader::getPrefixName () const {
......@@ -271,14 +245,9 @@ string Cpim::NsHeader::getPrefixName () const {
return d->prefixName;
}
bool Cpim::NsHeader::setPrefixName (const string &prefixName) {
if (prefixName.empty())
return false;
void Cpim::NsHeader::setPrefixName (const string &prefixName) {
L_D();
d->prefixName = prefixName;
return true;
}
string Cpim::NsHeader::getValue () const {
......@@ -305,9 +274,8 @@ public:
Cpim::RequireHeader::RequireHeader () : Header(*new RequireHeaderPrivate) {}
Cpim::RequireHeader::RequireHeader (const string &headerNames) : RequireHeader() {
L_D();
for (const string &header : Utils::split(headerNames, ",")) {
d->headerNames.push_back(header);
addHeaderName(header);
}
}
......@@ -321,14 +289,9 @@ list<string> Cpim::RequireHeader::getHeaderNames () const {
return d->headerNames;
}
bool Cpim::RequireHeader::addHeaderName (const string &headerName) {
if (headerName.empty())
return false;
void Cpim::RequireHeader::addHeaderName (const string &headerName) {
L_D();
d->headerNames.push_back(headerName);
return true;
}
string Cpim::RequireHeader::getValue () const {
......@@ -359,9 +322,8 @@ public:
Cpim::SubjectHeader::SubjectHeader () : Header(*new SubjectHeaderPrivate) {}
Cpim::SubjectHeader::SubjectHeader (const string &subject, const string &language) : SubjectHeader() {
L_D();
d->subject = subject;
d->language = language;
setSubject(subject);
setLanguage(language);
}
string Cpim::SubjectHeader::getSubject () const {
......@@ -369,14 +331,9 @@ string Cpim::SubjectHeader::getSubject () const {
return d->subject;
}
bool Cpim::SubjectHeader::setSubject (const string &subject) {
if (subject.empty())
return false;
void Cpim::SubjectHeader::setSubject (const string &subject) {
L_D();
d->subject = subject;
return true;
}
string Cpim::SubjectHeader::getLanguage () const {
......@@ -384,14 +341,9 @@ string Cpim::SubjectHeader::getLanguage () const {
return d->language;
}
bool Cpim::SubjectHeader::setLanguage (const string &language) {
if (!language.empty())
return false;
void Cpim::SubjectHeader::setLanguage (const string &language) {
L_D();
d->language = language;
return true;
}
string Cpim::SubjectHeader::getValue () const {
......
......@@ -57,10 +57,10 @@ namespace Cpim {
ContactHeader (const std::string &uri, const std::string &formalName = "");
std::string getUri () const;
bool setUri (const std::string &uri);
void setUri (const std::string &uri);
std::string getFormalName () const;
bool setFormalName (const std::string &formalName);
void setFormalName (const std::string &formalName);
std::string getValue () const override;
......@@ -94,7 +94,7 @@ namespace Cpim {
}
std::string getToken () const;
bool setToken (std::string token);
void setToken (std::string token);
std::string getValue () const override;
......@@ -126,9 +126,9 @@ namespace Cpim {
}
time_t getTime () const;
bool setTime (const time_t time);
void setTime (const time_t time);
bool setTime (const tm &time, const tm &timeOffset, const std::string &signOffset);
void setTime (const tm &time, const tm &timeOffset, const std::string &signOffset);
std::string getValue () const override;
......@@ -160,10 +160,10 @@ namespace Cpim {
}
std::string getPrefixName () const;
bool setPrefixName (const std::string &prefixName);
void setPrefixName (const std::string &prefixName);
std::string getUri () const;
bool setUri (const std::string &uri);
void setUri (const std::string &uri);
std::string getValue () const override;
......@@ -192,7 +192,7 @@ namespace Cpim {
}
std::list<std::string> getHeaderNames () const;
bool addHeaderName (const std::string &headerName);
void addHeaderName (const std::string &headerName);
std::string getValue () const override;
......@@ -220,10 +220,10 @@ namespace Cpim {
}
std::string getSubject () const;
bool setSubject (const std::string &subject);
void setSubject (const std::string &subject);
std::string getLanguage () const;
bool setLanguage (const std::string &language);
void setLanguage (const std::string &language);
std::string getValue () const override;
......
......@@ -34,24 +34,23 @@ LINPHONE_BEGIN_NAMESPACE
class Cpim::GenericHeaderPrivate : public HeaderPrivate {
public:
GenericHeaderPrivate () : parameters(make_shared<list<pair<string, string> > >()) {}
GenericHeaderPrivate () : parameters(make_shared<list<pair<string, string>>>()) {}
string name;
string value;
shared_ptr<list<pair<string, string> > > parameters;
shared_ptr<list<pair<string, string>>> parameters;
};
Cpim::GenericHeader::GenericHeader () : Header(*new GenericHeaderPrivate) {}
Cpim::GenericHeader::GenericHeader (string name, string value, string parameters) : GenericHeader() {
L_D();
d->name = name;
d->value = value;
setName(name);
setValue(value);
for (const auto &parameter : Utils::split(parameters, ';')) {
size_t equalIndex = parameter.find('=');
if (equalIndex != string::npos)
d->parameters->push_back(make_pair(parameter.substr(0, equalIndex), parameter.substr(equalIndex + 1)));
addParameter(parameter.substr(0, equalIndex), parameter.substr(equalIndex + 1));
}
}
......@@ -60,18 +59,15 @@ string Cpim::GenericHeader::getName () const {
return d->name;
}
bool Cpim::GenericHeader::setName (const string &name) {
void Cpim::GenericHeader::setName (const string &name) {
L_D();
static const set<string> reserved = {
"From", "To", "cc", "DateTime", "Subject", "NS", "Require"
};
if (reserved.find(name) != reserved.end())
return false;
d->name = name;
return true;
if (reserved.find(name) == reserved.end())
d->name = name;
}
string Cpim::GenericHeader::getValue () const {
......@@ -79,14 +75,9 @@ string Cpim::GenericHeader::getValue () const {
return d->value;
}
bool Cpim::GenericHeader::setValue (const string &value) {
if (value.empty())
return false;
void Cpim::GenericHeader::setValue (const string &value) {
L_D();
d->value = value;
return true;
}
Cpim::GenericHeader::ParameterList Cpim::GenericHeader::getParameters () const {
......@@ -94,10 +85,9 @@ Cpim::GenericHeader::ParameterList Cpim::GenericHeader::getParameters () const {
return d->parameters;
}
bool Cpim::GenericHeader::addParameter (const string &key, const string &value) {
void Cpim::GenericHeader::addParameter (const string &key, const string &value) {
L_D();
d->parameters->push_back(make_pair(key, value));
return true;
}
void Cpim::GenericHeader::removeParameter (const string &key, const string &value) {
......
......@@ -41,15 +41,15 @@ namespace Cpim {
GenericHeader (std::string name, std::string value, std::string parameters = "");
std::string getName () const override;
bool setName (const std::string &name);
void setName (const std::string &name);
std::string getValue () const override;
bool setValue (const std::string &value);
void setValue (const std::string &value);
typedef std::shared_ptr<const std::list<std::pair<std::string, std::string> > > ParameterList;
typedef std::shared_ptr<const std::list<std::pair<std::string, std::string>>> ParameterList;
ParameterList getParameters () const;
bool addParameter (const std::string &key, const std::string &value);
void addParameter (const std::string &key, const std::string &value);
void removeParameter (const std::string &key, const std::string &value);
std::string asString () const override;
......
......@@ -52,11 +52,9 @@ Cpim::Message::HeaderList Cpim::Message::getMessageHeaders () const {
return d->messageHeaders;
}
bool Cpim::Message::addMessageHeader (const Header &messageHeader) {
void Cpim::Message::addMessageHeader (const Header &messageHeader) {
L_D();
d->messageHeaders->push_back(Parser::getInstance()->cloneHeader(messageHeader));
return true;
}
void Cpim::Message::removeMessageHeader (const Header &messageHeader) {
......@@ -73,11 +71,9 @@ Cpim::Message::HeaderList Cpim::Message::getContentHeaders () const {
return d->contentHeaders;
}
bool Cpim::Message::addContentHeader (const Header &contentHeader) {
void Cpim::Message::addContentHeader (const Header &contentHeader) {
L_D();
d->contentHeaders->push_back(Parser::getInstance()->cloneHeader(contentHeader));
return true;
}
void Cpim::Message::removeContentHeader (const Header &contentHeader) {
......
......@@ -37,11 +37,11 @@ namespace Cpim {
typedef std::shared_ptr<std::list<std::shared_ptr<const Cpim::Header> > > HeaderList;
HeaderList getMessageHeaders () const;
bool addMessageHeader (const Header &messageHeader);
void addMessageHeader (const Header &messageHeader);
void removeMessageHeader (const Header &messageHeader);
HeaderList getContentHeaders () const;
bool addContentHeader (const Header &contentHeader);
void addContentHeader (const Header &contentHeader);
void removeContentHeader (const Header &contentHeader);
std::string getContent () const;
......
......@@ -17,6 +17,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <set>
#include <unordered_map>
#include <belr/abnf.h>
......@@ -65,7 +66,12 @@ namespace Cpim {
}
void setName (const string &name) {
mName = name;
static const set<string> reserved = {
"From", "To", "cc", "DateTime", "Subject", "NS", "Require"
};
if (reserved.find(name) == reserved.end())
mName = name;
}
string getParameters () const {
......@@ -537,15 +543,19 @@ namespace Cpim {
// Add message headers.
for (const auto &headerNode : mMessageHeaders) {
const shared_ptr<const Header> header = headerNode->createHeader();
if (!header || !message->addMessageHeader(*header))
if (!header)
return nullptr;
message->addMessageHeader(*header);
}
// Add content headers.
for (const auto &headerNode : mContentHeaders) {
const shared_ptr<const Header> header = headerNode->createHeader();
if (!header || !message->addContentHeader(*header))
if (!header)
return nullptr;
message->addContentHeader(*header);
}
return message;
......
......@@ -67,10 +67,8 @@ static void set_generic_header_name () {
for (const auto &entry : entries) {
Cpim::GenericHeader genericHeader(entry.first, "");
genericHeader.setName(entry.first);
const string name = genericHeader.getName();
BC_ASSERT_STRING_EQUAL(name.c_str(), entry.first.c_str());
BC_ASSERT_STRING_EQUAL(name.c_str(), "");
}
}
......@@ -168,22 +166,22 @@ static void build_message () {
Cpim::GenericHeader wackyMessageHeader("MyFeatures.WackyMessageOption", "Use-silly-font");
if (!BC_ASSERT_TRUE(message.addMessageHeader(fromHeader))) return;
if (!BC_ASSERT_TRUE(message.addMessageHeader(toHeader))) return;
if (!BC_ASSERT_TRUE(message.addMessageHeader(dateTimeHeader))) return;
if (!BC_ASSERT_TRUE(message.addMessageHeader(subjectHeader))) return;
if (!BC_ASSERT_TRUE(message.addMessageHeader(subjectWithLanguageHeader))) return;
if (!BC_ASSERT_TRUE(message.addMessageHeader(nsHeader))) return;
if (!BC_ASSERT_TRUE(message.addMessageHeader(requireHeader))) return;
if (!BC_ASSERT_TRUE(message.addMessageHeader(vitalMessageHeader))) return;
if (!BC_ASSERT_TRUE(message.addMessageHeader(wackyMessageHeader))) return;
message.addMessageHeader(fromHeader);
message.addMessageHeader(toHeader);
message.addMessageHeader(dateTimeHeader);
message.addMessageHeader(subjectHeader);
message.addMessageHeader(subjectWithLanguageHeader);
message.addMessageHeader(nsHeader);
message.addMessageHeader(requireHeader);
message.addMessageHeader(vitalMessageHeader);
message.addMessageHeader(wackyMessageHeader);
// Set Content headers.
Cpim::GenericHeader contentTypeHeader("Content-Type", "text/xml; charset=utf-8");
if (!BC_ASSERT_TRUE(message.addContentHeader(contentTypeHeader))) return;
message.addContentHeader(contentTypeHeader);
Cpim::GenericHeader contentIdHeader("Content-ID", "<1234567890@foo.com>");
if (!BC_ASSERT_TRUE(message.addContentHeader(contentIdHeader))) return;
Cpim::GenericHeader contentIdHeader("Content-ID", "<1234567890@foo.com>");
message.addContentHeader(contentIdHeader);
const string content = "<body>"
"Here is the text of my message."
......
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