Common Lisp Package: RFC2388

README:

This package contains an implementation of RFC 2388, which is used to process form data posted with HTTP POST method using enctype "multipart/form-data".

The main functions of interest are PARSE-MIME and PARSE-HEADER.

Use PARSE-MIME to parse MIME content. It can be given either a string or a stream. It returns a list of MIME parts. The second argument must be a boundary, which is a string that seperates MIME parts. See below how to obtain it.

PARSE-HEADER is used by PARSE-MIME internally, but you can use it to parse headers yourself. For instance, when data is posted to server using POST method, a header describing the content type is sent as well. Usually its content is application/x-www-form-urlencoded or something similar. But users may set it to multipart/form-data, with additional parameter named "boundary". This is how one would usually parse the posted data:

lisp (let* ((header (parse-header <request-content-type> :value)) (boundary (or (assoc "boundary" (header-parameters header) :test #'string-equal) (error "Form data is missing a boundary: ~S" <request-content-type>)))) (parse-mime <request-posted-content> boundary))

The :VALUE keyword parameter to PARSE-HEADER means that parsing should begin with header value, not name (because header name is "content-type" and the web server has already seperated them, at least in this scenario).

FUNCTION

Public

CONTENT-TYPE (PART &KEY AS-STRING)

Returns the Content-Type header of mime-part PART.

FIND-HEADER (LABEL HEADERS)

Find header by label from set of headers.

FIND-PARAMETER (NAME PARAMS)

Find header parameter by name from set of parameters.

Undocumented

FIND-CONTENT-DISPOSITION-HEADER (HEADERS)

GET-FILE-NAME (HEADERS)

HEADER-NAME (STRUCTURE)

SETFHEADER-NAME (NEW-VALUE STRUCTURE)

HEADER-PARAMETERS (STRUCTURE)

SETFHEADER-PARAMETERS (NEW-VALUE STRUCTURE)

HEADER-VALUE (STRUCTURE)

SETFHEADER-VALUE (NEW-VALUE STRUCTURE)

MAKE-MIME-PART (CONTENTS HEADERS)

MIME-PART-CONTENTS (STRUCTURE)

SETFMIME-PART-CONTENTS (NEW-VALUE STRUCTURE)

MIME-PART-HEADERS (STRUCTURE)

SETFMIME-PART-HEADERS (NEW-VALUE STRUCTURE)

Private

COPY-CONTENT-TYPE (SEQUENCE)

Return a copy of SEQUENCE which is EQUAL to SEQUENCE but not EQ.

COPY-HEADER (SEQUENCE)

Return a copy of SEQUENCE which is EQUAL to SEQUENCE but not EQ.

COPY-MIME-PART (SEQUENCE)

Return a copy of SEQUENCE which is EQUAL to SEQUENCE but not EQ.

LWSP-CHAR-P (CHAR)

Returns true if CHAR is a linear-whitespace-char (LWSP-char). Either space or tab, in short.

PARSE-CONTENT-TYPE (STRING)

Returns content-type which is parsed from STRING.

READ-UNTIL-NEXT-BOUNDARY (STREAM BOUNDARY &OPTIONAL DISCARD OUT-STREAM)

Reads from STREAM up to the next boundary. Returns two values: read data (nil if DISCARD is true), and true if the boundary is not last (i.e., there's more data).

SKIP-LINEAR-WHITESPACE (STRING &KEY (START 0) END)

Returns the position of first non-linear-whitespace character in STRING bound by START and END.

UNPARSE-CONTENT-TYPE (CT)

Returns content-type CT in string representation.

Undocumented

CONTENT-TYPE-SUB (STRUCTURE)

SETFCONTENT-TYPE-SUB (NEW-VALUE STRUCTURE)

CONTENT-TYPE-SUPER (STRUCTURE)

SETFCONTENT-TYPE-SUPER (NEW-VALUE STRUCTURE)

MAKE-CONTENT-TYPE (SUPER SUB)

MAKE-HEADER (NAME VALUE PARAMETERS)

MAKE-TMP-FILE-NAME

GENERIC-FUNCTION

Public

PARSE-HEADER (SOURCE &OPTIONAL START-STATE)

Parses SOURCE and returs a single MIME header. Header is a list of the form (NAME VALUE PARAMETERS), PARAMETERS is a list of (NAME . VALUE)

PARSE-MIME (SOURCE BOUNDARY &KEY WRITE-CONTENT-TO-FILE (WRITE-CONTENT-TO-FILE T))

Parses MIME entities, returning them as a list. Each element in the list is of form: (body headers), where BODY is the contents of MIME part, and HEADERS are all headers for that part. BOUNDARY is a string used to separate MIME entities.