Skip to content

Commit

Permalink
* lib/camping.rb: efficient multipart forms, at last.
Browse files Browse the repository at this point in the history
 * lib/camping-unabridged.rb: ditto.
 * camping.gemspec: moved to Rakefile.
  • Loading branch information
_why committed May 6, 2006
1 parent f808b4f commit 4bf0b27
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 52 deletions.
33 changes: 0 additions & 33 deletions camping.gemspec

This file was deleted.

41 changes: 28 additions & 13 deletions lib/camping-unabridged.rb
Original file line number Diff line number Diff line change
Expand Up @@ -357,23 +357,38 @@ def initialize(r, e, m) #:nodoc:
qs = C.qs_parse(e.QUERY_STRING)
@in = r
if %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)|n.match(e.CONTENT_TYPE)
b = "--#$1"
@in.read.split(/(?:\r?\n|\A)#{ Regexp::quote( b ) }(?:--)?\r\n/m).each { |pt|
h,v=pt.split("\r\n\r\n",2);fh={}
[:name, :filename].each { |x|
fh[x] = $1 if h =~ /^Content-Disposition: form-data;.*(?:\s#{x}="([^"]+)")/m
}
fn = fh[:name]
if fh[:filename]
fh[:type]=$1 if h =~ /^Content-Type: (.+?)(\r\n|\Z)/m
fh[:tempfile]=Tempfile.new(:C).instance_eval {binmode;write v;rewind;self}
b = /(?:\r?\n|\A)#{Regexp::quote("--#$1")}(?:--)?\r$/
until @in.eof?
fh=H[]
for l in @in
case l
when "\r\n": break
when /^Content-Disposition: form-data;/
fh.u H[*$'.scan(/(?:\s(\w+)="([^"]+)")/).flatten]
when /^Content-Type: (.+?)(\r$|\Z)/m
puts "=> fh[type] = #$1"
fh[:type] = $1
end
end
fn=fh[:name]
o=if fh[:filename]
fh[:tempfile]=Tempfile.new(:C).binmode
else
fh=v
fh=""
end
while l=@in.read(16384)
if l=~b
o<<$`.chomp
@in.seek(-$'.size,IO::SEEK_CUR)
break
end
o<<l
end
qs[fn]=fh if fn
}
fh[:tempfile].rewind if fh.is_a?H
end
elsif @method == "post"
qs.u(C.qs_parse(@in.read))
qs.merge!(C.qs_parse(@in.read))
end
@cookies, @input = @k.dup, qs.dup
end
Expand Down
13 changes: 7 additions & 6 deletions lib/camping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ module Base;include Helpers;attr_accessor :input,:cookies,:env,:headers,:body,
@status,@method,@env,@headers,@root=200,m.downcase,e,{'Content-Type'=>"text/htm\
l"},e.SCRIPT_NAME.sub(/\/$/,'');@k=C.kp e.HTTP_COOKIE;q=C.qs_parse e.QUERY_STRING
@in=r;if %r|\Amultipart/form-.*boundary=\"?([^\";,]+)|n.match e.CONTENT_TYPE;b=
"--#$1";@in.read.split(/(?:\r?\n|\A)#{Regexp.quote b}(?:--)?\r\n/m).map{|y|h,v=y.
split "\r\n\r\n",2;fh={};[:name,:filename].map{|x|fh[x]=$1 if h=~/^Content-Di.+\
: form-data;.*(?:\s#{x}="([^"]+)")/m};fn=fh[:name];if fh[:filename];fh[:type]=
$1 if h=~/^Content-Type: (.+?)(\r\n|\Z)/m;fh[:tempfile]=Tempfile.new(:C).
instance_eval{binmode;write v;rewind;self}else;fh=v end;q[fn]=fh if fn}elsif
@method=="post";q.u C.qs_parse(@in.read) end;@cookies,@input=
/(?:\r?\n|\A)#{Regexp::quote("--#$1")}(?:--)?\r$/;until @in.eof?;fh=H[];for l in
@in;case l;when "\r\n":break;when /^Content-Disposition: form-data;/:fh.u H[*$'.
scan(/(?:\s(\w+)="([^"]+)")/).flatten];when /^Content-Type: (.+?)(\r$|\Z)/m;fh[
:type]=$1;end;end;fn=fh[:name];o=if fh[:filename];fh[:tempfile]=Tempfile.new(:C).
binmode;else;fh="";end;while l=@in.read(16384);if l=~b;o<<$`.chomp;@in.seek(-$'.
size,IO::SEEK_CUR);break;end;o<<l;end;q[fn]=fh if fn;fh[:tempfile].rewind if
fh.is_a?H;end;elsif @method=="post";q.u C.qs_parse(@in.read) end;@cookies,@input=
@k.dup,q.dup end;def service *a;@body=send(@method,*a)if respond_to?@method
@headers["Set-Cookie"][email protected]{|k,v|"#{k}=#{C.escape(v)}; path=#{self/"/"}\
" if v != @k[k]}.compact;self end;def to_s;"Status: #{@status}\n#{@headers.map{
Expand Down

0 comments on commit 4bf0b27

Please sign in to comment.