From: Michael Schilling Date: 2006-02-10T12:24:44+09:00 Subject: Re: can't get multipart_form from cgi.rb to work Hello again, as I couldn't reproduce the described multipart_form error (see description below) directly on the command-line I looked into the cgi.rb source and found the chunk of code that raises the exception: Original code in cgi.rb ---------------------------------------------------- def read_multipart(boundary, content_length) params = Hash.new([]) boundary = "--" + boundary buf = "" bufsize = 10 * 1024 # start multipart/form-data stdinput.binmode if defined? stdinput.binmode boundary_size = boundary.size + EOL.size content_length -= boundary_size status = stdinput.read(boundary_size) if nil == status raise EOFError, "no content body" elsif boundary + EOL != status raise EOFError, "bad content body" end --------------------------------------------------- After playing around with this part of the code, I have found more questions than answers. When I negate the if clause before the exception (see code below), the expression is still true (even so, it was false for my program, before the change in cgi.rb), and the status variable will even be written into the apache logs with something, that looks right (see below for the logs - in the line with 'no content body'): So whatever I do, I can't get rid of the exception. Something seems to be very wrong here, but I have no clue, what it could be. Is this some kind of bug in cgi.rb? Maybe I should try a different approach, but I like a good challenge ;-) Any more help is very much appreciated! Bye, Michael Changed code in cgi.rb --------------------------------------------------- def read_multipart(boundary, content_length) params = Hash.new([]) boundary = "--" + boundary buf = "" bufsize = 10 * 1024 # start multipart/form-data stdinput.binmode if defined? stdinput.binmode boundary_size = boundary.size + EOL.size content_length -= boundary_size status = stdinput.read(boundary_size) if nil != status # set this to _not_ equal raise EOFError, "no content body. Status: " + status.to_s # and add here the status as a string elsif boundary + EOL != status raise EOFError, "bad content body" end --------------------------------------------------- apache error log with original cgi.rb code: ----------------------------------------------------------- [Fri Feb 10 03:56:33 2006] [error] [client 129.70.170.3] /usr/lib/ruby/1.8/cgi.rb:979:in `read_multipart', referer: http://calvin.physik.uni-bielefeld.de/cgi-bin/pdf2eps.rb [Fri Feb 10 03:56:33 2006] [error] [client 129.70.170.3] : , referer: http://calvin.physik.uni-bielefeld.de/cgi-bin/pdf2eps.rb [Fri Feb 10 03:56:33 2006] [error] [client 129.70.170.3] no content body. Status: ;, referer: http://calvin.physik.uni-bielefeld.de/cgi-bin/pdf2eps.rb [Fri Feb 10 03:56:33 2006] [error] [client 129.70.170.3] (, referer: http://calvin.physik.uni-bielefeld.de/cgi-bin/pdf2eps.rb [Fri Feb 10 03:56:33 2006] [error] [client 129.70.170.3] EOFError, referer: http://calvin.physik.uni-bielefeld.de/cgi-bin/pdf2eps.rb ----------------------------------------------------------- apache error log with changed cgi.rb code: ----------------------------------------------------------- [Fri Feb 10 03:57:02 2006] [error] [client 129.70.170.3] /usr/lib/ruby/1.8/cgi.rb:979:in `read_multipart', referer: http://calvin.physik.uni-bielefeld.de/cgi-bin/pdf2eps.rb [Fri Feb 10 03:57:02 2006] [error] [client 129.70.170.3] : , referer: http://calvin.physik.uni-bielefeld.de/cgi-bin/pdf2eps.rb [Fri Feb 10 03:57:02 2006] [error] [client 129.70.170.3] no content body. Status: Content-Disposition: form-data; name="sowas";, referer: http://calvin.physik.uni-bielefeld.de/cgi-bin/pdf2eps.rb [Fri Feb 10 03:57:02 2006] [error] [client 129.70.170.3] (, referer: http://calvin.physik.uni-bielefeld.de/cgi-bin/pdf2eps.rb [Fri Feb 10 03:57:02 2006] [error] [client 129.70.170.3] EOFError, referer: http://calvin.physik.uni-bielefeld.de/cgi-bin/pdf2eps.rb ----------------------------------------------------------- ------------- ORIGINAL MAILS ------------------------------------ konsu wrote on Thursday the 9th of February 2006: > i was just suggesting to mimic the web server's behaviour and simulate form > submission using environment variables since cgi relies just on these > variables and nothing more. to eliminate any apache misconfiguration. in my > earlier response i suggested to set REQUEST_METHOD=GET but to thest the form > submission you really need REQUEST_METHOD=POST and also you need to set the > corresponding variables that carry the request body. i do not remember the > names. the source for cgi.rb in the ruby lib directory might help you. > > konstantin > > > "Michael Schilling" wrote in message > news:20060208213611.GB16112@calvin.physik.uni-bielefeld.de... > > Hi, > > > > thank you for the suggestions. I set the variables as you suggested > > (with export REQUEST_URI=/path in Linux) but always only get the form. I > > don't exactly know how a string from the multipart_form looks like, so > > I couldn't really test the behaviour. > > > > When I use the normal form method, and do not set the variables as you > > suggested, I get the expected result and no error from > > 'myscript.rb filename=something'. With the multipart_form I also get no > > error, but also no output -> only the form. > > > > I also thought, that this problem might be a misconfiguration of apache, > > but a guy on #ruby-lang told me, that it is very unlikely when the > > normal form method works fine. > > > > So I can't reproduce the error without using the browser, but don't get > > the expected result either. > > > > > > Thanks for any help. > > > > > > Bye, Michael > > > > > > > > > > konsu wrote on thursday the 9th of February 2006: > > > >> try running your script from the command line. but set the url and method > >> environment variables first. on windows i do this: > >> > >> > >> set REQUEST_URI=/path > >> set REQUEST_METHOD=GET > >> ruby muscript.cgi . > > > >> > >> this will allow you to avoid dealing with apache for a while, and will > >> show > >> more errors too. > >> > >> konstantin > >> > >> > >> "Michael Schilling" wrote in message > >> news:20060208203511.GA16112@calvin.physik.uni-bielefeld.de... > >> > Hello everyone, > >> > > >> > I'm fairly new to ruby and wanted to write a very simple cgi program, > >> > which uploads a file and returns a converted file. Because it is so > >> > easy > >> > (and I want to learn about ruby) I tried to do it with the standard > >> > cgi.rb library. But I can't get the multipart_form (I need it for the > >> > file upload) to work right. Everything works fine using the normal form > >> > method, but with the multipart_form I always get an EOF Error 'no > >> > content body' in the apache log. As soon as there is any input from an > >> > multipart_form, an error occurs at: checkfile = CGI.new > >> > > >> > Below you'll find my code (I boiled it down to the important stuff) and > >> > the > >> > corresponding part of my apache2 error_log. I searched the web, several > >> > mailing lists and the ruby docs, but couldn't find an answer. I also > >> > asked on #ruby-lang in freenode, but no one there knew the cgi.rb > >> > library very well. > >> > > >> > Any help, on why this server error is happening, is very much > >> > appreciated. > >> > > >> > Bye, > >> > Michael <-- who hopes, that this is the right list for the question > >> > > >> > > >> > > >> > My code: > >> > ------------------------------------------------------------------ > >> > #!/usr/bin/ruby -w > >> > > >> > require "cgi" > >> > > >> > # make_form create a very simple html form > >> > def make_form > >> > make_form = CGI.new("html4") > >> > make_form.out { > >> > make_form.html { > >> > make_form.body { "\n" + > >> > # make_form.form{ "\n" + # everything works using the normal > >> > form > >> > make_form.multipart_form{ "\n" + # but not with > >> > multipart_form > >> > to upload a file > >> > make_form.file_field("filename",40,500000) + "\n" + > >> > make_form.br + > >> > make_form.submit("test it") + "\n" > >> > } > >> > } > >> > } > >> > } > >> > end > >> > > >> > # answer should show the filename of the uploaded file > >> > def answer(file_in) > >> > > >> > answer = CGI.new("html4") > >> > answer.out { > >> > answer.html { > >> > answer.body{ "\n" + > >> > answer.p{ "This should be the filename:" + file_in + "\n" } + > >> > answer.br > >> > } > >> > } > >> > } > >> > end > >> > > >> > > >> > > >> > begin > >> > checkfile = CGI.new > >> > parameter = checkfile['filename'] > >> > # parameter = checkfile.params['filename'][0] # this doesn't work > >> > either > >> > > >> > rescue > >> > answer("ERROR: " + $!.to_s) # send eror message to browser if possible > >> > end > >> > > >> > > >> > # create form if no data was send to the script, otherwise answer > >> > if parameter == "" > >> > make_form() > >> > else > >> > answer(parameter.to_s) > >> > end > >> > ------------------------------------------------------------------ > >> > > >> > > >> > Apache log: > >> > ------------------------------------------------------------------ > >> > [Wed Feb 08 21:08:57 2006] [error] [client 191.168.1.1] > >> > /usr/lib/ruby/1.8/cgi.rb:979:in `read_multipart', referer: > >> > http://localhost/cgi-bin/multiform-test.rb > >> > [Wed Feb 08 21:08:57 2006] [error] [client 191.168.1.1] : , referer: > >> > http://localhost/cgi-bin/multiform-test.rb > >> > [Wed Feb 08 21:08:57 2006] [error] [client 191.168.1.1] no content > >> > body, > >> > referer: http://localhost/cgi-bin/multiform-test.rb > >> > [Wed Feb 08 21:08:57 2006] [error] [client 191.168.1.1] (, referer: > >> > http://localhost/cgi-bin/multiform-test.rb > >> > [Wed Feb 08 21:08:57 2006] [error] [client 191.168.1.1] EOFError, > >> > referer: > >> > http://localhost/cgi-bin/multiform-test.rb > >> > [Wed Feb 08 21:08:57 2006] [error] [client 191.168.1.1] ), referer: > >> > http://localhost/cgi-bin/multiform-test.rb > >> > [Wed Feb 08 21:08:57 2006] [error] [client 191.168.1.1] \tfrom > >> > /usr/lib/ruby/1.8/cgi.rb:1104:in `initialize_query', referer: > >> > http://localhost/cgi-bin/multiform-test.rb > >> > [Wed Feb 08 21:08:57 2006] [error] [client 191.168.1.1] \tfrom > >> > /usr/lib/ruby/1.8/cgi.rb:2270:in `initialize', referer: > >> > http://localhost/cgi-bin/multiform-test.rb > >> > [Wed Feb 08 21:08:57 2006] [error] [client 191.168.1.1] \tfrom > >> > /home/httpd/cgi-bin/multiform-test.rb:25:in `answer', referer: > >> > http://localhost/cgi-bin/multiform-test.rb > >> > [Wed Feb 08 21:08:57 2006] [error] [client 191.168.1.1] \tfrom > >> > /home/httpd/cgi-bin/multiform-test.rb:52, referer: > >> > http://localhost/cgi-bin/multiform-test.rb > >> > [Wed Feb 08 21:08:57 2006] [error] [client 191.168.1.1] Premature end > >> > of > >> > script headers: multiform-test.rb, referer: > >> > http://localhost/cgi-bin/multiform-test.rb > >> > ------------------------------------------------------------------ > >> > > >> > > >> > > >> > -- > >> > Michael Schilling > >> > > >> > eMail : ruby@untiefe.de > >> > URL : http://glcu.sf.net/ > >> > > >> > > >> > "Change my name I remain the same." - Moloko