Lather.Mtom.Mime (lather v1.0.42)

View Source

MIME 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

build_content_type_header(boundary, type, start)

@spec build_content_type_header(String.t(), String.t(), String.t()) :: String.t()

Builds a Content-Type header for multipart/related messages.

Parameters

  • boundary - Multipart boundary
  • type - Root part type
  • start - Root part Content-ID

Returns

  • Complete Content-Type header value

Examples

header = Mime.build_content_type_header("uuid:123", "application/xop+xml", "root@soap")

build_multipart_message(soap_envelope, attachments, options \\ [])

@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 binary
  • attachments - List of Attachment structs
  • options - 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)

extract_boundary(content_type)

@spec extract_boundary(String.t()) :: {:ok, String.t()} | {:error, atom()}

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"")

generate_boundary()

@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"

parse_headers(header_section)

@spec parse_headers(binary()) :: map()

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>"}

parse_multipart_message(content_type, body, options \\ [])

@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 value
  • body - The multipart message body
  • options - 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)

validate_content_type(content_type)

@spec validate_content_type(String.t()) :: :ok | {:error, atom()}

Validates a multipart/related Content-Type header.

Parameters

  • content_type - Content-Type header to validate

Returns

  • :ok if valid, {:error, reason} if invalid