From 933081e72f724afb223cfd63ef239c43e237bbe2 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 7 Feb 2018 17:50:37 +0100 Subject: [PATCH 01/44] Started to remove old LinphoneContent and use a C wrapper above C++ Content --- coreapi/CMakeLists.txt | 1 - coreapi/Makefile.am | 1 - coreapi/private_structs.h | 11 -- include/CMakeLists.txt | 2 +- include/linphone/Makefile.am | 1 - .../linphone/{content.h => api/c-content.h} | 54 +++--- include/linphone/api/c-types.h | 6 + include/linphone/types.h | 6 - src/CMakeLists.txt | 1 + src/c-wrapper/api/c-content.cpp | 177 ++++++++++++++++++ src/c-wrapper/c-wrapper.h | 2 +- 11 files changed, 212 insertions(+), 50 deletions(-) rename include/linphone/{content.h => api/c-content.h} (87%) create mode 100644 src/c-wrapper/api/c-content.cpp diff --git a/coreapi/CMakeLists.txt b/coreapi/CMakeLists.txt index 03600de80..76a35259c 100644 --- a/coreapi/CMakeLists.txt +++ b/coreapi/CMakeLists.txt @@ -64,7 +64,6 @@ set(LINPHONE_SOURCE_FILES_C carddav.c chat.c contactprovider.c - content.c dial_plan.c dict.c ec-calibrator.c diff --git a/coreapi/Makefile.am b/coreapi/Makefile.am index 4e63d8879..c2b2ee53b 100644 --- a/coreapi/Makefile.am +++ b/coreapi/Makefile.am @@ -39,7 +39,6 @@ liblinphone_la_SOURCES=\ chat_file_transfer.c \ conference.cc conference_private.h \ contactprovider.c contact_providers_priv.h \ - content.c \ dial_plan.c \ dict.c \ ec-calibrator.c \ diff --git a/coreapi/private_structs.h b/coreapi/private_structs.h index 449609ba9..f7c6faba0 100644 --- a/coreapi/private_structs.h +++ b/coreapi/private_structs.h @@ -444,17 +444,6 @@ struct _EchoTester { unsigned int rate; }; -struct _LinphoneContent { - belle_sip_object_t base; - void *user_data; - SalBodyHandler *body_handler; - char *name; /**< used by RCS File transfer messages to store the original filename of the file to be downloaded from server */ - char *key; /**< used by RCS File transfer messages to store the key to encrypt file if needed */ - size_t keyLength; /**< Length of key in bytes */ - void *cryptoContext; /**< crypto context used to encrypt file for RCS file transfer */ - bool_t owned_fields; -}; - BELLE_SIP_DECLARE_VPTR_NO_EXPORT(LinphoneContent); struct _LinphoneBuffer { diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 055f1840d..90cba8715 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -33,7 +33,6 @@ set(ROOT_HEADER_FILES chat.h conference.h contactprovider.h - content.h core_utils.h core.h defs.h @@ -84,6 +83,7 @@ set(C_API_HEADER_FILES c-chat-message.h c-chat-room-cbs.h c-chat-room.h + c-content.h c-dial-plan.h c-event-log.h c-participant.h diff --git a/include/linphone/Makefile.am b/include/linphone/Makefile.am index dc9488e0b..56c2c3dda 100644 --- a/include/linphone/Makefile.am +++ b/include/linphone/Makefile.am @@ -14,7 +14,6 @@ linphone_include_HEADERS=\ chat.h \ conference.h \ contactprovider.h \ - content.h \ core.h \ core_utils.h \ defs.h \ diff --git a/include/linphone/content.h b/include/linphone/api/c-content.h similarity index 87% rename from include/linphone/content.h rename to include/linphone/api/c-content.h index 78ebc3114..fa030a309 100644 --- a/include/linphone/content.h +++ b/include/linphone/api/c-content.h @@ -1,33 +1,32 @@ /* -content.h -Copyright (C) 2010-2014 Belledonne Communications SARL + * c-content.h + * Copyright (C) 2010-2018 Belledonne Communications SARL + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. +#ifndef _L_C_CONTENT_H_ +#define _L_C_CONTENT_H_ -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -*/ - -#ifndef LINPHONE_CONTENT_H_ -#define LINPHONE_CONTENT_H_ - - -#include "linphone/types.h" +#include "linphone/api/c-types.h" +// ============================================================================= #ifdef __cplusplus -extern "C" { -#endif - + extern "C" { +#endif // ifdef __cplusplus /** * @addtogroup misc @@ -218,9 +217,8 @@ LINPHONE_PUBLIC void linphone_content_set_key(LinphoneContent *content, const ch * @} */ - #ifdef __cplusplus -} -#endif + } +#endif // ifdef __cplusplus -#endif /* LINPHONE_CONTENT_H_ */ +#endif // ifndef _L_C_CONTENT_H_ \ No newline at end of file diff --git a/include/linphone/api/c-types.h b/include/linphone/api/c-types.h index 6c109375f..30594f8ab 100644 --- a/include/linphone/api/c-types.h +++ b/include/linphone/api/c-types.h @@ -156,6 +156,12 @@ typedef struct _LinphoneDialPlan LinphoneDialPlan; **/ typedef struct _LinphoneParticipant LinphoneParticipant; +/** + * The LinphoneContent object holds data that can be embedded in a signaling message. + * @ingroup misc +**/ +typedef struct _LinphoneContent LinphoneContent; + // ============================================================================= // C Enums. // ============================================================================= diff --git a/include/linphone/types.h b/include/linphone/types.h index d3b03ce25..90a48c1ca 100644 --- a/include/linphone/types.h +++ b/include/linphone/types.h @@ -367,12 +367,6 @@ typedef unsigned int LinphoneContactSearchID; */ LINPHONE_DEPRECATED typedef LinphoneContactSearchID ContactSearchID; -/** - * The LinphoneContent object holds data that can be embedded in a signaling message. - * @ingroup misc -**/ -typedef struct _LinphoneContent LinphoneContent; - /** * Linphone core main object created by function linphone_core_new() . * @ingroup initializing diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 59ddaffc1..35634fd09 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -164,6 +164,7 @@ set(LINPHONE_CXX_OBJECTS_SOURCE_FILES c-wrapper/api/c-chat-room-cbs.cpp c-wrapper/api/c-chat-room.cpp c-wrapper/api/c-core.cpp + c-wrapper/api/c-content.cpp c-wrapper/api/c-dial-plan.cpp c-wrapper/api/c-event-log.cpp c-wrapper/api/c-participant.cpp diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp new file mode 100644 index 000000000..cfb569732 --- /dev/null +++ b/src/c-wrapper/api/c-content.cpp @@ -0,0 +1,177 @@ +/* + * c-content.cpp + * Copyright (C) 2010-2018 Belledonne Communications SARL + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "linphone/api/c-content.h" +#include "linphone/wrapper_utils.h" + +#include "c-wrapper/c-wrapper.h" + +#include "content/content.h" + +// ============================================================================= + +using namespace std; + +L_DECLARE_C_CLONABLE_OBJECT_IMPL(Content, + SalBodyHandler *body_handler; + void *cryptoContext; /**< crypto context used to encrypt file for RCS file transfer */ +) + +// ============================================================================= +// Reference and user data handling functions. +// ============================================================================= + +LinphoneContent * linphone_content_ref(LinphoneContent *content) { + belle_sip_object_ref(content); + return content; +} + +void linphone_content_unref(LinphoneContent *content) { + belle_sip_object_unref(content); +} + +void *linphone_content_get_user_data(const LinphoneContent *content) { + return L_GET_USER_DATA_FROM_C_OBJECT(content); +} + +void linphone_content_set_user_data(LinphoneContent *content, void *ud) { + return L_SET_USER_DATA_FROM_C_OBJECT(content, ud); +} + +// ============================================================================= + +const char * linphone_content_get_type(const LinphoneContent *content) { + return NULL; +} + +void linphone_content_set_type(LinphoneContent *content, const char *type) { + +} + +const char * linphone_content_get_subtype(const LinphoneContent *content) { + return NULL; +} + +void linphone_content_set_subtype(LinphoneContent *content, const char *subtype) { + +} + +uint8_t * linphone_content_get_buffer(const LinphoneContent *content) { + return NULL; +} + +void linphone_content_set_buffer(LinphoneContent *content, const uint8_t *buffer, size_t size) { + +} + +const char * linphone_content_get_string_buffer(const LinphoneContent *content) { + return NULL; +} + +void linphone_content_set_string_buffer(LinphoneContent *content, const char *buffer) { + +} + +size_t linphone_content_get_size(const LinphoneContent *content) { + return 0; +} + +void linphone_content_set_size(LinphoneContent *content, size_t size) { + +} + +const char * linphone_content_get_encoding(const LinphoneContent *content) { + return NULL; +} + +void linphone_content_set_encoding(LinphoneContent *content, const char *encoding) { + +} + +const char * linphone_content_get_name(const LinphoneContent *content) { + return NULL; +} + +void linphone_content_set_name(LinphoneContent *content, const char *name) { + +} + +bool_t linphone_content_is_multipart(const LinphoneContent *content) { + return FALSE; +} + +LinphoneContent * linphone_content_get_part(const LinphoneContent *content, int idx) { + return NULL; +} + +LinphoneContent * linphone_content_find_part_by_header(const LinphoneContent *content, const char *header_name, const char *header_value) { + return NULL; +} + +const char * linphone_content_get_custom_header(const LinphoneContent *content, const char *header_name) { + return NULL; +} + +const char *linphone_content_get_key(const LinphoneContent *content) { + return NULL; +} + +size_t linphone_content_get_key_size(const LinphoneContent *content) { + return 0; +} + +void linphone_content_set_key(LinphoneContent *content, const char *key, const size_t keyLength) { + +} + +// ============================================================================= +// Private functions. +// ============================================================================= + +LinphoneContent * linphone_content_new(void) { + return NULL; +} + +LinphoneContent * linphone_content_copy(const LinphoneContent *ref) { + return NULL; +} + +static LinphoneContent * linphone_content_new_with_body_handler(SalBodyHandler *body_handler) { + return NULL; +} + +LinphoneContent * linphone_core_create_content(LinphoneCore *lc) { + return NULL; +} + +/* crypto context is managed(allocated/freed) by the encryption function, so provide the address of field in the private structure */ +void ** linphone_content_get_cryptoContext_address(LinphoneContent *content) { + return &(content->cryptoContext);; +} + +LinphoneContent * linphone_content_from_sal_body_handler(SalBodyHandler *body_handler) { + if (body_handler) { + return linphone_content_new_with_body_handler(body_handler); + } + return NULL; +} + +SalBodyHandler * sal_body_handler_from_content(const LinphoneContent *content) { + return NULL; +} \ No newline at end of file diff --git a/src/c-wrapper/c-wrapper.h b/src/c-wrapper/c-wrapper.h index 2e1260e5e..b3bf0c8e0 100644 --- a/src/c-wrapper/c-wrapper.h +++ b/src/c-wrapper/c-wrapper.h @@ -37,6 +37,7 @@ F(ChatMessage, ChatMessage) \ F(AbstractChatRoom, ChatRoom) \ F(Core, Core) \ + F(Content, Content) \ F(DialPlan, DialPlan) \ F(EventLog, EventLog) \ F(MediaSessionParams, CallParams) \ @@ -83,7 +84,6 @@ BELLE_SIP_TYPE_ID(LinphoneConferenceParams), BELLE_SIP_TYPE_ID(LinphoneConfig), BELLE_SIP_TYPE_ID(LinphoneContactProvider), BELLE_SIP_TYPE_ID(LinphoneContactSearch), -BELLE_SIP_TYPE_ID(LinphoneContent), BELLE_SIP_TYPE_ID(LinphoneCoreCbs), BELLE_SIP_TYPE_ID(LinphoneErrorInfo), BELLE_SIP_TYPE_ID(LinphoneEvent), From 122098ac04ffb0549fd6c6ab18c1035d4f5e6276 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 8 Feb 2018 11:24:30 +0100 Subject: [PATCH 02/44] More work on C/C++ Contents --- include/linphone/api/c-chat-room.h | 2 +- src/c-wrapper/api/c-chat-message.cpp | 4 +- src/c-wrapper/api/c-chat-room.cpp | 4 +- src/c-wrapper/api/c-content.cpp | 127 ++++++++++++++---- src/chat/chat-message/chat-message-p.h | 4 +- src/chat/chat-message/chat-message.cpp | 31 +++-- src/chat/chat-room/abstract-chat-room.h | 3 +- src/chat/chat-room/chat-room.cpp | 2 +- src/chat/chat-room/chat-room.h | 3 +- src/chat/chat-room/proxy-chat-room.cpp | 2 +- src/chat/chat-room/proxy-chat-room.h | 3 +- .../file-transfer-chat-message-modifier.cpp | 8 +- src/content/content.cpp | 7 - src/content/content.h | 3 - src/content/file-content.cpp | 10 -- src/content/file-content.h | 3 - src/content/file-transfer-content.cpp | 9 -- src/content/file-transfer-content.h | 3 - 18 files changed, 132 insertions(+), 96 deletions(-) diff --git a/include/linphone/api/c-chat-room.h b/include/linphone/api/c-chat-room.h index 392bce10f..aea3a7f61 100644 --- a/include/linphone/api/c-chat-room.h +++ b/include/linphone/api/c-chat-room.h @@ -90,7 +90,7 @@ LINPHONE_PUBLIC LinphoneChatMessage* linphone_chat_room_create_message_2(Linphon * @param initial_content #LinphoneContent initial content. #LinphoneCoreVTable.file_transfer_send is invoked later to notify file transfer progress and collect next chunk of the message if LinphoneContent.data is NULL. * @return a new #LinphoneChatMessage */ -LINPHONE_PUBLIC LinphoneChatMessage* linphone_chat_room_create_file_transfer_message(LinphoneChatRoom *cr, const LinphoneContent* initial_content); +LINPHONE_PUBLIC LinphoneChatMessage* linphone_chat_room_create_file_transfer_message(LinphoneChatRoom *cr, LinphoneContent* initial_content); /** * get peer address \link linphone_core_get_chat_room() associated to \endlink this #LinphoneChatRoom diff --git a/src/c-wrapper/api/c-chat-message.cpp b/src/c-wrapper/api/c-chat-message.cpp index da453b39c..59bffa2d9 100644 --- a/src/c-wrapper/api/c-chat-message.cpp +++ b/src/c-wrapper/api/c-chat-message.cpp @@ -283,7 +283,9 @@ int linphone_chat_message_set_text(LinphoneChatMessage *msg, const char* text) { } LinphoneContent *linphone_chat_message_get_file_transfer_information(LinphoneChatMessage *msg) { - return L_GET_PRIVATE_FROM_C_OBJECT(msg)->getFileTransferInformation(); + const LinphonePrivate::Content *content = L_GET_PRIVATE_FROM_C_OBJECT(msg)->getFileTransferInformation(); + if (content) return L_GET_C_BACK_PTR(content); + return NULL; } // ============================================================================= diff --git a/src/c-wrapper/api/c-chat-room.cpp b/src/c-wrapper/api/c-chat-room.cpp index 3288f1073..11f89e053 100644 --- a/src/c-wrapper/api/c-chat-room.cpp +++ b/src/c-wrapper/api/c-chat-room.cpp @@ -350,8 +350,8 @@ const bctbx_list_t *linphone_chat_room_get_composing_addresses (LinphoneChatRoom return cr->composingAddresses; } -LinphoneChatMessage *linphone_chat_room_create_file_transfer_message(LinphoneChatRoom *cr, const LinphoneContent *initial_content) { - shared_ptr cppPtr = L_GET_CPP_PTR_FROM_C_OBJECT(cr)->createFileTransferMessage(initial_content); +LinphoneChatMessage *linphone_chat_room_create_file_transfer_message(LinphoneChatRoom *cr, LinphoneContent *initial_content) { + shared_ptr cppPtr = L_GET_CPP_PTR_FROM_C_OBJECT(cr)->createFileTransferMessage(L_GET_CPP_PTR_FROM_C_OBJECT(initial_content)); LinphoneChatMessage *object = L_INIT(ChatMessage); L_SET_CPP_PTR_FROM_C_OBJECT(object, cppPtr); return object; diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp index cfb569732..7af4b4073 100644 --- a/src/c-wrapper/api/c-content.cpp +++ b/src/c-wrapper/api/c-content.cpp @@ -23,6 +23,9 @@ #include "c-wrapper/c-wrapper.h" #include "content/content.h" +#include "content/content-type.h" +#include "content/file-content.h" +#include "content/file-transfer-content.h" // ============================================================================= @@ -31,6 +34,7 @@ using namespace std; L_DECLARE_C_CLONABLE_OBJECT_IMPL(Content, SalBodyHandler *body_handler; void *cryptoContext; /**< crypto context used to encrypt file for RCS file transfer */ + mutable char *name; ) // ============================================================================= @@ -57,112 +61,176 @@ void linphone_content_set_user_data(LinphoneContent *content, void *ud) { // ============================================================================= const char * linphone_content_get_type(const LinphoneContent *content) { - return NULL; + return L_STRING_TO_C(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType().getType()); } void linphone_content_set_type(LinphoneContent *content, const char *type) { - + LinphonePrivate::ContentType ct = L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType(); + ct.setType(L_C_TO_STRING(type)); + L_GET_CPP_PTR_FROM_C_OBJECT(content)->setContentType(ct); } const char * linphone_content_get_subtype(const LinphoneContent *content) { - return NULL; + return L_STRING_TO_C(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType().getSubType()); } void linphone_content_set_subtype(LinphoneContent *content, const char *subtype) { - + LinphonePrivate::ContentType ct = L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType(); + ct.setSubType(L_C_TO_STRING(subtype)); + L_GET_CPP_PTR_FROM_C_OBJECT(content)->setContentType(ct); } uint8_t * linphone_content_get_buffer(const LinphoneContent *content) { - return NULL; + return (uint8_t *)L_STRING_TO_C(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getBodyAsString()); } void linphone_content_set_buffer(LinphoneContent *content, const uint8_t *buffer, size_t size) { - + L_GET_CPP_PTR_FROM_C_OBJECT(content)->setBody(buffer, size); } const char * linphone_content_get_string_buffer(const LinphoneContent *content) { - return NULL; + return L_STRING_TO_C(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getBodyAsString()); } void linphone_content_set_string_buffer(LinphoneContent *content, const char *buffer) { - + L_GET_CPP_PTR_FROM_C_OBJECT(content)->setBody(L_C_TO_STRING(buffer)); } size_t linphone_content_get_size(const LinphoneContent *content) { - return 0; + size_t size = L_GET_CPP_PTR_FROM_C_OBJECT(content)->getSize(); + if (size == 0) { + size = sal_body_handler_get_size(content->body_handler); + } + return size; } void linphone_content_set_size(LinphoneContent *content, size_t size) { - + sal_body_handler_set_size(content->body_handler, size); } const char * linphone_content_get_encoding(const LinphoneContent *content) { - return NULL; + return sal_body_handler_get_encoding(content->body_handler); } void linphone_content_set_encoding(LinphoneContent *content, const char *encoding) { - + sal_body_handler_set_encoding(content->body_handler, encoding); } const char * linphone_content_get_name(const LinphoneContent *content) { - return NULL; + const LinphonePrivate::Content *c = L_GET_CPP_PTR_FROM_C_OBJECT(content); + if (c->isFile()) { + const LinphonePrivate::FileContent *fc = static_cast(c); + if (content->name) ms_free(content->name); + content->name = ms_strdup(L_STRING_TO_C(fc->getFileName())); + } else if (c->getContentType() == LinphonePrivate::ContentType::FileTransfer) { + const LinphonePrivate::FileTransferContent *ftc = static_cast(c); + if (content->name) ms_free(content->name); + content->name = ms_strdup(L_STRING_TO_C(ftc->getFileName())); + } + return content->name; } void linphone_content_set_name(LinphoneContent *content, const char *name) { - + LinphonePrivate::Content *c = L_GET_CPP_PTR_FROM_C_OBJECT(content); + if (c->isFile()) { + LinphonePrivate::FileContent *fc = static_cast(c); + fc->setFileName(L_C_TO_STRING(name)); + } else if (c->getContentType() == LinphonePrivate::ContentType::FileTransfer) { + LinphonePrivate::FileTransferContent *ftc = static_cast(c); + ftc->setFileName(L_C_TO_STRING(name)); + } + content->name = ms_strdup(name); } bool_t linphone_content_is_multipart(const LinphoneContent *content) { - return FALSE; + return sal_body_handler_is_multipart(content->body_handler); } LinphoneContent * linphone_content_get_part(const LinphoneContent *content, int idx) { - return NULL; + SalBodyHandler *part_body_handler; + if (!linphone_content_is_multipart(content)) return NULL; + part_body_handler = sal_body_handler_get_part(content->body_handler, idx); + return linphone_content_from_sal_body_handler(part_body_handler); } LinphoneContent * linphone_content_find_part_by_header(const LinphoneContent *content, const char *header_name, const char *header_value) { - return NULL; + SalBodyHandler *part_body_handler; + if (!linphone_content_is_multipart(content)) return NULL; + part_body_handler = sal_body_handler_find_part_by_header(content->body_handler, header_name, header_value); + return linphone_content_from_sal_body_handler(part_body_handler); } const char * linphone_content_get_custom_header(const LinphoneContent *content, const char *header_name) { - return NULL; + return sal_body_handler_get_header(content->body_handler, header_name); } const char *linphone_content_get_key(const LinphoneContent *content) { + const LinphonePrivate::Content *c = L_GET_CPP_PTR_FROM_C_OBJECT(content); + if (c->isFile()) { + const LinphonePrivate::FileContent *fc = static_cast(c); + return L_STRING_TO_C(fc->getFileKey()); + } return NULL; } size_t linphone_content_get_key_size(const LinphoneContent *content) { + const LinphonePrivate::Content *c = L_GET_CPP_PTR_FROM_C_OBJECT(content); + if (c->isFile()) { + const LinphonePrivate::FileContent *fc = static_cast(c); + return fc->getFileKey().length(); + } return 0; } void linphone_content_set_key(LinphoneContent *content, const char *key, const size_t keyLength) { - + LinphonePrivate::Content *c = L_GET_CPP_PTR_FROM_C_OBJECT(content); + if (c->isFile()) { + LinphonePrivate::FileContent *fc = static_cast(c); + fc->setFileKey(L_C_TO_STRING(key)); + } } // ============================================================================= // Private functions. // ============================================================================= -LinphoneContent * linphone_content_new(void) { - return NULL; -} - -LinphoneContent * linphone_content_copy(const LinphoneContent *ref) { - return NULL; +static void linphone_content_set_sal_body_handler(LinphoneContent *content, SalBodyHandler *body_handler) { + if (content->body_handler != NULL) { + sal_body_handler_unref(content->body_handler); + content->body_handler = NULL; + } + content->body_handler = sal_body_handler_ref(body_handler); } static LinphoneContent * linphone_content_new_with_body_handler(SalBodyHandler *body_handler) { - return NULL; + LinphoneContent *content = L_INIT(Content); + content->cryptoContext = NULL; + if (body_handler == NULL) { + linphone_content_set_sal_body_handler(content, sal_body_handler_new()); + } else { + linphone_content_set_sal_body_handler(content, body_handler); + } + LinphonePrivate::Content *c = new LinphonePrivate::Content(); + L_SET_CPP_PTR_FROM_C_OBJECT(content, c); + return content; +} + +LinphoneContent * linphone_content_new(void) { + return linphone_content_new_with_body_handler(NULL); +} + +LinphoneContent * linphone_content_copy(const LinphoneContent *ref) { + //TODO + return (LinphoneContent *)belle_sip_object_ref(belle_sip_object_clone(BELLE_SIP_OBJECT(ref))); } LinphoneContent * linphone_core_create_content(LinphoneCore *lc) { - return NULL; + return linphone_content_new(); } /* crypto context is managed(allocated/freed) by the encryption function, so provide the address of field in the private structure */ void ** linphone_content_get_cryptoContext_address(LinphoneContent *content) { - return &(content->cryptoContext);; + return &(content->cryptoContext); } LinphoneContent * linphone_content_from_sal_body_handler(SalBodyHandler *body_handler) { @@ -173,5 +241,6 @@ LinphoneContent * linphone_content_from_sal_body_handler(SalBodyHandler *body_ha } SalBodyHandler * sal_body_handler_from_content(const LinphoneContent *content) { - return NULL; + if (content == NULL) return NULL; + return content->body_handler; } \ No newline at end of file diff --git a/src/chat/chat-message/chat-message-p.h b/src/chat/chat-message/chat-message-p.h index 0eae6c24e..b47355e3b 100644 --- a/src/chat/chat-message/chat-message-p.h +++ b/src/chat/chat-message/chat-message-p.h @@ -119,8 +119,8 @@ public: bool hasFileTransferContent () const; const Content* getFileTransferContent () const; - LinphoneContent *getFileTransferInformation () const; - void setFileTransferInformation (const LinphoneContent *content); + const Content* getFileTransferInformation () const; + void setFileTransferInformation (Content *content); bool downloadFile (); diff --git a/src/chat/chat-message/chat-message.cpp b/src/chat/chat-message/chat-message.cpp index d6f7d3f95..855dbd902 100644 --- a/src/chat/chat-message/chat-message.cpp +++ b/src/chat/chat-message/chat-message.cpp @@ -284,33 +284,36 @@ void ChatMessagePrivate::setText (const string &text) { } } -LinphoneContent *ChatMessagePrivate::getFileTransferInformation () const { +const Content *ChatMessagePrivate::getFileTransferInformation () const { if (hasFileTransferContent()) { - return getFileTransferContent()->toLinphoneContent(); + return getFileTransferContent(); } for (const Content *c : contents) { if (c->isFile()) { FileContent *fileContent = (FileContent *)c; - return fileContent->toLinphoneContent(); + return fileContent; } } return NULL; } -void ChatMessagePrivate::setFileTransferInformation (const LinphoneContent *c_content) { +void ChatMessagePrivate::setFileTransferInformation (Content *content) { L_Q(); - // Create a FileContent, it will create the FileTransferContent at upload time - FileContent *fileContent = new FileContent(); - ContentType contentType(linphone_content_get_type(c_content), linphone_content_get_subtype(c_content)); - fileContent->setContentType(contentType); - fileContent->setFileSize(linphone_content_get_size(c_content)); - fileContent->setFileName(linphone_content_get_name(c_content)); - if (linphone_content_get_string_buffer(c_content) != NULL) { - fileContent->setBody(linphone_content_get_string_buffer(c_content)); + if (content->isFile()) { + q->addContent(*content); + } else { + // This scenario is more likely to happen because the caller is using the C API + LinphoneContent *c_content = L_GET_C_BACK_PTR(content); + FileContent *fileContent = new FileContent(); + fileContent->setContentType(content->getContentType()); + fileContent->setFileSize(linphone_content_get_size(c_content)); // This information is only available from C Content if it was created from C API + fileContent->setFileName(linphone_content_get_name(c_content)); // This information is only available from C Content if it was created from C API + if (!content->isEmpty()) { + fileContent->setBody(content->getBody()); + } + q->addContent(*fileContent); } - - q->addContent(*fileContent); } bool ChatMessagePrivate::downloadFile () { diff --git a/src/chat/chat-room/abstract-chat-room.h b/src/chat/chat-room/abstract-chat-room.h index 46f5aec29..6cd260a82 100644 --- a/src/chat/chat-room/abstract-chat-room.h +++ b/src/chat/chat-room/abstract-chat-room.h @@ -87,8 +87,7 @@ public: virtual std::shared_ptr createChatMessage () = 0; virtual std::shared_ptr createChatMessage (const std::string &text) = 0; - // TODO: Remove LinphoneContent by LinphonePrivate::Content. - virtual std::shared_ptr createFileTransferMessage (const LinphoneContent *initialContent) = 0; + virtual std::shared_ptr createFileTransferMessage (Content *initialContent) = 0; virtual std::shared_ptr findChatMessage (const std::string &messageId) const = 0; virtual std::shared_ptr findChatMessage ( diff --git a/src/chat/chat-room/chat-room.cpp b/src/chat/chat-room/chat-room.cpp index 14e92cf85..03b8cd079 100644 --- a/src/chat/chat-room/chat-room.cpp +++ b/src/chat/chat-room/chat-room.cpp @@ -393,7 +393,7 @@ shared_ptr ChatRoom::createChatMessage (const string &text) { return chatMessage; } -shared_ptr ChatRoom::createFileTransferMessage (const LinphoneContent *initialContent) { +shared_ptr ChatRoom::createFileTransferMessage (Content *initialContent) { shared_ptr chatMessage = createChatMessage(); chatMessage->getPrivate()->setFileTransferInformation(initialContent); return chatMessage; diff --git a/src/chat/chat-room/chat-room.h b/src/chat/chat-room/chat-room.h index 35d887e59..9c6dbb999 100644 --- a/src/chat/chat-room/chat-room.h +++ b/src/chat/chat-room/chat-room.h @@ -61,8 +61,7 @@ public: std::shared_ptr createChatMessage () override; std::shared_ptr createChatMessage (const std::string &text) override; - // TODO: Remove LinphoneContent by LinphonePrivate::Content. - std::shared_ptr createFileTransferMessage (const LinphoneContent *initialContent) override; + std::shared_ptr createFileTransferMessage (Content *initialContent) override; std::shared_ptr findChatMessage (const std::string &messageId) const override; std::shared_ptr findChatMessage ( diff --git a/src/chat/chat-room/proxy-chat-room.cpp b/src/chat/chat-room/proxy-chat-room.cpp index bf984d2a1..29a9ddb80 100644 --- a/src/chat/chat-room/proxy-chat-room.cpp +++ b/src/chat/chat-room/proxy-chat-room.cpp @@ -264,7 +264,7 @@ shared_ptr ProxyChatRoom::createChatMessage (const string &text) { return d->chatRoom->createChatMessage(text); } -shared_ptr ProxyChatRoom::createFileTransferMessage (const LinphoneContent *initialContent) { +shared_ptr ProxyChatRoom::createFileTransferMessage (Content *initialContent) { L_D(); return d->chatRoom->createFileTransferMessage(initialContent); } diff --git a/src/chat/chat-room/proxy-chat-room.h b/src/chat/chat-room/proxy-chat-room.h index 16a5c49aa..6b9a75428 100644 --- a/src/chat/chat-room/proxy-chat-room.h +++ b/src/chat/chat-room/proxy-chat-room.h @@ -62,8 +62,7 @@ public: std::shared_ptr createChatMessage () override; std::shared_ptr createChatMessage (const std::string &text) override; - // TODO: Remove LinphoneContent by LinphonePrivate::Content. - std::shared_ptr createFileTransferMessage (const LinphoneContent *initialContent) override; + std::shared_ptr createFileTransferMessage (Content *initialContent) override; std::shared_ptr findChatMessage (const std::string &messageId) const override; std::shared_ptr findChatMessage ( diff --git a/src/chat/modifier/file-transfer-chat-message-modifier.cpp b/src/chat/modifier/file-transfer-chat-message-modifier.cpp index 30d088d2c..ac99d2201 100644 --- a/src/chat/modifier/file-transfer-chat-message-modifier.cpp +++ b/src/chat/modifier/file-transfer-chat-message-modifier.cpp @@ -100,7 +100,7 @@ void FileTransferChatMessageModifier::fileTransferOnProgress ( LinphoneChatMessage *msg = L_GET_C_BACK_PTR(message); LinphoneChatMessageCbs *cbs = linphone_chat_message_get_callbacks(msg); - LinphoneContent *content = currentFileContentToTransfer->toLinphoneContent(); + LinphoneContent *content = L_GET_C_BACK_PTR((Content *)currentFileContentToTransfer); if (linphone_chat_message_cbs_get_file_transfer_progress_indication(cbs)) { linphone_chat_message_cbs_get_file_transfer_progress_indication(cbs)(msg, content, offset, total); } else { @@ -150,7 +150,7 @@ int FileTransferChatMessageModifier::onSendBody ( LinphoneChatMessageCbs *cbs = linphone_chat_message_get_callbacks(msg); LinphoneChatMessageCbsFileTransferSendCb file_transfer_send_cb = linphone_chat_message_cbs_get_file_transfer_send(cbs); - LinphoneContent *content = currentFileContentToTransfer->toLinphoneContent(); + LinphoneContent *content = L_GET_C_BACK_PTR((Content *)currentFileContentToTransfer); if (file_transfer_send_cb) { LinphoneBuffer *lb = file_transfer_send_cb(msg, content, offset, *size); if (lb) { @@ -706,7 +706,7 @@ void FileTransferChatMessageModifier::onRecvBody (belle_sip_user_body_handler_t if (currentFileContentToTransfer->getFilePath().empty()) { LinphoneChatMessage *msg = L_GET_C_BACK_PTR(message); LinphoneChatMessageCbs *cbs = linphone_chat_message_get_callbacks(msg); - LinphoneContent *content = currentFileContentToTransfer->toLinphoneContent(); + LinphoneContent *content = L_GET_C_BACK_PTR((Content *)currentFileContentToTransfer); if (linphone_chat_message_cbs_get_file_transfer_recv(cbs)) { LinphoneBuffer *lb = linphone_buffer_new_from_data(buffer, size); linphone_chat_message_cbs_get_file_transfer_recv(cbs)(msg, content, lb); @@ -747,7 +747,7 @@ void FileTransferChatMessageModifier::onRecvEnd (belle_sip_user_body_handler_t * if (currentFileContentToTransfer->getFilePath().empty()) { LinphoneChatMessage *msg = L_GET_C_BACK_PTR(message); LinphoneChatMessageCbs *cbs = linphone_chat_message_get_callbacks(msg); - LinphoneContent *content = currentFileContentToTransfer->toLinphoneContent(); + LinphoneContent *content = L_GET_C_BACK_PTR((Content *)currentFileContentToTransfer); if (linphone_chat_message_cbs_get_file_transfer_recv(cbs)) { LinphoneBuffer *lb = linphone_buffer_new(); linphone_chat_message_cbs_get_file_transfer_recv(cbs)(msg, content, lb); diff --git a/src/content/content.cpp b/src/content/content.cpp index 732996494..93fd2f6e4 100644 --- a/src/content/content.cpp +++ b/src/content/content.cpp @@ -172,11 +172,4 @@ bool Content::isFile () const { return false; } -LinphoneContent *Content::toLinphoneContent () const { - LinphoneContent *content = linphone_core_create_content(nullptr); - linphone_content_set_type(content, getContentType().getType().c_str()); - linphone_content_set_subtype(content, getContentType().getSubType().c_str()); - return content; -} - LINPHONE_END_NAMESPACE diff --git a/src/content/content.h b/src/content/content.h index 4885feadf..da3c9e055 100644 --- a/src/content/content.h +++ b/src/content/content.h @@ -71,9 +71,6 @@ public: virtual bool isFile () const; - // TODO: Remove me later. - virtual LinphoneContent *toLinphoneContent () const; - protected: explicit Content (ContentPrivate &p); diff --git a/src/content/file-content.cpp b/src/content/file-content.cpp index 09de486b6..a2d1df851 100644 --- a/src/content/file-content.cpp +++ b/src/content/file-content.cpp @@ -135,14 +135,4 @@ bool FileContent::isFile () const { return true; } -LinphoneContent *FileContent::toLinphoneContent () const { - LinphoneContent *content = linphone_core_create_content(nullptr); - linphone_content_set_type(content, getContentType().getType().c_str()); - linphone_content_set_subtype(content, getContentType().getSubType().c_str()); - linphone_content_set_name(content, getFileName().c_str()); - linphone_content_set_size(content, getFileSize()); - linphone_content_set_key(content, getFileKey().c_str(), getFileKey().size()); - return content; -} - LINPHONE_END_NAMESPACE diff --git a/src/content/file-content.h b/src/content/file-content.h index 4876370fb..79dfe5629 100644 --- a/src/content/file-content.h +++ b/src/content/file-content.h @@ -53,9 +53,6 @@ public: bool isFile () const override; - // TODO: Remove me later. - LinphoneContent *toLinphoneContent () const override; - private: L_DECLARE_PRIVATE(FileContent); }; diff --git a/src/content/file-transfer-content.cpp b/src/content/file-transfer-content.cpp index 4ce62bc19..628080c7c 100644 --- a/src/content/file-transfer-content.cpp +++ b/src/content/file-transfer-content.cpp @@ -146,15 +146,6 @@ size_t FileTransferContent::getFileSize () const { return d->fileSize; } -LinphoneContent *FileTransferContent::toLinphoneContent () const { - LinphoneContent *content = linphone_core_create_content(nullptr); - linphone_content_set_type(content, getContentType().getType().c_str()); - linphone_content_set_subtype(content, getContentType().getSubType().c_str()); - linphone_content_set_name(content, getFileName().c_str()); - linphone_content_set_size(content, getFileSize()); - return content; -} - bool FileTransferContent::isFile () const { return false; } diff --git a/src/content/file-transfer-content.h b/src/content/file-transfer-content.h index 4b668a0ac..4abc4369f 100644 --- a/src/content/file-transfer-content.h +++ b/src/content/file-transfer-content.h @@ -57,9 +57,6 @@ public: bool isFile () const override; - // TODO: Remove me later. - LinphoneContent *toLinphoneContent () const override; - private: L_DECLARE_PRIVATE(FileTransferContent); }; From 96c01f70525159610edd4f7ee9fb1d8233c4fc35 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 8 Feb 2018 13:06:39 +0100 Subject: [PATCH 03/44] More work --- src/c-wrapper/api/c-chat-message.cpp | 4 +- src/c-wrapper/api/c-content.cpp | 18 +++---- src/chat/chat-message/chat-message.cpp | 16 +++--- src/chat/chat-message/chat-message.h | 4 +- src/chat/chat-room/chat-room.cpp | 4 +- .../file-transfer-chat-message-modifier.cpp | 54 +++++++++++-------- .../multipart-chat-message-modifier.cpp | 2 +- src/content/file-content.cpp | 18 +------ src/content/file-content.h | 3 -- src/content/file-transfer-content.cpp | 20 +++++++ src/content/file-transfer-content.h | 6 +++ src/db/main-db.cpp | 2 +- tester/cpim-tester.cpp | 2 +- tester/multipart-tester.cpp | 8 +-- 14 files changed, 90 insertions(+), 71 deletions(-) diff --git a/src/c-wrapper/api/c-chat-message.cpp b/src/c-wrapper/api/c-chat-message.cpp index 59bffa2d9..1bf1faee5 100644 --- a/src/c-wrapper/api/c-chat-message.cpp +++ b/src/c-wrapper/api/c-chat-message.cpp @@ -222,7 +222,7 @@ void linphone_chat_message_add_text_content(LinphoneChatMessage *msg, const char LinphonePrivate::ContentType contentType = LinphonePrivate::ContentType::PlainText; content->setContentType(contentType); content->setBody(L_C_TO_STRING(c_content)); - L_GET_CPP_PTR_FROM_C_OBJECT(msg)->addContent(*content); + L_GET_CPP_PTR_FROM_C_OBJECT(msg)->addContent(content); } bool_t linphone_chat_message_has_text_content(const LinphoneChatMessage *msg) { @@ -284,7 +284,7 @@ int linphone_chat_message_set_text(LinphoneChatMessage *msg, const char* text) { LinphoneContent *linphone_chat_message_get_file_transfer_information(LinphoneChatMessage *msg) { const LinphonePrivate::Content *content = L_GET_PRIVATE_FROM_C_OBJECT(msg)->getFileTransferInformation(); - if (content) return L_GET_C_BACK_PTR(content); + if (content) return linphone_content_ref(L_GET_C_BACK_PTR(content)); return NULL; } diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp index 7af4b4073..6691ec9da 100644 --- a/src/c-wrapper/api/c-content.cpp +++ b/src/c-wrapper/api/c-content.cpp @@ -166,27 +166,27 @@ const char * linphone_content_get_custom_header(const LinphoneContent *content, const char *linphone_content_get_key(const LinphoneContent *content) { const LinphonePrivate::Content *c = L_GET_CPP_PTR_FROM_C_OBJECT(content); - if (c->isFile()) { - const LinphonePrivate::FileContent *fc = static_cast(c); - return L_STRING_TO_C(fc->getFileKey()); + if (c->getContentType() == LinphonePrivate::ContentType::FileTransfer) { + const LinphonePrivate::FileTransferContent *ftc = static_cast(c); + return ftc->getFileKeyAsString(); } return NULL; } size_t linphone_content_get_key_size(const LinphoneContent *content) { const LinphonePrivate::Content *c = L_GET_CPP_PTR_FROM_C_OBJECT(content); - if (c->isFile()) { - const LinphonePrivate::FileContent *fc = static_cast(c); - return fc->getFileKey().length(); + if (c->getContentType() == LinphonePrivate::ContentType::FileTransfer) { + const LinphonePrivate::FileTransferContent *ftc = static_cast(c); + return ftc->getFileKey().size(); } return 0; } void linphone_content_set_key(LinphoneContent *content, const char *key, const size_t keyLength) { LinphonePrivate::Content *c = L_GET_CPP_PTR_FROM_C_OBJECT(content); - if (c->isFile()) { - LinphonePrivate::FileContent *fc = static_cast(c); - fc->setFileKey(L_C_TO_STRING(key)); + if (c->getContentType() == LinphonePrivate::ContentType::FileTransfer) { + LinphonePrivate::FileTransferContent *ftc = static_cast(c); + ftc->setFileKey(key, keyLength); } } diff --git a/src/chat/chat-message/chat-message.cpp b/src/chat/chat-message/chat-message.cpp index 855dbd902..2c5130802 100644 --- a/src/chat/chat-message/chat-message.cpp +++ b/src/chat/chat-message/chat-message.cpp @@ -301,7 +301,7 @@ void ChatMessagePrivate::setFileTransferInformation (Content *content) { L_Q(); if (content->isFile()) { - q->addContent(*content); + q->addContent(content); } else { // This scenario is more likely to happen because the caller is using the C API LinphoneContent *c_content = L_GET_C_BACK_PTR(content); @@ -312,7 +312,7 @@ void ChatMessagePrivate::setFileTransferInformation (Content *content) { if (!content->isEmpty()) { fileContent->setBody(content->getBody()); } - q->addContent(*fileContent); + q->addContent(fileContent); } } @@ -354,7 +354,7 @@ void ChatMessagePrivate::sendImdn (Imdn::Type imdnType, LinphoneReason reason) { Content *content = new Content(); content->setContentType("message/imdn+xml"); content->setBody(Imdn::createXml(imdnId, time, imdnType, reason)); - msg->addContent(*content); + msg->addContent(content); if (reason != LinphoneReasonNone) msg->getPrivate()->setEncryptionPrevented(true); @@ -676,7 +676,7 @@ void ChatMessagePrivate::send () { if (content->getContentType() == ContentType::FileTransfer) { FileTransferContent *fileTransferContent = (FileTransferContent *)content; it = contents.erase(it); - q->addContent(*fileTransferContent->getFileContent()); + q->addContent(fileTransferContent->getFileContent()); delete fileTransferContent; } else { it++; @@ -881,18 +881,18 @@ const list &ChatMessage::getContents () const { return d->contents; } -void ChatMessage::addContent (Content &content) { +void ChatMessage::addContent (Content *content) { L_D(); if (d->isReadOnly) return; - d->contents.push_back(&content); + d->contents.push_back(content); } -void ChatMessage::removeContent (const Content &content) { +void ChatMessage::removeContent (const Content *content) { L_D(); if (d->isReadOnly) return; - d->contents.remove(&const_cast(content)); + d->contents.remove(const_cast(content)); } const Content &ChatMessage::getInternalContent () const { diff --git a/src/chat/chat-message/chat-message.h b/src/chat/chat-message/chat-message.h index af7623b77..3b0b26417 100644 --- a/src/chat/chat-message/chat-message.h +++ b/src/chat/chat-message/chat-message.h @@ -94,8 +94,8 @@ public: void setToBeStored (bool value); const std::list &getContents () const; - void addContent (Content &content); - void removeContent (const Content &content); + void addContent (Content *content); + void removeContent (const Content *content); const Content &getInternalContent () const; void setInternalContent (const Content &content); diff --git a/src/chat/chat-room/chat-room.cpp b/src/chat/chat-room/chat-room.cpp index 03b8cd079..2c1e0054a 100644 --- a/src/chat/chat-room/chat-room.cpp +++ b/src/chat/chat-room/chat-room.cpp @@ -82,7 +82,7 @@ void ChatRoomPrivate::sendIsComposingNotification () { Content *content = new Content(); content->setContentType(ContentType::ImIsComposing); content->setBody(payload); - chatMessage->addContent(*content); + chatMessage->addContent(content); chatMessage->getPrivate()->send(); } } @@ -389,7 +389,7 @@ shared_ptr ChatRoom::createChatMessage (const string &text) { Content *content = new Content(); content->setContentType(ContentType::PlainText); content->setBody(text); - chatMessage->addContent(*content); + chatMessage->addContent(content); return chatMessage; } diff --git a/src/chat/modifier/file-transfer-chat-message-modifier.cpp b/src/chat/modifier/file-transfer-chat-message-modifier.cpp index ac99d2201..674ddb308 100644 --- a/src/chat/modifier/file-transfer-chat-message-modifier.cpp +++ b/src/chat/modifier/file-transfer-chat-message-modifier.cpp @@ -246,12 +246,6 @@ void FileTransferChatMessageModifier::processResponseFromPostFile (const belle_h } // shall we encrypt the file if (is_file_encryption_enabled && message->getChatRoom()) { - LinphoneImEncryptionEngineCbs *imee_cbs = linphone_im_encryption_engine_get_callbacks(imee); - LinphoneImEncryptionEngineCbsGenerateFileTransferKeyCb generate_file_transfer_key_cb = - linphone_im_encryption_engine_cbs_get_generate_file_transfer_key(imee_cbs); - if (generate_file_transfer_key_cb) { - generate_file_transfer_key_cb(imee, L_GET_C_BACK_PTR(message->getChatRoom()), L_GET_C_BACK_PTR(message)); - } // temporary storage for the Content-disposition header value : use a generic filename to not leak it // Actual filename stored in msg->file_transfer_information->name will be set in encrypted msg // sended to the @@ -294,9 +288,32 @@ void FileTransferChatMessageModifier::processResponseFromPostFile (const belle_h const char *body = belle_sip_message_get_body((belle_sip_message_t *)event->response); if (body && strlen(body) > 0) { FileTransferContent *fileTransferContent = new FileTransferContent(); + fileTransferContent->setContentType(ContentType::FileTransfer); + message->addContent(fileTransferContent); + + LinphoneImEncryptionEngine *imee = linphone_core_get_im_encryption_engine(message->getCore()->getCCore()); + bool_t is_file_encryption_enabled = FALSE; + if (imee && message->getChatRoom()) { + LinphoneImEncryptionEngineCbs *imee_cbs = linphone_im_encryption_engine_get_callbacks(imee); + LinphoneImEncryptionEngineCbsIsEncryptionEnabledForFileTransferCb is_encryption_enabled_for_file_transfer_cb = + linphone_im_encryption_engine_cbs_get_is_encryption_enabled_for_file_transfer(imee_cbs); + if (is_encryption_enabled_for_file_transfer_cb) { + is_file_encryption_enabled = is_encryption_enabled_for_file_transfer_cb(imee, L_GET_C_BACK_PTR(message->getChatRoom())); + } + } + if (is_file_encryption_enabled && message->getChatRoom()) { + LinphoneImEncryptionEngineCbs *imee_cbs = linphone_im_encryption_engine_get_callbacks(imee); + LinphoneImEncryptionEngineCbsGenerateFileTransferKeyCb generate_file_transfer_key_cb = + linphone_im_encryption_engine_cbs_get_generate_file_transfer_key(imee_cbs); + if (generate_file_transfer_key_cb) { + generate_file_transfer_key_cb(imee, L_GET_C_BACK_PTR(message->getChatRoom()), L_GET_C_BACK_PTR(message)); + } + } + // if we have an encryption key for the file, we must insert it into the msg and restore the correct filename - string content_key = currentFileContentToTransfer->getFileKey(); - if (!content_key.empty()) { + const char *content_key = fileTransferContent->getFileKeyAsString(); + size_t content_key_size = fileTransferContent->getFileKey().size(); + if (content_key_size > 0) { // parse the msg body xmlDocPtr xmlMessageBody = xmlParseDoc((const xmlChar *)body); @@ -309,16 +326,15 @@ void FileTransferChatMessageModifier::processResponseFromPostFile (const belle_h xmlChar *typeAttribute = xmlGetProp(cur, (const xmlChar *)"type"); // this is the node we are looking for : add a file-key children node if (!xmlStrcmp(typeAttribute, (const xmlChar *)"file")) { - size_t content_key_size = content_key.length(); // need to parse the children node to update the file-name one xmlNodePtr fileInfoNodeChildren = cur->xmlChildrenNode; // convert key to base64 size_t b64Size; - bctbx_base64_encode(nullptr, &b64Size, (unsigned char *)content_key.c_str(), content_key_size); + bctbx_base64_encode(nullptr, &b64Size, (unsigned char *)content_key, content_key_size); unsigned char *keyb64 = (unsigned char *)ms_malloc0(b64Size + 1); int xmlStringLength; - bctbx_base64_encode(keyb64, &b64Size, (unsigned char *)content_key.c_str(), content_key_size); + bctbx_base64_encode(keyb64, &b64Size, (unsigned char *)content_key, content_key_size); keyb64[b64Size] = '\0'; // libxml need a null terminated string // add the node containing the key to the file-info node @@ -354,11 +370,9 @@ void FileTransferChatMessageModifier::processResponseFromPostFile (const belle_h } FileContent *fileContent = currentFileContentToTransfer; - fileTransferContent->setContentType(ContentType::FileTransfer); fileTransferContent->setFileContent(fileContent); - message->removeContent(*fileContent); - message->addContent(*fileTransferContent); + message->removeContent(fileContent); message->updateState(ChatMessage::State::FileTransferDone); releaseHttpRequest(); @@ -566,7 +580,7 @@ ChatMessageModifier::Result FileTransferChatMessageModifier::decode (const share fileTransferContent->setContentType(internalContent.getContentType()); fileTransferContent->setBody(internalContent.getBody()); fillFileTransferContentInformationsFromVndGsmaRcsFtHttpXml(fileTransferContent); - message->addContent(*fileTransferContent); + message->addContent(fileTransferContent); return ChatMessageModifier::Result::Done; } @@ -641,8 +655,7 @@ static void createFileTransferInformationsFromVndGsmaRcsFtHttpXml (FileTransferC uint8_t *keyBuffer = (uint8_t *)malloc(keyLength); // decode the key into local key buffer bctbx_base64_decode(keyBuffer, &keyLength, (unsigned char *)keyb64, strlen((const char *)keyb64)); - string key = string((const char *)keyBuffer); - fileContent->setFileKey(key); + fileTransferContent->setFileKey((const char *)keyBuffer, keyLength); // duplicate key value into the linphone content private structure xmlFree(keyb64); free(keyBuffer); @@ -715,7 +728,6 @@ void FileTransferChatMessageModifier::onRecvBody (belle_sip_user_body_handler_t // Legacy: call back given by application level linphone_core_notify_file_transfer_recv(message->getCore()->getCCore(), msg, content, (const char *)buffer, size); } - linphone_content_unref(content); } } else { lWarning() << "File transfer decrypt failed with code " << (int)retval; @@ -763,12 +775,12 @@ void FileTransferChatMessageModifier::onRecvEnd (belle_sip_user_body_handler_t * if (retval <= 0 && message->getState() != ChatMessage::State::FileTransferError) { // Remove the FileTransferContent from the message and store the FileContent FileContent *fileContent = currentFileContentToTransfer; - message->addContent(*fileContent); + message->addContent(fileContent); for (Content *content : message->getContents()) { if (content->getContentType() == ContentType::FileTransfer) { FileTransferContent *fileTransferContent = (FileTransferContent*)content; if (fileTransferContent->getFileContent() == fileContent) { - message->removeContent(*content); + message->removeContent(content); delete fileTransferContent; break; } @@ -821,7 +833,7 @@ void FileTransferChatMessageModifier::processResponseHeadersFromGetFile (const b } else { lWarning() << "No file transfer information for msg [" << this << "]: creating..."; FileContent *content = createFileTransferInformationFromHeaders(response); - message->addContent(*content); + message->addContent(content); } size_t body_size = 0; diff --git a/src/chat/modifier/multipart-chat-message-modifier.cpp b/src/chat/modifier/multipart-chat-message-modifier.cpp index 4abf91210..97081a4d0 100644 --- a/src/chat/modifier/multipart-chat-message-modifier.cpp +++ b/src/chat/modifier/multipart-chat-message-modifier.cpp @@ -120,7 +120,7 @@ ChatMessageModifier::Result MultipartChatMessageModifier::decode (const shared_p } content->setContentType(contentType); content->setBody(contentBody); - message->addContent(*content); + message->addContent(content); lInfo() << "Parsed and added content with type " << contentType.asString(); } diff --git a/src/content/file-content.cpp b/src/content/file-content.cpp index a2d1df851..8f6425474 100644 --- a/src/content/file-content.cpp +++ b/src/content/file-content.cpp @@ -36,7 +36,6 @@ public: string fileName; string filePath; size_t fileSize = 0; - string fileKey; }; // ----------------------------------------------------------------------------- @@ -48,7 +47,6 @@ FileContent::FileContent (const FileContent &src) : Content(*new FileContentPriv d->fileName = src.getFileName(); d->filePath = src.getFilePath(); d->fileSize = src.getFileSize(); - d->fileKey = src.getFileKey(); } FileContent::FileContent (FileContent &&src) : Content(*new FileContentPrivate) { @@ -56,7 +54,6 @@ FileContent::FileContent (FileContent &&src) : Content(*new FileContentPrivate) d->fileName = move(src.getPrivate()->fileName); d->filePath = move(src.getPrivate()->filePath); d->fileSize = move(src.getPrivate()->fileSize); - d->fileKey = move(src.getPrivate()->fileKey); } FileContent &FileContent::operator= (const FileContent &src) { @@ -66,7 +63,6 @@ FileContent &FileContent::operator= (const FileContent &src) { d->fileName = src.getFileName(); d->filePath = src.getFilePath(); d->fileSize = src.getFileSize(); - d->fileKey = src.getFileKey(); } return *this; @@ -78,7 +74,6 @@ FileContent &FileContent::operator= (FileContent &&src) { d->fileName = move(src.getPrivate()->fileName); d->filePath = move(src.getPrivate()->filePath); d->fileSize = move(src.getPrivate()->fileSize); - d->fileKey = move(src.getPrivate()->fileKey); return *this; } @@ -87,8 +82,7 @@ bool FileContent::operator== (const FileContent &content) const { return Content::operator==(content) && d->fileName == content.getFileName() && d->filePath == content.getFilePath() && - d->fileSize == content.getFileSize() && - d->fileKey == content.getFileKey(); + d->fileSize == content.getFileSize(); } void FileContent::setFileSize (size_t size) { @@ -121,16 +115,6 @@ const string &FileContent::getFilePath () const { return d->filePath; } -void FileContent::setFileKey (const string &key) { - L_D(); - d->fileKey = key; -} - -const string &FileContent::getFileKey () const { - L_D(); - return d->fileKey; -} - bool FileContent::isFile () const { return true; } diff --git a/src/content/file-content.h b/src/content/file-content.h index 79dfe5629..c46786dd5 100644 --- a/src/content/file-content.h +++ b/src/content/file-content.h @@ -48,9 +48,6 @@ public: void setFilePath (const std::string &path); const std::string &getFilePath () const; - void setFileKey (const std::string &key); - const std::string &getFileKey () const; - bool isFile () const override; private: diff --git a/src/content/file-transfer-content.cpp b/src/content/file-transfer-content.cpp index 628080c7c..d1d67c00d 100644 --- a/src/content/file-transfer-content.cpp +++ b/src/content/file-transfer-content.cpp @@ -38,6 +38,7 @@ public: string filePath; FileContent *fileContent = nullptr; size_t fileSize = 0; + std::vector fileKey; }; // ----------------------------------------------------------------------------- @@ -51,6 +52,7 @@ FileTransferContent::FileTransferContent (const FileTransferContent &src) : Cont d->filePath = src.getFilePath(); d->fileContent = src.getFileContent(); d->fileSize = src.getFileSize(); + d->fileKey = src.getFileKey(); } FileTransferContent::FileTransferContent (FileTransferContent &&src) : Content(*new FileTransferContentPrivate) { @@ -60,6 +62,7 @@ FileTransferContent::FileTransferContent (FileTransferContent &&src) : Content(* d->filePath = move(src.getPrivate()->filePath); d->fileContent = move(src.getPrivate()->fileContent); d->fileSize = move(src.getPrivate()->fileSize); + d->fileKey = move(src.getPrivate()->fileKey); } FileTransferContent &FileTransferContent::operator= (const FileTransferContent &src) { @@ -71,6 +74,7 @@ FileTransferContent &FileTransferContent::operator= (const FileTransferContent & d->filePath = src.getFilePath(); d->fileContent = src.getFileContent(); d->fileSize = src.getFileSize(); + d->fileKey = src.getFileKey(); } return *this; @@ -84,6 +88,7 @@ FileTransferContent &FileTransferContent::operator= (FileTransferContent &&src) d->filePath = move(src.getPrivate()->filePath); d->fileContent = move(src.getPrivate()->fileContent); d->fileSize = move(src.getPrivate()->fileSize); + d->fileKey = move(src.getPrivate()->fileKey); return *this; } @@ -146,6 +151,21 @@ size_t FileTransferContent::getFileSize () const { return d->fileSize; } +void FileTransferContent::setFileKey (const char *key, size_t size) { + L_D(); + d->fileKey = vector(key, key + size); +} + +const vector &FileTransferContent::getFileKey () const { + L_D(); + return d->fileKey; +} + +const char *FileTransferContent::getFileKeyAsString() const { + L_D(); + return d->fileKey.data(); +} + bool FileTransferContent::isFile () const { return false; } diff --git a/src/content/file-transfer-content.h b/src/content/file-transfer-content.h index 4abc4369f..da23053cb 100644 --- a/src/content/file-transfer-content.h +++ b/src/content/file-transfer-content.h @@ -20,6 +20,8 @@ #ifndef _L_FILE_TRANSFER_CONTENT_H_ #define _L_FILE_TRANSFER_CONTENT_H_ +#include + #include "content.h" // ============================================================================= @@ -55,6 +57,10 @@ public: void setFileSize (size_t size); size_t getFileSize () const; + void setFileKey (const char *key, size_t size); + const std::vector &getFileKey () const; + const char *getFileKeyAsString () const; + bool isFile () const override; private: diff --git a/src/db/main-db.cpp b/src/db/main-db.cpp index 1e76e96ae..943426029 100644 --- a/src/db/main-db.cpp +++ b/src/db/main-db.cpp @@ -708,7 +708,7 @@ shared_ptr MainDbPrivate::selectConferenceChatMessageEvent ( string data; fetchContentAppData(session, *content, contentId, data); } - chatMessage->addContent(*content); + chatMessage->addContent(content); } } diff --git a/tester/cpim-tester.cpp b/tester/cpim-tester.cpp index a98ca133d..54a36b568 100644 --- a/tester/cpim-tester.cpp +++ b/tester/cpim-tester.cpp @@ -426,7 +426,7 @@ static void cpim_chat_message_modifier_base(bool_t use_multipart) { Content *content = new Content(); content->setContentType(ContentType::PlainText); content->setBody("Hello Part 2"); - marieMessage->addContent(*content); + marieMessage->addContent(content); } marieMessage->send(); diff --git a/tester/multipart-tester.cpp b/tester/multipart-tester.cpp index ad5d731f3..0472df0eb 100644 --- a/tester/multipart-tester.cpp +++ b/tester/multipart-tester.cpp @@ -53,13 +53,13 @@ static void chat_message_multipart_modifier_base(bool first_file_transfer, bool content->setContentType("video/mkv"); content->setFilePath(send_filepath); content->setFileName("sintel_trailer_opus_h264.mkv"); - marieMessage->addContent(*content); + marieMessage->addContent(content); bc_free(send_filepath); } else { Content *content = new Content(); content->setContentType(ContentType::PlainText); content->setBody("Hello Part 1"); - marieMessage->addContent(*content); + marieMessage->addContent(content); } if (second_file_transfer) { @@ -68,13 +68,13 @@ static void chat_message_multipart_modifier_base(bool first_file_transfer, bool content->setContentType("file/vcf"); content->setFilePath(send_filepath); content->setFileName("vcards.vcf"); - marieMessage->addContent(*content); + marieMessage->addContent(content); bc_free(send_filepath); } else { Content *content = new Content(); content->setContentType(ContentType::PlainText); content->setBody("Hello Part 2"); - marieMessage->addContent(*content); + marieMessage->addContent(content); } linphone_core_set_file_transfer_server(marie->lc,"https://www.linphone.org:444/lft.php"); From 450548f612d3d547c3eb3ad8d65502f0659674ed Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Thu, 8 Feb 2018 16:28:48 +0100 Subject: [PATCH 04/44] fix(core): add missing content.h include --- coreapi/callbacks.c | 3 ++- coreapi/friendlist.c | 1 + coreapi/info.c | 1 + coreapi/lime.c | 6 ++++-- coreapi/linphonecore.c | 6 ++++-- coreapi/proxy.c | 2 ++ coreapi/quality_reporting.c | 2 ++ include/linphone/core.h | 1 - src/c-wrapper/api/c-chat-message.cpp | 1 + src/c-wrapper/api/c-content.cpp | 2 +- src/chat/chat-message/chat-message.cpp | 3 ++- src/chat/modifier/file-transfer-chat-message-modifier.cpp | 8 +++++--- .../handlers/local-conference-event-handler.cpp | 1 + src/conference/session/call-session.cpp | 7 +++---- 14 files changed, 29 insertions(+), 15 deletions(-) diff --git a/coreapi/callbacks.c b/coreapi/callbacks.c index 25967bcb8..e99f02e17 100644 --- a/coreapi/callbacks.c +++ b/coreapi/callbacks.c @@ -17,14 +17,15 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - #include "c-wrapper/internal/c-sal.h" #include "sal/call-op.h" #include "sal/message-op.h" #include "sal/refer-op.h" +#include "linphone/api/c-content.h" #include "linphone/core.h" #include "linphone/utils/utils.h" + #include "private.h" #include "mediastreamer2/mediastream.h" #include "linphone/lpconfig.h" diff --git a/coreapi/friendlist.c b/coreapi/friendlist.c index 159409449..9475b877c 100644 --- a/coreapi/friendlist.c +++ b/coreapi/friendlist.c @@ -19,6 +19,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include +#include "linphone/api/c-content.h" #include "linphone/core.h" #include "c-wrapper/c-wrapper.h" diff --git a/coreapi/info.c b/coreapi/info.c index 5b12089bb..f68bb4522 100644 --- a/coreapi/info.c +++ b/coreapi/info.c @@ -23,6 +23,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#include "linphone/api/c-content.h" #include "linphone/core.h" #include "linphone/lpconfig.h" diff --git a/coreapi/lime.c b/coreapi/lime.c index 4270f9d2a..965ccd10c 100644 --- a/coreapi/lime.c +++ b/coreapi/lime.c @@ -17,6 +17,8 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include "linphone/api/c-content.h" + #include "lime.h" #ifdef HAVE_CONFIG_H #include "config.h" @@ -787,8 +789,8 @@ int lime_im_encryption_engine_process_incoming_message_cb(LinphoneImEncryptionEn LinphoneCore *lc = linphone_im_encryption_engine_get_core(engine); int errcode = -1; /* check if we have a xml/cipher message to be decrypted */ - if (linphone_chat_message_get_content_type(msg) && - (strcmp("xml/cipher", linphone_chat_message_get_content_type(msg)) == 0 || + if (linphone_chat_message_get_content_type(msg) && + (strcmp("xml/cipher", linphone_chat_message_get_content_type(msg)) == 0 || strcmp("application/cipher.vnd.gsma.rcs-ft-http+xml", linphone_chat_message_get_content_type(msg)) == 0)) { errcode = 0; int retval; diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index 114749d3a..9e01822e1 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -18,9 +18,11 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include "linphone/api/c-content.h" #include "linphone/core.h" -#include "linphone/sipsetup.h" #include "linphone/lpconfig.h" +#include "linphone/sipsetup.h" + #include "private.h" #include "quality_reporting.h" #include "lime.h" @@ -2320,7 +2322,7 @@ void linphone_core_start (LinphoneCore *lc) { lp_config_set_string(lc->config,"misc","uuid",tmp); }else if (strcmp(uuid,"0")!=0) /*to allow to disable sip.instance*/ lc->sal->set_uuid(uuid); - + if (lc->sal->get_root_ca()) { belle_tls_crypto_config_set_root_ca(lc->http_crypto_config, lc->sal->get_root_ca()); belle_http_provider_set_tls_crypto_config(lc->http_provider, lc->http_crypto_config); diff --git a/coreapi/proxy.c b/coreapi/proxy.c index d5f3ea5ad..1f7d88519 100644 --- a/coreapi/proxy.c +++ b/coreapi/proxy.c @@ -20,10 +20,12 @@ Copyright (C) 2000 Simon MORLAT (simon.morlat@linphone.org) #include +#include "linphone/api/c-content.h" #include "linphone/core_utils.h" #include "linphone/core.h" #include "linphone/lpconfig.h" #include "linphone/sipsetup.h" + #include "mediastreamer2/mediastream.h" #include "enum.h" diff --git a/coreapi/quality_reporting.c b/coreapi/quality_reporting.c index 571a8faa0..a20b03b8e 100644 --- a/coreapi/quality_reporting.c +++ b/coreapi/quality_reporting.c @@ -21,7 +21,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "config.h" #endif +#include "linphone/api/c-content.h" #include "linphone/core.h" + #include "private.h" #include "c-wrapper/internal/c-sal.h" #include "sal/sal.h" diff --git a/include/linphone/core.h b/include/linphone/core.h index d2efdce47..d1ebbeb53 100644 --- a/include/linphone/core.h +++ b/include/linphone/core.h @@ -42,7 +42,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "linphone/call_stats.h" #include "linphone/chat.h" #include "linphone/conference.h" -#include "linphone/content.h" #include "linphone/dictionary.h" #include "linphone/error_info.h" #include "linphone/event.h" diff --git a/src/c-wrapper/api/c-chat-message.cpp b/src/c-wrapper/api/c-chat-message.cpp index 1bf1faee5..7f8e1b8c6 100644 --- a/src/c-wrapper/api/c-chat-message.cpp +++ b/src/c-wrapper/api/c-chat-message.cpp @@ -18,6 +18,7 @@ */ #include "linphone/api/c-chat-message.h" +#include "linphone/api/c-content.h" #include "linphone/utils/utils.h" #include "linphone/wrapper_utils.h" diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp index 6691ec9da..b4f77a316 100644 --- a/src/c-wrapper/api/c-content.cpp +++ b/src/c-wrapper/api/c-content.cpp @@ -243,4 +243,4 @@ LinphoneContent * linphone_content_from_sal_body_handler(SalBodyHandler *body_ha SalBodyHandler * sal_body_handler_from_content(const LinphoneContent *content) { if (content == NULL) return NULL; return content->body_handler; -} \ No newline at end of file +} diff --git a/src/chat/chat-message/chat-message.cpp b/src/chat/chat-message/chat-message.cpp index 2c5130802..fd3157614 100644 --- a/src/chat/chat-message/chat-message.cpp +++ b/src/chat/chat-message/chat-message.cpp @@ -19,6 +19,7 @@ #include "object/object-p.h" +#include "linphone/api/c-content.h" #include "linphone/core.h" #include "linphone/lpconfig.h" #include "linphone/utils/utils.h" @@ -668,7 +669,7 @@ void ChatMessagePrivate::send () { } else { msgOp->send_message(ContentType::PlainText.asString().c_str(), internalContent.getBodyAsUtf8String().c_str()); } - + // Restore FileContents and remove FileTransferContents list::iterator it = contents.begin(); while (it != contents.end()) { diff --git a/src/chat/modifier/file-transfer-chat-message-modifier.cpp b/src/chat/modifier/file-transfer-chat-message-modifier.cpp index 674ddb308..ec04c61f0 100644 --- a/src/chat/modifier/file-transfer-chat-message-modifier.cpp +++ b/src/chat/modifier/file-transfer-chat-message-modifier.cpp @@ -17,15 +17,17 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "c-wrapper/c-wrapper.h" +#include "linphone/api/c-content.h" + #include "address/address.h" +#include "bctoolbox/crypto.h" +#include "c-wrapper/c-wrapper.h" #include "chat/chat-message/chat-message-p.h" +#include "chat/chat-room/chat-room-p.h" #include "content/content-type.h" #include "content/content.h" -#include "chat/chat-room/chat-room-p.h" #include "core/core.h" #include "logger/logger.h" -#include "bctoolbox/crypto.h" #include "file-transfer-chat-message-modifier.h" diff --git a/src/conference/handlers/local-conference-event-handler.cpp b/src/conference/handlers/local-conference-event-handler.cpp index 7b3989b72..96da99d07 100644 --- a/src/conference/handlers/local-conference-event-handler.cpp +++ b/src/conference/handlers/local-conference-event-handler.cpp @@ -19,6 +19,7 @@ #include +#include "linphone/api/c-content.h" #include "linphone/utils/utils.h" #include "conference/local-conference.h" diff --git a/src/conference/session/call-session.cpp b/src/conference/session/call-session.cpp index 97aa06f6f..bb3ac773e 100644 --- a/src/conference/session/call-session.cpp +++ b/src/conference/session/call-session.cpp @@ -19,19 +19,18 @@ #include -#include "c-wrapper/c-wrapper.h" +#include "linphone/api/c-content.h" +#include "linphone/core.h" #include "address/address-p.h" +#include "c-wrapper/c-wrapper.h" #include "call/call-p.h" #include "conference/params/call-session-params-p.h" #include "conference/session/call-session-p.h" #include "conference/session/call-session.h" #include "core/core-p.h" - #include "logger/logger.h" -#include "linphone/core.h" - #include "private.h" using namespace std; From bfe175f85310148804d1682252b0141b838f05f6 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Thu, 8 Feb 2018 16:30:28 +0100 Subject: [PATCH 05/44] fix(c-api): add missing content include --- include/linphone/api/c-api.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/linphone/api/c-api.h b/include/linphone/api/c-api.h index 0b671d6ee..c37f7549a 100644 --- a/include/linphone/api/c-api.h +++ b/include/linphone/api/c-api.h @@ -23,14 +23,15 @@ #include "linphone/utils/general.h" #include "linphone/api/c-address.h" -#include "linphone/api/c-call.h" #include "linphone/api/c-call-cbs.h" #include "linphone/api/c-call-stats.h" +#include "linphone/api/c-call.h" #include "linphone/api/c-callbacks.h" -#include "linphone/api/c-chat-message.h" #include "linphone/api/c-chat-message-cbs.h" -#include "linphone/api/c-chat-room.h" +#include "linphone/api/c-chat-message.h" #include "linphone/api/c-chat-room-cbs.h" +#include "linphone/api/c-chat-room.h" +#include "linphone/api/c-content.h" #include "linphone/api/c-dial-plan.h" #include "linphone/api/c-event-log.h" #include "linphone/api/c-participant.h" From 1b74fdadefef9c721b35687b7528473b3c92a358 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 8 Feb 2018 17:25:35 +0100 Subject: [PATCH 06/44] Do not crash if key is NULL --- coreapi/lime.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/coreapi/lime.c b/coreapi/lime.c index 965ccd10c..cdf697b95 100644 --- a/coreapi/lime.c +++ b/coreapi/lime.c @@ -421,6 +421,8 @@ int lime_encryptFile(void **cryptoContext, unsigned char *key, size_t length, ch int lime_decryptFile(void **cryptoContext, unsigned char *key, size_t length, char *plain, char *cipher) { bctbx_aes_gcm_context_t *gcmContext; + if (key == NULL) return -1; + if (*cryptoContext == NULL) { /* first call to the function, allocate a crypto context and initialise it */ /* key contains 192bits of key || 64 bits of Initialisation Vector, no additional data */ gcmContext = bctbx_aes_gcm_context_new(key, 24, NULL, 0, key+24, 8, BCTBX_GCM_DECRYPT); @@ -895,6 +897,7 @@ int lime_im_encryption_engine_process_downloading_file_cb(LinphoneImEncryptionEn LinphoneContent *content = linphone_chat_message_get_file_transfer_information(msg); if (!content) return -1; + if (!linphone_content_get_key(content)) { linphone_content_unref(content); return -1; From dbdbeaa46f426b6efdabc24ffdbb5c2f20adaa66 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 8 Feb 2018 17:38:16 +0100 Subject: [PATCH 07/44] Added and using isFileTransfer --- src/chat/chat-message/chat-message.cpp | 16 ++++++++-------- src/chat/chat-message/chat-message.h | 2 +- .../file-transfer-chat-message-modifier.cpp | 8 ++++---- src/content/content.cpp | 4 ++++ src/content/content.h | 1 + src/content/file-content.cpp | 4 ++++ src/content/file-content.h | 1 + src/content/file-transfer-content.cpp | 4 ++++ src/content/file-transfer-content.h | 1 + 9 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/chat/chat-message/chat-message.cpp b/src/chat/chat-message/chat-message.cpp index fd3157614..df72a1b46 100644 --- a/src/chat/chat-message/chat-message.cpp +++ b/src/chat/chat-message/chat-message.cpp @@ -162,7 +162,7 @@ const Content* ChatMessagePrivate::getTextContent() const { bool ChatMessagePrivate::hasFileTransferContent() const { for (const Content *c : contents) { - if (c->getContentType() == ContentType::FileTransfer) { + if (c->isFileTransfer()) { return true; } } @@ -171,7 +171,7 @@ bool ChatMessagePrivate::hasFileTransferContent() const { const Content* ChatMessagePrivate::getFileTransferContent() const { for (const Content *c : contents) { - if (c->getContentType() == ContentType::FileTransfer) { + if (c->isFileTransfer()) { return c; } } @@ -321,8 +321,8 @@ bool ChatMessagePrivate::downloadFile () { L_Q(); for (auto &content : contents) - if (content->getContentType() == ContentType::FileTransfer) - return q->downloadFile(*static_cast(content)); + if (content->isFileTransfer()) + return q->downloadFile(static_cast(content)); return false; } @@ -674,8 +674,8 @@ void ChatMessagePrivate::send () { list::iterator it = contents.begin(); while (it != contents.end()) { Content *content = *it; - if (content->getContentType() == ContentType::FileTransfer) { - FileTransferContent *fileTransferContent = (FileTransferContent *)content; + if (content->isFileTransfer()) { + FileTransferContent *fileTransferContent = static_cast(content); it = contents.erase(it); q->addContent(fileTransferContent->getFileContent()); delete fileTransferContent; @@ -965,9 +965,9 @@ void ChatMessage::sendDisplayNotification () { d->sendImdn(Imdn::Type::Display, LinphoneReasonNone); } -bool ChatMessage::downloadFile(FileTransferContent &fileTransferContent) { +bool ChatMessage::downloadFile(FileTransferContent *fileTransferContent) { L_D(); - return d->fileTransferChatMessageModifier.downloadFile(getSharedFromThis(), &fileTransferContent); + return d->fileTransferChatMessageModifier.downloadFile(getSharedFromThis(), fileTransferContent); } void ChatMessage::cancelFileTransfer () { diff --git a/src/chat/chat-message/chat-message.h b/src/chat/chat-message/chat-message.h index 3b0b26417..47d8c365a 100644 --- a/src/chat/chat-message/chat-message.h +++ b/src/chat/chat-message/chat-message.h @@ -104,7 +104,7 @@ public: void addCustomHeader (const std::string &headerName, const std::string &headerValue); void removeCustomHeader (const std::string &headerName); - bool downloadFile (FileTransferContent &content); + bool downloadFile (FileTransferContent *content); private: ChatMessage (const std::shared_ptr &chatRoom, ChatMessage::Direction direction); diff --git a/src/chat/modifier/file-transfer-chat-message-modifier.cpp b/src/chat/modifier/file-transfer-chat-message-modifier.cpp index ec04c61f0..1032c5af8 100644 --- a/src/chat/modifier/file-transfer-chat-message-modifier.cpp +++ b/src/chat/modifier/file-transfer-chat-message-modifier.cpp @@ -587,8 +587,8 @@ ChatMessageModifier::Result FileTransferChatMessageModifier::decode (const share } for (Content *content : message->getContents()) { - if (content->getContentType() == ContentType::FileTransfer) { - FileTransferContent *fileTransferContent = (FileTransferContent *)content; + if (content->isFileTransfer()) { + FileTransferContent *fileTransferContent = static_cast(content); fillFileTransferContentInformationsFromVndGsmaRcsFtHttpXml(fileTransferContent); } } @@ -779,8 +779,8 @@ void FileTransferChatMessageModifier::onRecvEnd (belle_sip_user_body_handler_t * FileContent *fileContent = currentFileContentToTransfer; message->addContent(fileContent); for (Content *content : message->getContents()) { - if (content->getContentType() == ContentType::FileTransfer) { - FileTransferContent *fileTransferContent = (FileTransferContent*)content; + if (content->isFileTransfer()) { + FileTransferContent *fileTransferContent = static_cast(content); if (fileTransferContent->getFileContent() == fileContent) { message->removeContent(content); delete fileTransferContent; diff --git a/src/content/content.cpp b/src/content/content.cpp index 93fd2f6e4..786c9be8d 100644 --- a/src/content/content.cpp +++ b/src/content/content.cpp @@ -172,4 +172,8 @@ bool Content::isFile () const { return false; } +bool Content::isFileTransfer () const { + return false; +} + LINPHONE_END_NAMESPACE diff --git a/src/content/content.h b/src/content/content.h index da3c9e055..281ad78d2 100644 --- a/src/content/content.h +++ b/src/content/content.h @@ -70,6 +70,7 @@ public: bool isEmpty () const; virtual bool isFile () const; + virtual bool isFileTransfer () const; protected: explicit Content (ContentPrivate &p); diff --git a/src/content/file-content.cpp b/src/content/file-content.cpp index 8f6425474..63c727dc6 100644 --- a/src/content/file-content.cpp +++ b/src/content/file-content.cpp @@ -119,4 +119,8 @@ bool FileContent::isFile () const { return true; } +bool FileContent::isFileTransfer () const { + return false; +} + LINPHONE_END_NAMESPACE diff --git a/src/content/file-content.h b/src/content/file-content.h index c46786dd5..82bc1bcff 100644 --- a/src/content/file-content.h +++ b/src/content/file-content.h @@ -49,6 +49,7 @@ public: const std::string &getFilePath () const; bool isFile () const override; + bool isFileTransfer () const override; private: L_DECLARE_PRIVATE(FileContent); diff --git a/src/content/file-transfer-content.cpp b/src/content/file-transfer-content.cpp index d1d67c00d..57ecff245 100644 --- a/src/content/file-transfer-content.cpp +++ b/src/content/file-transfer-content.cpp @@ -170,4 +170,8 @@ bool FileTransferContent::isFile () const { return false; } +bool FileTransferContent::isFileTransfer () const { + return true; +} + LINPHONE_END_NAMESPACE diff --git a/src/content/file-transfer-content.h b/src/content/file-transfer-content.h index da23053cb..d107fcd52 100644 --- a/src/content/file-transfer-content.h +++ b/src/content/file-transfer-content.h @@ -62,6 +62,7 @@ public: const char *getFileKeyAsString () const; bool isFile () const override; + bool isFileTransfer () const override; private: L_DECLARE_PRIVATE(FileTransferContent); From 1024c1942bddfc584c59811839b12035433d86ce Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 8 Feb 2018 17:50:25 +0100 Subject: [PATCH 08/44] Some invalid read fixes --- src/c-wrapper/api/c-content.cpp | 12 ++++++------ src/content/file-transfer-content.cpp | 5 +++++ src/content/file-transfer-content.h | 1 + 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp index b4f77a316..3b7e009bd 100644 --- a/src/c-wrapper/api/c-content.cpp +++ b/src/c-wrapper/api/c-content.cpp @@ -122,7 +122,7 @@ const char * linphone_content_get_name(const LinphoneContent *content) { const LinphonePrivate::FileContent *fc = static_cast(c); if (content->name) ms_free(content->name); content->name = ms_strdup(L_STRING_TO_C(fc->getFileName())); - } else if (c->getContentType() == LinphonePrivate::ContentType::FileTransfer) { + } else if (c->isFileTransfer()) { const LinphonePrivate::FileTransferContent *ftc = static_cast(c); if (content->name) ms_free(content->name); content->name = ms_strdup(L_STRING_TO_C(ftc->getFileName())); @@ -135,7 +135,7 @@ void linphone_content_set_name(LinphoneContent *content, const char *name) { if (c->isFile()) { LinphonePrivate::FileContent *fc = static_cast(c); fc->setFileName(L_C_TO_STRING(name)); - } else if (c->getContentType() == LinphonePrivate::ContentType::FileTransfer) { + } else if (c->isFileTransfer()) { LinphonePrivate::FileTransferContent *ftc = static_cast(c); ftc->setFileName(L_C_TO_STRING(name)); } @@ -166,7 +166,7 @@ const char * linphone_content_get_custom_header(const LinphoneContent *content, const char *linphone_content_get_key(const LinphoneContent *content) { const LinphonePrivate::Content *c = L_GET_CPP_PTR_FROM_C_OBJECT(content); - if (c->getContentType() == LinphonePrivate::ContentType::FileTransfer) { + if (c->isFileTransfer()) { const LinphonePrivate::FileTransferContent *ftc = static_cast(c); return ftc->getFileKeyAsString(); } @@ -175,16 +175,16 @@ const char *linphone_content_get_key(const LinphoneContent *content) { size_t linphone_content_get_key_size(const LinphoneContent *content) { const LinphonePrivate::Content *c = L_GET_CPP_PTR_FROM_C_OBJECT(content); - if (c->getContentType() == LinphonePrivate::ContentType::FileTransfer) { + if (c->isFileTransfer()) { const LinphonePrivate::FileTransferContent *ftc = static_cast(c); - return ftc->getFileKey().size(); + return ftc->getFileKeySize(); } return 0; } void linphone_content_set_key(LinphoneContent *content, const char *key, const size_t keyLength) { LinphonePrivate::Content *c = L_GET_CPP_PTR_FROM_C_OBJECT(content); - if (c->getContentType() == LinphonePrivate::ContentType::FileTransfer) { + if (c->isFileTransfer()) { LinphonePrivate::FileTransferContent *ftc = static_cast(c); ftc->setFileKey(key, keyLength); } diff --git a/src/content/file-transfer-content.cpp b/src/content/file-transfer-content.cpp index 57ecff245..6dffb441d 100644 --- a/src/content/file-transfer-content.cpp +++ b/src/content/file-transfer-content.cpp @@ -166,6 +166,11 @@ const char *FileTransferContent::getFileKeyAsString() const { return d->fileKey.data(); } +size_t FileTransferContent::getFileKeySize() const { + L_D(); + return d->fileKey.size(); +} + bool FileTransferContent::isFile () const { return false; } diff --git a/src/content/file-transfer-content.h b/src/content/file-transfer-content.h index d107fcd52..0f29dc489 100644 --- a/src/content/file-transfer-content.h +++ b/src/content/file-transfer-content.h @@ -60,6 +60,7 @@ public: void setFileKey (const char *key, size_t size); const std::vector &getFileKey () const; const char *getFileKeyAsString () const; + size_t getFileKeySize() const; bool isFile () const override; bool isFileTransfer () const override; From b9b2ca63119127b2d59be2b2110d83e8bba85f50 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Fri, 9 Feb 2018 10:57:25 +0100 Subject: [PATCH 09/44] feat(MainDb): add a security to delete basic chat room on migration when peer sip address is a server sip address chat room --- src/db/main-db.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/db/main-db.cpp b/src/db/main-db.cpp index 943426029..4b507cb6e 100644 --- a/src/db/main-db.cpp +++ b/src/db/main-db.cpp @@ -89,8 +89,10 @@ public: } catch (const soci::soci_error &e) { lWarning() << "Catched exception in MainDb::" << info.name << "."; soci::soci_error::error_category category = e.get_error_category(); - if ((category == soci::soci_error::connection_error - || category == soci::soci_error::unknown) && info.mainDb->forceReconnect()) { + if ( + (category == soci::soci_error::connection_error || category == soci::soci_error::unknown) && + info.mainDb->forceReconnect() + ) { mResult = mFunction(); return; } @@ -2528,6 +2530,19 @@ void MainDb::migrateBasicToClientGroupChatRoom ( const long long &localSipAddressId = d->insertSipAddress(newChatRoomId.getLocalAddress().asString()); const int &capabilities = clientGroupChatRoom->getCapabilities(); + { + shared_ptr buggyChatRoom = getCore()->findChatRoom(newChatRoomId); + if (buggyChatRoom) { + lError() << "Chat room was found with the same chat room id of a new migrated ClientGroupChatRoom!!!"; + AbstractChatRoom::CapabilitiesMask capabilities = buggyChatRoom->getCapabilities(); + if (capabilities & AbstractChatRoom::Capabilities::Basic) { + lError() << "Delete invalid basic chat room..."; + Core::deleteChatRoom(buggyChatRoom); + } else + lError() << "Unable to delete invalid chat room with capabilities: " << capabilities << "."; + } + } + *session << "UPDATE chat_room" " SET capabilities = :capabilities," " peer_sip_address_id = :peerSipAddressId," From 575370919d140cc2041ad50da21afd84a3608c07 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Fri, 9 Feb 2018 13:41:27 +0100 Subject: [PATCH 10/44] More work on c-content.cpp --- src/c-wrapper/api/c-content.cpp | 94 ++++++++++++++++++++++----------- 1 file changed, 63 insertions(+), 31 deletions(-) diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp index 3b7e009bd..99318aa23 100644 --- a/src/c-wrapper/api/c-content.cpp +++ b/src/c-wrapper/api/c-content.cpp @@ -32,9 +32,14 @@ using namespace std; L_DECLARE_C_CLONABLE_OBJECT_IMPL(Content, - SalBodyHandler *body_handler; void *cryptoContext; /**< crypto context used to encrypt file for RCS file transfer */ mutable char *name; + mutable char *type; + mutable char *subtype; + mutable char *body; + mutable size_t size; + mutable char *encoding; + mutable char *key; ) // ============================================================================= @@ -61,7 +66,9 @@ void linphone_content_set_user_data(LinphoneContent *content, void *ud) { // ============================================================================= const char * linphone_content_get_type(const LinphoneContent *content) { - return L_STRING_TO_C(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType().getType()); + if (content->type) ms_free(content->type); + content->type = ms_strdup(L_STRING_TO_C(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType().getType())); + return content->type; } void linphone_content_set_type(LinphoneContent *content, const char *type) { @@ -71,7 +78,9 @@ void linphone_content_set_type(LinphoneContent *content, const char *type) { } const char * linphone_content_get_subtype(const LinphoneContent *content) { - return L_STRING_TO_C(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType().getSubType()); + if (content->subtype) ms_free(content->subtype); + content->subtype = ms_strdup(L_STRING_TO_C(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType().getSubType())); + return content->subtype; } void linphone_content_set_subtype(LinphoneContent *content, const char *subtype) { @@ -89,7 +98,9 @@ void linphone_content_set_buffer(LinphoneContent *content, const uint8_t *buffer } const char * linphone_content_get_string_buffer(const LinphoneContent *content) { - return L_STRING_TO_C(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getBodyAsString()); + if (content->body) ms_free(content->body); + content->body = ms_strdup(L_STRING_TO_C(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getBodyAsString())); + return content->body; } void linphone_content_set_string_buffer(LinphoneContent *content, const char *buffer) { @@ -99,21 +110,22 @@ void linphone_content_set_string_buffer(LinphoneContent *content, const char *bu size_t linphone_content_get_size(const LinphoneContent *content) { size_t size = L_GET_CPP_PTR_FROM_C_OBJECT(content)->getSize(); if (size == 0) { - size = sal_body_handler_get_size(content->body_handler); + size = content->size; } return size; } void linphone_content_set_size(LinphoneContent *content, size_t size) { - sal_body_handler_set_size(content->body_handler, size); + content->size = size; } const char * linphone_content_get_encoding(const LinphoneContent *content) { - return sal_body_handler_get_encoding(content->body_handler); + return content->encoding; } void linphone_content_set_encoding(LinphoneContent *content, const char *encoding) { - sal_body_handler_set_encoding(content->body_handler, encoding); + if (content->encoding) ms_free(content->encoding); + content->encoding = ms_strdup(encoding); } const char * linphone_content_get_name(const LinphoneContent *content) { @@ -131,6 +143,8 @@ const char * linphone_content_get_name(const LinphoneContent *content) { } void linphone_content_set_name(LinphoneContent *content, const char *name) { + if (content->name) ms_free(content->name); + LinphonePrivate::Content *c = L_GET_CPP_PTR_FROM_C_OBJECT(content); if (c->isFile()) { LinphonePrivate::FileContent *fc = static_cast(c); @@ -139,38 +153,58 @@ void linphone_content_set_name(LinphoneContent *content, const char *name) { LinphonePrivate::FileTransferContent *ftc = static_cast(c); ftc->setFileName(L_C_TO_STRING(name)); } + content->name = ms_strdup(name); } bool_t linphone_content_is_multipart(const LinphoneContent *content) { - return sal_body_handler_is_multipart(content->body_handler); + SalBodyHandler *body_handler = sal_body_handler_from_content(content); + bool_t multipart = sal_body_handler_is_multipart(body_handler); + sal_body_handler_unref(body_handler); + return multipart; } LinphoneContent * linphone_content_get_part(const LinphoneContent *content, int idx) { SalBodyHandler *part_body_handler; - if (!linphone_content_is_multipart(content)) return NULL; - part_body_handler = sal_body_handler_get_part(content->body_handler, idx); + SalBodyHandler *body_handler = sal_body_handler_from_content(content); + if (!sal_body_handler_is_multipart(body_handler)) { + sal_body_handler_unref(body_handler); + return NULL; + } + part_body_handler = sal_body_handler_get_part(body_handler, idx); + sal_body_handler_unref(body_handler); return linphone_content_from_sal_body_handler(part_body_handler); } LinphoneContent * linphone_content_find_part_by_header(const LinphoneContent *content, const char *header_name, const char *header_value) { SalBodyHandler *part_body_handler; - if (!linphone_content_is_multipart(content)) return NULL; - part_body_handler = sal_body_handler_find_part_by_header(content->body_handler, header_name, header_value); + SalBodyHandler *body_handler = sal_body_handler_from_content(content); + if (!sal_body_handler_is_multipart(body_handler)) { + sal_body_handler_unref(body_handler); + return NULL; + } + part_body_handler = sal_body_handler_find_part_by_header(body_handler, header_name, header_value); + sal_body_handler_unref(body_handler); return linphone_content_from_sal_body_handler(part_body_handler); } const char * linphone_content_get_custom_header(const LinphoneContent *content, const char *header_name) { - return sal_body_handler_get_header(content->body_handler, header_name); + SalBodyHandler *body_handler = sal_body_handler_from_content(content); + const char *header = sal_body_handler_get_header(body_handler, header_name); + sal_body_handler_unref(body_handler); + return header; } const char *linphone_content_get_key(const LinphoneContent *content) { + if (content->key) ms_free(content->key); + const LinphonePrivate::Content *c = L_GET_CPP_PTR_FROM_C_OBJECT(content); if (c->isFileTransfer()) { const LinphonePrivate::FileTransferContent *ftc = static_cast(c); - return ftc->getFileKeyAsString(); + content->key = ms_strdup(ftc->getFileKeyAsString()); } - return NULL; + + return content->key; } size_t linphone_content_get_key_size(const LinphoneContent *content) { @@ -194,24 +228,17 @@ void linphone_content_set_key(LinphoneContent *content, const char *key, const s // Private functions. // ============================================================================= -static void linphone_content_set_sal_body_handler(LinphoneContent *content, SalBodyHandler *body_handler) { - if (content->body_handler != NULL) { - sal_body_handler_unref(content->body_handler); - content->body_handler = NULL; - } - content->body_handler = sal_body_handler_ref(body_handler); -} - static LinphoneContent * linphone_content_new_with_body_handler(SalBodyHandler *body_handler) { LinphoneContent *content = L_INIT(Content); content->cryptoContext = NULL; - if (body_handler == NULL) { - linphone_content_set_sal_body_handler(content, sal_body_handler_new()); - } else { - linphone_content_set_sal_body_handler(content, body_handler); - } LinphonePrivate::Content *c = new LinphonePrivate::Content(); L_SET_CPP_PTR_FROM_C_OBJECT(content, c); + if (body_handler != NULL) { + linphone_content_set_type(content, sal_body_handler_get_type(body_handler)); + linphone_content_set_subtype(content, sal_body_handler_get_subtype(body_handler)); + linphone_content_set_string_buffer(content, (char *)sal_body_handler_get_data(body_handler)); + linphone_content_set_encoding(content, sal_body_handler_get_encoding(body_handler)); + } return content; } @@ -220,7 +247,6 @@ LinphoneContent * linphone_content_new(void) { } LinphoneContent * linphone_content_copy(const LinphoneContent *ref) { - //TODO return (LinphoneContent *)belle_sip_object_ref(belle_sip_object_clone(BELLE_SIP_OBJECT(ref))); } @@ -242,5 +268,11 @@ LinphoneContent * linphone_content_from_sal_body_handler(SalBodyHandler *body_ha SalBodyHandler * sal_body_handler_from_content(const LinphoneContent *content) { if (content == NULL) return NULL; - return content->body_handler; + SalBodyHandler *body_handler = sal_body_handler_new(); + sal_body_handler_set_type(body_handler, belle_sip_strdup(linphone_content_get_type(content))); + sal_body_handler_set_subtype(body_handler, belle_sip_strdup(linphone_content_get_subtype(content))); + sal_body_handler_set_size(body_handler, linphone_content_get_size(content)); + sal_body_handler_set_data(body_handler, belle_sip_strdup(linphone_content_get_string_buffer(content))); + sal_body_handler_set_encoding(body_handler, belle_sip_strdup(linphone_content_get_encoding(content))); + return body_handler; } From 7e92d238a17d9718644ba0803887724737e6777f Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Fri, 9 Feb 2018 14:33:39 +0100 Subject: [PATCH 11/44] Fixed for c-content.cpp --- src/c-wrapper/api/c-content.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp index 99318aa23..2cf40c119 100644 --- a/src/c-wrapper/api/c-content.cpp +++ b/src/c-wrapper/api/c-content.cpp @@ -90,7 +90,7 @@ void linphone_content_set_subtype(LinphoneContent *content, const char *subtype) } uint8_t * linphone_content_get_buffer(const LinphoneContent *content) { - return (uint8_t *)L_STRING_TO_C(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getBodyAsString()); + return (uint8_t *)L_GET_CPP_PTR_FROM_C_OBJECT(content)->getBody().data(); } void linphone_content_set_buffer(LinphoneContent *content, const uint8_t *buffer, size_t size) { @@ -99,12 +99,12 @@ void linphone_content_set_buffer(LinphoneContent *content, const uint8_t *buffer const char * linphone_content_get_string_buffer(const LinphoneContent *content) { if (content->body) ms_free(content->body); - content->body = ms_strdup(L_STRING_TO_C(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getBodyAsString())); + content->body = ms_strdup(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getBodyAsUtf8String().c_str()); return content->body; } void linphone_content_set_string_buffer(LinphoneContent *content, const char *buffer) { - L_GET_CPP_PTR_FROM_C_OBJECT(content)->setBody(L_C_TO_STRING(buffer)); + L_GET_CPP_PTR_FROM_C_OBJECT(content)->setBodyFromUtf8(L_C_TO_STRING(buffer)); } size_t linphone_content_get_size(const LinphoneContent *content) { @@ -269,10 +269,10 @@ LinphoneContent * linphone_content_from_sal_body_handler(SalBodyHandler *body_ha SalBodyHandler * sal_body_handler_from_content(const LinphoneContent *content) { if (content == NULL) return NULL; SalBodyHandler *body_handler = sal_body_handler_new(); - sal_body_handler_set_type(body_handler, belle_sip_strdup(linphone_content_get_type(content))); - sal_body_handler_set_subtype(body_handler, belle_sip_strdup(linphone_content_get_subtype(content))); + sal_body_handler_set_type(body_handler, linphone_content_get_type(content)); + sal_body_handler_set_subtype(body_handler, linphone_content_get_subtype(content)); sal_body_handler_set_size(body_handler, linphone_content_get_size(content)); sal_body_handler_set_data(body_handler, belle_sip_strdup(linphone_content_get_string_buffer(content))); - sal_body_handler_set_encoding(body_handler, belle_sip_strdup(linphone_content_get_encoding(content))); + if (content->encoding) sal_body_handler_set_encoding(body_handler, linphone_content_get_encoding(content)); return body_handler; } From ee87c0dcc1d3b02dc1ded5ef646967c75b9743fd Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Fri, 9 Feb 2018 16:22:41 +0100 Subject: [PATCH 12/44] Yet another c-content fix --- src/c-wrapper/api/c-content.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp index 2cf40c119..b38f55c12 100644 --- a/src/c-wrapper/api/c-content.cpp +++ b/src/c-wrapper/api/c-content.cpp @@ -172,8 +172,9 @@ LinphoneContent * linphone_content_get_part(const LinphoneContent *content, int return NULL; } part_body_handler = sal_body_handler_get_part(body_handler, idx); + LinphoneContent *result = linphone_content_from_sal_body_handler(part_body_handler); sal_body_handler_unref(body_handler); - return linphone_content_from_sal_body_handler(part_body_handler); + return result; } LinphoneContent * linphone_content_find_part_by_header(const LinphoneContent *content, const char *header_name, const char *header_value) { @@ -184,8 +185,9 @@ LinphoneContent * linphone_content_find_part_by_header(const LinphoneContent *co return NULL; } part_body_handler = sal_body_handler_find_part_by_header(body_handler, header_name, header_value); + LinphoneContent *result = linphone_content_from_sal_body_handler(part_body_handler); sal_body_handler_unref(body_handler); - return linphone_content_from_sal_body_handler(part_body_handler); + return result; } const char * linphone_content_get_custom_header(const LinphoneContent *content, const char *header_name) { @@ -236,8 +238,18 @@ static LinphoneContent * linphone_content_new_with_body_handler(SalBodyHandler * if (body_handler != NULL) { linphone_content_set_type(content, sal_body_handler_get_type(body_handler)); linphone_content_set_subtype(content, sal_body_handler_get_subtype(body_handler)); - linphone_content_set_string_buffer(content, (char *)sal_body_handler_get_data(body_handler)); - linphone_content_set_encoding(content, sal_body_handler_get_encoding(body_handler)); + if (!sal_body_handler_is_multipart(body_handler)) { + linphone_content_set_string_buffer(content, (char *)sal_body_handler_get_data(body_handler)); + } else { + string body; + belle_sip_multipart_body_handler_t *mpbh = BELLE_SIP_MULTIPART_BODY_HANDLER(body_handler); + 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); + body += (const char *)belle_sip_memory_body_handler_get_buffer(BELLE_SIP_MEMORY_BODY_HANDLER(part)); + } + linphone_content_set_string_buffer(content, body.c_str()); + } + if (sal_body_handler_get_encoding(body_handler)) linphone_content_set_encoding(content, sal_body_handler_get_encoding(body_handler)); } return content; } From 9a45223db4493b62b2cf5657b01b344caf9e6e35 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Fri, 9 Feb 2018 17:12:28 +0100 Subject: [PATCH 13/44] Use ContentManager in c-content --- src/c-wrapper/api/c-content.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp index b38f55c12..5006d23d9 100644 --- a/src/c-wrapper/api/c-content.cpp +++ b/src/c-wrapper/api/c-content.cpp @@ -24,6 +24,7 @@ #include "content/content.h" #include "content/content-type.h" +#include "content/content-manager.h" #include "content/file-content.h" #include "content/file-transfer-content.h" @@ -158,10 +159,7 @@ void linphone_content_set_name(LinphoneContent *content, const char *name) { } bool_t linphone_content_is_multipart(const LinphoneContent *content) { - SalBodyHandler *body_handler = sal_body_handler_from_content(content); - bool_t multipart = sal_body_handler_is_multipart(body_handler); - sal_body_handler_unref(body_handler); - return multipart; + return L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType() == LinphonePrivate::ContentType::Multipart; } LinphoneContent * linphone_content_get_part(const LinphoneContent *content, int idx) { @@ -241,13 +239,15 @@ static LinphoneContent * linphone_content_new_with_body_handler(SalBodyHandler * if (!sal_body_handler_is_multipart(body_handler)) { linphone_content_set_string_buffer(content, (char *)sal_body_handler_get_data(body_handler)); } else { - string body; belle_sip_multipart_body_handler_t *mpbh = BELLE_SIP_MULTIPART_BODY_HANDLER(body_handler); + 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); - body += (const char *)belle_sip_memory_body_handler_get_buffer(BELLE_SIP_MEMORY_BODY_HANDLER(part)); + LinphoneContent *part_content = linphone_content_new_with_body_handler((SalBodyHandler *)part); + contents.push_back(*L_GET_CPP_PTR_FROM_C_OBJECT(part_content)); } - linphone_content_set_string_buffer(content, body.c_str()); + LinphonePrivate::Content multipartContent = LinphonePrivate::ContentManager::contentListToMultipart(contents); + linphone_content_set_string_buffer(content, multipartContent.getBodyAsUtf8String().c_str()); } if (sal_body_handler_get_encoding(body_handler)) linphone_content_set_encoding(content, sal_body_handler_get_encoding(body_handler)); } From 7a5a78c9b89123c20cefc982a2da0871a33d7d69 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 12 Feb 2018 10:18:45 +0100 Subject: [PATCH 14/44] Fixes for multipart content --- src/c-wrapper/api/c-content.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp index 5006d23d9..8e135ecfa 100644 --- a/src/c-wrapper/api/c-content.cpp +++ b/src/c-wrapper/api/c-content.cpp @@ -280,11 +280,20 @@ LinphoneContent * linphone_content_from_sal_body_handler(SalBodyHandler *body_ha SalBodyHandler * sal_body_handler_from_content(const LinphoneContent *content) { if (content == NULL) return NULL; - SalBodyHandler *body_handler = sal_body_handler_new(); + SalBodyHandler *body_handler; + if (L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType() == LinphonePrivate::ContentType::Multipart) { + size_t size = linphone_content_get_size(content); + char *buffer = ms_strdup(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getBodyAsUtf8String().c_str()); + const char *boundary = L_STRING_TO_C(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType().getParameter()); + belle_sip_multipart_body_handler_t *bh = belle_sip_multipart_body_handler_new_from_buffer(buffer, size, boundary); + body_handler = (SalBodyHandler *)BELLE_SIP_BODY_HANDLER(bh); + } else { + body_handler = sal_body_handler_new(); + sal_body_handler_set_data(body_handler, belle_sip_strdup(linphone_content_get_string_buffer(content))); + } sal_body_handler_set_type(body_handler, linphone_content_get_type(content)); sal_body_handler_set_subtype(body_handler, linphone_content_get_subtype(content)); sal_body_handler_set_size(body_handler, linphone_content_get_size(content)); - sal_body_handler_set_data(body_handler, belle_sip_strdup(linphone_content_get_string_buffer(content))); if (content->encoding) sal_body_handler_set_encoding(body_handler, linphone_content_get_encoding(content)); return body_handler; } From f58dff4a2fefac0847b613891a32210d4c9d9ca5 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 12 Feb 2018 11:14:41 +0100 Subject: [PATCH 15/44] Fixed linphone_content_get_buffer --- src/c-wrapper/api/c-content.cpp | 2 +- tester/eventapi_tester.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp index 8e135ecfa..211985bbb 100644 --- a/src/c-wrapper/api/c-content.cpp +++ b/src/c-wrapper/api/c-content.cpp @@ -91,7 +91,7 @@ void linphone_content_set_subtype(LinphoneContent *content, const char *subtype) } uint8_t * linphone_content_get_buffer(const LinphoneContent *content) { - return (uint8_t *)L_GET_CPP_PTR_FROM_C_OBJECT(content)->getBody().data(); + return (uint8_t *)linphone_content_get_string_buffer(content); } void linphone_content_set_buffer(LinphoneContent *content, const uint8_t *buffer, size_t size) { diff --git a/tester/eventapi_tester.c b/tester/eventapi_tester.c index 2119f3500..66ae0f165 100644 --- a/tester/eventapi_tester.c +++ b/tester/eventapi_tester.c @@ -41,7 +41,7 @@ void linphone_notify_received(LinphoneCore *lc, LinphoneEvent *lev, const char * if (!BC_ASSERT_PTR_NOT_NULL(content)) return; if (!linphone_content_is_multipart(content) && (!ua || !strstr(ua, "flexisip"))) { /*disable check for full presence server support*/ /*hack to disable content checking for list notify */ - BC_ASSERT_STRING_EQUAL(notify_content,(const char*)linphone_content_get_buffer(content)); + BC_ASSERT_STRING_EQUAL((const char*)linphone_content_get_buffer(content),notify_content); } mgr=get_manager(lc); mgr->stat.number_of_NotifyReceived++; From de6283928d2acea29f5922b9e87c5c6f1d51d815 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 19 Mar 2018 17:26:35 +0100 Subject: [PATCH 16/44] Fixed issue with merge --- src/chat/modifier/file-transfer-chat-message-modifier.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/chat/modifier/file-transfer-chat-message-modifier.cpp b/src/chat/modifier/file-transfer-chat-message-modifier.cpp index fcdf15842..14733e4c8 100644 --- a/src/chat/modifier/file-transfer-chat-message-modifier.cpp +++ b/src/chat/modifier/file-transfer-chat-message-modifier.cpp @@ -298,7 +298,6 @@ void FileTransferChatMessageModifier::processResponseFromPostFile (const belle_h if (body && strlen(body) > 0) { FileTransferContent *fileTransferContent = new FileTransferContent(); fileTransferContent->setContentType(ContentType::FileTransfer); - message->addContent(fileTransferContent); LinphoneImEncryptionEngine *imee = linphone_core_get_im_encryption_engine(message->getCore()->getCCore()); bool_t is_file_encryption_enabled = FALSE; From b248771be135e8cb97431192ae205d2f168517c5 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 19 Mar 2018 17:34:14 +0100 Subject: [PATCH 17/44] Removed content.c as it is not compiled anymore --- coreapi/content.c | 243 ---------------------------------------------- 1 file changed, 243 deletions(-) delete mode 100644 coreapi/content.c diff --git a/coreapi/content.c b/coreapi/content.c deleted file mode 100644 index 8d20964bd..000000000 --- a/coreapi/content.c +++ /dev/null @@ -1,243 +0,0 @@ -/* -linphone -Copyright (C) 2010-2014 Belledonne Communications SARL - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -*/ - -#include "linphone/core.h" - -#include "c-wrapper/c-wrapper.h" - -// TODO: From coreapi. Remove me later. -#include "private.h" - -static void linphone_content_set_sal_body_handler(LinphoneContent *content, SalBodyHandler *body_handler) { - if (content->body_handler != NULL) { - sal_body_handler_unref(content->body_handler); - content->body_handler = NULL; - } - content->body_handler = sal_body_handler_ref(body_handler); -} - -static LinphoneContent * linphone_content_new_with_body_handler(SalBodyHandler *body_handler) { - LinphoneContent *content = belle_sip_object_new(LinphoneContent); - belle_sip_object_ref(content); - content->owned_fields = TRUE; - content->cryptoContext = NULL; /* this field is managed externally by encryption/decryption functions so be careful to initialise it to NULL */ - if (body_handler == NULL) { - linphone_content_set_sal_body_handler(content, sal_body_handler_new()); - } else { - linphone_content_set_sal_body_handler(content, body_handler); - } - return content; -} - -static void linphone_content_destroy(LinphoneContent *content) { - if (content->owned_fields == TRUE) { - if (content->body_handler) sal_body_handler_unref(content->body_handler); - if (content->name) belle_sip_free(content->name); - if (content->key) belle_sip_free(content->key); - /* note : crypto context is allocated/destroyed by the encryption function */ - } -} - -static void linphone_content_clone(LinphoneContent *obj, const LinphoneContent *ref) { - obj->owned_fields = TRUE; - linphone_content_set_sal_body_handler(obj, sal_body_handler_new()); - if ((linphone_content_get_type(ref) != NULL) || (linphone_content_get_subtype(ref) != NULL)) { - linphone_content_set_type(obj, linphone_content_get_type(ref)); - linphone_content_set_subtype(obj, linphone_content_get_subtype(ref)); - } - if (linphone_content_get_encoding(ref) != NULL) { - linphone_content_set_encoding(obj, linphone_content_get_encoding(ref)); - } - linphone_content_set_name(obj, linphone_content_get_name(ref)); - linphone_content_set_key(obj, linphone_content_get_key(ref), linphone_content_get_key_size(ref)); - if (linphone_content_get_buffer(ref) != NULL) { - linphone_content_set_buffer(obj, linphone_content_get_buffer(ref), linphone_content_get_size(ref)); - } else { - linphone_content_set_size(obj, linphone_content_get_size(ref)); - } -} - - -BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphoneContent); - -BELLE_SIP_INSTANCIATE_VPTR(LinphoneContent, belle_sip_object_t, - (belle_sip_object_destroy_t)linphone_content_destroy, - (belle_sip_object_clone_t)linphone_content_clone, - NULL, // marshal - TRUE -); - - -LinphoneContent * linphone_core_create_content(LinphoneCore *lc) { - return linphone_content_new(); -} - -LinphoneContent * linphone_content_ref(LinphoneContent *content) { - belle_sip_object_ref(content); - return content; -} - -void linphone_content_unref(LinphoneContent *content) { - belle_sip_object_unref(content); -} - -void *linphone_content_get_user_data(const LinphoneContent *content) { - return content->user_data; -} - -void linphone_content_set_user_data(LinphoneContent *content, void *ud) { - content->user_data = ud; -} - -const char * linphone_content_get_type(const LinphoneContent *content) { - return sal_body_handler_get_type(content->body_handler); -} - -void linphone_content_set_type(LinphoneContent *content, const char *type) { - sal_body_handler_set_type(content->body_handler, type); -} - -const char * linphone_content_get_subtype(const LinphoneContent *content) { - return sal_body_handler_get_subtype(content->body_handler); -} - -void linphone_content_set_subtype(LinphoneContent *content, const char *subtype) { - sal_body_handler_set_subtype(content->body_handler, subtype); -} - -uint8_t * linphone_content_get_buffer(const LinphoneContent *content) { - return (uint8_t *)sal_body_handler_get_data(content->body_handler); -} - -void linphone_content_set_buffer(LinphoneContent *content, const uint8_t *buffer, size_t size) { - void *data; - sal_body_handler_set_size(content->body_handler, size); - data = belle_sip_malloc(size + 1); - memcpy(data, buffer, size); - ((char *)data)[size] = '\0'; - sal_body_handler_set_data(content->body_handler, data); -} - -const char * linphone_content_get_string_buffer(const LinphoneContent *content) { - return (const char *)linphone_content_get_buffer(content); -} - -void linphone_content_set_string_buffer(LinphoneContent *content, const char *buffer) { - sal_body_handler_set_size(content->body_handler, strlen(buffer)); - sal_body_handler_set_data(content->body_handler, belle_sip_strdup(buffer)); -} - -size_t linphone_content_get_size(const LinphoneContent *content) { - return sal_body_handler_get_size(content->body_handler); -} - -void linphone_content_set_size(LinphoneContent *content, size_t size) { - sal_body_handler_set_size(content->body_handler, size); -} - -const char * linphone_content_get_encoding(const LinphoneContent *content) { - return sal_body_handler_get_encoding(content->body_handler); -} - -void linphone_content_set_encoding(LinphoneContent *content, const char *encoding) { - sal_body_handler_set_encoding(content->body_handler, encoding); -} - -const char * linphone_content_get_name(const LinphoneContent *content) { - return content->name; -} - -void linphone_content_set_name(LinphoneContent *content, const char *name) { - if (content->name != NULL) { - belle_sip_free(content->name); - content->name = NULL; - } - if (name != NULL) { - content->name = belle_sip_strdup(name); - } -} - -size_t linphone_content_get_key_size(const LinphoneContent *content) { - return content->keyLength; -} - -const char * linphone_content_get_key(const LinphoneContent *content) { - return content->key; -} - -void linphone_content_set_key(LinphoneContent *content, const char *key, const size_t keyLength) { - if (content->key != NULL) { - belle_sip_free(content->key); - content->key = NULL; - } - if (key != NULL) { - content->key = reinterpret_cast(belle_sip_malloc(keyLength + 1)); - memcpy(content->key, key, keyLength); - content->key[keyLength] = '\0'; - content->keyLength = keyLength; - } -} - -/* crypto context is managed(allocated/freed) by the encryption function, so provide the address of field in the private structure */ -void ** linphone_content_get_cryptoContext_address(LinphoneContent *content) { - return &(content->cryptoContext); -} - -bool_t linphone_content_is_multipart(const LinphoneContent *content) { - return sal_body_handler_is_multipart(content->body_handler); -} - -LinphoneContent * linphone_content_get_part(const LinphoneContent *content, int idx) { - SalBodyHandler *part_body_handler; - if (!linphone_content_is_multipart(content)) return NULL; - part_body_handler = sal_body_handler_get_part(content->body_handler, idx); - return linphone_content_from_sal_body_handler(part_body_handler); -} - -LinphoneContent * linphone_content_find_part_by_header(const LinphoneContent *content, const char *header_name, const char *header_value) { - SalBodyHandler *part_body_handler; - if (!linphone_content_is_multipart(content)) return NULL; - part_body_handler = sal_body_handler_find_part_by_header(content->body_handler, header_name, header_value); - return linphone_content_from_sal_body_handler(part_body_handler); -} - -const char * linphone_content_get_custom_header(const LinphoneContent *content, const char *header_name) { - return sal_body_handler_get_header(content->body_handler, header_name); -} - - -LinphoneContent * linphone_content_new(void) { - return linphone_content_new_with_body_handler(NULL); -} - -LinphoneContent * linphone_content_copy(const LinphoneContent *ref) { - return (LinphoneContent *)belle_sip_object_ref(belle_sip_object_clone(BELLE_SIP_OBJECT(ref))); -} - -LinphoneContent * linphone_content_from_sal_body_handler(SalBodyHandler *body_handler) { - if (body_handler) { - return linphone_content_new_with_body_handler(body_handler); - } - return NULL; -} - -SalBodyHandler * sal_body_handler_from_content(const LinphoneContent *content) { - if (content == NULL) return NULL; - return content->body_handler; -} From 472f010e81dc09a1f3a29ff3656e9348ce114ec9 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 20 Mar 2018 11:44:53 +0100 Subject: [PATCH 18/44] Fixed leak of LinphoneContent --- src/c-wrapper/api/c-chat-message.cpp | 2 +- src/chat/modifier/file-transfer-chat-message-modifier.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/c-wrapper/api/c-chat-message.cpp b/src/c-wrapper/api/c-chat-message.cpp index c9ee6184b..b191027ed 100644 --- a/src/c-wrapper/api/c-chat-message.cpp +++ b/src/c-wrapper/api/c-chat-message.cpp @@ -291,7 +291,7 @@ int linphone_chat_message_set_text(LinphoneChatMessage *msg, const char* text) { LinphoneContent *linphone_chat_message_get_file_transfer_information(LinphoneChatMessage *msg) { const LinphonePrivate::Content *content = L_GET_PRIVATE_FROM_C_OBJECT(msg)->getFileTransferInformation(); - if (content) return linphone_content_ref(L_GET_C_BACK_PTR(content)); + if (content) return L_GET_C_BACK_PTR(content); return NULL; } diff --git a/src/chat/modifier/file-transfer-chat-message-modifier.cpp b/src/chat/modifier/file-transfer-chat-message-modifier.cpp index 14733e4c8..6bebbfa0b 100644 --- a/src/chat/modifier/file-transfer-chat-message-modifier.cpp +++ b/src/chat/modifier/file-transfer-chat-message-modifier.cpp @@ -723,6 +723,7 @@ void FileTransferChatMessageModifier::onRecvBody (belle_sip_user_body_handler_t // Legacy: call back given by application level linphone_core_notify_file_transfer_recv(message->getCore()->getCCore(), msg, content, (const char *)buffer, size); } + linphone_content_unref(content); } } else { lWarning() << "File transfer decrypt failed with code " << (int)retval; From 61b45fd7a413eedd1d5000e0a05149e7878f1b71 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 20 Mar 2018 14:20:11 +0100 Subject: [PATCH 19/44] Fixed issue with server presence & content headers --- src/c-wrapper/api/c-content.cpp | 22 ++++++++++++++++++++-- src/content/content-manager.cpp | 18 ++++++++++-------- src/content/content-type.cpp | 3 +++ src/content/content-type.h | 2 ++ 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp index 211985bbb..30021cbb7 100644 --- a/src/c-wrapper/api/c-content.cpp +++ b/src/c-wrapper/api/c-content.cpp @@ -159,7 +159,7 @@ void linphone_content_set_name(LinphoneContent *content, const char *name) { } bool_t linphone_content_is_multipart(const LinphoneContent *content) { - return L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType() == LinphonePrivate::ContentType::Multipart; + return L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType().isMultipart(); } LinphoneContent * linphone_content_get_part(const LinphoneContent *content, int idx) { @@ -244,6 +244,13 @@ static LinphoneContent * linphone_content_new_with_body_handler(SalBodyHandler * 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); LinphoneContent *part_content = linphone_content_new_with_body_handler((SalBodyHandler *)part); + + const belle_sip_list_t *headers = belle_sip_body_handler_get_headers(part); + for (; headers != NULL; headers = headers->next) { + belle_sip_header_t *header = BELLE_SIP_HEADER(headers->data); + L_GET_CPP_PTR_FROM_C_OBJECT(part_content)->addHeader(belle_sip_header_get_name(header), belle_sip_header_get_unparsed_value(header)); + } + contents.push_back(*L_GET_CPP_PTR_FROM_C_OBJECT(part_content)); } LinphonePrivate::Content multipartContent = LinphonePrivate::ContentManager::contentListToMultipart(contents); @@ -281,7 +288,7 @@ LinphoneContent * linphone_content_from_sal_body_handler(SalBodyHandler *body_ha SalBodyHandler * sal_body_handler_from_content(const LinphoneContent *content) { if (content == NULL) return NULL; SalBodyHandler *body_handler; - if (L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType() == LinphonePrivate::ContentType::Multipart) { + if (L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType().isMultipart()) { size_t size = linphone_content_get_size(content); char *buffer = ms_strdup(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getBodyAsUtf8String().c_str()); const char *boundary = L_STRING_TO_C(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType().getParameter()); @@ -291,6 +298,17 @@ SalBodyHandler * sal_body_handler_from_content(const LinphoneContent *content) { body_handler = sal_body_handler_new(); sal_body_handler_set_data(body_handler, belle_sip_strdup(linphone_content_get_string_buffer(content))); } + + for (const auto &header : L_GET_CPP_PTR_FROM_C_OBJECT(content)->getHeaders()) { + belle_sip_header_t *additionalHeader = BELLE_SIP_HEADER( + belle_sip_header_create( + header.first.c_str(), + header.second.c_str() + ) + ); + belle_sip_body_handler_add_header(BELLE_SIP_BODY_HANDLER(body_handler), additionalHeader); + } + sal_body_handler_set_type(body_handler, linphone_content_get_type(content)); sal_body_handler_set_subtype(body_handler, linphone_content_get_subtype(content)); sal_body_handler_set_size(body_handler, linphone_content_get_size(content)); diff --git a/src/content/content-manager.cpp b/src/content/content-manager.cpp index 94ee69ce9..d4c286c79 100644 --- a/src/content/content-manager.cpp +++ b/src/content/content-manager.cpp @@ -45,23 +45,25 @@ list ContentManager::multipartToContentList (const Content &content) { 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); - belle_sip_header_content_type_t *partContentType = nullptr; + Content content; + 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) { - partContentType = BELLE_SIP_HEADER_CONTENT_TYPE(header); - break; + belle_sip_header_content_type_t * partContentType = BELLE_SIP_HEADER_CONTENT_TYPE(header); + content.setContentType(ContentType( + belle_sip_header_content_type_get_type(partContentType), + belle_sip_header_content_type_get_subtype(partContentType) + )); + } else { + content.addHeader(belle_sip_header_get_name(header), belle_sip_header_get_unparsed_value(header)); } } - Content content; content.setBody(static_cast( belle_sip_memory_body_handler_get_buffer(BELLE_SIP_MEMORY_BODY_HANDLER(part)) )); - content.setContentType(ContentType( - belle_sip_header_content_type_get_type(partContentType), - belle_sip_header_content_type_get_subtype(partContentType) - )); + contents.push_back(move(content)); } diff --git a/src/content/content-type.cpp b/src/content/content-type.cpp index 78c356af6..e457ff828 100644 --- a/src/content/content-type.cpp +++ b/src/content/content-type.cpp @@ -172,6 +172,9 @@ string ContentType::asString () const { return ""; } +bool ContentType::isMultipart() const { + return getType() == "multipart"; +} bool ContentType::isFile () const { // TODO Remove when not needed anymore in step 2.1 of maindb diff --git a/src/content/content-type.h b/src/content/content-type.h index b13063115..bdf51c75d 100644 --- a/src/content/content-type.h +++ b/src/content/content-type.h @@ -60,6 +60,8 @@ public: std::string asString () const; + bool isMultipart() const; + static bool isFile (const ContentType &contentType); static const ContentType ConferenceInfo; From fd6b049203f3502ac1ad1086580517468e7898c8 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 20 Mar 2018 14:46:45 +0100 Subject: [PATCH 20/44] Fixed LinphoneContent leak --- src/c-wrapper/api/c-content.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp index 30021cbb7..d487e8847 100644 --- a/src/c-wrapper/api/c-content.cpp +++ b/src/c-wrapper/api/c-content.cpp @@ -252,6 +252,7 @@ static LinphoneContent * linphone_content_new_with_body_handler(SalBodyHandler * } contents.push_back(*L_GET_CPP_PTR_FROM_C_OBJECT(part_content)); + linphone_content_unref(part_content); } LinphonePrivate::Content multipartContent = LinphonePrivate::ContentManager::contentListToMultipart(contents); linphone_content_set_string_buffer(content, multipartContent.getBodyAsUtf8String().c_str()); From cf2b6ca2bf87a2333c02d093362981fb25dfbd57 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 21 Mar 2018 10:51:19 +0100 Subject: [PATCH 21/44] Fixed content type parameters between SalBodyHandler and Content --- coreapi/bellesip_sal/sal_impl.c | 20 ++++++++++++++++++++ src/c-wrapper/api/c-content.cpp | 24 ++++++++++++++++++------ src/c-wrapper/internal/c-sal.h | 2 ++ 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/coreapi/bellesip_sal/sal_impl.c b/coreapi/bellesip_sal/sal_impl.c index ad11525fb..71578364b 100644 --- a/coreapi/bellesip_sal/sal_impl.c +++ b/coreapi/bellesip_sal/sal_impl.c @@ -348,6 +348,26 @@ void sal_body_handler_set_subtype(SalBodyHandler *body_handler, const char *subt belle_sip_header_content_type_set_subtype(content_type, subtype); } +char * sal_body_handler_get_content_type_parameters(const SalBodyHandler *body_handler) { + belle_sip_header_content_type_t *content_type = BELLE_SIP_HEADER_CONTENT_TYPE(sal_body_handler_find_header(body_handler, "Content-Type")); + if (content_type != NULL) { + char buff[2048]; + size_t buff_size = sizeof(buff); + size_t offset = 0; + belle_sip_parameters_marshal(BELLE_SIP_PARAMETERS(content_type), buff, buff_size, &offset); + buff[offset]='\0'; + return strdup(buff); + } + return NULL; +} + +void sal_body_handler_set_content_type_parameters(SalBodyHandler *body_handler, const char *params) { + belle_sip_header_content_type_t *content_type = BELLE_SIP_HEADER_CONTENT_TYPE(sal_body_handler_find_header(body_handler, "Content-Type")); + if (content_type != NULL) { + belle_sip_parameters_set(BELLE_SIP_PARAMETERS(content_type), params); + } +} + const char * sal_body_handler_get_encoding(const SalBodyHandler *body_handler) { belle_sip_header_t *content_encoding = sal_body_handler_find_header(body_handler, "Content-Encoding"); if (content_encoding != NULL) { diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp index d487e8847..572444ddf 100644 --- a/src/c-wrapper/api/c-content.cpp +++ b/src/c-wrapper/api/c-content.cpp @@ -233,9 +233,14 @@ static LinphoneContent * linphone_content_new_with_body_handler(SalBodyHandler * content->cryptoContext = NULL; LinphonePrivate::Content *c = new LinphonePrivate::Content(); L_SET_CPP_PTR_FROM_C_OBJECT(content, c); + if (body_handler != NULL) { - linphone_content_set_type(content, sal_body_handler_get_type(body_handler)); - linphone_content_set_subtype(content, sal_body_handler_get_subtype(body_handler)); + LinphonePrivate::ContentType ct = c->getContentType(); + ct.setType(sal_body_handler_get_type(body_handler)); + ct.setSubType(sal_body_handler_get_subtype(body_handler)); + ct.setParameter(sal_body_handler_get_content_type_parameters(body_handler)); + c->setContentType(ct); + if (!sal_body_handler_is_multipart(body_handler)) { linphone_content_set_string_buffer(content, (char *)sal_body_handler_get_data(body_handler)); } else { @@ -257,8 +262,10 @@ static LinphoneContent * linphone_content_new_with_body_handler(SalBodyHandler * LinphonePrivate::Content multipartContent = LinphonePrivate::ContentManager::contentListToMultipart(contents); linphone_content_set_string_buffer(content, multipartContent.getBodyAsUtf8String().c_str()); } + if (sal_body_handler_get_encoding(body_handler)) linphone_content_set_encoding(content, sal_body_handler_get_encoding(body_handler)); } + return content; } @@ -288,11 +295,14 @@ LinphoneContent * linphone_content_from_sal_body_handler(SalBodyHandler *body_ha SalBodyHandler * sal_body_handler_from_content(const LinphoneContent *content) { if (content == NULL) return NULL; + SalBodyHandler *body_handler; - if (L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType().isMultipart()) { + LinphonePrivate::ContentType contentType = L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType(); + + if (contentType.isMultipart()) { size_t size = linphone_content_get_size(content); char *buffer = ms_strdup(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getBodyAsUtf8String().c_str()); - const char *boundary = L_STRING_TO_C(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType().getParameter()); + const char *boundary = L_STRING_TO_C(contentType.getParameter()); belle_sip_multipart_body_handler_t *bh = belle_sip_multipart_body_handler_new_from_buffer(buffer, size, boundary); body_handler = (SalBodyHandler *)BELLE_SIP_BODY_HANDLER(bh); } else { @@ -310,9 +320,11 @@ SalBodyHandler * sal_body_handler_from_content(const LinphoneContent *content) { belle_sip_body_handler_add_header(BELLE_SIP_BODY_HANDLER(body_handler), additionalHeader); } - sal_body_handler_set_type(body_handler, linphone_content_get_type(content)); - sal_body_handler_set_subtype(body_handler, linphone_content_get_subtype(content)); + sal_body_handler_set_type(body_handler, contentType.getType().c_str()); + sal_body_handler_set_subtype(body_handler, contentType.getSubType().c_str()); sal_body_handler_set_size(body_handler, linphone_content_get_size(content)); + sal_body_handler_set_content_type_parameters(body_handler, contentType.getParameter().c_str()); if (content->encoding) sal_body_handler_set_encoding(body_handler, linphone_content_get_encoding(content)); + return body_handler; } diff --git a/src/c-wrapper/internal/c-sal.h b/src/c-wrapper/internal/c-sal.h index 4aeca0042..3f53db90c 100644 --- a/src/c-wrapper/internal/c-sal.h +++ b/src/c-wrapper/internal/c-sal.h @@ -636,6 +636,8 @@ const char * sal_body_handler_get_type(const SalBodyHandler *body_handler); void sal_body_handler_set_type(SalBodyHandler *body_handler, const char *type); const char * sal_body_handler_get_subtype(const SalBodyHandler *body_handler); void sal_body_handler_set_subtype(SalBodyHandler *body_handler, const char *subtype); +char * sal_body_handler_get_content_type_parameters(const SalBodyHandler *body_handler); +void sal_body_handler_set_content_type_parameters(SalBodyHandler *body_handler, const char *params); const char * sal_body_handler_get_encoding(const SalBodyHandler *body_handler); void sal_body_handler_set_encoding(SalBodyHandler *body_handler, const char *encoding); void * sal_body_handler_get_data(const SalBodyHandler *body_handler); From 8a30c727dbe925e4783dddc078a1598773321871 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 21 Mar 2018 13:20:49 +0100 Subject: [PATCH 22/44] Improved parameter use in ContentType --- coreapi/bellesip_sal/sal_impl.c | 21 ++-- include/linphone/api/c-content.h | 8 ++ src/CMakeLists.txt | 2 + src/c-wrapper/api/c-content.cpp | 27 +++-- src/c-wrapper/internal/c-sal.h | 5 +- src/chat/chat-message/chat-message.cpp | 5 +- .../multipart-chat-message-modifier.cpp | 5 +- src/content/content-manager.cpp | 2 +- src/content/content-type.cpp | 91 ++++++++++++--- src/content/content-type.h | 16 ++- src/content/header-param.cpp | 105 ++++++++++++++++++ src/content/header-param.h | 61 ++++++++++ 12 files changed, 307 insertions(+), 41 deletions(-) create mode 100644 src/content/header-param.cpp create mode 100644 src/content/header-param.h diff --git a/coreapi/bellesip_sal/sal_impl.c b/coreapi/bellesip_sal/sal_impl.c index 71578364b..99c859f52 100644 --- a/coreapi/bellesip_sal/sal_impl.c +++ b/coreapi/bellesip_sal/sal_impl.c @@ -348,23 +348,26 @@ void sal_body_handler_set_subtype(SalBodyHandler *body_handler, const char *subt belle_sip_header_content_type_set_subtype(content_type, subtype); } -char * sal_body_handler_get_content_type_parameters(const SalBodyHandler *body_handler) { +const belle_sip_list_t * sal_body_handler_get_content_type_parameters_names(const SalBodyHandler *body_handler) { belle_sip_header_content_type_t *content_type = BELLE_SIP_HEADER_CONTENT_TYPE(sal_body_handler_find_header(body_handler, "Content-Type")); if (content_type != NULL) { - char buff[2048]; - size_t buff_size = sizeof(buff); - size_t offset = 0; - belle_sip_parameters_marshal(BELLE_SIP_PARAMETERS(content_type), buff, buff_size, &offset); - buff[offset]='\0'; - return strdup(buff); + return belle_sip_parameters_get_parameter_names(BELLE_SIP_PARAMETERS(content_type)); } return NULL; } -void sal_body_handler_set_content_type_parameters(SalBodyHandler *body_handler, const char *params) { +const char * sal_body_handler_get_content_type_parameter(const SalBodyHandler *body_handler, const char *name) { belle_sip_header_content_type_t *content_type = BELLE_SIP_HEADER_CONTENT_TYPE(sal_body_handler_find_header(body_handler, "Content-Type")); if (content_type != NULL) { - belle_sip_parameters_set(BELLE_SIP_PARAMETERS(content_type), params); + return belle_sip_parameters_get_parameter(BELLE_SIP_PARAMETERS(content_type), name); + } + return NULL; +} + +void sal_body_handler_set_content_type_parameter(SalBodyHandler *body_handler, const char *paramName, const char *paramValue) { + belle_sip_header_content_type_t *content_type = BELLE_SIP_HEADER_CONTENT_TYPE(sal_body_handler_find_header(body_handler, "Content-Type")); + if (content_type != NULL) { + belle_sip_parameters_set_parameter(BELLE_SIP_PARAMETERS(content_type), paramName, paramValue); } } diff --git a/include/linphone/api/c-content.h b/include/linphone/api/c-content.h index fa030a309..5cdd27663 100644 --- a/include/linphone/api/c-content.h +++ b/include/linphone/api/c-content.h @@ -88,6 +88,14 @@ LINPHONE_PUBLIC const char * linphone_content_get_subtype(const LinphoneContent */ LINPHONE_PUBLIC void linphone_content_set_subtype(LinphoneContent *content, const char *subtype); +/** + * Adds a parameter to the ContentType header. + * @param[in] content LinphoneContent object. + * @param[in] name the name of the parameter to add. + * @param[in] value the value of the parameter to add. + */ +LINPHONE_PUBLIC void linphone_content_add_content_type_parameter(LinphoneContent *content, const char *name, const char *value); + /** * Get the content data buffer, usually a string. * @param[in] content LinphoneContent object. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 11377efe9..66d42b91e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -99,6 +99,7 @@ set(LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES content/content.h content/file-content.h content/file-transfer-content.h + content/header-param.h core/core-accessor.h core/core-listener.h core/core-p.h @@ -223,6 +224,7 @@ set(LINPHONE_CXX_OBJECTS_SOURCE_FILES content/content.cpp content/file-content.cpp content/file-transfer-content.cpp + content/header-param.cpp core/core-accessor.cpp core/core-call.cpp core/core-chat-room.cpp diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp index 572444ddf..5e6c2e94a 100644 --- a/src/c-wrapper/api/c-content.cpp +++ b/src/c-wrapper/api/c-content.cpp @@ -24,6 +24,7 @@ #include "content/content.h" #include "content/content-type.h" +#include "content/header-param.h" #include "content/content-manager.h" #include "content/file-content.h" #include "content/file-transfer-content.h" @@ -90,6 +91,12 @@ void linphone_content_set_subtype(LinphoneContent *content, const char *subtype) L_GET_CPP_PTR_FROM_C_OBJECT(content)->setContentType(ct); } +void linphone_content_add_content_type_parameter(LinphoneContent *content, const char *name, const char *value) { + LinphonePrivate::ContentType ct = L_GET_CPP_PTR_FROM_C_OBJECT(content)->getContentType(); + ct.addParameter(L_C_TO_STRING(name), L_C_TO_STRING(value)); + L_GET_CPP_PTR_FROM_C_OBJECT(content)->setContentType(ct); +} + uint8_t * linphone_content_get_buffer(const LinphoneContent *content) { return (uint8_t *)linphone_content_get_string_buffer(content); } @@ -235,13 +242,15 @@ static LinphoneContent * linphone_content_new_with_body_handler(SalBodyHandler * L_SET_CPP_PTR_FROM_C_OBJECT(content, c); if (body_handler != NULL) { - LinphonePrivate::ContentType ct = c->getContentType(); - ct.setType(sal_body_handler_get_type(body_handler)); - ct.setSubType(sal_body_handler_get_subtype(body_handler)); - ct.setParameter(sal_body_handler_get_content_type_parameters(body_handler)); - c->setContentType(ct); + linphone_content_set_type(content, sal_body_handler_get_type(body_handler)); + linphone_content_set_subtype(content, sal_body_handler_get_subtype(body_handler)); + for (const belle_sip_list_t *params = sal_body_handler_get_content_type_parameters_names(body_handler); params; params = params->next) { + const char *paramName = (const char *)(params->data); + const char *paramValue = sal_body_handler_get_content_type_parameter(body_handler, paramName); + linphone_content_add_content_type_parameter(content, paramName, paramValue); + } - if (!sal_body_handler_is_multipart(body_handler)) { + if (!linphone_content_is_multipart(content)) { linphone_content_set_string_buffer(content, (char *)sal_body_handler_get_data(body_handler)); } else { belle_sip_multipart_body_handler_t *mpbh = BELLE_SIP_MULTIPART_BODY_HANDLER(body_handler); @@ -302,7 +311,7 @@ SalBodyHandler * sal_body_handler_from_content(const LinphoneContent *content) { if (contentType.isMultipart()) { size_t size = linphone_content_get_size(content); char *buffer = ms_strdup(L_GET_CPP_PTR_FROM_C_OBJECT(content)->getBodyAsUtf8String().c_str()); - const char *boundary = L_STRING_TO_C(contentType.getParameter()); + const char *boundary = L_STRING_TO_C(contentType.getParameter("boundary").getValue()); belle_sip_multipart_body_handler_t *bh = belle_sip_multipart_body_handler_new_from_buffer(buffer, size, boundary); body_handler = (SalBodyHandler *)BELLE_SIP_BODY_HANDLER(bh); } else { @@ -323,7 +332,9 @@ SalBodyHandler * sal_body_handler_from_content(const LinphoneContent *content) { sal_body_handler_set_type(body_handler, contentType.getType().c_str()); sal_body_handler_set_subtype(body_handler, contentType.getSubType().c_str()); sal_body_handler_set_size(body_handler, linphone_content_get_size(content)); - sal_body_handler_set_content_type_parameters(body_handler, contentType.getParameter().c_str()); + for (const auto ¶m : contentType.getParameters()) { + sal_body_handler_set_content_type_parameter(body_handler, param.getName().c_str(), param.getValue().c_str()); + } if (content->encoding) sal_body_handler_set_encoding(body_handler, linphone_content_get_encoding(content)); return body_handler; diff --git a/src/c-wrapper/internal/c-sal.h b/src/c-wrapper/internal/c-sal.h index 3f53db90c..dd2e53f8f 100644 --- a/src/c-wrapper/internal/c-sal.h +++ b/src/c-wrapper/internal/c-sal.h @@ -636,8 +636,9 @@ const char * sal_body_handler_get_type(const SalBodyHandler *body_handler); void sal_body_handler_set_type(SalBodyHandler *body_handler, const char *type); const char * sal_body_handler_get_subtype(const SalBodyHandler *body_handler); void sal_body_handler_set_subtype(SalBodyHandler *body_handler, const char *subtype); -char * sal_body_handler_get_content_type_parameters(const SalBodyHandler *body_handler); -void sal_body_handler_set_content_type_parameters(SalBodyHandler *body_handler, const char *params); +const belle_sip_list_t * sal_body_handler_get_content_type_parameters_names(const SalBodyHandler *body_handler); +const char * sal_body_handler_get_content_type_parameter(const SalBodyHandler *body_handler, const char *name); +void sal_body_handler_set_content_type_parameter(SalBodyHandler *body_handler, const char *paramName, const char *paramValue); const char * sal_body_handler_get_encoding(const SalBodyHandler *body_handler); void sal_body_handler_set_encoding(SalBodyHandler *body_handler, const char *encoding); void * sal_body_handler_get_data(const SalBodyHandler *body_handler); diff --git a/src/chat/chat-message/chat-message.cpp b/src/chat/chat-message/chat-message.cpp index ca15d8c6a..ff2a595e7 100644 --- a/src/chat/chat-message/chat-message.cpp +++ b/src/chat/chat-message/chat-message.cpp @@ -36,6 +36,7 @@ #include "chat/modifier/file-transfer-chat-message-modifier.h" #include "chat/modifier/multipart-chat-message-modifier.h" #include "content/file-content.h" +#include "content/header-param.h" #include "content/content.h" #include "core/core.h" #include "core/core-p.h" @@ -450,7 +451,7 @@ static void forceUtf8Content (Content &content) { if (contentType != ContentType::PlainText) return; - string charset = contentType.getParameter(); + string charset = contentType.getParameter("charset").getValue(); if (charset.empty()) return; @@ -469,7 +470,7 @@ static void forceUtf8Content (Content &content) { if (!utf8Body.empty()) { // TODO: use move operator if possible in the future! content.setBodyFromUtf8(utf8Body); - contentType.setParameter(string(contentType.getParameter()).replace(begin, end - begin, "UTF-8")); + contentType.addParameter("charset", "UTF-8"); content.setContentType(contentType); } } diff --git a/src/chat/modifier/multipart-chat-message-modifier.cpp b/src/chat/modifier/multipart-chat-message-modifier.cpp index 5d0a34446..8e110119e 100644 --- a/src/chat/modifier/multipart-chat-message-modifier.cpp +++ b/src/chat/modifier/multipart-chat-message-modifier.cpp @@ -24,6 +24,7 @@ #include "chat/chat-message/chat-message.h" #include "chat/chat-room/chat-room.h" #include "content/content-type.h" +#include "content/header-param.h" #include "content/file-transfer-content.h" #include "logger/logger.h" #include "core/core.h" @@ -60,7 +61,7 @@ ChatMessageModifier::Result MultipartChatMessageModifier::encode ( Content newContent; ContentType newContentType(ContentType::Multipart); - newContentType.setParameter("boundary=" + boundary); + newContentType.addParameter("boundary", boundary); newContent.setContentType(newContentType); newContent.setBody(multipartMessage.str()); message->setInternalContent(newContent); @@ -70,7 +71,7 @@ ChatMessageModifier::Result MultipartChatMessageModifier::encode ( ChatMessageModifier::Result MultipartChatMessageModifier::decode (const shared_ptr &message, int &errorCode) { if (message->getInternalContent().getContentType().getType() == "multipart") { - string boundary = message->getInternalContent().getContentType().getParameter(); + string boundary = message->getInternalContent().getContentType().getParameter("boundary").getValue(); if (boundary.empty()) { lError() << "Boundary parameter of content-type not found: " << message->getInternalContent().getContentType().asString(); return ChatMessageModifier::Result::Error; diff --git a/src/content/content-manager.cpp b/src/content/content-manager.cpp index d4c286c79..8e3908310 100644 --- a/src/content/content-manager.cpp +++ b/src/content/content-manager.cpp @@ -112,7 +112,7 @@ Content ContentManager::contentListToMultipart (const list &contents) { belle_sip_object_unref(mpbh); ContentType contentType = ContentType::Multipart; - contentType.setParameter("boundary=" + string(MultipartBoundary)); + contentType.addParameter("boundary", string(MultipartBoundary)); content.setContentType(contentType); return content; diff --git a/src/content/content-type.cpp b/src/content/content-type.cpp index e457ff828..72fe78fd2 100644 --- a/src/content/content-type.cpp +++ b/src/content/content-type.cpp @@ -18,8 +18,10 @@ */ #include "linphone/utils/utils.h" +#include "linphone/utils/algorithm.h" #include "content-type.h" +#include "header-param.h" #include "object/clonable-object-p.h" // ============================================================================= @@ -34,7 +36,7 @@ class ContentTypePrivate : public ClonableObjectPrivate { public: string type; string subType; - string parameter; + std::list parameters; }; // ----------------------------------------------------------------------------- @@ -68,8 +70,15 @@ ContentType::ContentType (const string &contentType) : ClonableObject(*new Conte d->type.clear(); } - if (posParam != string::npos) - setParameter(Utils::trim(contentType.substr(posParam + 1))); + if (posParam != string::npos) { + string params = contentType.substr(posParam, end); + string token; + while ((pos = params.find(";")) != std::string::npos) { + token = params.substr(0, pos); + addParameter(HeaderParam(token)); + params.erase(0, pos + 1); + } + } } ContentType::ContentType (const string &type, const string &subType) : ClonableObject(*new ContentTypePrivate) { @@ -82,22 +91,34 @@ ContentType::ContentType (const string &type, const string &subType) : ClonableO ContentType::ContentType ( const string &type, const string &subType, - const string ¶meter + const HeaderParam ¶meter ) : ClonableObject(*new ContentTypePrivate) { L_D(); if (setType(type) && !setSubType(subType)) d->type.clear(); - setParameter(parameter); + addParameter(parameter); } -ContentType::ContentType (const ContentType &other) : ContentType(other.getType(), other.getSubType(), other.getParameter()) {} +ContentType::ContentType ( + const string &type, + const string &subType, + const std::list ¶meters +) : ClonableObject(*new ContentTypePrivate) { + L_D(); + + if (setType(type) && !setSubType(subType)) + d->type.clear(); + addParameters(parameters); +} + +ContentType::ContentType (const ContentType &other) : ContentType(other.getType(), other.getSubType(), other.getParameters()) {} ContentType &ContentType::operator= (const ContentType &other) { if (this != &other) { setType(other.getType()); setSubType(other.getSubType()); - setParameter(other.getParameter()); + addParameters(other.getParameters()); } return *this; @@ -106,7 +127,7 @@ ContentType &ContentType::operator= (const ContentType &other) { bool ContentType::operator== (const ContentType &other) const { return getType() == other.getType() && getSubType() == other.getSubType() && - getParameter() == other.getParameter(); + getParameters() == other.getParameters(); } bool ContentType::operator!= (const ContentType &other) const { @@ -141,14 +162,54 @@ bool ContentType::setSubType (const string &subType) { return false; } -const string &ContentType::getParameter () const { +const std::list &ContentType::getParameters () const { L_D(); - return d->parameter; + + return d->parameters; } -void ContentType::setParameter (const string ¶meter) { +void ContentType::addParameter (const std::string ¶mName, const std::string ¶mValue) { + addParameter(HeaderParam(paramName, paramValue)); +} + +void ContentType::addParameter (const HeaderParam ¶m) { L_D(); - d->parameter = parameter; + removeParameter(param); + d->parameters.push_back(param); +} + +void ContentType::addParameters(const std::list ¶ms) { + for (auto it = std::begin(params); it!=std::end(params); ++it) { + HeaderParam param = *it; + addParameter(param.getName(), param.getValue()); + } +} + +void ContentType::removeParameter (const std::string ¶mName) { + L_D(); + auto it = findParameter(paramName); + if (it != d->parameters.cend()) + d->parameters.remove(*it); +} + +void ContentType::removeParameter (const HeaderParam ¶m) { + removeParameter(param.getName()); +} + +std::list::const_iterator ContentType::findParameter (const std::string ¶mName) const { + L_D(); + return findIf(d->parameters, [¶mName](const HeaderParam ¶m) { + return param.getName() == paramName; + }); +} + +const HeaderParam &ContentType::getParameter (const std::string ¶mName) const { + L_D(); + std::list::const_iterator it = findParameter(paramName); + if (it != d->parameters.cend()) { + return *it; + } + return Utils::getEmptyConstRefObject(); } bool ContentType::isEmpty () const { @@ -165,8 +226,10 @@ string ContentType::asString () const { L_D(); if (isValid()) { string asString = d->type + "/" + d->subType; - if (!d->parameter.empty()) - asString += "; " + d->parameter; + for (auto it = std::begin(getParameters()); it!=std::end(getParameters()); ++it) { + HeaderParam param = *it; + asString += param.asString(); + } return asString; } return ""; diff --git a/src/content/content-type.h b/src/content/content-type.h index bdf51c75d..3b155e4cd 100644 --- a/src/content/content-type.h +++ b/src/content/content-type.h @@ -20,6 +20,8 @@ #ifndef _L_CONTENT_TYPE_H_ #define _L_CONTENT_TYPE_H_ +#include + #include "object/clonable-object.h" // ============================================================================= @@ -27,12 +29,14 @@ LINPHONE_BEGIN_NAMESPACE class ContentTypePrivate; +class HeaderParam; class LINPHONE_PUBLIC ContentType : public ClonableObject { public: explicit ContentType (const std::string &contentType = ""); ContentType (const std::string &type, const std::string &subType); - ContentType (const std::string &type, const std::string &subType, const std::string ¶meter); + ContentType (const std::string &type, const std::string &subType, const HeaderParam ¶meter); + ContentType (const std::string &type, const std::string &subType, const std::list ¶meters); ContentType (const ContentType &other); ContentType &operator= (const ContentType &other); @@ -55,8 +59,14 @@ public: const std::string &getSubType () const; bool setSubType (const std::string &subType); - const std::string &getParameter () const; - void setParameter (const std::string ¶meter); + const std::list &getParameters () const; + void addParameter (const std::string ¶mName, const std::string ¶mValue); + void addParameter (const HeaderParam ¶m); + void addParameters(const std::list ¶ms); + void removeParameter (const std::string ¶mName); + void removeParameter (const HeaderParam ¶m); + std::list::const_iterator findParameter (const std::string ¶mName) const; + const HeaderParam &getParameter (const std::string ¶mName) const; std::string asString () const; diff --git a/src/content/header-param.cpp b/src/content/header-param.cpp new file mode 100644 index 000000000..31c89caa5 --- /dev/null +++ b/src/content/header-param.cpp @@ -0,0 +1,105 @@ +/* + * header-param.cpp + * Copyright (C) 2010-2018 Belledonne Communications SARL + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "linphone/utils/utils.h" + +#include "header-param.h" +#include "object/clonable-object-p.h" + +// ============================================================================= + +using namespace std; + +LINPHONE_BEGIN_NAMESPACE + +// ----------------------------------------------------------------------------- + +class HeaderParamPrivate : public ClonableObjectPrivate { +public: + string name; + string value; +}; + +// ----------------------------------------------------------------------------- + +HeaderParam::HeaderParam (const string ¶m) : ClonableObject(*new HeaderParamPrivate) { + size_t pos = param.find("="); + size_t end = param.length(); + + if (pos == string::npos) + return; + + setName(param.substr(0, pos)); + setValue(param.substr(pos + 1, end)); +} + +HeaderParam::HeaderParam (const string &name, const string &value) : ClonableObject(*new HeaderParamPrivate) { + setName(name); + setValue(value); +} + +HeaderParam::HeaderParam (const HeaderParam &other) : HeaderParam(other.getName(), other.getValue()) {} + +HeaderParam &HeaderParam::operator= (const HeaderParam &other) { + if (this != &other) { + setName(other.getName()); + setValue(other.getValue()); + } + + return *this; +} + +bool HeaderParam::operator== (const HeaderParam &other) const { + return getName() == other.getName() && + getValue() == other.getValue(); +} + +bool HeaderParam::operator!= (const HeaderParam &other) const { + return !(*this == other); +} + +const string &HeaderParam::getName () const { + L_D(); + return d->name; +} + +bool HeaderParam::setName (const string &name) { + L_D(); + d->name = name; + return true; +} + +const string &HeaderParam::getValue () const { + L_D(); + return d->value; +} + +bool HeaderParam::setValue (const string &value) { + L_D(); + d->value = value; + return true; +} + +string HeaderParam::asString () const { + L_D(); + string asString = ";" + d->name + "=" + d->value; + return asString; +} + +LINPHONE_END_NAMESPACE diff --git a/src/content/header-param.h b/src/content/header-param.h new file mode 100644 index 000000000..a9471b486 --- /dev/null +++ b/src/content/header-param.h @@ -0,0 +1,61 @@ +/* + * header-param.h + * Copyright (C) 2010-2018 Belledonne Communications SARL + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef _L_HEADER_PARAM_H_ +#define _L_HEADER_PARAM_H_ + +#include "object/clonable-object.h" + +// ============================================================================= + +LINPHONE_BEGIN_NAMESPACE + +class HeaderParamPrivate; + +class LINPHONE_PUBLIC HeaderParam : public ClonableObject { +public: + explicit HeaderParam (const std::string &header = ""); + HeaderParam (const std::string &name, const std::string &value); + HeaderParam (const HeaderParam &other); + + HeaderParam &operator= (const HeaderParam &other); + + bool operator== (const HeaderParam &other) const; + bool operator!= (const HeaderParam &other) const; + + // Delete these operators to prevent putting complicated content-type strings + // in the code. Instead define static const HeaderParam objects below. + bool operator== (const std::string &other) const = delete; + bool operator!= (const std::string &other) const = delete; + + const std::string &getName () const; + bool setName (const std::string &name); + + const std::string &getValue () const; + bool setValue (const std::string &value); + + std::string asString () const; + +private: + L_DECLARE_PRIVATE(HeaderParam); +}; + +LINPHONE_END_NAMESPACE + +#endif // ifndef _L_HEADER_PARAM_H_ From b19c228fe4e11692a644dfe7132a5b5a7a078273 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 21 Mar 2018 14:29:27 +0100 Subject: [PATCH 23/44] Fixed leak --- src/c-wrapper/api/c-content.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp index 5e6c2e94a..393059486 100644 --- a/src/c-wrapper/api/c-content.cpp +++ b/src/c-wrapper/api/c-content.cpp @@ -283,7 +283,7 @@ LinphoneContent * linphone_content_new(void) { } LinphoneContent * linphone_content_copy(const LinphoneContent *ref) { - return (LinphoneContent *)belle_sip_object_ref(belle_sip_object_clone(BELLE_SIP_OBJECT(ref))); + return (LinphoneContent *)(belle_sip_object_clone(BELLE_SIP_OBJECT(ref))); } LinphoneContent * linphone_core_create_content(LinphoneCore *lc) { From c93eee237bf84bd9ffba3b7c1ad637b522a06e40 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 21 Mar 2018 15:13:10 +0100 Subject: [PATCH 24/44] Fixed parsing of content type parameters from ContentType(string) constructor --- .../modifier/multipart-chat-message-modifier.cpp | 9 ++------- src/content/content-type.cpp | 13 +++++++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/chat/modifier/multipart-chat-message-modifier.cpp b/src/chat/modifier/multipart-chat-message-modifier.cpp index 8e110119e..1365d03f8 100644 --- a/src/chat/modifier/multipart-chat-message-modifier.cpp +++ b/src/chat/modifier/multipart-chat-message-modifier.cpp @@ -77,18 +77,13 @@ ChatMessageModifier::Result MultipartChatMessageModifier::decode (const shared_p return ChatMessageModifier::Result::Error; } - size_t pos = boundary.find("="); - if (pos == string::npos) { - lError() << "Parameter seems invalid: " << boundary; - return ChatMessageModifier::Result::Error; - } - boundary = "--" + boundary.substr(pos + 1); + boundary = "--" + boundary; lInfo() << "Multipart boundary is " << boundary; const vector body = message->getInternalContent().getBody(); string contentsString(body.begin(), body.end()); - pos = contentsString.find(boundary); + size_t pos = contentsString.find(boundary); if (pos == string::npos) { lError() << "Boundary not found in body !"; return ChatMessageModifier::Result::Error; diff --git a/src/content/content-type.cpp b/src/content/content-type.cpp index 72fe78fd2..6036f3d46 100644 --- a/src/content/content-type.cpp +++ b/src/content/content-type.cpp @@ -71,13 +71,18 @@ ContentType::ContentType (const string &contentType) : ClonableObject(*new Conte } if (posParam != string::npos) { - string params = contentType.substr(posParam, end); + string params = contentType.substr(posParam + 1); string token; - while ((pos = params.find(";")) != std::string::npos) { - token = params.substr(0, pos); + do { + posParam = params.find(";"); + if (posParam != string::npos) { + token = params; + } else { + token = params.substr(0, posParam); + } addParameter(HeaderParam(token)); params.erase(0, pos + 1); - } + } while (posParam != std::string::npos); } } From 57b07c4545c994b1bcac56384fc3387357c3933a Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 21 Mar 2018 15:35:32 +0100 Subject: [PATCH 25/44] Fixed header param parsing & dump if header as no value --- src/content/header-param.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/content/header-param.cpp b/src/content/header-param.cpp index 31c89caa5..cbc07d8a7 100644 --- a/src/content/header-param.cpp +++ b/src/content/header-param.cpp @@ -42,11 +42,12 @@ HeaderParam::HeaderParam (const string ¶m) : ClonableObject(*new HeaderParam size_t pos = param.find("="); size_t end = param.length(); - if (pos == string::npos) - return; - - setName(param.substr(0, pos)); - setValue(param.substr(pos + 1, end)); + if (pos == string::npos) { + setName(param); + } else { + setName(param.substr(0, pos)); + setValue(param.substr(pos + 1, end)); + } } HeaderParam::HeaderParam (const string &name, const string &value) : ClonableObject(*new HeaderParamPrivate) { @@ -98,7 +99,9 @@ bool HeaderParam::setValue (const string &value) { string HeaderParam::asString () const { L_D(); - string asString = ";" + d->name + "=" + d->value; + string asString = ";" + d->name; + if (!d->value.empty()) + asString += "=" + d->value; return asString; } From 5fd6a68b94d7111c15bdfd3a8583bc111aaf335e Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 21 Mar 2018 16:09:36 +0100 Subject: [PATCH 26/44] Fixed another issue with ContentType & HeaderParam constructors doing parsing --- src/content/content-type.cpp | 2 +- src/content/header-param.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/content/content-type.cpp b/src/content/content-type.cpp index 6036f3d46..4af2f2195 100644 --- a/src/content/content-type.cpp +++ b/src/content/content-type.cpp @@ -75,7 +75,7 @@ ContentType::ContentType (const string &contentType) : ClonableObject(*new Conte string token; do { posParam = params.find(";"); - if (posParam != string::npos) { + if (posParam == string::npos) { token = params; } else { token = params.substr(0, posParam); diff --git a/src/content/header-param.cpp b/src/content/header-param.cpp index cbc07d8a7..9605a2f84 100644 --- a/src/content/header-param.cpp +++ b/src/content/header-param.cpp @@ -46,7 +46,7 @@ HeaderParam::HeaderParam (const string ¶m) : ClonableObject(*new HeaderParam setName(param); } else { setName(param.substr(0, pos)); - setValue(param.substr(pos + 1, end)); + setValue(param.substr(pos + 1, end - (pos + 1))); } } From ba1d6bc8b0cb966b6dfd8b97548899e04d44c4dc Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 21 Mar 2018 17:01:00 +0100 Subject: [PATCH 27/44] Remove a lot of useless code --- src/c-wrapper/api/c-content.cpp | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp index 393059486..6d6bdde34 100644 --- a/src/c-wrapper/api/c-content.cpp +++ b/src/c-wrapper/api/c-content.cpp @@ -254,22 +254,9 @@ static LinphoneContent * linphone_content_new_with_body_handler(SalBodyHandler * linphone_content_set_string_buffer(content, (char *)sal_body_handler_get_data(body_handler)); } else { belle_sip_multipart_body_handler_t *mpbh = BELLE_SIP_MULTIPART_BODY_HANDLER(body_handler); - 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); - LinphoneContent *part_content = linphone_content_new_with_body_handler((SalBodyHandler *)part); - - const belle_sip_list_t *headers = belle_sip_body_handler_get_headers(part); - for (; headers != NULL; headers = headers->next) { - belle_sip_header_t *header = BELLE_SIP_HEADER(headers->data); - L_GET_CPP_PTR_FROM_C_OBJECT(part_content)->addHeader(belle_sip_header_get_name(header), belle_sip_header_get_unparsed_value(header)); - } - - contents.push_back(*L_GET_CPP_PTR_FROM_C_OBJECT(part_content)); - linphone_content_unref(part_content); - } - LinphonePrivate::Content multipartContent = LinphonePrivate::ContentManager::contentListToMultipart(contents); - linphone_content_set_string_buffer(content, multipartContent.getBodyAsUtf8String().c_str()); + char *body = belle_sip_object_to_string(mpbh); + linphone_content_set_string_buffer(content, body); + belle_sip_free(body); } if (sal_body_handler_get_encoding(body_handler)) linphone_content_set_encoding(content, sal_body_handler_get_encoding(body_handler)); From 0dfd57b50588953214899404c42724a254406b91 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Fri, 23 Mar 2018 10:37:31 +0100 Subject: [PATCH 28/44] Clean code related to multiparts' Contents --- coreapi/bellesip_sal/sal_impl.c | 5 + src/c-wrapper/internal/c-sal.h | 1 + .../multipart-chat-message-modifier.cpp | 95 ++++--------------- .../local-conference-event-handler.cpp | 4 +- src/content/content-manager.cpp | 95 ++++++------------- src/content/content-manager.h | 2 +- tester/content-manager-tester.cpp | 14 +-- 7 files changed, 65 insertions(+), 151 deletions(-) diff --git a/coreapi/bellesip_sal/sal_impl.c b/coreapi/bellesip_sal/sal_impl.c index 99c859f52..771155918 100644 --- a/coreapi/bellesip_sal/sal_impl.c +++ b/coreapi/bellesip_sal/sal_impl.c @@ -419,6 +419,11 @@ SalBodyHandler * sal_body_handler_get_part(const SalBodyHandler *body_handler, i return (SalBodyHandler *)belle_sip_list_nth_data(l, idx); } +const belle_sip_list_t * sal_body_handler_get_parts(const SalBodyHandler *body_handler) { + if (!sal_body_handler_is_multipart(body_handler)) return NULL; + return belle_sip_multipart_body_handler_get_parts(BELLE_SIP_MULTIPART_BODY_HANDLER(body_handler)); +} + SalBodyHandler * sal_body_handler_find_part_by_header(const SalBodyHandler *body_handler, const char *header_name, const char *header_value) { const belle_sip_list_t *l = belle_sip_multipart_body_handler_get_parts(BELLE_SIP_MULTIPART_BODY_HANDLER(body_handler)); for (; l != NULL; l = l->next) { diff --git a/src/c-wrapper/internal/c-sal.h b/src/c-wrapper/internal/c-sal.h index dd2e53f8f..ebf373229 100644 --- a/src/c-wrapper/internal/c-sal.h +++ b/src/c-wrapper/internal/c-sal.h @@ -647,6 +647,7 @@ size_t sal_body_handler_get_size(const SalBodyHandler *body_handler); void sal_body_handler_set_size(SalBodyHandler *body_handler, size_t size); bool_t sal_body_handler_is_multipart(const SalBodyHandler *body_handler); SalBodyHandler * sal_body_handler_get_part(const SalBodyHandler *body_handler, int idx); +const belle_sip_list_t * sal_body_handler_get_parts(const SalBodyHandler *body_handler); SalBodyHandler * sal_body_handler_find_part_by_header(const SalBodyHandler *body_handler, const char *header_name, const char *header_value); const char * sal_body_handler_get_header(const SalBodyHandler *body_handler, const char *header_name); diff --git a/src/chat/modifier/multipart-chat-message-modifier.cpp b/src/chat/modifier/multipart-chat-message-modifier.cpp index 1365d03f8..13041bdb4 100644 --- a/src/chat/modifier/multipart-chat-message-modifier.cpp +++ b/src/chat/modifier/multipart-chat-message-modifier.cpp @@ -20,14 +20,10 @@ // TODO: Remove me later. #include "private.h" -#include "address/address.h" #include "chat/chat-message/chat-message.h" -#include "chat/chat-room/chat-room.h" #include "content/content-type.h" -#include "content/header-param.h" +#include "content/content-manager.h" #include "content/file-transfer-content.h" -#include "logger/logger.h" -#include "core/core.h" #include "multipart-chat-message-modifier.h" @@ -44,85 +40,30 @@ ChatMessageModifier::Result MultipartChatMessageModifier::encode ( if (message->getContents().size() <= 1) return ChatMessageModifier::Result::Skipped; - LinphoneCore *lc = message->getChatRoom()->getCore()->getCCore(); - char tmp[64]; - lc->sal->create_uuid(tmp, sizeof(tmp)); - string boundary = tmp; - stringstream multipartMessage; - - multipartMessage << "--" << boundary; - for (Content *content : message->getContents()) { - multipartMessage << "\r\n"; - multipartMessage << "Content-Type: " << content->getContentType().asString() << "\r\n\r\n"; - multipartMessage << content->getBodyAsString() << "\r\n\r\n"; - multipartMessage << "--" << boundary; - } - multipartMessage << "--"; - - Content newContent; - ContentType newContentType(ContentType::Multipart); - newContentType.addParameter("boundary", boundary); - newContent.setContentType(newContentType); - newContent.setBody(multipartMessage.str()); - message->setInternalContent(newContent); + Content content = ContentManager::contentListToMultipart(message->getContents()); + message->setInternalContent(content); return ChatMessageModifier::Result::Done; } ChatMessageModifier::Result MultipartChatMessageModifier::decode (const shared_ptr &message, int &errorCode) { - if (message->getInternalContent().getContentType().getType() == "multipart") { - string boundary = message->getInternalContent().getContentType().getParameter("boundary").getValue(); - if (boundary.empty()) { - lError() << "Boundary parameter of content-type not found: " << message->getInternalContent().getContentType().asString(); - return ChatMessageModifier::Result::Error; - } - - boundary = "--" + boundary; - lInfo() << "Multipart boundary is " << boundary; - - const vector body = message->getInternalContent().getBody(); - string contentsString(body.begin(), body.end()); - - size_t pos = contentsString.find(boundary); - if (pos == string::npos) { - lError() << "Boundary not found in body !"; - return ChatMessageModifier::Result::Error; - } - - size_t start = pos + boundary.length() + 2; // 2 is the size of \r\n - size_t end; - do { - end = contentsString.find(boundary, start); - if (end != string::npos) { - string contentString = contentsString.substr(start, end - start); - - size_t contentTypePos = contentString.find(": ") + 2; // 2 is the size of : - size_t endOfLinePos = contentString.find("\r\n"); - if (contentTypePos >= endOfLinePos) { - lError() << "Content should start by a 'Content-Type: ' line !"; - continue; + if (message->getInternalContent().getContentType().isMultipart()) { + for (Content &c : ContentManager::multipartToContentList(message->getInternalContent())) { + Content *content; + if (c.getContentType() == ContentType::FileTransfer) { + content = new FileTransferContent(); + content->setContentType(c.getContentType()); + content->setContentDisposition(c.getContentDisposition()); + content->setContentEncoding(c.getContentEncoding()); + for (const pair &pair : c.getHeaders()) { + content->addHeader(pair.first, pair.second); } - string contentTypeString = contentString.substr(contentTypePos, endOfLinePos - contentTypePos); - ContentType contentType(contentTypeString); - - endOfLinePos += 4; // 4 is two time the size of \r\n - string contentBody = contentString.substr(endOfLinePos, contentString.length() - (endOfLinePos + 4)); // 4 is two time the size of \r\n - - Content *content; - if (contentType == ContentType::FileTransfer) { - content = new FileTransferContent(); - } else { - content = new Content(); - } - content->setContentType(contentType); - content->setBody(contentBody); - message->addContent(content); - - lInfo() << "Parsed and added content with type " << contentType.asString(); + content->setBodyFromUtf8(c.getBodyAsUtf8String()); + } else { + content = new Content(c); } - start = end + boundary.length() + 2; // 2 is the size of \r\n - } while (end != string::npos); - + message->addContent(content); + } return ChatMessageModifier::Result::Done; } return ChatMessageModifier::Result::Skipped; diff --git a/src/conference/handlers/local-conference-event-handler.cpp b/src/conference/handlers/local-conference-event-handler.cpp index b2e4895f2..8f56e74e5 100644 --- a/src/conference/handlers/local-conference-event-handler.cpp +++ b/src/conference/handlers/local-conference-event-handler.cpp @@ -107,7 +107,7 @@ string LocalConferenceEventHandlerPrivate::createNotifyMultipart (int notifyId) static_cast(notifyId) ); - list contents; + list contents; for (const auto &eventLog : events) { Content content; content.setContentType(ContentType("application","conference-info")); @@ -181,7 +181,7 @@ string LocalConferenceEventHandlerPrivate::createNotifyMultipart (int notifyId) continue; } content.setBody(body); - contents.push_back(content); + contents.push_back(&content); } if (contents.empty()) diff --git a/src/content/content-manager.cpp b/src/content/content-manager.cpp index 8e3908310..cbecfaa83 100644 --- a/src/content/content-manager.cpp +++ b/src/content/content-manager.cpp @@ -19,6 +19,10 @@ #include +#include "c-wrapper/c-wrapper.h" + +#include "linphone/api/c-content.h" + #include "content-manager.h" #include "content-type.h" #include "content/content.h" @@ -36,86 +40,47 @@ namespace { // ----------------------------------------------------------------------------- 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( - body.c_str(), body.length(), MultipartBoundary - ); - belle_sip_object_ref(mpbh); + LinphoneContent *cContent = L_GET_C_BACK_PTR(&content); + SalBodyHandler *sbh = sal_body_handler_ref(sal_body_handler_from_content(cContent)); 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); - Content content; - - 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) { - belle_sip_header_content_type_t * partContentType = BELLE_SIP_HEADER_CONTENT_TYPE(header); - content.setContentType(ContentType( - belle_sip_header_content_type_get_type(partContentType), - belle_sip_header_content_type_get_subtype(partContentType) - )); - } else { - content.addHeader(belle_sip_header_get_name(header), belle_sip_header_get_unparsed_value(header)); - } - } - - content.setBody(static_cast( - belle_sip_memory_body_handler_get_buffer(BELLE_SIP_MEMORY_BODY_HANDLER(part)) - )); - - contents.push_back(move(content)); + for (const belle_sip_list_t *parts = sal_body_handler_get_parts(sbh); parts; parts = parts->next) { + SalBodyHandler *part = (SalBodyHandler *)parts->data; + LinphoneContent *cContent = linphone_content_from_sal_body_handler(part); + Content *cppContent = L_GET_CPP_PTR_FROM_C_OBJECT(cContent); + contents.push_back(*cppContent); + linphone_content_unref(cContent); } - belle_sip_object_unref(mpbh); + sal_body_handler_unref(sbh); + linphone_content_unref(cContent); return contents; } -Content ContentManager::contentListToMultipart (const list &contents) { +Content ContentManager::contentListToMultipart (const list &contents) { belle_sip_multipart_body_handler_t *mpbh = belle_sip_multipart_body_handler_new( nullptr, nullptr, nullptr, MultipartBoundary ); - belle_sip_object_ref(mpbh); + mpbh = (belle_sip_multipart_body_handler_t *)belle_sip_object_ref(mpbh); - for (const auto &content : contents) { - const ContentType &contentType = content.getContentType(); - belle_sip_header_t *cContentType = BELLE_SIP_HEADER( - belle_sip_header_content_type_create( - contentType.getType().c_str(), - string(contentType.getSubType() + "; charset=\"UTF-8\"").c_str() - ) - ); - - 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); - - for (const auto &header : content.getHeaders()) { - belle_sip_header_t *additionalHeader = BELLE_SIP_HEADER( - belle_sip_header_create( - header.first.c_str(), - header.second.c_str() - ) - ); - belle_sip_body_handler_add_header(BELLE_SIP_BODY_HANDLER(mbh), additionalHeader); - } - - belle_sip_multipart_body_handler_add_part(mpbh, BELLE_SIP_BODY_HANDLER(mbh)); + for (Content *content : contents) { + LinphoneContent *cContent = L_GET_C_BACK_PTR(content); + SalBodyHandler *sbh = sal_body_handler_from_content(cContent); + belle_sip_multipart_body_handler_add_part(mpbh, BELLE_SIP_BODY_HANDLER(sbh)); + linphone_content_unref(cContent); } - char *desc = belle_sip_object_to_string(mpbh); - Content content; - content.setBody(desc); - belle_sip_free(desc); + SalBodyHandler *sbh = (SalBodyHandler *)mpbh; + sal_body_handler_set_type(sbh, ContentType::Multipart.getType().c_str()); + sal_body_handler_set_subtype(sbh, ContentType::Multipart.getSubType().c_str()); + sal_body_handler_set_content_type_parameter(sbh, "boundary", MultipartBoundary); + LinphoneContent *cContent = linphone_content_from_sal_body_handler(sbh); + Content *content = L_GET_CPP_PTR_FROM_C_OBJECT(cContent); + Content returnContent = *content; + linphone_content_unref(cContent); belle_sip_object_unref(mpbh); - ContentType contentType = ContentType::Multipart; - contentType.addParameter("boundary", string(MultipartBoundary)); - content.setContentType(contentType); - - return content; + return returnContent; } LINPHONE_END_NAMESPACE diff --git a/src/content/content-manager.h b/src/content/content-manager.h index 8251d3c3b..4d19076f0 100644 --- a/src/content/content-manager.h +++ b/src/content/content-manager.h @@ -32,7 +32,7 @@ class Content; namespace ContentManager { std::list multipartToContentList (const Content &content); - Content contentListToMultipart (const std::list &contents); + Content contentListToMultipart (const std::list &contents); } LINPHONE_END_NAMESPACE diff --git a/tester/content-manager-tester.cpp b/tester/content-manager-tester.cpp index e21cc6399..8c688889b 100644 --- a/tester/content-manager-tester.cpp +++ b/tester/content-manager-tester.cpp @@ -234,26 +234,28 @@ void multipart_to_list () { generatedStr4.erase(std::remove(generatedStr4.begin(), generatedStr4.end(), '\r'), generatedStr4.end()); generatedStr4.erase(std::remove(generatedStr4.begin(), generatedStr4.end(), '\n'), generatedStr4.end()); ms_message("\n\n----- Generated part 4 -----"); - ms_message("%s", generatedStr3.c_str()); + ms_message("%s", generatedStr4.c_str()); ms_message("\n\n----- Original part 4 -----"); ms_message("%s", originalStr4.c_str()); BC_ASSERT_TRUE(originalStr4 == generatedStr4); } void list_to_multipart () { + ContentType contentType = ContentType("application", "rlmi+xml"); + contentType.addParameter("charset", "\"UTF-8\""); Content content1; content1.setBody(part1); - content1.setContentType(ContentType("application", "rlmi+xml")); + content1.setContentType(contentType); Content content2; content2.setBody(part2); - content2.setContentType(ContentType("application", "pidf+xml")); + content2.setContentType(contentType); Content content3; content3.setBody(part3); - content3.setContentType(ContentType("application", "pidf+xml")); + content3.setContentType(contentType); Content content4; content4.setBody(part4); - content4.setContentType(ContentType("application", "pidf+xml")); - list contents = {content1, content2, content3, content4}; + content4.setContentType(contentType); + list contents = {&content1, &content2, &content3, &content4}; Content multipartContent = ContentManager::contentListToMultipart(contents); string originalStr(multipart); From acf44578adcbb7dd91fd1db634598c7065e62649 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Fri, 23 Mar 2018 14:19:42 +0100 Subject: [PATCH 29/44] Fixed Content Manager tester --- tester/content-manager-tester.cpp | 80 +++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 3 deletions(-) diff --git a/tester/content-manager-tester.cpp b/tester/content-manager-tester.cpp index 8c688889b..de371dfb4 100644 --- a/tester/content-manager-tester.cpp +++ b/tester/content-manager-tester.cpp @@ -27,7 +27,7 @@ using namespace LinphonePrivate; using namespace std; -static const char* multipart = \ +static const char* source_multipart = \ "-----------------------------14737809831466499882746641449\r\n" \ "Content-Type: application/rlmi+xml;charset=\"UTF-8\"\r\n\r\n" \ "" \ @@ -95,6 +95,78 @@ static const char* multipart = \ "" \ "-----------------------------14737809831466499882746641449--\r\n"; +static const char* generated_multipart = \ +"-----------------------------14737809831466499882746641449\r\n" \ +"Content-Type: application/rlmi+xml;charset=\"UTF-8\"\r\n\r\n" \ +"Content-Length:582" \ +"" \ +"" \ +" " \ +" " \ +" " \ +" " \ +" " \ +" " \ +" " \ +" " \ +" " \ +"" \ +"-----------------------------14737809831466499882746641449\r\n" \ +"Content-Type: application/pidf+xml;charset=\"UTF-8\"\r\n\r\n" \ +"Content-Length:561" \ +"" \ +"" \ +" " \ +" " \ +" open" \ +" " \ +" sip:+YYYYYYYYYY@sip.linphone.org;user=phone" \ +" 2017-10-25T13:18:26" \ +" " \ +" " \ +" " \ +" " \ +" " \ +" " \ +"" \ +"-----------------------------14737809831466499882746641449\r\n" \ +"Content-Type: application/pidf+xml;charset=\"UTF-8\"\r\n\r\n" \ +"Content-Length:561" \ +"" \ +"" \ +" " \ +" " \ +" open" \ +" " \ +" sip:+XXXXXXXXXX@sip.linphone.org;user=phone" \ +" 2017-10-25T13:18:26" \ +" " \ +" " \ +" " \ +" " \ +" " \ +" " \ +"" \ +"-----------------------------14737809831466499882746641449\r\n" \ +"Content-Type: application/pidf+xml;charset=\"UTF-8\"\r\n\r\n" \ +"Content-Length:546" \ +"" \ +"" \ +" " \ +" " \ +" open" \ +" " \ +" sip:someone@sip.linphone.org" \ +" 2017-10-25T13:18:26" \ +" " \ +" " \ +" " \ +" " \ +" " \ +" " \ +"" \ +"-----------------------------14737809831466499882746641449--\r\n"; + static const char* part1 = \ "" \ "" \ @@ -162,7 +234,7 @@ static const char* part4 = \ void multipart_to_list () { Content multipartContent; - multipartContent.setBody(multipart); + multipartContent.setBody(source_multipart); multipartContent.setContentType(ContentType("multipart", "related")); list contents = ContentManager::multipartToContentList(multipartContent); @@ -246,6 +318,8 @@ void list_to_multipart () { Content content1; content1.setBody(part1); content1.setContentType(contentType); + contentType = ContentType("application", "pidf+xml"); + contentType.addParameter("charset", "\"UTF-8\""); Content content2; content2.setBody(part2); content2.setContentType(contentType); @@ -258,7 +332,7 @@ void list_to_multipart () { list contents = {&content1, &content2, &content3, &content4}; Content multipartContent = ContentManager::contentListToMultipart(contents); - string originalStr(multipart); + string originalStr(generated_multipart); 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(), '\r'), originalStr.end()); From 49b93c6f9a70fc52ce0bba389e63ff74dcd46cd6 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Fri, 23 Mar 2018 15:20:48 +0100 Subject: [PATCH 30/44] Created Header class as parent of ContentType --- src/CMakeLists.txt | 7 +- src/c-wrapper/api/c-content.cpp | 2 +- src/chat/chat-message/chat-message.cpp | 2 +- src/content/content-type.cpp | 66 ++--------------- src/content/content-type.h | 14 +--- src/content/header/header-p.h | 41 +++++++++++ src/content/{ => header}/header-param.cpp | 0 src/content/{ => header}/header-param.h | 0 src/content/header/header.cpp | 88 +++++++++++++++++++++++ src/content/header/header.h | 54 ++++++++++++++ 10 files changed, 199 insertions(+), 75 deletions(-) create mode 100644 src/content/header/header-p.h rename src/content/{ => header}/header-param.cpp (100%) rename src/content/{ => header}/header-param.h (100%) create mode 100644 src/content/header/header.cpp create mode 100644 src/content/header/header.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 66d42b91e..b7c47abf8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -99,7 +99,9 @@ set(LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES content/content.h content/file-content.h content/file-transfer-content.h - content/header-param.h + content/header/header.h + content/header/header-p.h + content/header/header-param.h core/core-accessor.h core/core-listener.h core/core-p.h @@ -224,7 +226,8 @@ set(LINPHONE_CXX_OBJECTS_SOURCE_FILES content/content.cpp content/file-content.cpp content/file-transfer-content.cpp - content/header-param.cpp + content/header/header.cpp + content/header/header-param.cpp core/core-accessor.cpp core/core-call.cpp core/core-chat-room.cpp diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp index 6d6bdde34..2b68cbbde 100644 --- a/src/c-wrapper/api/c-content.cpp +++ b/src/c-wrapper/api/c-content.cpp @@ -24,7 +24,7 @@ #include "content/content.h" #include "content/content-type.h" -#include "content/header-param.h" +#include "content/header/header-param.h" #include "content/content-manager.h" #include "content/file-content.h" #include "content/file-transfer-content.h" diff --git a/src/chat/chat-message/chat-message.cpp b/src/chat/chat-message/chat-message.cpp index ff2a595e7..407a9e734 100644 --- a/src/chat/chat-message/chat-message.cpp +++ b/src/chat/chat-message/chat-message.cpp @@ -36,7 +36,7 @@ #include "chat/modifier/file-transfer-chat-message-modifier.h" #include "chat/modifier/multipart-chat-message-modifier.h" #include "content/file-content.h" -#include "content/header-param.h" +#include "content/header/header-param.h" #include "content/content.h" #include "core/core.h" #include "core/core-p.h" diff --git a/src/content/content-type.cpp b/src/content/content-type.cpp index 4af2f2195..be3b7da00 100644 --- a/src/content/content-type.cpp +++ b/src/content/content-type.cpp @@ -18,11 +18,10 @@ */ #include "linphone/utils/utils.h" -#include "linphone/utils/algorithm.h" #include "content-type.h" -#include "header-param.h" -#include "object/clonable-object-p.h" +#include "header/header-p.h" +#include "header/header-param.h" // ============================================================================= @@ -32,11 +31,10 @@ LINPHONE_BEGIN_NAMESPACE // ----------------------------------------------------------------------------- -class ContentTypePrivate : public ClonableObjectPrivate { +class ContentTypePrivate : public HeaderPrivate { public: string type; string subType; - std::list parameters; }; // ----------------------------------------------------------------------------- @@ -54,7 +52,7 @@ const ContentType ContentType::Sdp("application/sdp"); // ----------------------------------------------------------------------------- -ContentType::ContentType (const string &contentType) : ClonableObject(*new ContentTypePrivate) { +ContentType::ContentType (const string &contentType) : Header(*new ContentTypePrivate) { L_D(); size_t pos = contentType.find('/'); @@ -86,7 +84,7 @@ ContentType::ContentType (const string &contentType) : ClonableObject(*new Conte } } -ContentType::ContentType (const string &type, const string &subType) : ClonableObject(*new ContentTypePrivate) { +ContentType::ContentType (const string &type, const string &subType) : Header(*new ContentTypePrivate) { L_D(); if (setType(type) && !setSubType(subType)) @@ -97,7 +95,7 @@ ContentType::ContentType ( const string &type, const string &subType, const HeaderParam ¶meter -) : ClonableObject(*new ContentTypePrivate) { +) : Header(*new ContentTypePrivate) { L_D(); if (setType(type) && !setSubType(subType)) @@ -109,7 +107,7 @@ ContentType::ContentType ( const string &type, const string &subType, const std::list ¶meters -) : ClonableObject(*new ContentTypePrivate) { +) : Header(*new ContentTypePrivate) { L_D(); if (setType(type) && !setSubType(subType)) @@ -167,56 +165,6 @@ bool ContentType::setSubType (const string &subType) { return false; } -const std::list &ContentType::getParameters () const { - L_D(); - - return d->parameters; -} - -void ContentType::addParameter (const std::string ¶mName, const std::string ¶mValue) { - addParameter(HeaderParam(paramName, paramValue)); -} - -void ContentType::addParameter (const HeaderParam ¶m) { - L_D(); - removeParameter(param); - d->parameters.push_back(param); -} - -void ContentType::addParameters(const std::list ¶ms) { - for (auto it = std::begin(params); it!=std::end(params); ++it) { - HeaderParam param = *it; - addParameter(param.getName(), param.getValue()); - } -} - -void ContentType::removeParameter (const std::string ¶mName) { - L_D(); - auto it = findParameter(paramName); - if (it != d->parameters.cend()) - d->parameters.remove(*it); -} - -void ContentType::removeParameter (const HeaderParam ¶m) { - removeParameter(param.getName()); -} - -std::list::const_iterator ContentType::findParameter (const std::string ¶mName) const { - L_D(); - return findIf(d->parameters, [¶mName](const HeaderParam ¶m) { - return param.getName() == paramName; - }); -} - -const HeaderParam &ContentType::getParameter (const std::string ¶mName) const { - L_D(); - std::list::const_iterator it = findParameter(paramName); - if (it != d->parameters.cend()) { - return *it; - } - return Utils::getEmptyConstRefObject(); -} - bool ContentType::isEmpty () const { L_D(); return d->type.empty() && d->subType.empty(); diff --git a/src/content/content-type.h b/src/content/content-type.h index 3b155e4cd..2ed267065 100644 --- a/src/content/content-type.h +++ b/src/content/content-type.h @@ -20,9 +20,8 @@ #ifndef _L_CONTENT_TYPE_H_ #define _L_CONTENT_TYPE_H_ -#include - #include "object/clonable-object.h" +#include "header/header.h" // ============================================================================= @@ -31,7 +30,7 @@ LINPHONE_BEGIN_NAMESPACE class ContentTypePrivate; class HeaderParam; -class LINPHONE_PUBLIC ContentType : public ClonableObject { +class LINPHONE_PUBLIC ContentType : public Header { public: explicit ContentType (const std::string &contentType = ""); ContentType (const std::string &type, const std::string &subType); @@ -59,15 +58,6 @@ public: const std::string &getSubType () const; bool setSubType (const std::string &subType); - const std::list &getParameters () const; - void addParameter (const std::string ¶mName, const std::string ¶mValue); - void addParameter (const HeaderParam ¶m); - void addParameters(const std::list ¶ms); - void removeParameter (const std::string ¶mName); - void removeParameter (const HeaderParam ¶m); - std::list::const_iterator findParameter (const std::string ¶mName) const; - const HeaderParam &getParameter (const std::string ¶mName) const; - std::string asString () const; bool isMultipart() const; diff --git a/src/content/header/header-p.h b/src/content/header/header-p.h new file mode 100644 index 000000000..3af1e3688 --- /dev/null +++ b/src/content/header/header-p.h @@ -0,0 +1,41 @@ +/* + * header-p.h + * Copyright (C) 2010-2018 Belledonne Communications SARL + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef _L_HEADER_P_H_ +#define _L_HEADER_P_H_ + +#include + +#include "object/clonable-object-p.h" + +#include "header.h" + +// ============================================================================= + +LINPHONE_BEGIN_NAMESPACE + +class HeaderPrivate : public ClonableObjectPrivate { +private: + std::list parameters; + L_DECLARE_PUBLIC(Header); +}; + +LINPHONE_END_NAMESPACE + +#endif // ifndef _L_HEADER_P_H_ \ No newline at end of file diff --git a/src/content/header-param.cpp b/src/content/header/header-param.cpp similarity index 100% rename from src/content/header-param.cpp rename to src/content/header/header-param.cpp diff --git a/src/content/header-param.h b/src/content/header/header-param.h similarity index 100% rename from src/content/header-param.h rename to src/content/header/header-param.h diff --git a/src/content/header/header.cpp b/src/content/header/header.cpp new file mode 100644 index 000000000..a621a2e8b --- /dev/null +++ b/src/content/header/header.cpp @@ -0,0 +1,88 @@ +/* + * header.cpp + * Copyright (C) 2010-2018 Belledonne Communications SARL + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "linphone/utils/utils.h" +#include "linphone/utils/algorithm.h" + +#include "header-p.h" +#include "header-param.h" + +// ============================================================================= + +using namespace std; + +LINPHONE_BEGIN_NAMESPACE + +// ----------------------------------------------------------------------------- + +Header::Header(HeaderPrivate &p) : ClonableObject(p) { + +} + +const std::list &Header::getParameters () const { + L_D(); + + return d->parameters; +} + +void Header::addParameter (const std::string ¶mName, const std::string ¶mValue) { + addParameter(HeaderParam(paramName, paramValue)); +} + +void Header::addParameter (const HeaderParam ¶m) { + L_D(); + removeParameter(param); + d->parameters.push_back(param); +} + +void Header::addParameters(const std::list ¶ms) { + for (auto it = std::begin(params); it!=std::end(params); ++it) { + HeaderParam param = *it; + addParameter(param.getName(), param.getValue()); + } +} + +void Header::removeParameter (const std::string ¶mName) { + L_D(); + auto it = findParameter(paramName); + if (it != d->parameters.cend()) + d->parameters.remove(*it); +} + +void Header::removeParameter (const HeaderParam ¶m) { + removeParameter(param.getName()); +} + +std::list::const_iterator Header::findParameter (const std::string ¶mName) const { + L_D(); + return findIf(d->parameters, [¶mName](const HeaderParam ¶m) { + return param.getName() == paramName; + }); +} + +const HeaderParam &Header::getParameter (const std::string ¶mName) const { + L_D(); + std::list::const_iterator it = findParameter(paramName); + if (it != d->parameters.cend()) { + return *it; + } + return Utils::getEmptyConstRefObject(); +} + +LINPHONE_END_NAMESPACE \ No newline at end of file diff --git a/src/content/header/header.h b/src/content/header/header.h new file mode 100644 index 000000000..9fb3dab47 --- /dev/null +++ b/src/content/header/header.h @@ -0,0 +1,54 @@ +/* + * header.h + * Copyright (C) 2010-2018 Belledonne Communications SARL + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef _L_HEADER_H_ +#define _L_HEADER_H_ + +#include + +#include "object/clonable-object.h" + +// ============================================================================= + +LINPHONE_BEGIN_NAMESPACE + +class HeaderPrivate; +class HeaderParam; + +class LINPHONE_PUBLIC Header : public ClonableObject { +public: + const std::list &getParameters () const; + void addParameter (const std::string ¶mName, const std::string ¶mValue); + void addParameter (const HeaderParam ¶m); + void addParameters(const std::list ¶ms); + void removeParameter (const std::string ¶mName); + void removeParameter (const HeaderParam ¶m); + std::list::const_iterator findParameter (const std::string ¶mName) const; + const HeaderParam &getParameter (const std::string ¶mName) const; + +protected: + explicit Header (HeaderPrivate &p); + +private: + L_DECLARE_PRIVATE(Header); +}; + +LINPHONE_END_NAMESPACE + +#endif // ifndef _L_HEADER_H_ From c0a7c027a6c54fd79b73ae0fda288f643fc38aef Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Fri, 23 Mar 2018 17:56:09 +0100 Subject: [PATCH 31/44] Improved multipart tester --- tester/multipart-tester.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tester/multipart-tester.cpp b/tester/multipart-tester.cpp index b16e306bd..d3b9f21b5 100644 --- a/tester/multipart-tester.cpp +++ b/tester/multipart-tester.cpp @@ -58,7 +58,7 @@ static void chat_message_multipart_modifier_base(bool first_file_transfer, bool } else { Content *content = new Content(); content->setContentType(ContentType::PlainText); - content->setBody("Hello Part 1"); + content->setBody("Hello part 1"); marieMessage->addContent(content); } @@ -73,7 +73,7 @@ static void chat_message_multipart_modifier_base(bool first_file_transfer, bool } else { Content *content = new Content(); content->setContentType(ContentType::PlainText); - content->setBody("Hello Part 2"); + content->setBody("Hello part 2"); marieMessage->addContent(content); } @@ -83,6 +83,9 @@ static void chat_message_multipart_modifier_base(bool first_file_transfer, bool BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneMessageReceived,1)); BC_ASSERT_PTR_NOT_NULL(pauline->stat.last_received_chat_message); BC_ASSERT_STRING_EQUAL(linphone_chat_message_get_content_type(pauline->stat.last_received_chat_message), "multipart/mixed"); + if (!first_file_transfer) { + BC_ASSERT_STRING_EQUAL(linphone_chat_message_get_text(pauline->stat.last_received_chat_message), "Hello part 1"); + } linphone_core_manager_destroy(marie); linphone_core_manager_destroy(pauline); From 8f2be0252a03c9fc8361d3221e94f6c5325163dd Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 26 Mar 2018 10:57:08 +0200 Subject: [PATCH 32/44] Fixed 2 issues related to content type parameters : one in = operator, one in the Sal --- src/content/content-type.cpp | 1 + src/content/header/header.cpp | 5 +++++ src/content/header/header.h | 1 + src/sal/op.cpp | 8 +++++++- tester/multipart-tester.cpp | 16 +++++++++++++--- 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/content/content-type.cpp b/src/content/content-type.cpp index be3b7da00..e34241a40 100644 --- a/src/content/content-type.cpp +++ b/src/content/content-type.cpp @@ -121,6 +121,7 @@ ContentType &ContentType::operator= (const ContentType &other) { if (this != &other) { setType(other.getType()); setSubType(other.getSubType()); + cleanParameters(); addParameters(other.getParameters()); } diff --git a/src/content/header/header.cpp b/src/content/header/header.cpp index a621a2e8b..0c8017e60 100644 --- a/src/content/header/header.cpp +++ b/src/content/header/header.cpp @@ -35,6 +35,11 @@ Header::Header(HeaderPrivate &p) : ClonableObject(p) { } +void Header::cleanParameters() { + L_D(); + d->parameters.clear(); +} + const std::list &Header::getParameters () const { L_D(); diff --git a/src/content/header/header.h b/src/content/header/header.h index 9fb3dab47..822839516 100644 --- a/src/content/header/header.h +++ b/src/content/header/header.h @@ -33,6 +33,7 @@ class HeaderParam; class LINPHONE_PUBLIC Header : public ClonableObject { public: + void cleanParameters(); const std::list &getParameters () const; void addParameter (const std::string ¶mName, const std::string ¶mValue); void addParameter (const HeaderParam ¶m); diff --git a/src/sal/op.cpp b/src/sal/op.cpp index e5e211f3b..497a7a99b 100644 --- a/src/sal/op.cpp +++ b/src/sal/op.cpp @@ -1029,12 +1029,18 @@ void SalOp::process_incoming_message(const belle_sip_request_event_t *event) { /* if we just deciphered a message, use the deciphered part(which can be a rcs xml body pointing to the file to retreive from server)*/ salmsg.text=(!external_body)?belle_sip_message_get_body(BELLE_SIP_MESSAGE(req)):NULL; salmsg.url=NULL; - salmsg.content_type = ms_strdup_printf("%s/%s", belle_sip_header_content_type_get_type(content_type), belle_sip_header_content_type_get_subtype(content_type)); + + char buffer[1024]; + size_t offset = 0; + belle_sip_parameters_marshal(BELLE_SIP_PARAMETERS(content_type), buffer, 1024, &offset); + buffer[offset] = '\0'; + salmsg.content_type = ms_strdup_printf("%s/%s%s", belle_sip_header_content_type_get_type(content_type), belle_sip_header_content_type_get_subtype(content_type), buffer); if (external_body && belle_sip_parameters_get_parameter(BELLE_SIP_PARAMETERS(content_type),"URL")) { size_t url_length=strlen(belle_sip_parameters_get_parameter(BELLE_SIP_PARAMETERS(content_type),"URL")); salmsg.url = ms_strdup(belle_sip_parameters_get_parameter(BELLE_SIP_PARAMETERS(content_type),"URL")+1); /* skip first "*/ ((char*)salmsg.url)[url_length-2]='\0'; /*remove trailing "*/ } + salmsg.message_id=message_id; salmsg.time=date ? belle_sip_header_date_get_time(date) : time(NULL); this->root->callbacks.message_received(this,&salmsg); diff --git a/tester/multipart-tester.cpp b/tester/multipart-tester.cpp index d3b9f21b5..c94dc9a2e 100644 --- a/tester/multipart-tester.cpp +++ b/tester/multipart-tester.cpp @@ -82,9 +82,19 @@ static void chat_message_multipart_modifier_base(bool first_file_transfer, bool BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneMessageReceived,1)); BC_ASSERT_PTR_NOT_NULL(pauline->stat.last_received_chat_message); - BC_ASSERT_STRING_EQUAL(linphone_chat_message_get_content_type(pauline->stat.last_received_chat_message), "multipart/mixed"); - if (!first_file_transfer) { - BC_ASSERT_STRING_EQUAL(linphone_chat_message_get_text(pauline->stat.last_received_chat_message), "Hello part 1"); + + if (first_file_transfer || second_file_transfer) { + LinphoneContent *content = linphone_chat_message_get_file_transfer_information(pauline->stat.last_received_chat_message); + BC_ASSERT_PTR_NOT_NULL(content); + linphone_content_unref(content); + } + if (!first_file_transfer || !second_file_transfer) { + const char *content = linphone_chat_message_get_text_content(pauline->stat.last_received_chat_message); + BC_ASSERT_PTR_NOT_NULL(content); + if (!first_file_transfer) + BC_ASSERT_STRING_EQUAL(content, "Hello part 1"); + else if (!second_file_transfer) + BC_ASSERT_STRING_EQUAL(content, "Hello part 2"); } linphone_core_manager_destroy(marie); From 3a6fe5193901e0abe538fb4a49beff17bb2a0b33 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 26 Mar 2018 15:58:51 +0200 Subject: [PATCH 33/44] Fixed content type parsing + added test --- src/content/content-type.cpp | 2 +- tester/content-manager-tester.cpp | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/content/content-type.cpp b/src/content/content-type.cpp index e34241a40..f8e5be8ca 100644 --- a/src/content/content-type.cpp +++ b/src/content/content-type.cpp @@ -79,7 +79,7 @@ ContentType::ContentType (const string &contentType) : Header(*new ContentTypePr token = params.substr(0, posParam); } addParameter(HeaderParam(token)); - params.erase(0, pos + 1); + params.erase(0, posParam + 1); } while (posParam != std::string::npos); } } diff --git a/tester/content-manager-tester.cpp b/tester/content-manager-tester.cpp index de371dfb4..9509b5442 100644 --- a/tester/content-manager-tester.cpp +++ b/tester/content-manager-tester.cpp @@ -352,9 +352,18 @@ void list_to_multipart () { BC_ASSERT_TRUE(originalStr == generatedStr); } +static void content_type_parsing(void) { + const string type = "message/external-body;access-type=URL;URL=\"https://www.linphone.org/img/linphone-open-source-voip-projectX2.png\""; + ContentType contentType = ContentType(type); + BC_ASSERT_STRING_EQUAL("message", contentType.getType().c_str()); + BC_ASSERT_STRING_EQUAL("external-body", contentType.getSubType().c_str()); + BC_ASSERT_TRUE(type == contentType.asString()); +} + test_t content_manager_tests[] = { TEST_NO_TAG("Multipart to list", multipart_to_list), - TEST_NO_TAG("List to multipart", list_to_multipart) + TEST_NO_TAG("List to multipart", list_to_multipart), + TEST_NO_TAG("Content type parsing", content_type_parsing) }; test_suite_t content_manager_test_suite = { From 75b80bbe98c1f18801738bd756715bbac084bf23 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 26 Mar 2018 16:05:09 +0200 Subject: [PATCH 34/44] Do not compare parameters when comparing ContentType --- src/content/content-type.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/content/content-type.cpp b/src/content/content-type.cpp index f8e5be8ca..4f357f3ee 100644 --- a/src/content/content-type.cpp +++ b/src/content/content-type.cpp @@ -130,8 +130,7 @@ ContentType &ContentType::operator= (const ContentType &other) { bool ContentType::operator== (const ContentType &other) const { return getType() == other.getType() && - getSubType() == other.getSubType() && - getParameters() == other.getParameters(); + getSubType() == other.getSubType(); } bool ContentType::operator!= (const ContentType &other) const { From 22218cb188c0dd6a9801ae0e11e31f07412a9924 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 26 Mar 2018 16:07:26 +0200 Subject: [PATCH 35/44] Improved log + fixed external body url format --- coreapi/friendlist.c | 2 +- src/chat/chat-message/chat-message.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/coreapi/friendlist.c b/coreapi/friendlist.c index 249bae980..6f196721e 100644 --- a/coreapi/friendlist.c +++ b/coreapi/friendlist.c @@ -962,7 +962,7 @@ void linphone_friend_list_notify_presence_received(LinphoneFriendList *list, Lin const char *subtype = linphone_content_get_subtype(body); if ((strcmp(type, "multipart") != 0) || (strcmp(subtype, "related") != 0)) { - ms_warning("multipart presence notified but it is not 'multipart/related'"); + ms_warning("multipart presence notified but it is not 'multipart/related', instead is '%s/%s'", type, subtype); return; } diff --git a/src/chat/chat-message/chat-message.cpp b/src/chat/chat-message/chat-message.cpp index 407a9e734..26232bbeb 100644 --- a/src/chat/chat-message/chat-message.cpp +++ b/src/chat/chat-message/chat-message.cpp @@ -741,7 +741,7 @@ void ChatMessagePrivate::send () { auto msgOp = dynamic_cast(op); if (!externalBodyUrl.empty()) { - char *content_type = ms_strdup_printf("message/external-body; access-type=URL; URL=\"%s\"", externalBodyUrl.c_str()); + char *content_type = ms_strdup_printf("message/external-body;access-type=URL;URL=\"%s\"", externalBodyUrl.c_str()); msgOp->send_message(content_type, NULL); ms_free(content_type); } else if (internalContent.getContentType().isValid()) { From 7e5ee05263388560205d0a0d88a0f737a2e03099 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 26 Mar 2018 17:19:04 +0200 Subject: [PATCH 36/44] Improved b64 tests --- tester/message_tester.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tester/message_tester.c b/tester/message_tester.c index 396f4b9c4..8922adf20 100644 --- a/tester/message_tester.c +++ b/tester/message_tester.c @@ -2294,15 +2294,19 @@ void text_message_with_custom_content_type_and_lime(void) { static int im_encryption_engine_process_incoming_message_cb(LinphoneImEncryptionEngine *engine, LinphoneChatRoom *room, LinphoneChatMessage *msg) { + ms_debug("IM encryption process incoming message with content type %s", linphone_chat_message_get_content_type(msg)); if (linphone_chat_message_get_content_type(msg)) { if (strcmp(linphone_chat_message_get_content_type(msg), "cipher/b64") == 0) { size_t b64Size = 0; unsigned char *output; - bctbx_base64_decode(NULL, &b64Size, (unsigned char *)linphone_chat_message_get_text(msg), strlen(linphone_chat_message_get_text(msg))); + const char *data = linphone_chat_message_get_text(msg); + ms_debug("IM encryption process incoming message crypted message is %s", data); + bctbx_base64_decode(NULL, &b64Size, (unsigned char *)data, strlen(data)); output = (unsigned char *)ms_malloc(b64Size+1), - bctbx_base64_decode(output, &b64Size, (unsigned char *)linphone_chat_message_get_text(msg), strlen(linphone_chat_message_get_text(msg))); + bctbx_base64_decode(output, &b64Size, (unsigned char *)data, strlen(data)); output[b64Size] = '\0'; linphone_chat_message_set_text(msg, (char *)output); + ms_debug("IM encryption process incoming message decrypted message is %s", output); ms_free(output); linphone_chat_message_set_content_type(msg, "text/plain"); return 0; From 2081f71c78aeef2a3b6605b79669b06c5e8c6a7e Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 27 Mar 2018 10:10:22 +0200 Subject: [PATCH 37/44] Added more tests for ContentType parsing --- tester/content-manager-tester.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tester/content-manager-tester.cpp b/tester/content-manager-tester.cpp index 9509b5442..296231086 100644 --- a/tester/content-manager-tester.cpp +++ b/tester/content-manager-tester.cpp @@ -21,6 +21,7 @@ #include "content/content-manager.h" #include "content/content-type.h" #include "content/content.h" +#include "content/header/header-param.h" #include "liblinphone_tester.h" #include "tester_utils.h" @@ -353,10 +354,31 @@ void list_to_multipart () { } static void content_type_parsing(void) { - const string type = "message/external-body;access-type=URL;URL=\"https://www.linphone.org/img/linphone-open-source-voip-projectX2.png\""; + string type = "message/external-body;access-type=URL;URL=\"https://www.linphone.org/img/linphone-open-source-voip-projectX2.png\""; ContentType contentType = ContentType(type); BC_ASSERT_STRING_EQUAL("message", contentType.getType().c_str()); BC_ASSERT_STRING_EQUAL("external-body", contentType.getSubType().c_str()); + BC_ASSERT_STRING_EQUAL("URL", contentType.getParameter("access-type").getValue().c_str()); + BC_ASSERT_STRING_EQUAL("\"https://www.linphone.org/img/linphone-open-source-voip-projectX2.png\"", contentType.getParameter("URL").getValue().c_str()); + BC_ASSERT_STRING_EQUAL("", contentType.getParameter("boundary").getValue().c_str()); + BC_ASSERT_EQUAL(2, contentType.getParameters().size(), int, "%d"); + BC_ASSERT_TRUE(type == contentType.asString()); + + type = "multipart/mixed;boundary=-----------------------------14737809831466499882746641450"; + contentType = ContentType(type); + BC_ASSERT_STRING_EQUAL("multipart", contentType.getType().c_str()); + BC_ASSERT_STRING_EQUAL("mixed", contentType.getSubType().c_str()); + BC_ASSERT_STRING_EQUAL("-----------------------------14737809831466499882746641450", contentType.getParameter("boundary").getValue().c_str()); + BC_ASSERT_STRING_EQUAL("", contentType.getParameter("access-type").getValue().c_str()); + BC_ASSERT_EQUAL(1, contentType.getParameters().size(), int, "%d"); + BC_ASSERT_TRUE(type == contentType.asString()); + + type = "plain/text"; + contentType = ContentType(type); + BC_ASSERT_STRING_EQUAL("plain", contentType.getType().c_str()); + BC_ASSERT_STRING_EQUAL("text", contentType.getSubType().c_str()); + BC_ASSERT_STRING_EQUAL("", contentType.getParameter("boundary").getValue().c_str()); + BC_ASSERT_EQUAL(0, contentType.getParameters().size(), int, "%d"); BC_ASSERT_TRUE(type == contentType.asString()); } From 813ab614ebdcdadf79af64a9dbde9c12cc3644da Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 27 Mar 2018 11:48:40 +0200 Subject: [PATCH 38/44] Use Header class instead of pair in Content + improved tester --- coreapi/bellesip_sal/sal_impl.c | 4 ++ src/c-wrapper/api/c-content.cpp | 15 +++-- src/c-wrapper/internal/c-sal.h | 1 + .../multipart-chat-message-modifier.cpp | 5 +- src/content/content-manager.cpp | 8 +-- src/content/content-p.h | 4 +- src/content/content-type.cpp | 3 +- src/content/content.cpp | 18 ++++-- src/content/content.h | 6 +- src/content/header/header-p.h | 2 + src/content/header/header.cpp | 62 ++++++++++++++++++- src/content/header/header.h | 19 +++++- tester/content-manager-tester.cpp | 26 +++++--- 13 files changed, 140 insertions(+), 33 deletions(-) diff --git a/coreapi/bellesip_sal/sal_impl.c b/coreapi/bellesip_sal/sal_impl.c index 771155918..bdf3683dd 100644 --- a/coreapi/bellesip_sal/sal_impl.c +++ b/coreapi/bellesip_sal/sal_impl.c @@ -446,3 +446,7 @@ const char * sal_body_handler_get_header(const SalBodyHandler *body_handler, con } return NULL; } + +const belle_sip_list_t* sal_body_handler_get_headers(const SalBodyHandler *body_handler) { + return belle_sip_body_handler_get_headers(BELLE_SIP_BODY_HANDLER(body_handler)); +} diff --git a/src/c-wrapper/api/c-content.cpp b/src/c-wrapper/api/c-content.cpp index 2b68cbbde..580c78ca3 100644 --- a/src/c-wrapper/api/c-content.cpp +++ b/src/c-wrapper/api/c-content.cpp @@ -25,6 +25,7 @@ #include "content/content.h" #include "content/content-type.h" #include "content/header/header-param.h" +#include "content/header/header.h" #include "content/content-manager.h" #include "content/file-content.h" #include "content/file-transfer-content.h" @@ -259,6 +260,13 @@ static LinphoneContent * linphone_content_new_with_body_handler(SalBodyHandler * belle_sip_free(body); } + belle_sip_list_t *headers = (belle_sip_list_t *)sal_body_handler_get_headers(body_handler); + while (headers) { + belle_sip_header_t *cHeader = BELLE_SIP_HEADER(headers->data); + LinphonePrivate::Header header = LinphonePrivate::Header(belle_sip_header_get_name(cHeader), belle_sip_header_get_unparsed_value(cHeader)); + L_GET_CPP_PTR_FROM_C_OBJECT(content)->addHeader(header); + headers = headers->next; + } if (sal_body_handler_get_encoding(body_handler)) linphone_content_set_encoding(content, sal_body_handler_get_encoding(body_handler)); } @@ -307,12 +315,7 @@ SalBodyHandler * sal_body_handler_from_content(const LinphoneContent *content) { } for (const auto &header : L_GET_CPP_PTR_FROM_C_OBJECT(content)->getHeaders()) { - belle_sip_header_t *additionalHeader = BELLE_SIP_HEADER( - belle_sip_header_create( - header.first.c_str(), - header.second.c_str() - ) - ); + belle_sip_header_t *additionalHeader = belle_sip_header_parse(header.asString().c_str()); belle_sip_body_handler_add_header(BELLE_SIP_BODY_HANDLER(body_handler), additionalHeader); } diff --git a/src/c-wrapper/internal/c-sal.h b/src/c-wrapper/internal/c-sal.h index ebf373229..f34c4ee5f 100644 --- a/src/c-wrapper/internal/c-sal.h +++ b/src/c-wrapper/internal/c-sal.h @@ -650,6 +650,7 @@ SalBodyHandler * sal_body_handler_get_part(const SalBodyHandler *body_handler, i const belle_sip_list_t * sal_body_handler_get_parts(const SalBodyHandler *body_handler); SalBodyHandler * sal_body_handler_find_part_by_header(const SalBodyHandler *body_handler, const char *header_name, const char *header_value); const char * sal_body_handler_get_header(const SalBodyHandler *body_handler, const char *header_name); +const belle_sip_list_t* sal_body_handler_get_headers(const SalBodyHandler *body_handler); /*this function parses a document with key=value pairs separated by new lines, and extracts the value for a given key*/ int sal_lines_get_value(const char *data, const char *key, char *value, size_t value_size); diff --git a/src/chat/modifier/multipart-chat-message-modifier.cpp b/src/chat/modifier/multipart-chat-message-modifier.cpp index 13041bdb4..8ed8312d9 100644 --- a/src/chat/modifier/multipart-chat-message-modifier.cpp +++ b/src/chat/modifier/multipart-chat-message-modifier.cpp @@ -22,6 +22,7 @@ #include "chat/chat-message/chat-message.h" #include "content/content-type.h" +#include "content/header/header.h" #include "content/content-manager.h" #include "content/file-transfer-content.h" @@ -55,8 +56,8 @@ ChatMessageModifier::Result MultipartChatMessageModifier::decode (const shared_p content->setContentType(c.getContentType()); content->setContentDisposition(c.getContentDisposition()); content->setContentEncoding(c.getContentEncoding()); - for (const pair &pair : c.getHeaders()) { - content->addHeader(pair.first, pair.second); + for (const Header &header : c.getHeaders()) { + content->addHeader(header); } content->setBodyFromUtf8(c.getBodyAsUtf8String()); } else { diff --git a/src/content/content-manager.cpp b/src/content/content-manager.cpp index cbecfaa83..ff341bcf2 100644 --- a/src/content/content-manager.cpp +++ b/src/content/content-manager.cpp @@ -29,14 +29,14 @@ // ============================================================================= -using namespace std; - -LINPHONE_BEGIN_NAMESPACE - namespace { constexpr const char MultipartBoundary[] = "---------------------------14737809831466499882746641449"; } +using namespace std; + +LINPHONE_BEGIN_NAMESPACE + // ----------------------------------------------------------------------------- list ContentManager::multipartToContentList (const Content &content) { diff --git a/src/content/content-p.h b/src/content/content-p.h index 167712313..9dc4e1d31 100644 --- a/src/content/content-p.h +++ b/src/content/content-p.h @@ -29,13 +29,15 @@ LINPHONE_BEGIN_NAMESPACE +class Header; + class ContentPrivate : public ClonableObjectPrivate { private: std::vector body; ContentType contentType; ContentDisposition contentDisposition; std::string contentEncoding; - std::list> headers; + std::list
headers; L_DECLARE_PUBLIC(Content); }; diff --git a/src/content/content-type.cpp b/src/content/content-type.cpp index 4f357f3ee..35391783b 100644 --- a/src/content/content-type.cpp +++ b/src/content/content-type.cpp @@ -179,8 +179,7 @@ string ContentType::asString () const { L_D(); if (isValid()) { string asString = d->type + "/" + d->subType; - for (auto it = std::begin(getParameters()); it!=std::end(getParameters()); ++it) { - HeaderParam param = *it; + for (const auto ¶m : getParameters()) { asString += param.asString(); } return asString; diff --git a/src/content/content.cpp b/src/content/content.cpp index f27e731d6..91761e268 100644 --- a/src/content/content.cpp +++ b/src/content/content.cpp @@ -25,6 +25,7 @@ #include "content-p.h" #include "content-type.h" +#include "header/header.h" // ============================================================================= @@ -197,10 +198,17 @@ bool Content::isFileTransfer () const { void Content::addHeader (const string &headerName, const string &headerValue) { L_D(); removeHeader(headerName); - d->headers.push_back(make_pair(headerName, headerValue)); + Header header = Header(headerName, headerValue); + d->headers.push_back(header); } -const list> &Content::getHeaders () const { +void Content::addHeader (const Header &header) { + L_D(); + removeHeader(header.getName()); + d->headers.push_back(header); +} + +const list
&Content::getHeaders () const { L_D(); return d->headers; } @@ -212,10 +220,10 @@ void Content::removeHeader (const string &headerName) { d->headers.remove(*it); } -list>::const_iterator Content::findHeader (const string &headerName) const { +list
::const_iterator Content::findHeader (const string &headerName) const { L_D(); - return findIf(d->headers, [&headerName](const pair &pair) { - return pair.first == headerName; + return findIf(d->headers, [&headerName](const Header &header) { + return header.getName() == headerName; }); } diff --git a/src/content/content.h b/src/content/content.h index 94d832643..50066133f 100644 --- a/src/content/content.h +++ b/src/content/content.h @@ -35,6 +35,7 @@ LINPHONE_BEGIN_NAMESPACE class ContentDisposition; class ContentType; class ContentPrivate; +class Header; class LINPHONE_PUBLIC Content : public ClonableObject, public AppDataContainer { public: @@ -76,10 +77,11 @@ public: virtual bool isFile () const; virtual bool isFileTransfer () const; - const std::list> &getHeaders () const; + const std::list
&getHeaders () const; void addHeader (const std::string &headerName, const std::string &headerValue); + void addHeader (const Header &header); void removeHeader (const std::string &headerName); - std::list>::const_iterator findHeader (const std::string &headerName) const; + std::list
::const_iterator findHeader (const std::string &headerName) const; protected: explicit Content (ContentPrivate &p); diff --git a/src/content/header/header-p.h b/src/content/header/header-p.h index 3af1e3688..fd4663efe 100644 --- a/src/content/header/header-p.h +++ b/src/content/header/header-p.h @@ -32,6 +32,8 @@ LINPHONE_BEGIN_NAMESPACE class HeaderPrivate : public ClonableObjectPrivate { private: + std::string name; + std::string value; std::list parameters; L_DECLARE_PUBLIC(Header); }; diff --git a/src/content/header/header.cpp b/src/content/header/header.cpp index 0c8017e60..561b7ce05 100644 --- a/src/content/header/header.cpp +++ b/src/content/header/header.cpp @@ -35,14 +35,64 @@ Header::Header(HeaderPrivate &p) : ClonableObject(p) { } -void Header::cleanParameters() { +Header::Header (const string &name, const string &value) : ClonableObject(*new HeaderPrivate) { + setName(name); + setValue(value); +} + +Header::Header (const string &name, const string &value, const list ¶ms) : Header(name, value) { + addParameters(params); +} + +Header::Header (const Header &other) : Header(other.getName(), other.getValue(), other.getParameters()) {} + +Header &Header::operator= (const Header &other) { + if (this != &other) { + setName(other.getName()); + setValue(other.getValue()); + cleanParameters(); + addParameters(other.getParameters()); + } + + return *this; +} + +bool Header::operator== (const Header &other) const { + return getName() == other.getName() && + getValue() == other.getValue(); +} + +bool Header::operator!= (const Header &other) const { + return !(*this == other); +} + +void Header::setName (const string &name) { + L_D(); + d->name = name; +} + +string Header::getName () const { + L_D(); + return d->name; +} + +void Header::setValue (const string &value) { + L_D(); + d->value = value; +} + +string Header::getValue () const { + L_D(); + return d->value; +} + +void Header::cleanParameters () { L_D(); d->parameters.clear(); } const std::list &Header::getParameters () const { L_D(); - return d->parameters; } @@ -90,4 +140,12 @@ const HeaderParam &Header::getParameter (const std::string ¶mName) const { return Utils::getEmptyConstRefObject(); } +string Header::asString () const { + string asString = getName() + ":" + getValue(); + for (const auto ¶m : getParameters()) { + asString += param.asString(); + } + return asString; +} + LINPHONE_END_NAMESPACE \ No newline at end of file diff --git a/src/content/header/header.h b/src/content/header/header.h index 822839516..a0b8083fe 100644 --- a/src/content/header/header.h +++ b/src/content/header/header.h @@ -33,7 +33,22 @@ class HeaderParam; class LINPHONE_PUBLIC Header : public ClonableObject { public: - void cleanParameters(); + Header (const std::string &name, const std::string &value); + Header (const std::string &name, const std::string &value, const std::list ¶ms); + Header (const Header &other); + + Header &operator= (const Header &other); + + bool operator== (const Header &other) const; + bool operator!= (const Header &other) const; + + void setName (const std::string &name); + std::string getName () const; + + void setValue (const std::string &value); + std::string getValue () const; + + void cleanParameters (); const std::list &getParameters () const; void addParameter (const std::string ¶mName, const std::string ¶mValue); void addParameter (const HeaderParam ¶m); @@ -43,6 +58,8 @@ public: std::list::const_iterator findParameter (const std::string ¶mName) const; const HeaderParam &getParameter (const std::string ¶mName) const; + std::string asString () const; + protected: explicit Header (HeaderPrivate &p); diff --git a/tester/content-manager-tester.cpp b/tester/content-manager-tester.cpp index 296231086..24c4817d5 100644 --- a/tester/content-manager-tester.cpp +++ b/tester/content-manager-tester.cpp @@ -61,6 +61,7 @@ static const char* source_multipart = \ " " \ "" \ "-----------------------------14737809831466499882746641449\r\n" \ +"Content-Encoding: b64\r\n" \ "Content-Type: application/pidf+xml;charset=\"UTF-8\"\r\n\r\n" \ "" \ "" \ @@ -78,6 +79,7 @@ static const char* source_multipart = \ " " \ "" \ "-----------------------------14737809831466499882746641449\r\n" \ +"Content-Id: toto;param1=value1;param2;param3=value3\r\n" \ "Content-Type: application/pidf+xml;charset=\"UTF-8\"\r\n\r\n" \ "" \ "" \ @@ -98,8 +100,8 @@ static const char* source_multipart = \ static const char* generated_multipart = \ "-----------------------------14737809831466499882746641449\r\n" \ -"Content-Type: application/rlmi+xml;charset=\"UTF-8\"\r\n\r\n" \ -"Content-Length:582" \ +"Content-Type: application/rlmi+xml;charset=\"UTF-8\"\r\n" \ +"Content-Length:582\r\n\r\n" \ "" \ "" \ " " \ @@ -113,8 +115,8 @@ static const char* generated_multipart = \ " " \ "" \ "-----------------------------14737809831466499882746641449\r\n" \ -"Content-Type: application/pidf+xml;charset=\"UTF-8\"\r\n\r\n" \ -"Content-Length:561" \ +"Content-Type: application/pidf+xml;charset=\"UTF-8\"\r\n" \ +"Content-Length:561\r\n\r\n" \ "" \ "" \ " " \ @@ -131,8 +133,9 @@ static const char* generated_multipart = \ " " \ "" \ "-----------------------------14737809831466499882746641449\r\n" \ -"Content-Type: application/pidf+xml;charset=\"UTF-8\"\r\n\r\n" \ -"Content-Length:561" \ +"Content-Encoding:b64\r\n" \ +"Content-Type: application/pidf+xml;charset=\"UTF-8\"\r\n" \ +"Content-Length:561\r\n\r\n" \ "" \ "" \ " " \ @@ -149,8 +152,9 @@ static const char* generated_multipart = \ " " \ "" \ "-----------------------------14737809831466499882746641449\r\n" \ -"Content-Type: application/pidf+xml;charset=\"UTF-8\"\r\n\r\n" \ -"Content-Length:546" \ +"Content-Id:toto;param1=value1;param2;param3=value3\r\n" \ +"Content-Type: application/pidf+xml;charset=\"UTF-8\"\r\n" \ +"Content-Length:546\r\n\r\n" \ "" \ "" \ " " \ @@ -326,8 +330,14 @@ void list_to_multipart () { content2.setContentType(contentType); Content content3; content3.setBody(part3); + content3.addHeader("Content-Encoding", "b64"); content3.setContentType(contentType); Content content4; + Header header = Header("Content-Id", "toto"); + header.addParameter("param1", "value1"); + header.addParameter("param2", ""); + header.addParameter("param3", "value3"); + content4.addHeader(header); content4.setBody(part4); content4.setContentType(contentType); list contents = {&content1, &content2, &content3, &content4}; From 646cff8460b50d146b0f407841e9f4f233c4d98e Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 27 Mar 2018 12:16:42 +0200 Subject: [PATCH 39/44] Improved presence tester, now more reliable --- tester/presence_server_tester.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tester/presence_server_tester.c b/tester/presence_server_tester.c index 62bb8a364..4aa3b4ee8 100644 --- a/tester/presence_server_tester.c +++ b/tester/presence_server_tester.c @@ -430,8 +430,10 @@ static void test_presence_list_base(bool_t enable_compression) { lcs = bctbx_list_append(lcs, pauline->lc); wait_for_list(lcs, &laure->stat.number_of_NotifyPresenceReceived, 2, 4000); - BC_ASSERT_EQUAL(laure->stat.number_of_NotifyPresenceReceived, 2, int, "%d"); - BC_ASSERT_EQUAL(linphone_friend_list_get_expected_notification_version(linphone_core_get_default_friend_list(laure->lc)), 1, int, "%d"); + BC_ASSERT_GREATER(laure->stat.number_of_NotifyPresenceReceived, 2, int, "%d"); + BC_ASSERT_LOWER(laure->stat.number_of_NotifyPresenceReceived, 3, int, "%d"); + BC_ASSERT_GREATER(linphone_friend_list_get_expected_notification_version(linphone_core_get_default_friend_list(laure->lc)), 1, int, "%d"); + BC_ASSERT_LOWER(linphone_friend_list_get_expected_notification_version(linphone_core_get_default_friend_list(laure->lc)), 2, int, "%d"); lf = linphone_friend_list_find_friend_by_address(linphone_core_get_default_friend_list(laure->lc), get_identity_address(marie)); if (!BC_ASSERT_PTR_NOT_NULL(lf)) goto end; BC_ASSERT_EQUAL(linphone_friend_get_status(lf), LinphoneStatusBusy, int, "%d"); @@ -485,7 +487,8 @@ static void test_presence_list_base(bool_t enable_compression) { /* The number of PresenceReceived events can be 3 or 4 here. TODO: ideally it should always be 3. */ BC_ASSERT_GREATER(laure->stat.number_of_NotifyPresenceReceived, 3, int, "%d"); BC_ASSERT_LOWER(laure->stat.number_of_NotifyPresenceReceived, 4, int, "%d"); - BC_ASSERT_EQUAL(linphone_friend_list_get_expected_notification_version(linphone_core_get_default_friend_list(laure->lc)), 2, int, "%d"); + BC_ASSERT_GREATER(linphone_friend_list_get_expected_notification_version(linphone_core_get_default_friend_list(laure->lc)), 2, int, "%d"); + BC_ASSERT_LOWER(linphone_friend_list_get_expected_notification_version(linphone_core_get_default_friend_list(laure->lc)), 3, int, "%d"); lf = linphone_friend_list_find_friend_by_address(linphone_core_get_default_friend_list(laure->lc), get_identity_address(marie)); BC_ASSERT_EQUAL(linphone_friend_get_status(lf), LinphoneStatusOnThePhone, int, "%d"); From 7009b68b9a149fe5484ddc36992dbedc9be8ad62 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 27 Mar 2018 12:45:47 +0200 Subject: [PATCH 40/44] Added << operator for ContentType and Header --- src/content/content-type.cpp | 13 +++++++++++++ src/content/content-type.h | 1 + src/content/header/header.cpp | 5 +++++ src/content/header/header.h | 1 + tester/content-manager-tester.cpp | 4 ++++ 5 files changed, 24 insertions(+) diff --git a/src/content/content-type.cpp b/src/content/content-type.cpp index 35391783b..e5d58c7a7 100644 --- a/src/content/content-type.cpp +++ b/src/content/content-type.cpp @@ -55,6 +55,7 @@ const ContentType ContentType::Sdp("application/sdp"); ContentType::ContentType (const string &contentType) : Header(*new ContentTypePrivate) { L_D(); + setName("Content-Type"); size_t pos = contentType.find('/'); size_t posParam = contentType.find(";"); size_t end = contentType.length(); @@ -82,11 +83,13 @@ ContentType::ContentType (const string &contentType) : Header(*new ContentTypePr params.erase(0, posParam + 1); } while (posParam != std::string::npos); } + setValue(d->type + "/" + d->subType); } ContentType::ContentType (const string &type, const string &subType) : Header(*new ContentTypePrivate) { L_D(); + setName("Content-Type"); if (setType(type) && !setSubType(subType)) d->type.clear(); } @@ -98,6 +101,7 @@ ContentType::ContentType ( ) : Header(*new ContentTypePrivate) { L_D(); + setName("Content-Type"); if (setType(type) && !setSubType(subType)) d->type.clear(); addParameter(parameter); @@ -110,6 +114,7 @@ ContentType::ContentType ( ) : Header(*new ContentTypePrivate) { L_D(); + setName("Content-Type"); if (setType(type) && !setSubType(subType)) d->type.clear(); addParameters(parameters); @@ -119,6 +124,7 @@ ContentType::ContentType (const ContentType &other) : ContentType(other.getType( ContentType &ContentType::operator= (const ContentType &other) { if (this != &other) { + setName("Content-Type"); setType(other.getType()); setSubType(other.getSubType()); cleanParameters(); @@ -146,6 +152,7 @@ bool ContentType::setType (const string &type) { L_D(); if (type.find('/') == string::npos) { d->type = Utils::stringToLower(type); + setValue(d->type + "/" + d->subType); return true; } return false; @@ -160,6 +167,7 @@ bool ContentType::setSubType (const string &subType) { L_D(); if (subType.find('/') == string::npos) { d->subType = Utils::stringToLower(subType); + setValue(d->type + "/" + d->subType); return true; } return false; @@ -187,6 +195,11 @@ string ContentType::asString () const { return ""; } +ostream &operator<<(ostream& stream, const ContentType& contentType) { + stream << contentType.asString(); + return stream; +} + bool ContentType::isMultipart() const { return getType() == "multipart"; } diff --git a/src/content/content-type.h b/src/content/content-type.h index 2ed267065..10f895e4d 100644 --- a/src/content/content-type.h +++ b/src/content/content-type.h @@ -59,6 +59,7 @@ public: bool setSubType (const std::string &subType); std::string asString () const; + friend std::ostream &operator<<(std::ostream&, const ContentType&); bool isMultipart() const; diff --git a/src/content/header/header.cpp b/src/content/header/header.cpp index 561b7ce05..14ce99f85 100644 --- a/src/content/header/header.cpp +++ b/src/content/header/header.cpp @@ -148,4 +148,9 @@ string Header::asString () const { return asString; } +ostream &operator<<(ostream& stream, const Header& header) { + stream << header.asString(); + return stream; +} + LINPHONE_END_NAMESPACE \ No newline at end of file diff --git a/src/content/header/header.h b/src/content/header/header.h index a0b8083fe..5d111900d 100644 --- a/src/content/header/header.h +++ b/src/content/header/header.h @@ -59,6 +59,7 @@ public: const HeaderParam &getParameter (const std::string ¶mName) const; std::string asString () const; + friend std::ostream &operator<<(std::ostream&, const Header&); protected: explicit Header (HeaderPrivate &p); diff --git a/tester/content-manager-tester.cpp b/tester/content-manager-tester.cpp index 24c4817d5..52d461ef9 100644 --- a/tester/content-manager-tester.cpp +++ b/tester/content-manager-tester.cpp @@ -24,6 +24,7 @@ #include "content/header/header-param.h" #include "liblinphone_tester.h" #include "tester_utils.h" +#include "logger/logger.h" using namespace LinphonePrivate; using namespace std; @@ -372,6 +373,7 @@ static void content_type_parsing(void) { BC_ASSERT_STRING_EQUAL("\"https://www.linphone.org/img/linphone-open-source-voip-projectX2.png\"", contentType.getParameter("URL").getValue().c_str()); BC_ASSERT_STRING_EQUAL("", contentType.getParameter("boundary").getValue().c_str()); BC_ASSERT_EQUAL(2, contentType.getParameters().size(), int, "%d"); + lInfo() << "Content-Type is " << contentType; BC_ASSERT_TRUE(type == contentType.asString()); type = "multipart/mixed;boundary=-----------------------------14737809831466499882746641450"; @@ -381,6 +383,7 @@ static void content_type_parsing(void) { BC_ASSERT_STRING_EQUAL("-----------------------------14737809831466499882746641450", contentType.getParameter("boundary").getValue().c_str()); BC_ASSERT_STRING_EQUAL("", contentType.getParameter("access-type").getValue().c_str()); BC_ASSERT_EQUAL(1, contentType.getParameters().size(), int, "%d"); + lInfo() << "Content-Type is " << contentType; BC_ASSERT_TRUE(type == contentType.asString()); type = "plain/text"; @@ -389,6 +392,7 @@ static void content_type_parsing(void) { BC_ASSERT_STRING_EQUAL("text", contentType.getSubType().c_str()); BC_ASSERT_STRING_EQUAL("", contentType.getParameter("boundary").getValue().c_str()); BC_ASSERT_EQUAL(0, contentType.getParameters().size(), int, "%d"); + lInfo() << "Content-Type is " << contentType; BC_ASSERT_TRUE(type == contentType.asString()); } From af2a607014a341da246174f5c914a697880e8d47 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 27 Mar 2018 14:39:32 +0200 Subject: [PATCH 41/44] Improved ContentType's asString() method --- src/chat/chat-message/chat-message.cpp | 2 +- .../modifier/cpim-chat-message-modifier.cpp | 2 +- src/content/content-type.cpp | 22 ------------------- src/content/content-type.h | 3 --- src/content/header/header.cpp | 12 +++++++--- 5 files changed, 11 insertions(+), 30 deletions(-) diff --git a/src/chat/chat-message/chat-message.cpp b/src/chat/chat-message/chat-message.cpp index 26232bbeb..ddfd018a6 100644 --- a/src/chat/chat-message/chat-message.cpp +++ b/src/chat/chat-message/chat-message.cpp @@ -582,7 +582,7 @@ LinphoneReason ChatMessagePrivate::receive () { foundSupportContentType = true; break; } else - lError() << "Unsupported content-type: " << c->getContentType().asString(); + lError() << "Unsupported content-type: " << c->getContentType(); } if (!foundSupportContentType) { diff --git a/src/chat/modifier/cpim-chat-message-modifier.cpp b/src/chat/modifier/cpim-chat-message-modifier.cpp index 8958c0f82..03c35fed0 100644 --- a/src/chat/modifier/cpim-chat-message-modifier.cpp +++ b/src/chat/modifier/cpim-chat-message-modifier.cpp @@ -98,7 +98,7 @@ ChatMessageModifier::Result CpimChatMessageModifier::decode (const shared_ptrgetContents().front(); if (content->getContentType() != ContentType::Cpim) { - lError() << "[CPIM] Message is not CPIM but " << content->getContentType().asString(); + lError() << "[CPIM] Message is not CPIM but " << content->getContentType(); return ChatMessageModifier::Result::Skipped; } diff --git a/src/content/content-type.cpp b/src/content/content-type.cpp index e5d58c7a7..3d6b4689f 100644 --- a/src/content/content-type.cpp +++ b/src/content/content-type.cpp @@ -55,7 +55,6 @@ const ContentType ContentType::Sdp("application/sdp"); ContentType::ContentType (const string &contentType) : Header(*new ContentTypePrivate) { L_D(); - setName("Content-Type"); size_t pos = contentType.find('/'); size_t posParam = contentType.find(";"); size_t end = contentType.length(); @@ -89,7 +88,6 @@ ContentType::ContentType (const string &contentType) : Header(*new ContentTypePr ContentType::ContentType (const string &type, const string &subType) : Header(*new ContentTypePrivate) { L_D(); - setName("Content-Type"); if (setType(type) && !setSubType(subType)) d->type.clear(); } @@ -101,7 +99,6 @@ ContentType::ContentType ( ) : Header(*new ContentTypePrivate) { L_D(); - setName("Content-Type"); if (setType(type) && !setSubType(subType)) d->type.clear(); addParameter(parameter); @@ -114,7 +111,6 @@ ContentType::ContentType ( ) : Header(*new ContentTypePrivate) { L_D(); - setName("Content-Type"); if (setType(type) && !setSubType(subType)) d->type.clear(); addParameters(parameters); @@ -124,7 +120,6 @@ ContentType::ContentType (const ContentType &other) : ContentType(other.getType( ContentType &ContentType::operator= (const ContentType &other) { if (this != &other) { - setName("Content-Type"); setType(other.getType()); setSubType(other.getSubType()); cleanParameters(); @@ -183,23 +178,6 @@ bool ContentType::isValid () const { return !d->type.empty() && !d->subType.empty(); } -string ContentType::asString () const { - L_D(); - if (isValid()) { - string asString = d->type + "/" + d->subType; - for (const auto ¶m : getParameters()) { - asString += param.asString(); - } - return asString; - } - return ""; -} - -ostream &operator<<(ostream& stream, const ContentType& contentType) { - stream << contentType.asString(); - return stream; -} - bool ContentType::isMultipart() const { return getType() == "multipart"; } diff --git a/src/content/content-type.h b/src/content/content-type.h index 10f895e4d..c9aa7af77 100644 --- a/src/content/content-type.h +++ b/src/content/content-type.h @@ -58,9 +58,6 @@ public: const std::string &getSubType () const; bool setSubType (const std::string &subType); - std::string asString () const; - friend std::ostream &operator<<(std::ostream&, const ContentType&); - bool isMultipart() const; static bool isFile (const ContentType &contentType); diff --git a/src/content/header/header.cpp b/src/content/header/header.cpp index 14ce99f85..92920e024 100644 --- a/src/content/header/header.cpp +++ b/src/content/header/header.cpp @@ -17,6 +17,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include + #include "linphone/utils/utils.h" #include "linphone/utils/algorithm.h" @@ -141,11 +143,15 @@ const HeaderParam &Header::getParameter (const std::string ¶mName) const { } string Header::asString () const { - string asString = getName() + ":" + getValue(); + stringstream asString; + if (!getName().empty()) { + asString << getName() << ":"; + } + asString << getValue(); for (const auto ¶m : getParameters()) { - asString += param.asString(); + asString << param.asString(); } - return asString; + return asString.str(); } ostream &operator<<(ostream& stream, const Header& header) { From ee88045383626abd452ba66dec64b3d6bd3e447e Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 27 Mar 2018 16:10:17 +0200 Subject: [PATCH 42/44] Renamed content_manager_tester + improved header parsing + new tests --- src/content/content-type.cpp | 1 - src/content/content.cpp | 9 ++++ src/content/content.h | 1 + src/content/header/header.cpp | 41 ++++++++++++++----- src/content/header/header.h | 1 + tester/CMakeLists.txt | 2 +- ...manager-tester.cpp => contents-tester.cpp} | 33 ++++++++++++--- tester/liblinphone_tester.h | 2 +- tester/tester.c | 2 +- 9 files changed, 73 insertions(+), 19 deletions(-) rename tester/{content-manager-tester.cpp => contents-tester.cpp} (92%) diff --git a/src/content/content-type.cpp b/src/content/content-type.cpp index 3d6b4689f..7aee05247 100644 --- a/src/content/content-type.cpp +++ b/src/content/content-type.cpp @@ -82,7 +82,6 @@ ContentType::ContentType (const string &contentType) : Header(*new ContentTypePr params.erase(0, posParam + 1); } while (posParam != std::string::npos); } - setValue(d->type + "/" + d->subType); } ContentType::ContentType (const string &type, const string &subType) : Header(*new ContentTypePrivate) { diff --git a/src/content/content.cpp b/src/content/content.cpp index 91761e268..ad74de27f 100644 --- a/src/content/content.cpp +++ b/src/content/content.cpp @@ -213,6 +213,15 @@ const list
&Content::getHeaders () const { return d->headers; } +const Header &Content::getHeader (const string &headerName) const { + L_D(); + list
::const_iterator it = findHeader(headerName); + if (it != d->headers.cend()) { + return *it; + } + return Utils::getEmptyConstRefObject
(); +} + void Content::removeHeader (const string &headerName) { L_D(); auto it = findHeader(headerName); diff --git a/src/content/content.h b/src/content/content.h index 50066133f..3c29d3a1a 100644 --- a/src/content/content.h +++ b/src/content/content.h @@ -78,6 +78,7 @@ public: virtual bool isFileTransfer () const; const std::list
&getHeaders () const; + const Header &getHeader (const std::string &headerName) const; void addHeader (const std::string &headerName, const std::string &headerValue); void addHeader (const Header &header); void removeHeader (const std::string &headerName); diff --git a/src/content/header/header.cpp b/src/content/header/header.cpp index 92920e024..5924cdf5b 100644 --- a/src/content/header/header.cpp +++ b/src/content/header/header.cpp @@ -33,13 +33,34 @@ LINPHONE_BEGIN_NAMESPACE // ----------------------------------------------------------------------------- -Header::Header(HeaderPrivate &p) : ClonableObject(p) { +Header::Header(HeaderPrivate &p) : ClonableObject(p) {} -} +Header::Header() : ClonableObject(*new HeaderPrivate) {} Header::Header (const string &name, const string &value) : ClonableObject(*new HeaderPrivate) { setName(name); - setValue(value); + + size_t posParam = value.find(";"); + if (posParam == string::npos) { + setValue(value); + return; + } + + string parsedValue = value.substr(0, posParam); + string params = value.substr(posParam + 1); + string token; + do { + posParam = params.find(";"); + if (posParam == string::npos) { + token = params; + } else { + token = params.substr(0, posParam); + } + addParameter(HeaderParam(token)); + params.erase(0, posParam + 1); + } while (posParam != std::string::npos); + + setValue(parsedValue); } Header::Header (const string &name, const string &value, const list ¶ms) : Header(name, value) { @@ -93,12 +114,12 @@ void Header::cleanParameters () { d->parameters.clear(); } -const std::list &Header::getParameters () const { +const list &Header::getParameters () const { L_D(); return d->parameters; } -void Header::addParameter (const std::string ¶mName, const std::string ¶mValue) { +void Header::addParameter (const string ¶mName, const string ¶mValue) { addParameter(HeaderParam(paramName, paramValue)); } @@ -108,14 +129,14 @@ void Header::addParameter (const HeaderParam ¶m) { d->parameters.push_back(param); } -void Header::addParameters(const std::list ¶ms) { +void Header::addParameters(const list ¶ms) { for (auto it = std::begin(params); it!=std::end(params); ++it) { HeaderParam param = *it; addParameter(param.getName(), param.getValue()); } } -void Header::removeParameter (const std::string ¶mName) { +void Header::removeParameter (const string ¶mName) { L_D(); auto it = findParameter(paramName); if (it != d->parameters.cend()) @@ -126,16 +147,16 @@ void Header::removeParameter (const HeaderParam ¶m) { removeParameter(param.getName()); } -std::list::const_iterator Header::findParameter (const std::string ¶mName) const { +list::const_iterator Header::findParameter (const string ¶mName) const { L_D(); return findIf(d->parameters, [¶mName](const HeaderParam ¶m) { return param.getName() == paramName; }); } -const HeaderParam &Header::getParameter (const std::string ¶mName) const { +const HeaderParam &Header::getParameter (const string ¶mName) const { L_D(); - std::list::const_iterator it = findParameter(paramName); + list::const_iterator it = findParameter(paramName); if (it != d->parameters.cend()) { return *it; } diff --git a/src/content/header/header.h b/src/content/header/header.h index 5d111900d..583572523 100644 --- a/src/content/header/header.h +++ b/src/content/header/header.h @@ -33,6 +33,7 @@ class HeaderParam; class LINPHONE_PUBLIC Header : public ClonableObject { public: + Header (); Header (const std::string &name, const std::string &value); Header (const std::string &name, const std::string &value, const std::list ¶ms); Header (const Header &other); diff --git a/tester/CMakeLists.txt b/tester/CMakeLists.txt index b14e28572..e07217934 100644 --- a/tester/CMakeLists.txt +++ b/tester/CMakeLists.txt @@ -199,7 +199,7 @@ set(SOURCE_FILES_C set(SOURCE_FILES_CXX clonable-object-tester.cpp conference-event-tester.cpp - content-manager-tester.cpp + contents-tester.cpp cpim-tester.cpp main-db-tester.cpp multipart-tester.cpp diff --git a/tester/content-manager-tester.cpp b/tester/contents-tester.cpp similarity index 92% rename from tester/content-manager-tester.cpp rename to tester/contents-tester.cpp index 52d461ef9..99ecee08d 100644 --- a/tester/content-manager-tester.cpp +++ b/tester/contents-tester.cpp @@ -298,6 +298,7 @@ void multipart_to_list () { ms_message("\n\n----- Original part 3 -----"); ms_message("%s", originalStr3.c_str()); BC_ASSERT_TRUE(originalStr3 == generatedStr3); + BC_ASSERT_TRUE(content3.getHeader("Content-Encoding").getValue() == "b64"); Content content4 = contents.front(); contents.pop_front(); @@ -316,6 +317,10 @@ void multipart_to_list () { ms_message("\n\n----- Original part 4 -----"); ms_message("%s", originalStr4.c_str()); BC_ASSERT_TRUE(originalStr4 == generatedStr4); + BC_ASSERT_TRUE(content4.getHeader("Content-Id").getValue() == "toto"); + BC_ASSERT_TRUE(content4.getHeader("Content-Id").getParameter("param1").getValue() == "value1"); + BC_ASSERT_TRUE(content4.getHeader("Content-Id").getParameter("param2").getValue().empty()); + BC_ASSERT_TRUE(content4.getHeader("Content-Id").getParameter("param3").getValue() == "value3"); } void list_to_multipart () { @@ -396,17 +401,35 @@ static void content_type_parsing(void) { BC_ASSERT_TRUE(type == contentType.asString()); } -test_t content_manager_tests[] = { +static void content_header_parsing(void) { + string value = "toto;param1=value1;param2;param3=value3"; + Header header = Header("Content-Id", value); + BC_ASSERT_TRUE(header.getValue() == "toto"); + BC_ASSERT_TRUE(header.getParameter("param1").getValue() == "value1"); + BC_ASSERT_TRUE(header.getParameter("param2").getValue().empty()); + BC_ASSERT_TRUE(header.getParameter("param3").getValue() == "value3"); + BC_ASSERT_EQUAL(3, header.getParameters().size(), int, "%d"); + BC_ASSERT_STRING_EQUAL("", header.getParameter("encoding").getValue().c_str()); + + value = "b64"; + header = Header("Content-Encoding", value); + BC_ASSERT_TRUE(header.getValue() == value); + BC_ASSERT_EQUAL(0, header.getParameters().size(), int, "%d"); + BC_ASSERT_STRING_EQUAL("", header.getParameter("access-type").getValue().c_str()); +} + +test_t contents_tests[] = { TEST_NO_TAG("Multipart to list", multipart_to_list), TEST_NO_TAG("List to multipart", list_to_multipart), - TEST_NO_TAG("Content type parsing", content_type_parsing) + TEST_NO_TAG("Content type parsing", content_type_parsing), + TEST_NO_TAG("Content header parsing", content_header_parsing) }; -test_suite_t content_manager_test_suite = { - "Content manager", +test_suite_t contents_test_suite = { + "Contents", nullptr, nullptr, liblinphone_tester_before_each, liblinphone_tester_after_each, - sizeof(content_manager_tests) / sizeof(content_manager_tests[0]), content_manager_tests + sizeof(contents_tests) / sizeof(contents_tests[0]), contents_tests }; diff --git a/tester/liblinphone_tester.h b/tester/liblinphone_tester.h index 9824c2084..c96be9ed4 100644 --- a/tester/liblinphone_tester.h +++ b/tester/liblinphone_tester.h @@ -45,7 +45,7 @@ extern test_suite_t call_video_test_suite; extern test_suite_t clonable_object_test_suite; extern test_suite_t conference_event_test_suite; extern test_suite_t conference_test_suite; -extern test_suite_t content_manager_test_suite; +extern test_suite_t contents_test_suite; extern test_suite_t cpim_test_suite; extern test_suite_t dtmf_test_suite; extern test_suite_t event_test_suite; diff --git a/tester/tester.c b/tester/tester.c index c47cff87f..95e3a9fe5 100644 --- a/tester/tester.c +++ b/tester/tester.c @@ -604,7 +604,7 @@ void liblinphone_tester_add_suites() { bc_tester_add_suite(&stun_test_suite); bc_tester_add_suite(&event_test_suite); bc_tester_add_suite(&conference_event_test_suite); - bc_tester_add_suite(&content_manager_test_suite); + bc_tester_add_suite(&contents_test_suite); bc_tester_add_suite(&flexisip_test_suite); bc_tester_add_suite(&remote_provisioning_test_suite); bc_tester_add_suite(&quality_reporting_test_suite); From f6f3557d88aafa74190d2a3c2b5d0a225c2440fe Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 4 Apr 2018 15:42:31 +0200 Subject: [PATCH 43/44] Fixed an issue in CPIM tester --- tester/cpim-tester.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tester/cpim-tester.cpp b/tester/cpim-tester.cpp index 74eb70524..66cc661d8 100644 --- a/tester/cpim-tester.cpp +++ b/tester/cpim-tester.cpp @@ -428,7 +428,7 @@ static void cpim_chat_message_modifier_base(bool_t use_multipart) { marieMessage->send(); BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneMessageReceived,1)); - BC_ASSERT_STRING_EQUAL(marieMessage->getInternalContent().getContentType().asString().c_str(), ""); // Internal content is cleaned after message is sent or received + BC_ASSERT_TRUE(marieMessage->getInternalContent().getContentType().isEmpty()); // Internal content is cleaned after message is sent or received BC_ASSERT_PTR_NOT_NULL(pauline->stat.last_received_chat_message); if (pauline->stat.last_received_chat_message != NULL) { From 797d7e3f64e88c2562c2134dde0df85aa5339ae8 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 4 Apr 2018 15:51:47 +0200 Subject: [PATCH 44/44] Fixed another CPIM issue --- src/chat/cpim/message/cpim-message.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/chat/cpim/message/cpim-message.cpp b/src/chat/cpim/message/cpim-message.cpp index 84753f4ef..123ef6c66 100644 --- a/src/chat/cpim/message/cpim-message.cpp +++ b/src/chat/cpim/message/cpim-message.cpp @@ -149,9 +149,11 @@ string Cpim::Message::asString () const { string output; // TODO: Remove cpimHeaders - for (const auto &cpimHeader : *d->cpimHeaders) - output += cpimHeader->asString(); - output += "\r\n"; + if (d->cpimHeaders->size() > 0) { + for (const auto &cpimHeader : *d->cpimHeaders) + output += cpimHeader->asString(); + output += "\r\n"; + } // TODO Remove cpimHeaders if (d->messageHeaders->size() > 0) {