From 95d2262ca660ff5fa48027dd841f4c0c63aa7717 Mon Sep 17 00:00:00 2001 From: Magnus Holm Date: Sat, 24 May 2008 18:39:06 +0200 Subject: [PATCH] Synchronizing camping.rb and camping-unabridged.rb --- lib/camping-unabridged.rb | 41 ++++++++++------------ lib/camping.rb | 71 ++++++++++++++++----------------------- 2 files changed, 47 insertions(+), 65 deletions(-) diff --git a/lib/camping-unabridged.rb b/lib/camping-unabridged.rb index 338609f..3eceae1 100644 --- a/lib/camping-unabridged.rb +++ b/lib/camping-unabridged.rb @@ -329,11 +329,7 @@ def method_missing(*a,&b) # # See also: #r404, #r500 and #r501 def r(s, b, h = {}) - if Hash===b - t = b.dup - b = h.dup - h = t - end + Hash===b && (b, h = h, b) @status = s @headers.merge!(h) @body = b @@ -408,21 +404,20 @@ def to_a end def initialize(env) #:nodoc: - @request = Rack::Request.new(env) - @root = @request.script_name.sub(/\/$/,'') - @input = H[@request.params] - @cookies = H[@request.cookies] - - @response = Rack::Response.new - @headers = @response.headers - @body = @response.body - @status = @response.status + @request, @root, + @input, @cookies, + @response, @headers, + @body, @status = + Rack::Request.new(env), @request.script_name.sub(/\/$/,''), + H[@request.params], H[@request.cookies], + Rack::Response.new, @response.headers, + @response.body, @response.status - @input.each do |key, value| - if key[-2..-1] == "[]" - @input[key[0..-3]] = @input.delete(key) - elsif key =~ /(.*)\[([^\]])\]$/ - (@input[$1] ||= {})[$2] = @input.delete(key) + @input.each do |k, v| + if k[-2..-1] == "[]" + @input[k[0..-3]] = @input.delete(k) + elsif k =~ /(.*)\[([^\]])\]$/ + (@input[$1] ||= {})[$2] = @input.delete(k) end end end @@ -437,8 +432,8 @@ def service(*a) @response.body = send(@request.request_method.downcase, *a) || @body @response.status = @status @response.headers.merge!(@headers) - @cookies.each do |key, value| - @response.set_cookie(key, value) if o[key] != value + @cookies.each do |k, v| + @response.set_cookie(k, v) if o[k] != v end self end @@ -562,9 +557,9 @@ def goes(m) eval S.gsub(/Camping/,m.to_s), TOPLEVEL_BINDING end - def call(env) + def call(e) X.M - e = H[env.to_hash] + e = H[e.to_hash] k,m,*a=X.D e.PATH_INFO,(e.REQUEST_METHOD||'get').downcase e.REQUEST_METHOD = m k.new(e).service(*a).to_a diff --git a/lib/camping.rb b/lib/camping.rb index 1377d0f..9787c42 100644 --- a/lib/camping.rb +++ b/lib/camping.rb @@ -1,41 +1,34 @@ -%w[tempfile uri].map{|l|require l};class Object;def meta_def m,&b;(class<URL(*a)end;def r404 p=env.PATH -r 404,P%"#{p} not found"end;def r500 k,m,x +m.respond_to?:layout;s end;def r s,b,h={};(Hash===b&&(b,h=h,b));@status=s; +@headers.merge!(h);@body=b;end;def redirect *a;r 302,'','Location'=>URL(*a). +to_s;end;def r404 p=env.PATH;r 404,P%"#{p} not found"end;def r500 k,m,x r 500,P%"#{k}.#{m}"+"

#{x.class} #{x.message}:
    #{x. backtrace.map{|b|"
  • #{b}
  • "}}

"end;def r501 m=@method r 501,P%"#{m.upcase} not implemented"end;def to_a -[status,body,headers]end;def initialize r,e,m;@status,@method,@env,@headers, -@root=200,m,e,H['Content-Type','text/html'],e.SCRIPT_NAME.sub(/\/$/,'') -@k=C.kp e.HTTP_COOKIE;q=C.qsp e.QUERY_STRING;@in=r;case e.CONTENT_TYPE -when%r|\Amultipart/form-.*boundary=\"?([^\";,]+)|n -b=/(?:\r?\n|\A)#{Regexp.quote"--#$1"}(?:--)?\r$/;until@in.eof?;fh=H[] -for l in@in;case l;when Z;break;when/^Content-D.+?: 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;o=fh.tempfile=Tempfile.new(:C);o.binmode;else;fh="";end;s=8192 -k='';l=@in.read(s*2);while l;if(k<x;X::I.new(r,e,'r500').service k,m,x -end;def method_missing m,c,*a;X.M;k=X.const_get(c).new StringIO.new, -H['HTTP_HOST','','SCRIPT_NAME','','HTTP_COOKIE',''],m.to_s -H[a.pop].each{|e,f|k.send"#{e}=",f}if Hash===a[-1];k.service(*a)end end -module Views;include X,Helpers end;module Models;autoload:Base,'camping/db' -def Y;self;end end;autoload:Mab,'camping/mab'end +eval S.gsub(/Camping/,m.to_s),TOPLEVEL_BINDING end;def call e +X.M;e=H[e.to_hash];k,m,*a=X.D e.PATH_INFO,(e.REQUEST_METHOD||'get').downcase +e.REQUEST_METHOD=m;k.new(e).service(*a).to_a;rescue => x +e.REQUEST_METHOD='r500';X::I.new(e).service(k,m,x).to_a;end +def method_missing m,c,*a;X.M;h=Hash===a[-1]?H[a.pop]:{};e= +H[h[:env]||{}].u({'rack.input'=>StringIO.new,'REQUEST_METHOD'=>m.to_s}) +k=X.const_get(c).new(H[e]);k.send("input=",h[:input])if h[:input] +k.service(*a);end;end;module Views;include X,Helpers end;module Models +autoload:Base,'camping/db';def Y;self;end end;autoload:Mab,'camping/mab'end \ No newline at end of file