chickadee » multipart-form-data

multipart-form-data

Description

Reads & decodes HTTP multipart/form-data requests.

Source Code

https://bitbucket.org/knodium/multipart-form-data

Author

Andy Bennett

Requirements

intarweb, comparse, records

API

http-multipart-request-data-limit

http-multipart-request-data-limit parameter

Not implemented yet! :-(

read-multipart-form-data

(read-multipart-form-data request [max-length]) procedure

Implements the same interface as Intarweb's read-urlencoded-request-data.

multipart-form-data-decode

(multipart-form-data-decode str-port-or-seq boundary [max-length]) procedure

Decodes a multipart/form-data encoded body. This procedure requires the boundary token information from the HTTP headers.

Returns an alist: a list of name, value pairs or name, multipart-file pairs if the value was created from an HTML Forms File Input.

multipart-file?

(multipart-file? X) procedure

Tests to see if the value (cdrs of the pairs returned from multipart-form-data-decode) is the result of an HTML Forms File Input.

multipart-file-port

(multipart-file-port X) procedure

A port from which you can read the contents of the file.

mutipart-file-filename

(multipart-file-filename X) procedure

The filename that the browser supplied for the data read from the port.

multipart-file-headers

(multipart-file-headers X) procedure

A list of HTTP style headers that can tell you about the Content-Type and encoding of the data read from the port.

Use the header procedures from intarweb to access the value returned.

Examples

1

Here we present a decoder that can decide whether to decode multipart/form-data or application/x-www-form-urlencoded data based on the request headers:

(use intarweb multipart-form-data)

(define (read-request-data req)
  (let* ((content-type (header-values 'content-type (request-headers req)))
	 (_            (assert (= 1 (length content-type))))
	 (content-type (car content-type))
	 (proc         (case content-type
			 ((application/x-www-form-urlencoded)
			  read-urlencoded-request-data)
			 ((multipart/form-data)
			  read-multipart-form-data)
			 (else
			   (abort "Unable to decode these shenannigans!")))))
    (proc req)))

License

 Copyright (C) 2014, Andy Bennett
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 
 Redistributions of source code must retain the above copyright notice, this
 list of conditions and the following disclaimer.
 Redistributions in binary form must reproduce the above copyright notice,
 this list of conditions and the following disclaimer in the documentation
 and/or other materials provided with the distribution.
 Neither the name of the author nor the names of its contributors may be
 used to endorse or promote products derived from this software without
 specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.

Version History

Contents »