Skip to content

Commit

Permalink
Synchronizing camping.rb and camping-unabridged.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
judofyr committed May 24, 2008
1 parent e235c2b commit 95d2262
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 65 deletions.
41 changes: 18 additions & 23 deletions lib/camping-unabridged.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
71 changes: 29 additions & 42 deletions lib/camping.rb
@@ -1,41 +1,34 @@
%w[tempfile uri].map{|l|require l};class Object;def meta_def m,&b;(class<<self
self end).send:define_method,m,&b end end;module Camping;C=self
S=IO.read(__FILE__)rescue nil;P="<h1>Cam\\ping Problem!</h1><h2>%s</h2>"
%w[tempfile uri rack].map{|l|require l};class Object;def meta_def m,&b
(class<<self;self end).send:define_method,m,&b end end;module Camping;C=self
S=IO.read(__FILE__)rescue nil;P="<h1>Cam\ping Problem!</h1><h2>%s</h2>"
class H<Hash
def method_missing m,*a;m.to_s=~/=$/?self[$`]=a[0]:a==[]?self[m.to_s]:super end
alias u merge!;undef id,type;end;module Helpers;def R c,*g
p,h=/\(.+?\)/,g.grep(Hash);g-=h;raise"bad route"unless u=c.urls.find{|x|
break x if x.scan(p).size==g.size&&/^#{x}\/?$/=~(x=g.inject(x){|x,a|
x.sub p,C.escape((a[a.class.primary_key]rescue a))})}
h.any?? u+"?"+h[0].map{|x|x.map{|z|C.escape z}*"="}*"&":u end;def / p
x.sub p,Rack::Utils.escape((a[a.class.primary_key]rescue a))})}
h.any?? u+"?"+h[0].map{|x|x.map{|z|Rack::Utils.escape z}*"="}*"&":u end;def / p
p[/^\//]?@root+p:p end;def URL c='/',*a;c=R(c, *a) if c.respond_to?:urls
c=self/c;c="//"+@env.HTTP_HOST+c if c[/^\//];URI c end end;module Base
attr_accessor:input,:cookies,:env,:headers,:body,:status,:root;Z="\r\n"
c=self/c;c=@request.url.split("/",4)[0..-2].join("/")+c if c[/^\//];URI c end
end;module Base;attr_accessor:input,:cookies,:headers,:body,:status,:root
def method_missing *a,&b;a.shift if a[0]==:render;m=Mab.new({},self)
s=m.capture{send(*a,&b)};s=m.capture{send(:layout){s}}if/^_/!~a[0].to_s and
m.respond_to?:layout;s end;def r s,b,h={};@status=s;headers.u(h);@body=b
end;def redirect *a;r 302,'','Location'=>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}"+"<h3>#{x.class} #{x.message}: <ul>#{x.
backtrace.map{|b|"<li>#{b}</li>"}}</ul></h3>"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<<l)=~b;o<<$`.chomp
@in.seek(-$'.size,IO::SEEK_CUR);break end;o<<k.slice!(0...s);l=@in.read(s) end
C.qsp(fn,'&;',fh,q)if fn;fh.tempfile.rewind if fh.is_a?H end;when
"application/x-www-form-urlencoded": q.u(C.qsp(@in.read))end
@cookies,@input=@k.dup,q.dup end;def service *a;@body=send @method,*a
headers['Set-Cookie']=cookies.map{|k,v|"#{k}=#{C.escape(v)}; path=#{self/
"/"}"if v!=@k[k]}-[nil];self end;def to_s;"Status: #@status#{Z+(headers.inject([
]){|a,o|[*o[1]].map{|v|a<<[o[0],v]*": "if v&&v.to_s.any?};a}*Z)+Z+Z}#@body"end
@response.to_a;end;def initialize(env)
@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{|k,v|if k[-2..-1]=="[]";@input[k[0..-3]]=
@input.delete(k)elsif k=~/(.*)\[([^\]])\]$/;(@input[$1]||={})[$2]=
@input.delete(k)end};end;def service *a;o=@cookies.dup;@response.body=
(send(@request.request_method.downcase,*a)||@body);@response.status=@status;
@response.headers.merge!(@headers);@cookies.each{|k,v|@response.
set_cookie(k,v)unless(o[k]==v)};self;end
end;X=module Controllers;@r=[];class<<self;def r;@r end;def R *u;r=@r
Class.new{meta_def(:urls){u};meta_def(:inherited){|x|r<<x}}end
def D p,m;r.map{|k|k.urls.map{|x|return(k.instance_method(m)rescue nil)?
Expand All @@ -44,18 +37,12 @@ def D p,m;r.map{|k|k.urls.map{|x|return(k.instance_method(m)rescue nil)?
k.send:include,C,Base,Helpers,Models;@r=[k]+r if r-[k]==r
k.meta_def(:urls){["/#{c.downcase}"]}if !k.respond_to?:urls}end end;class I<R()
end;self end;class<<self;def goes m
eval S.gsub(/Camping/,m.to_s),TOPLEVEL_BINDING end;def escape s
s.to_s.gsub(/[^ \w.-]+/n){'%'+($&.unpack('H2'*$&.size)*'%').upcase}.tr' ','+'
end;def un s;s.tr('+',' ').gsub(/%([\da-f]{2})/in){[$1].pack'H*'}end
def qsp q,d='&;',y=nil,z=H[];m=proc{|_,o,n|o.u(n,&m)rescue([*o]<<n)}
(q.to_s.split(/[#{d}]+ */n)-[""]).inject((b,z=z,H[])[0]){|h,p|k,v=un(p).
split'=',2;h.u k.split(/[\]\[]+/).reverse.inject(y||v){|x,i|H[i,x]},&m}end
def kp s;c=qsp s,';,'end;def run r=$stdin,e=ENV;X.M;e=H[e.to_hash];k,m,*a=X.D e.
PATH_INFO=un("/#{e.PATH_INFO}".gsub(/\/+/,'/')),
(e.REQUEST_METHOD||'get').downcase
k.new(r,e,m).Y.service(*a);rescue=>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

0 comments on commit 95d2262

Please sign in to comment.