Commit faa6ef52 authored by Ronan's avatar Ronan
Browse files

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

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