WEBRick: Problem to get the multipart/form-data decoded correctly

Hi list

I have to admit that I feel this is a stupid question, but two days of
Web search(1) and trials with WEBRick and CGI did not bring me any
closer.
Here is the problem:

I am uploading files to my Webrick server via the standard HTTP method:
<form method="POST" enctype="multipart/form-data">
  <input type="file" name="data" /><input type="submit" /></form>

the Webrick do_POST handler than has the multipart/form-data in his req.body.

As astonishing as that sounds, I have not found a way to get the
file's content out of this.
I am sure there should be an easy way.

BTW I have browsed the ML archives and there are some references to
CGI but I just could not glue it together :frowning:

All help is appreciated.

Robert

(1) a badly known synonym for "googling" :wink:

···

--
[...] as simple as possible, but no simpler.
-- Attributed to Albert Einstein

Here is the problem:

I am uploading files to my Webrick server via the standard HTTP method:
<form method="POST" enctype="multipart/form-data">
  <input type="file" name="data" /><input type="submit" /></form>

the Webrick do_POST handler than has the multipart/form-data in his req.body.

As astonishing as that sounds, I have not found a way to get the
file's content out of this.
I am sure there should be an easy way.

Here's something I found from Google. It may not be definitive, sorry,
but I think it can help!

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/94785

Arle.

Bonsoir Robert.

Google didn't help me either, but I'm using a Ruby wiki called "Soks",
which runs on WEBrick and offers file uploads. Reading the code it
seems that the following should work:

  upload_data = request.query["data"]
  File.open(upload_data.filename, "wb") do |file|
    upload_data.each_data do |data|
      file << data.to_s
    end
  end

Take a look at WEBrick::HTTPUtils::FormData.

I haven't tested it, but I HTH.

Regards,
Pit

···

2007/8/9, Robert Dober <robert.dober@gmail.com>:

I have to admit that I feel this is a stupid question, but two days of
Web search(1) and trials with WEBRick and CGI did not bring me any
closer. (...)

> Here is the problem:
>
> I am uploading files to my Webrick server via the standard HTTP method:
> <form method="POST" enctype="multipart/form-data">
> <input type="file" name="data" /><input type="submit" /></form>
>
> the Webrick do_POST handler than has the multipart/form-data in his req.body.
>
> As astonishing as that sounds, I have not found a way to get the
> file's content out of this.
> I am sure there should be an easy way.

Here's something I found from Google. It may not be definitive, sorry,
but I think it can help!

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/94785

That was indeed one of my first hits and it was useful to get the form
right, please note however that he is not doing anything with the
uploaded data :(, just sending it back.

And this exactly where it hurts, I cannot glue the received data
(req.body) into CGI, which would seem to do the multipart/form-data
conversion just fine.

Actually I would not care to save the body as such into a file and
have an external converter do the job(1), if I only had such an
external converer :(.

Thx anyway
Robert
(1) even if not in Ruby, sorry for the OL in that case.

···

On 8/9/07, Arlen Christian Mart Cuss <celtic@sairyx.org> wrote:

Arle.

--
[...] as simple as possible, but no simpler.
-- Attributed to Albert Einstein

> I have to admit that I feel this is a stupid question, but two days of
> Web search(1) and trials with WEBRick and CGI did not bring me any
> closer. (...)

Bonsoir Robert.

Google didn't help me either, but I'm using a Ruby wiki called "Soks",
which runs on WEBrick and offers file uploads. Reading the code it
seems that the following should work:

  upload_data = request.query["data"]
  File.open(upload_data.filename, "wb") do |file|
    upload_data.each_data do |data|
      file << data.to_s
    end
  end

Take a look at WEBrick::HTTPUtils::FormData.

I haven't tested it, but I HTH.

Regards,
Pit

I knew it would be simple, but that simple!!!
Works like charm.
Merci beaucoup Pit, meine Dankbarkeit wird Dir für immer nachschleifen
(sorry folks for these old Viennese saying :wink:
Robert

···

On 8/10/07, Pit Capitain <pit.capitain@gmail.com> wrote:

2007/8/9, Robert Dober <robert.dober@gmail.com>:

--
[...] as simple as possible, but no simpler.
-- Attributed to Albert Einstein

I thought I share the outcome of this, a very primitive file server we
need for some file transfer for folks caught behind a firewall and a
mean Proxy. Only https passes.
This might be useful for some low to medium traffic transfers.
I will use it on demand only, it is not really tested and there might
(there are for sure) some security issues. I have checked the most
obvious.

Runs on any system you can execute `df -k` in your Ruby code (that
part can be commented out if one needs to do so).
Hope this might be helpful in case somebody searches the archives for

multipart/form-data File upload WEBrick File Server

Cheers
Robert

file-server.rb.gz (3.85 KB)