View source with formatted 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]).   43
   44/** <module> Create a MIME message
   45
   46Simple and partial implementation of MIME   encoding. MIME is covered by
   47RFC 2045. This library is used by  e.g., http_post_data/3 when using the
   48form_data(+ListOfData) input specification.
   49
   50MIME decoding is now  arranged  through   library(mime)  from  the  clib
   51package, based on the  external  librfc2045   library.  Most  likely the
   52functionality of this package will be moved to the same library someday.
   53Packing however is a lot simpler then parsing.
   54*/
   55
   56%!  mime_pack(+Inputs, +Out:stream, ?Boundary) is det.
   57%
   58%   Pack a number of inputs into a MIME package using a specified or
   59%   generated boundary. The  generated  boundary   consists  of  the
   60%   current time in milliseconds  since  the   epoch  and  10 random
   61%   hexadecimal numbers. Inputs is a  list   of  _documents_ that is
   62%   added to the mime message.  Each element is one of:
   63%
   64%     * Name = Value
   65%     Name the document. This emits a header of the form below. The
   66%     =filename= is present if Value is of the form file(File).
   67%     Value may be any of remaining value specifications.
   68%
   69%       ==
   70%       Content-Disposition: form-data; name="Name"[; filename="<File>"
   71%       ==
   72%
   73%     * html(Tokens)
   74%     Tokens is a list of HTML tokens as produced by html//1. The
   75%     token list is emitted using print_html/1.
   76%
   77%     * file(File)
   78%     Emit the contents of File. The =|Content-type|= is derived
   79%     from the File using file_mime_type/2.  If the content-type
   80%     is =|text/_|=, the file data is copied in text mode, which
   81%     implies that it is read in the default encoding of the system
   82%     and written using the encoding of the Out stream.  Otherwise
   83%     the file data is copied binary.
   84%
   85%     * stream(In, Len)
   86%     Content is the next Len units from In.  Data is copied using
   87%     copy_stream_data/3. Units is bytes for binary streams and
   88%     characters codes for text streams.
   89%
   90%     * stream(In)
   91%     Content of the stream In, copied using copy_stream_data/2.
   92%     This is often used with memory files (see new_memory_file/1).
   93%
   94%     * mime(Attributes, Value, [])
   95%     Create a MIME header from Attributes and add Value, which can
   96%     be any of remaining values of this list. Attributes may
   97%     contain type(ContentType) and/or character_set(CharSet).  This
   98%     can be used to give a content-type to values that otherwise
   99%     do not have a content-type.  For example:
  100%
  101%       ==
  102%       mime([type(text/html)], '<b>Hello World</b>', [])
  103%       ==
  104%
  105%     * mime([], '', Parts)
  106%     Creates a nested multipart MIME message.  Parts is passed
  107%     as Inputs to a recursive call to mime_pack/2.
  108%
  109%     * Atomic
  110%     Atomic values are passed to write/1. This embeds simple atoms
  111%     and numbers.
  112%
  113%   @param  Out is a stream opened for writing. Typically, it should
  114%           be opened in text mode using UTF-8 encoding.
  115%
  116%   @bug    Does not validate that the boundary does not appear in
  117%           any of the input documents.
  118
  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).
  205
  206
  207%!  make_boundary(+Inputs, ?Boundary) is det.
  208%
  209%   Generate a boundary.  This should check all input sources whether
  210%   the boundary is enclosed.
  211
  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])