multipart-form-data

  1. multipart-form-data
    1. Description
    2. Source Code
    3. Author
    4. Requirements
    5. API
      1. http-multipart-request-data-limit
      2. read-multipart-form-data
      3. multipart-form-data-decode
      4. multipart-file?
      5. multipart-file-port
      6. mutipart-file-filename
      7. multipart-file-headers
    6. Examples
      1. 1
    7. License
    8. Version History

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

[parameter] http-multipart-request-data-limit

Not implemented yet! :-(

read-multipart-form-data

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

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

multipart-form-data-decode

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

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?

[procedure] (multipart-file? X)

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

[procedure] (multipart-file-port X)

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

mutipart-file-filename

[procedure] (multipart-file-filename X)

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

multipart-file-headers

[procedure] (multipart-file-headers X)

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