Version 6 of File Upload with tcl's http

Updated 2005-06-02 10:32:20

CMcC 20050227: The process of uploading a file to a server entails encoding the file (and other form elements) as multipart/form-data. The code below handles that.

The second part of the upload will use http to send a -query with -type multipart/form-data, causing the client to POST the mime multipart/form-data to the server. Voila.

Note: Has anyone been able to upload a file with this anywhere? Not only that it does not read the file in binary mode (which causes data corruption).. also the returned data does contain headerlines that do not get put in the actual http header, if you simply pass the returned block to the -query parameter of http::geturl .. which causes a wrong content-type and then everything goes down the drain.. also, but thats the smallest issue, most places like it when you pass a filename aswell - but that could just be added to the disposition of add_binary as 'filename="somename.jpg"'..

Note II: I did some changes to bring the script a little closer to a state, where its actually working, but its still not fully doing what it should because of the "header-problem"..


BR 2005-06-02 - I also tried to make it work. I wanted to test a Java upload servlet which was based on Jakarta's common-fileupload.

Problems:

  • The mime package doesn't have a getheaders method and getbody doesn't work for multipart. I think buildmessage should be split into those two functions. That is the "headers-problem" mentioned above.
  • The mime package appends a spurious additional \r\n to each item in the multipart data. That is because mime appends a \r\n to each item and than *also* adds a \r\n before the boundary.
  • commons-fileupload doesn't understand the quoted boundary spec correctly which the mime package produces in the Content-Type header. This is a bug in commons-fileupload, I guess, but maybe the mime package should not make things complicated here.
  • The http package can't source POST data from memory, it wants a file (or alternatively, the mime package can't produce a channel for use).

 # Provide multipart/form-data for http

 package provide form-data 1.0
 package require mime

 namespace eval form-data {}

 proc form-data::compose {partv {type multipart/form-data}} {
     upvar 1 $partv parts

     set mime [mime::initialize -canonical $type -parts $parts]
     set packaged [mime::buildmessage $mime]
     foreach part $parts {
         mime::finalize $part
     }
     mime::finalize $mime

     return $packaged
 }

 proc form-data::add_binary {partv name filename value type} {
     upvar 1 $partv parts
     set disposition "form-data; name=\"${name}\"; filename=\"$filename\""
     lappend parts [mime::initialize -canonical $type \
                    -string $value \
                    -encoding binary \
                    -header [list Content-Disposition $disposition]]
 }

 proc form-data::add_field {partv name value} {
     upvar 1 $partv parts
     set disposition "form-data; name=\"${name}\""
     lappend parts [mime::initialize -canonical text/plain -string $value \
                        -header [list Content-Disposition $disposition]]
 }

 proc form-data::format {name filename value type args} {
     set parts {}
     foreach {n v} $args {
         add_field parts $n $v
     }
     add_binary parts $name $filename $value $type
     return [compose parts]
 }

 if {[info script] eq $argv0} {

     # format a gif file upload according to the following form:
     #<FORM METHOD="POST" ENCTYPE="multipart/form-data" ACTION="upload.php"> 
     #<INPUT TYPE="HIDDEN" NAME="MAX_FILE_SIZE" VALUE=" "> 
     #<INPUT TYPE="HIDDEN" NAME="action" VALUE="1"> 
     #<INPUT TYPE="FILE" NAME="file1">
     #<INPUT TYPE="SUBMIT" VALUE="Host It"> <br> 
     #<INPUT TYPE="text" NAME="img_resize"  SIZE="4" MAXLENGTH="4">
     #</FORM>

     # get contents of the gif
     set fd [open ./logo125.gif]
     fconfigure $fd -translation binary
     set image [read $fd]
     close $fd

     # set up other fields
     array set fields {
         MAX_FILE_SIZE " "
         action 1
         img_resize "100%"
     }

     # format the image and form
     puts [form-data::format file1 "logo125.gif" $image image/gif {expand}[array get fields]]
 }