Commit faa6ef52 authored by Ronan's avatar Ronan

fix(content-manager): clean code, avoid leaks

parent 4eb6176d
......@@ -218,9 +218,7 @@ string LocalConferenceEventHandlerPrivate::createNotifyMultipart (int notifyId)
contents.push_back(content);
}
ContentManager contentManager;
Content multipart = contentManager.contentsListToMultipart(contents);
return multipart.getBodyAsString();
return ContentManager::contentListToMultipart(contents).getBodyAsString();
}
string LocalConferenceEventHandlerPrivate::createNotifyParticipantAdded (const Address &addr, int notifyId) {
......
......@@ -220,11 +220,9 @@ void RemoteConferenceEventHandler::multipartNotifyReceived (const string &xmlBod
Content multipart;
multipart.setBody(xmlBody);
ContentManager manager;
list<Content> contents = manager.multipartToContentLists(multipart);
for (const auto &content : contents) {
for (const auto &content : ContentManager::multipartToContentList(multipart))
d->simpleNotifyReceived(content.getBodyAsString());
}
}
// -----------------------------------------------------------------------------
......
......@@ -17,89 +17,92 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <sstream>
#include "belle-sip/belle-sip.h"
#include <belle-sip/belle-sip.h>
#include "content-manager.h"
#include "content-type.h"
#include "linphone/content.h"
#include "logger/logger.h"
// =============================================================================
#include "content/content.h"
#define MULTIPART_BOUNDARY "---------------------------14737809831466499882746641449"
// =============================================================================
using namespace std;
LINPHONE_BEGIN_NAMESPACE
list<Content> ContentManager::multipartToContentLists (const Content &content) const {
belle_sip_multipart_body_handler_t *mpbh = belle_sip_multipart_body_handler_new_from_buffer((void *)content.getBodyAsString().c_str(), content.getBodyAsString().length(), MULTIPART_BOUNDARY);
list<Content> ContentManager::multipartToContentList (const Content &content) {
belle_sip_multipart_body_handler_t *mpbh = belle_sip_multipart_body_handler_new_from_buffer(
(void *)content.getBodyAsString().c_str(),
content.getBodyAsString().length(),
MULTIPART_BOUNDARY
);
belle_sip_object_ref(mpbh);
const belle_sip_list_t *parts = belle_sip_multipart_body_handler_get_parts(mpbh);
list<Content> contentsList = list<Content>();
while (parts) {
list<Content> contents;
for (const belle_sip_list_t *parts = belle_sip_multipart_body_handler_get_parts(mpbh); parts; parts = parts->next) {
belle_sip_body_handler_t *part = BELLE_SIP_BODY_HANDLER(parts->data);
const belle_sip_list_t *part_headers = belle_sip_body_handler_get_headers(part);
belle_sip_list_t *it;
belle_sip_header_content_type_t *part_content_type=NULL;;
for(it = (belle_sip_list_t *)part_headers;it!=NULL;it=it->next) {
belle_sip_header_content_type_t *part_content_type = nullptr;
for (belle_sip_list_t *it = (belle_sip_list_t *)part_headers; it; it = it->next) {
belle_sip_header_t *header = BELLE_SIP_HEADER(it->data);
if(strcasecmp("Content-Type",belle_sip_header_get_name(header)) == 0) {
part_content_type=BELLE_SIP_HEADER_CONTENT_TYPE(header);
if (strcasecmp("Content-Type", belle_sip_header_get_name(header)) == 0) {
part_content_type = BELLE_SIP_HEADER_CONTENT_TYPE(header);
break;
}
}
belle_sip_header_content_type_get_type(part_content_type);
belle_sip_memory_body_handler_get_buffer(BELLE_SIP_MEMORY_BODY_HANDLER(part));
Content retContent = Content();
ContentType type(belle_sip_header_content_type_get_type(part_content_type), belle_sip_header_content_type_get_subtype(part_content_type));
Content retContent;
retContent.setBody((const char *)belle_sip_memory_body_handler_get_buffer(BELLE_SIP_MEMORY_BODY_HANDLER(part)));
retContent.setContentType(type);
contentsList.push_back(retContent);
parts = parts->next;
retContent.setContentType(ContentType(
belle_sip_header_content_type_get_type(part_content_type),
belle_sip_header_content_type_get_subtype(part_content_type)
));
contents.push_back(retContent);
}
belle_sip_object_unref(mpbh);
return contentsList;
return contents;
}
Content ContentManager::contentsListToMultipart (const list<Content> &contents) const {
char *desc;
Content ContentManager::contentListToMultipart (const list<Content> &contents) {
string sub;
belle_sip_memory_body_handler_t *mbh = NULL;
belle_sip_multipart_body_handler_t *mpbh = belle_sip_multipart_body_handler_new(NULL, NULL, NULL, MULTIPART_BOUNDARY);
belle_sip_memory_body_handler_t *mbh = nullptr;
belle_sip_multipart_body_handler_t *mpbh = belle_sip_multipart_body_handler_new(
nullptr, nullptr, nullptr, MULTIPART_BOUNDARY
);
belle_sip_object_ref(mpbh);
for (const auto &content : contents) {
const ContentType &contentType = content.getContentType();
stringstream subtype;
sub = contentType.getSubType();
subtype << sub << "; charset=\"UTF-8\"";
belle_sip_header_t *content_type = BELLE_SIP_HEADER(
belle_sip_header_t *cContentType = BELLE_SIP_HEADER(
belle_sip_header_content_type_create(
contentType.getType().c_str(),
subtype.str().c_str()
)
);
string body = content.getBodyAsString();
mbh = belle_sip_memory_body_handler_new_copy_from_buffer(
(void *)content.getBodyAsString().c_str(),
content.getBodyAsString().length(),
NULL,
NULL
(void *)body.c_str(), body.length(), nullptr, nullptr
);
belle_sip_body_handler_add_header(BELLE_SIP_BODY_HANDLER(mbh), content_type);
belle_sip_body_handler_add_header(BELLE_SIP_BODY_HANDLER(mbh), cContentType);
belle_sip_multipart_body_handler_add_part(mpbh, BELLE_SIP_BODY_HANDLER(mbh));
}
desc = belle_sip_object_to_string(mpbh);
char *desc = belle_sip_object_to_string(mpbh);
Content content;
content.setBody(desc);
content.setContentType(ContentType("application", sub));
belle_sip_free(desc);
belle_sip_object_unref(mpbh);
Content retContent = Content();
ContentType type("application", sub);
retContent.setBody(desc);
retContent.setContentType(type);
return retContent;
return content;
}
LINPHONE_END_NAMESPACE
......@@ -24,18 +24,15 @@
#include "linphone/utils/general.h"
#include "content.h"
// =============================================================================
LINPHONE_BEGIN_NAMESPACE
class ContentManager {
public:
ContentManager () = default;
class Content;
std::list<Content> multipartToContentLists (const Content &content) const;
Content contentsListToMultipart (const std::list<Content> &contents) const;
namespace ContentManager {
std::list<Content> multipartToContentList (const Content &content);
Content contentListToMultipart (const std::list<Content> &contents);
};
LINPHONE_END_NAMESPACE
......
......@@ -22,14 +22,13 @@
#include <vector>
// TODO: Remove me.
#include "linphone/content.h"
#include "object/app-data-container.h"
#include "object/clonable-object.h"
// =============================================================================
L_DECL_C_STRUCT(LinphoneContent);
LINPHONE_BEGIN_NAMESPACE
class ContentType;
......
......@@ -160,13 +160,11 @@ static const char* part4 = \
"</presence>";
void multipart_to_list () {
ContentManager manager;
Content multipartContent;
multipartContent.setBody(multipart);
multipartContent.setContentType(ContentType("multipart", "related"));
list<Content> contents = manager.multipartToContentLists(multipartContent);
list<Content> contents = ContentManager::multipartToContentList(multipartContent);
BC_ASSERT_EQUAL(contents.size(), 4, int, "%d");
Content content1 = contents.front();
contents.pop_front();
......@@ -242,8 +240,6 @@ void multipart_to_list () {
}
void list_to_multipart () {
ContentManager manager;
Content content1;
content1.setBody(part1);
content1.setContentType(ContentType("application", "rlmi+xml"));
......@@ -258,7 +254,7 @@ void list_to_multipart () {
content4.setContentType(ContentType("application", "pidf+xml"));
list<Content> contents = {content1, content2, content3, content4};
Content multipartContent = manager.contentsListToMultipart(contents);
Content multipartContent = ContentManager::contentListToMultipart(contents);
string originalStr(multipart);
originalStr.erase(std::remove(originalStr.begin(), originalStr.end(), ' '), originalStr.end());
originalStr.erase(std::remove(originalStr.begin(), originalStr.end(), '\t'), originalStr.end());
......
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