[REBOL] Re: CGI, uploading file using HTTP, server side, help wanted (max 45 se
From: andreas::bolka::gmx::net at: 17-Jun-2003 21:35
Tuesday, June 17, 2003, 7:14:32 PM, Marc wrote:
> I need to upload files on a server in a situation where only HTTP is
> available. I used to do this in PHP but for some obscure reason I
> want to do it now in Rebol.
[...] regarding HTTP POST with mime type multipart/form-data I'd also
suggest a search thru the list archive, as there have been various
threads about this in the past.
I've written a 'decode-multipart-form-data function (attached) which
decodes multipart/form-data POST data and provides a block that tries
to be as similar to the result of 'decode-cgi as possible. You might
want to use this. If you have any questions, do not hesitate to ask.
--- cut here - decode-multipart-form-data.r ---
REBOL [
Title:
"decode-multipart-form-data"
Authors:
[ "Andreas Bolka" ]
Contributors:
[ ]
Date:
2003-02-22
History:
[ 2002-06-18 "initial release"
2003-02-21 "major bugfixes and cleanup. example improved."
2003-02-22 "another parsing bug fixed"
]
Version:
1.3
Purpose: {
Decodes POST-data encoded as "multipart/form-data" as defined by
RFC 2388.
The output is compatible to 'decode-cgi wherever possible. So the
output contains a list of set-word's and values, one pair for each data
field. example:
[ field1: "foo" field2: "bar" ]
Parts of the form-data with content-type text/plain and no filename
attribute in the content dispostition will be translated to basic name
value pairs as in the example above.
Parts having with a content-type different from text/plain and/or a
filename attribute in their content disposition will be translated to
object!'s with the following fields: filename, type, content.
An example. Imagine an HTML form like the following:
<form method="post" enctype="multipart/form-data">
<input type="text" name="field1" value="foo" />
<input type="file" name="field2" />
</form>
Once this form is submitted with "foo" in field1 and a file called
"bar.txt" containing the three bytes "nuf" in field2, this will result
in the following to be returned from 'decode-multipart-form-data:
[ field1: "foo"
field2: make object! [
filename: "bar.txt"
type: "text/plain"
content: "nuf"
]
]
}
]
decode-multipart-form-data: func [
p-content-type
p-post-data
/local list ct bd delim-beg delim-end non-cr non-lf non-crlf mime-part
] [
list: copy []
if not found? find p-content-type "multipart/form-data" [ return list ]
ct: copy p-content-type
bd: join "--" copy find/tail ct "boundary="
delim-beg: join bd crlf
delim-end: join crlf bd
non-cr: complement charset reduce [ cr ]
non-lf: complement charset reduce [ newline ]
non-crlf: [ non-cr | cr non-lf ]
mime-part: [
( ct-dispo: content: none ct-type: "text/plain" )
delim-beg ; mime-part start delimiter
"content-disposition: " copy ct-dispo any non-crlf crlf
opt [ "content-type: " copy ct-type any non-crlf crlf ]
crlf ; content delimiter
copy content
to delim-end crlf ; mime-part end delimiter
( handle-mime-part ct-dispo ct-type content )
]
handle-mime-part: func [ p-ct-dispo p-ct-type p-content /local tmp ] [
p-ct-dispo: parse p-ct-dispo {;="}
append list to-set-word (select p-ct-dispo "name")
either (none? tmp: select p-ct-dispo "filename")
and (found? find p-ct-type "text/plain") [
append list content
] [
append list make object! [
filename: copy tmp
type: copy p-ct-type
content: either none? p-content [ none ] [ copy p-content ]
]
]
]
use [ ct-dispo ct-type content ] [
parse/all p-post-data [ some mime-part "--" crlf ]
]
list
]
--- cut here - decode-multipart-form-data.r ---
--
Best regards,
Andreas mailto:[andreas--bolka--gmx--net]