From: Simon Kitching Date: 2003-11-03T14:29:08+09:00 Subject: Re: CGI uses file size to distinguish between regular values and files Hi David, I haven't used Ruby's file upload facility, but since no-one else has replied I'll chip in with my $0.02.. I suspect that the fact that tempfiles are used by this module is a private implementation detail, ie something that users are not supposed to be aware of, or depend upon. Are you sure there isn't a public API provided to access the uploaded/downloaded data "correctly", and that you aren't sneaking around the back way by trying to directly access the temp file created? If your code depends upon "private" implementation details of any library, then you are asking for trouble as the implementer is perfectly within their rights to change private implementation details in any point release; only "public" APIs can be relied upon to remain stable across releases. Yes, you may be able to optimise your code by accessing private APIs of a module, but then you can't expect the module author to care if your code breaks. The kind of optimisation you describe is fairly common when dealing with data flowing across networks; process it in-memory if it is small else use the filesystem as a data cache. And this is a *private* implementation detail that should not be exposed to the user of the module. Of course I haven't looked at CGI.rb's interface and may be off-track here. And because Ruby's documentation standards are generally poor, it may not immediately be obvious which are the public APIs and which the private ones if the author has been a little slack about using the public/protected/private keywoards... Regards, Simon On Mon, 2003-11-03 at 07:03, David Heinemeier Hansson wrote: > I've been having a ton of problems handling file uploads with CGI.rb > and after diving into the code, it looks like my woes can be narrowed > down to these lines in the read_multipart method: > > if 10240 < content_length > require "tempfile" > body = Tempfile.new("CGI") > else > begin > require "stringio" > body = StringIO.new > rescue LoadError > require "tempfile" > body = Tempfile.new("CGI") > end > end > > It sure looks like the sole differentiator on whether a request should > be treated with StringIOs or Tempfiles is the size of the request. If > this understanding is correct, I have no trouble understanding why I've > been pulling out my hair in frustration the last couple or many hours. > > If I upload just one file that brings the content_length above 10240, > ALL fields in the request is treated as Tempfiles. If I'm uploading a > small file that keeps the content_length below 10240, all the fields -- > including the file -- is treated as StringIOs. > > For starters, how would one move a small file, treated as StringIO, to > another location? With Tempfile, you just do Tempfile#local_path, and > move away. But it's not so straight forward with StringIO. > > It also seems very tedious that the application has to know whether the > file upload was big or small and act accordingly. > > Maybe I'm just missing something. Like that StringIO and Tempfile is > supposed to act the same and they just don't on my system for some > reason. Or this is a bug. > > Since my last cries for help haven't been overly succesful, I gather > that most consider CGI.rb to be a black box as well. So perhaps I > should get in touch with the original author. Does anyone know if Wakou > Aoyama is still actively maintaining CGI.rb? > -- > David Heinemeier Hansson. > http://www.loudthinking.com/ -- Broadcasting Brain > > >