mirror of
https://gitlab.linphone.org/BC/public/linphone-iphone.git
synced 2026-01-29 09:09:21 +00:00
feat(ContentManager): refactoring, avoid buffer copy, use move, use constexpr, avoid casts...
This commit is contained in:
parent
d440dfd3fd
commit
1ad9a0789f
1 changed files with 28 additions and 31 deletions
|
|
@ -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<Content> 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<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_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<const char *>(
|
||||
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<Content> ContentManager::multipartToContentList (const Content &content) {
|
|||
}
|
||||
|
||||
Content ContentManager::contentListToMultipart (const list<Content> &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<pair<string,string>> 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<Content> &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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue