diff --git a/coreapi/content.c b/coreapi/content.c index a5a66f1b9..cc0760b0a 100644 --- a/coreapi/content.c +++ b/coreapi/content.c @@ -196,29 +196,27 @@ void ** linphone_content_get_cryptoContext_address(LinphoneContent *content) { } bool_t linphone_content_is_multipart(const LinphoneContent *content) { - // TODO - return FALSE; + return sal_body_handler_is_multipart(content->body_handler); } -LinphoneContent * linphone_content_get_part(const LinphoneContent *content) { - // TODO - return NULL; +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); } -void linphone_content_add_part(LinphoneContent *content, LinphoneContent *part) { - // TODO +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) { - // TODO - return NULL; + return sal_body_handler_get_header(content->body_handler, header_name); } -void linphone_content_add_custom_header(LinphoneContent *content, const char *header_name, const char *header_value) { - // TODO -} - - LinphoneContent * linphone_content_new(void) { return linphone_content_new_with_body_handler(NULL); diff --git a/coreapi/content.h b/coreapi/content.h index ea3eda32c..104488d42 100644 --- a/coreapi/content.h +++ b/coreapi/content.h @@ -174,16 +174,38 @@ LINPHONE_PUBLIC const char * linphone_content_get_name(const LinphoneContent *co */ LINPHONE_PUBLIC void linphone_content_set_name(LinphoneContent *content, const char *name); +/** + * Tell whether a content is a multipart content. + * @param[in] content LinphoneContent object. + * @return A boolean value telling whether the content is multipart or not. + */ LINPHONE_PUBLIC bool_t linphone_content_is_multipart(const LinphoneContent *content); -LINPHONE_PUBLIC LinphoneContent * linphone_content_get_part(const LinphoneContent *content); +/** + * Get a part from a multipart content according to its index. + * @param[in] content LinphoneContent object. + * @param[in] idx The index of the part to get. + * @return A LinphoneContent object holding the part if found, NULL otherwise. + */ +LINPHONE_PUBLIC LinphoneContent * linphone_content_get_part(const LinphoneContent *content, int idx); -LINPHONE_PUBLIC void linphone_content_add_part(LinphoneContent *content, LinphoneContent *part); +/** + * Find a part from a multipart content looking for a part header with a specified value. + * @param[in] content LinphoneContent object. + * @param[in] header_name The name of the header to look for. + * @param[in] header_value The value of the header to look for. + * @return A LinphoneContent object object the part if found, NULL otherwise. + */ +LINPHONE_PUBLIC LinphoneContent * linphone_content_find_part_by_header(const LinphoneContent *content, const char *header_name, const char *header_value); +/** + * Get a custom header value of a content. + * @param[in] content LinphoneContent object. + * @param[in] header_name The name of the header to get the value from. + * @return The value of the header if found, NULL otherwise. + */ LINPHONE_PUBLIC const char * linphone_content_get_custom_header(const LinphoneContent *content, const char *header_name); -LINPHONE_PUBLIC void linphone_content_add_custom_header(LinphoneContent *content, const char *header_name, const char *header_value); - /** * @} */ diff --git a/coreapi/sal.c b/coreapi/sal.c index 9ed121463..491e100fb 100644 --- a/coreapi/sal.c +++ b/coreapi/sal.c @@ -946,6 +946,39 @@ void sal_body_handler_set_size(SalBodyHandler *body_handler, size_t size) { belle_sip_body_handler_set_size(BELLE_SIP_BODY_HANDLER(body_handler), size); } +bool_t sal_body_handler_is_multipart(const SalBodyHandler *body_handler) { + if (BELLE_SIP_IS_INSTANCE_OF(body_handler, belle_sip_multipart_body_handler_t)) return TRUE; + return FALSE; +} + +SalBodyHandler * sal_body_handler_get_part(const SalBodyHandler *body_handler, int idx) { + const belle_sip_list_t *l = belle_sip_multipart_body_handler_get_parts(BELLE_SIP_MULTIPART_BODY_HANDLER(body_handler)); + return (SalBodyHandler *)belle_sip_list_nth_data(l, idx); +} + +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) { + belle_sip_body_handler_t *bsbh = BELLE_SIP_BODY_HANDLER(l->data); + const belle_sip_list_t *headers = belle_sip_body_handler_get_headers(bsbh); + for (; headers != NULL; headers = headers->next) { + belle_sip_header_t *header = BELLE_SIP_HEADER(headers->data); + if ((strcmp(belle_sip_header_get_name(header), header_name) == 0) && (strcmp(belle_sip_header_get_unparsed_value(header), header_value) == 0)) { + return (SalBodyHandler *)bsbh; + } + } + } + return NULL; +} + +const char * sal_body_handler_get_header(const SalBodyHandler *body_handler, const char *header_name) { + belle_sip_header_t *header = sal_body_handler_find_header(body_handler, header_name); + if (header != NULL) { + return belle_sip_header_get_unparsed_value(header); + } + return NULL; +} + belle_sip_stack_t *sal_get_belle_sip_stack(Sal *sal) { return sal->stack; } diff --git a/include/sal/sal.h b/include/sal/sal.h index 468696b29..6031278bf 100644 --- a/include/sal/sal.h +++ b/include/sal/sal.h @@ -854,6 +854,10 @@ void * sal_body_handler_get_data(const SalBodyHandler *body_handler); void sal_body_handler_set_data(SalBodyHandler *body_handler, void *data); 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); +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); /*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);