Commit 3a0bcc67 authored by Ghislain MARY's avatar Ghislain MARY

Some more improvements to Content-Type and Content-Disposition handling.

parent 0cf7325c
...@@ -107,6 +107,8 @@ namespace Utils { ...@@ -107,6 +107,8 @@ namespace Utils {
return str ? str : ""; return str ? str : "";
} }
LINPHONE_PUBLIC std::string trim (const std::string &str);
template<typename T> template<typename T>
LINPHONE_PUBLIC const T &getEmptyConstRefObject () { LINPHONE_PUBLIC const T &getEmptyConstRefObject () {
static const T object; static const T object;
......
...@@ -33,33 +33,40 @@ LINPHONE_BEGIN_NAMESPACE ...@@ -33,33 +33,40 @@ LINPHONE_BEGIN_NAMESPACE
class ContentDispositionPrivate : public ClonableObjectPrivate { class ContentDispositionPrivate : public ClonableObjectPrivate {
public: public:
string disposition; string disposition;
string parameter;
}; };
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
const ContentDisposition ContentDisposition::RecipientList("recipient-list"); const ContentDisposition ContentDisposition::RecipientList("recipient-list");
const ContentDisposition ContentDisposition::RecipientListHistory("recipient-list-history; handling=optional");
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
ContentDisposition::ContentDisposition (const string &disposition) : ClonableObject(*new ContentDispositionPrivate) { ContentDisposition::ContentDisposition (const string &disposition) : ClonableObject(*new ContentDispositionPrivate) {
L_D(); L_D();
d->disposition = disposition; size_t posParam = disposition.find(";");
d->disposition = Utils::trim(disposition.substr(0, posParam));
if (posParam != string::npos)
setParameter(Utils::trim(disposition.substr(posParam + 1)));
} }
ContentDisposition::ContentDisposition (const ContentDisposition &other) ContentDisposition::ContentDisposition (const ContentDisposition &other)
: ContentDisposition(other.getPrivate()->disposition) {} : ContentDisposition(other.asString()) {}
ContentDisposition &ContentDisposition::operator= (const ContentDisposition &other) { ContentDisposition &ContentDisposition::operator= (const ContentDisposition &other) {
L_D(); L_D();
if (this != &other) { if (this != &other) {
d->disposition = other.getPrivate()->disposition; d->disposition = other.getPrivate()->disposition;
setParameter(other.getParameter());
} }
return *this; return *this;
} }
bool ContentDisposition::operator== (const ContentDisposition &other) const { bool ContentDisposition::operator== (const ContentDisposition &other) const {
L_D(); L_D();
return d->disposition == other.getPrivate()->disposition; return d->disposition == other.getPrivate()->disposition
&& getParameter() == other.getParameter();
} }
bool ContentDisposition::operator!= (const ContentDisposition &other) const { bool ContentDisposition::operator!= (const ContentDisposition &other) const {
...@@ -76,10 +83,24 @@ bool ContentDisposition::isValid () const { ...@@ -76,10 +83,24 @@ bool ContentDisposition::isValid () const {
return !d->disposition.empty(); return !d->disposition.empty();
} }
const string &ContentDisposition::getParameter () const {
L_D();
return d->parameter;
}
void ContentDisposition::setParameter (const string &parameter) {
L_D();
d->parameter = parameter;
}
string ContentDisposition::asString () const { string ContentDisposition::asString () const {
L_D(); L_D();
if (isValid()) if (isValid()) {
return d->disposition; string asString = d->disposition;
if (!d->parameter.empty())
asString += ";" + d->parameter;
return asString;
}
return ""; return "";
} }
......
...@@ -46,9 +46,13 @@ public: ...@@ -46,9 +46,13 @@ public:
bool isEmpty () const; bool isEmpty () const;
bool isValid () const; bool isValid () const;
const std::string &getParameter () const;
void setParameter (const std::string &parameter);
std::string asString () const; std::string asString () const;
static const ContentDisposition RecipientList; static const ContentDisposition RecipientList;
static const ContentDisposition RecipientListHistory;
private: private:
L_DECLARE_PRIVATE(ContentDisposition); L_DECLARE_PRIVATE(ContentDisposition);
......
...@@ -56,20 +56,20 @@ ContentType::ContentType (const string &contentType) : ClonableObject(*new Conte ...@@ -56,20 +56,20 @@ ContentType::ContentType (const string &contentType) : ClonableObject(*new Conte
L_D(); L_D();
size_t pos = contentType.find('/'); size_t pos = contentType.find('/');
size_t posParam = contentType.find("; "); size_t posParam = contentType.find(";");
size_t end = contentType.length(); size_t end = contentType.length();
if (pos == string::npos) if (pos == string::npos)
return; return;
if (setType(contentType.substr(0, pos))) { if (setType(Utils::trim(contentType.substr(0, pos)))) {
if (posParam != string::npos) if (posParam != string::npos)
end = posParam; end = posParam;
if (!setSubType(contentType.substr(pos + 1, end - (pos + 1)))) if (!setSubType(Utils::trim(contentType.substr(pos + 1, end - (pos + 1)))))
d->type.clear(); d->type.clear();
} }
if (posParam != string::npos) if (posParam != string::npos)
setParameter(contentType.substr(posParam + 2)); // We remove the blankspace after the ;. setParameter(Utils::trim(contentType.substr(posParam + 1)));
} }
ContentType::ContentType (const string &type, const string &subType) : ClonableObject(*new ContentTypePrivate) { ContentType::ContentType (const string &type, const string &subType) : ClonableObject(*new ContentTypePrivate) {
......
...@@ -175,6 +175,12 @@ char *Utils::utf8ToChar (uint32_t ic) { ...@@ -175,6 +175,12 @@ char *Utils::utf8ToChar (uint32_t ic) {
return result; return result;
} }
string Utils::trim (const string &str) {
auto itFront = find_if_not(str.begin(), str.end(), [] (int c) { return isspace(c); });
auto itBack = find_if_not(str.rbegin(), str.rend(), [] (int c) { return isspace(c); }).base();
return (itBack <= itFront ? string() : string(itFront, itBack));
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
tm Utils::getTimeTAsTm (time_t time) { tm Utils::getTimeTAsTm (time_t time) {
......
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