Upload file - multipart form-data

Posted by douglas_aparicio_bermudez on 2009-04-08 08:08

Greetings.
I need to do upload of a configuration file via tcl.
Sailing by the Web I saw an example in: http://code.activestate.com/recipes/109363/

Taking into east account example, I developed one similar.
Analyzing the execution by wireshark it shows to the message to me "[Malformed Packet: MIME multipart] ".
Apparently I believe that I am passing some things bad through HTTP.

Part of my code is the following one:

package require base64 ;# tcllib
package require http
.
.
.

#Beginning
set headers(Content-Type) "multipart/form-data; boundary=---------------------------7d967970288\r\n"
set headers(Cache-Control) "no-cache\r\n"
set headers(Cookie) "pageIdentifier=BKUPPAGE; sessionId=invalid\r\n"

set opts(FILE) [list]
lappend opts(FILE) ".upload_file_handle"
lappend opts(FILE) "commitedcfg.cfg"

set opts_file "commitedcfg.cfg"
set url_modifpass_cont "hag/emweb/bkupRestoreConfig.htm"
set url_modifpass $puerto_acceso$url_modifpass_cont
puts "url_modifpass: $url_modifpass"

## Configure the output channel
set outfd stdout
set type "multipart/form-data"

fconfigure $outfd -translation binary

set outputData {}
# set bound "-----NEXT_PART_[clock seconds].[pid]"
set bound "-----------------------------7d93532ea03f4"

foreach {elem files} $opts(FILE) {
set fid [open $files r]
fconfigure $fid -translation binary
puts "fid: $fid"
puts "tam: [file size $files]"
if {[catch {read $fid [file size $files]} data]} {
puts "entra aqui"
return -code error $data
}
puts "data: $data"
close $fid
puts "elem: $elem"
puts "file: $files"
append outputData "--$bound\nContent-Disposition: form-data; name=\"$elem\"; filename=\"[file tail $files]\"\r\n"
append outputData "Content-Type: text/plain\r\n\r\n"
append outputData "$data\n"

# append outputData "--$bound\nContent-Disposition: form-data;\
# name=\"$elem\"; filename=\"[file tail $files]\"\n\n$data\n"
}

#THESE TWO PARAMETERS THAT EAST PROCEDURE RECEIVES I OBTAINED IT
#WITH WIRESHARK WHEN I ANALYZED BEFORE DEVELOPING THIS PROGRAM (cmdUpload 55706C6F6164).
foreach {elem data} {cmdUpload 55706C6F6164} {
puts "elem1: $elem"
puts "data1: $data"
append outputData "\r\n--$bound\r\nContent-Disposition: form-data; name=\"$elem\"\n\n$data\n"

# append outputData "--$bound\r\nContent-Disposition: form-data; name=\"$elem\"\r\n\r\n$data\n"
# append outputData "--$bound\nContent-Disposition: form-data;\
# name=\"$elem\"\n\n$data\n"
}

if {![string length $outputData]} {
return -code error "No data given to post"
}

append outputData "\r\n${bound}--\r\n"
# append outputData "${bound}--"

puts "outputData: $outputData"

set request [list ::http::geturl $url_modifpass -headers [headers] -channel $outfd -type "multipart/form-data; boundary=$bound" -query $outputData]

# lappend request -headers [headers]

set token [eval $request]
::http::wait $token
if {[string compare stdout $outfd]} {
close $outfd
}

The result of the execution indicates things among others: "400 Bad Request"

I believe that the problem this when step the value "55706C6F6164" (of the second foreach) because analyzing with wireshark here it is truncated.

Any aid well is received.

Thanks

Douglas Aparicio Bermudez
(bermudezdouglas@gmail.com)