From 1ad9a0789fb6fd80177c043d012bd8acaf7eb01a Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Thu, 1 Mar 2018 11:51:50 +0100 Subject: [PATCH] feat(ContentManager): refactoring, avoid buffer copy, use move, use constexpr, avoid casts... --- src/content/content-manager.cpp | 59 ++++++++++++++++----------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/src/content/content-manager.cpp b/src/content/content-manager.cpp index 600f57a48..8640014a2 100644 --- a/src/content/content-manager.cpp +++ b/src/content/content-manager.cpp @@ -23,44 +23,46 @@ #include "content-type.h" #include "content/content.h" -#define MULTIPART_BOUNDARY "---------------------------14737809831466499882746641449" - // ============================================================================= using namespace std; LINPHONE_BEGIN_NAMESPACE +namespace { + constexpr const char MultipartBoundary[] = "---------------------------14737809831466499882746641449"; +} + // ----------------------------------------------------------------------------- list ContentManager::multipartToContentList (const Content &content) { + const string body = content.getBodyAsString(); 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 + body.c_str(), body.length(), MultipartBoundary ); belle_sip_object_ref(mpbh); list 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_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_content_type_t *partContentType = nullptr; + for (const belle_sip_list_t *it = belle_sip_body_handler_get_headers(part); 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); + partContentType = BELLE_SIP_HEADER_CONTENT_TYPE(header); break; } } - Content retContent; - retContent.setBody((const char *)belle_sip_memory_body_handler_get_buffer(BELLE_SIP_MEMORY_BODY_HANDLER(part))); - retContent.setContentType(ContentType( - belle_sip_header_content_type_get_type(part_content_type), - belle_sip_header_content_type_get_subtype(part_content_type) + Content content; + content.setBody(static_cast( + belle_sip_memory_body_handler_get_buffer(BELLE_SIP_MEMORY_BODY_HANDLER(part)) )); - contents.push_back(retContent); + content.setContentType(ContentType( + belle_sip_header_content_type_get_type(partContentType), + belle_sip_header_content_type_get_subtype(partContentType) + )); + contents.push_back(move(content)); } belle_sip_object_unref(mpbh); @@ -68,31 +70,27 @@ list ContentManager::multipartToContentList (const Content &content) { } Content ContentManager::contentListToMultipart (const list &contents) { - 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 + nullptr, nullptr, nullptr, MultipartBoundary ); belle_sip_object_ref(mpbh); for (const auto &content : contents) { const ContentType &contentType = content.getContentType(); - stringstream subtype; - subtype << contentType.getSubType() << "; charset=\"UTF-8\""; belle_sip_header_t *cContentType = BELLE_SIP_HEADER( belle_sip_header_content_type_create( contentType.getType().c_str(), - subtype.str().c_str() + string(contentType.getSubType() + "; charset=\"UTF-8\"").c_str() ) ); - string body = content.getBodyAsString(); - mbh = belle_sip_memory_body_handler_new_copy_from_buffer( - (void *)body.c_str(), body.length(), nullptr, nullptr + const string body = content.getBodyAsString(); + belle_sip_memory_body_handler_t *mbh = belle_sip_memory_body_handler_new_copy_from_buffer( + body.c_str(), body.length(), nullptr, nullptr ); belle_sip_body_handler_add_header(BELLE_SIP_BODY_HANDLER(mbh), cContentType); - const list> headers = content.getHeaders(); - for (const auto &header : headers) { + for (const auto &header : content.getHeaders()) { belle_sip_header_t *additionalHeader = BELLE_SIP_HEADER( belle_sip_header_create( header.first.c_str(), @@ -104,18 +102,17 @@ Content ContentManager::contentListToMultipart (const list &contents) { belle_sip_multipart_body_handler_add_part(mpbh, BELLE_SIP_BODY_HANDLER(mbh)); } - char *desc = belle_sip_object_to_string(mpbh); + char *desc = belle_sip_object_to_string(mpbh); Content content; content.setBody(desc); - ContentType contentType = ContentType::Multipart; - string boundary = "boundary=" + string(MULTIPART_BOUNDARY); - contentType.setParameter(boundary); - content.setContentType(contentType); - belle_sip_free(desc); belle_sip_object_unref(mpbh); + ContentType contentType = ContentType::Multipart; + contentType.setParameter("boundary=" + string(MultipartBoundary)); + content.setContentType(ContentType::Multipart); + return content; }