View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2002-2020, University of Amsterdam
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(mime_pack,
   37          [ mime_pack/3                 % +Input, +Stream, ?Boundary
   38          ]).   39:- autoload(html_write,[print_html/2]).   40:- autoload(mimetype,[file_mime_type/2]).   41:- autoload(library(error),[instantiation_error/1]).   42:- autoload(library(lists),[select/3]).

Create a MIME message

Simple and partial implementation of MIME encoding. MIME is covered by RFC 2045. This library is used by e.g., http_post_data/3 when using the form_data(+ListOfData) input specification.

MIME decoding is now arranged through library(mime) from the clib package, based on the external librfc2045 library. Most likely the functionality of this package will be moved to the same library someday. Packing however is a lot simpler then parsing. */

 mime_pack(+Inputs, +Out:stream, ?Boundary) is det
Pack a number of inputs into a MIME package using a specified or generated boundary. The generated boundary consists of the current time in milliseconds since the epoch and 10 random hexadecimal numbers. Inputs is a list of documents that is added to the mime message. Each element is one of:
Name = Value
Name the document. This emits a header of the form below. The filename is present if Value is of the form file(File). Value may be any of remaining value specifications.
Content-Disposition: form-data; name="Name"[; filename="<File>"
html(Tokens)
Tokens is a list of HTML tokens as produced by html//1. The token list is emitted using print_html/1.
file(File)
Emit the contents of File. The Content-type is derived from the File using file_mime_type/2. If the content-type is text/_, the file data is copied in text mode, which implies that it is read in the default encoding of the system and written using the encoding of the Out stream. Otherwise the file data is copied binary.
stream(In, Len)
Content is the next Len units from In. Data is copied using copy_stream_data/3. Units is bytes for binary streams and characters codes for text streams.
stream(In)
Content of the stream In, copied using copy_stream_data/2. This is often used with memory files (see new_memory_file/1).
mime(Attributes, Value, [])
Create a MIME header from Attributes and add Value, which can be any of remaining values of this list. Attributes may contain type(ContentType) and/or character_set(CharSet). This can be used to give a content-type to values that otherwise do not have a content-type. For example:
mime([type(text/html)], '<b>Hello World</b>', [])
mime([], , Parts)
Creates a nested multipart MIME message. Parts is passed as Inputs to a recursive call to mime_pack/2.
Atomic
Atomic values are passed to write/1. This embeds simple atoms and numbers.
Arguments:
Out- is a stream opened for writing. Typically, it should be opened in text mode using UTF-8 encoding.
bug
- Does not validate that the boundary does not appear in any of the input documents.
  119mime_pack(Inputs, OutputStream, Boundary) :-
  120    make_boundary(Inputs, Boundary),
  121    pack_list(Inputs, OutputStream, Boundary).
  122
  123pack_list([], Out, Boundary) :-
  124    format(Out, '--~w--\r\n', [Boundary]).
  125pack_list([H|T], Out, Boundary) :-
  126    format(Out, '--~w\r\n', [Boundary]),
  127    pack(H, Out),
  128    format(Out, '\r\n', []),
  129    pack_list(T, Out, Boundary).
  130
  131pack(X, _Out) :-
  132    var(X),
  133    !,
  134    instantiation_error(X).
  135pack(Name=Value, Out) :-
  136    !,
  137    (   Value = file(FileName)
  138    ->  format(Out, 'Content-Disposition: form-data; name="~w"; filename="~w"\r\n',
  139               [Name, FileName])
  140    ;   format(Out, 'Content-Disposition: form-data; name="~w"\r\n', [Name])
  141    ),
  142    pack(Value, Out).
  143pack(html(HTML), Out) :-
  144    !,
  145    format(Out, 'Content-Type: text/html\r\n\r\n', []),
  146    print_html(Out, HTML).
  147pack(file(File), Out) :-
  148    !,
  149    (   file_mime_type(File, Type)
  150    ->  true
  151    ;   Type = text/plain
  152    ),
  153    format(Out, 'Content-Type: ~w\r\n\r\n', [Type]),
  154    (   Type = text/_
  155    ->  setup_call_cleanup(
  156            open(File, read, In),
  157            copy_stream_data(In, Out),
  158            close(In))
  159    ;   stream_property(Out, encoding(OldEncoding)),
  160        setup_call_cleanup(
  161            set_stream(Out, encoding(octet)),
  162            setup_call_cleanup(
  163                open(File, read, In, [type(binary)]),
  164                copy_stream_data(In, Out),
  165                close(In)),
  166            set_stream(Out, encoding(OldEncoding)))
  167    ).
  168pack(stream(In, Len), Out) :-
  169    !,
  170    format(Out, '\r\n', []),
  171    copy_stream_data(In, Out, Len).
  172pack(stream(In), Out) :-
  173    !,
  174    format(Out, '\r\n', []),
  175    copy_stream_data(In, Out).
  176pack(mime(Atts, Data, []), Out) :-             % mime_parse compatibility
  177    !,
  178    write_mime_attributes(Atts, Out),
  179    pack(Data, Out).
  180pack(mime(_Atts, '', Parts), Out) :-
  181    make_boundary(Parts, Boundary),
  182    format('Content-type: multipart/mixed; boundary=~w\r\n\r\n',
  183           [Boundary]),
  184    mime_pack(Parts, Out, Boundary).
  185pack(Atom, Out) :-
  186    atomic(Atom),
  187    !,
  188    format(Out, '\r\n', []),
  189    write(Out, Atom).
  190pack(Value, _) :-
  191    throw(error(type_error(mime_part, Value), _)).
  192
  193write_mime_attributes([], _) :- !.
  194write_mime_attributes(Atts, Out) :-
  195    select(type(Type), Atts, A1),
  196    !,
  197    (   select(character_set(CharSet), A1, A2)
  198    ->  format(Out, 'Content-type: ~w; charset=~w\r\n', [Type, CharSet]),
  199        write_mime_attributes(A2, Out)
  200    ;   format(Out, 'Content-type: ~w\r\n', [Type]),
  201        write_mime_attributes(A1, Out)
  202    ).
  203write_mime_attributes([_|T], Out) :-
  204    write_mime_attributes(T, Out).
 make_boundary(+Inputs, ?Boundary) is det
Generate a boundary. This should check all input sources whether the boundary is enclosed.
  212make_boundary(_, Boundary) :-
  213    atomic(Boundary),
  214    !.
  215make_boundary(_, Boundary) :-
  216    get_time(Now),
  217    A is random(1<<16),
  218    B is random(1<<16),
  219    C is random(1<<16),
  220    D is random(1<<16),
  221    E is random(1<<16),
  222    format(atom(Boundary), '------~3f~16r~16r~16r~16r~16r',
  223           [Now, A, B, C, D, E])