Lather.Mtom.Mime (lather v1.0.42)
View SourceMIME utilities for MTOM multipart/related message handling.
This module provides functions for building and parsing multipart/related MIME messages used in MTOM (Message Transmission Optimization Mechanism).
MTOM messages use the multipart/related content type with the following structure:
Content-Type: multipart/related;
boundary="uuid:12345-abcde";
type="application/xop+xml";
start="<rootpart@lather.soap>"
--uuid:12345-abcde
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: 8bit
Content-ID: <rootpart@lather.soap>
<?xml version="1.0"?>
<soap:Envelope>
<!-- SOAP envelope with XOP includes -->
</soap:Envelope>
--uuid:12345-abcde
Content-Type: application/pdf
Content-Transfer-Encoding: binary
Content-ID: <attachment123@lather.soap>
%PDF-1.4 [binary data]...
--uuid:12345-abcde--
Summary
Functions
Builds a Content-Type header for multipart/related messages.
Builds a complete multipart/related MIME message with SOAP envelope and attachments.
Extracts the boundary parameter from a Content-Type header.
Generates a unique boundary string for multipart messages.
Parses MIME headers from a header section.
Parses a multipart/related MIME message.
Validates a multipart/related Content-Type header.
Functions
Builds a Content-Type header for multipart/related messages.
Parameters
boundary- Multipart boundarytype- Root part typestart- Root part Content-ID
Returns
- Complete Content-Type header value
Examples
header = Mime.build_content_type_header("uuid:123", "application/xop+xml", "root@soap")
@spec build_multipart_message(binary(), [Lather.Mtom.Attachment.t()], keyword()) :: {String.t(), binary()}
Builds a complete multipart/related MIME message with SOAP envelope and attachments.
Parameters
soap_envelope- The SOAP envelope XML as binaryattachments- List of Attachment structsoptions- Additional options
Options
:boundary- Custom boundary (auto-generated if not provided):soap_content_type- SOAP part content type (default: "application/xop+xml"):soap_charset- SOAP part charset (default: "UTF-8")
Returns
{content_type_header, multipart_body}- Complete MIME message
Examples
{content_type, body} = Mime.build_multipart_message(soap_xml, attachments)
Extracts the boundary parameter from a Content-Type header.
Parameters
content_type- Content-Type header value
Returns
{:ok, boundary}- Successfully extracted boundary{:error, reason}- Extraction failed
Examples
{:ok, boundary} = Mime.extract_boundary("multipart/related; boundary="uuid:123"")
@spec generate_boundary() :: String.t()
Generates a unique boundary string for multipart messages.
Returns
- Boundary string suitable for multipart MIME messages
Examples
boundary = Mime.generate_boundary()
# "uuid:a1b2c3d4-e5f6-7890-abcd-ef1234567890"
Parses MIME headers from a header section.
Parameters
header_section- Raw header text
Returns
- Map of parsed headers with lowercase keys
Examples
headers = Mime.parse_headers("Content-Type: application/pdf\r\nContent-ID: <att1>")
# %{"content-type" => "application/pdf", "content-id" => "<att1>"}
@spec parse_multipart_message(String.t(), binary(), keyword()) :: {:ok, {binary(), [map()]}} | {:error, term()}
Parses a multipart/related MIME message.
Parameters
content_type- The Content-Type header valuebody- The multipart message bodyoptions- Parsing options
Returns
{:ok, {soap_part, attachments}}- Successfully parsed message{:error, reason}- Parsing error
Examples
{:ok, {soap_xml, attachments}} = Mime.parse_multipart_message(content_type, body)
Validates a multipart/related Content-Type header.
Parameters
content_type- Content-Type header to validate
Returns
:okif valid,{:error, reason}if invalid