content-manager.cpp 4.21 KB
Newer Older
1 2
/*
 * content-manager.cpp
3
 * Copyright (C) 2010-2018 Belledonne Communications SARL
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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.
 */

20
#include <belle-sip/belle-sip.h>
21

22 23
#include "content-manager.h"
#include "content-type.h"
24
#include "content/content.h"
25

26 27
#define MULTIPART_BOUNDARY "---------------------------14737809831466499882746641449"

28 29
// =============================================================================

30 31 32 33
using namespace std;

LINPHONE_BEGIN_NAMESPACE

Ronan's avatar
Ronan committed
34 35
// -----------------------------------------------------------------------------

36 37 38 39 40 41
list<Content> ContentManager::multipartToContentList (const Content &content) {
	belle_sip_multipart_body_handler_t *mpbh = belle_sip_multipart_body_handler_new_from_buffer(
		(void *)content.getBodyAsString().c_str(),
		content.getBodyAsString().length(),
		MULTIPART_BOUNDARY
	);
42 43
	belle_sip_object_ref(mpbh);

44 45
	list<Content> contents;
	for (const belle_sip_list_t *parts = belle_sip_multipart_body_handler_get_parts(mpbh); parts; parts = parts->next) {
46 47
		belle_sip_body_handler_t *part = BELLE_SIP_BODY_HANDLER(parts->data);
		const belle_sip_list_t *part_headers = belle_sip_body_handler_get_headers(part);
48 49
		belle_sip_header_content_type_t *part_content_type = nullptr;
		for (belle_sip_list_t *it = (belle_sip_list_t *)part_headers; it; it = it->next) {
50
			belle_sip_header_t *header = BELLE_SIP_HEADER(it->data);
51 52
			if (strcasecmp("Content-Type", belle_sip_header_get_name(header)) == 0) {
				part_content_type = BELLE_SIP_HEADER_CONTENT_TYPE(header);
53 54 55
				break;
			}
		}
56 57

		Content retContent;
58
		retContent.setBody((const char *)belle_sip_memory_body_handler_get_buffer(BELLE_SIP_MEMORY_BODY_HANDLER(part)));
59 60 61 62 63
		retContent.setContentType(ContentType(
			belle_sip_header_content_type_get_type(part_content_type),
			belle_sip_header_content_type_get_subtype(part_content_type)
		));
		contents.push_back(retContent);
64 65 66
	}

	belle_sip_object_unref(mpbh);
67
	return contents;
68 69
}

70 71 72 73 74
Content ContentManager::contentListToMultipart (const list<Content> &contents) {
	belle_sip_memory_body_handler_t *mbh = nullptr;
	belle_sip_multipart_body_handler_t *mpbh = belle_sip_multipart_body_handler_new(
		nullptr, nullptr, nullptr, MULTIPART_BOUNDARY
	);
75
	belle_sip_object_ref(mpbh);
76

77 78 79
	for (const auto &content : contents) {
		const ContentType &contentType = content.getContentType();
		stringstream subtype;
80
		subtype << contentType.getSubType() << "; charset=\"UTF-8\"";
81
		belle_sip_header_t *cContentType = BELLE_SIP_HEADER(
82 83 84 85 86
			belle_sip_header_content_type_create(
				contentType.getType().c_str(),
				subtype.str().c_str()
			)
		);
87 88

		string body = content.getBodyAsString();
89
		mbh = belle_sip_memory_body_handler_new_copy_from_buffer(
90
			(void *)body.c_str(), body.length(), nullptr, nullptr
91
		);
92
		belle_sip_body_handler_add_header(BELLE_SIP_BODY_HANDLER(mbh), cContentType);
93 94 95 96 97 98 99 100 101 102 103 104

		const list<pair<string,string>> headers = content.getHeaders();
		for (const auto &header : headers) {
			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);
		}

105 106
		belle_sip_multipart_body_handler_add_part(mpbh, BELLE_SIP_BODY_HANDLER(mbh));
	}
107 108 109 110
	char *desc = belle_sip_object_to_string(mpbh);

	Content content;
	content.setBody(desc);
111
	ContentType contentType = ContentType::Multipart;
Sylvain Berfini's avatar
Sylvain Berfini committed
112 113
	string boundary = "boundary=" + string(MULTIPART_BOUNDARY);
	contentType.setParameter(boundary);
114
	content.setContentType(contentType);
115 116

	belle_sip_free(desc);
117 118
	belle_sip_object_unref(mpbh);

119
	return content;
120 121 122
}

LINPHONE_END_NAMESPACE