public
Fork of KirinDave/fuzed
Description: A new revision of Fuzed, the Erlang-based frontend for web apps.
Clone URL: git://github.com/mojombo/fuzed.git
Search Repo:
rails_node now works if bypassing chassis
mojombo (author)
Sat May 03 15:49:06 -0700 2008
commit  e94dbe65d31ac1835cec30358a5d7f0dd902f259
tree    2e959944054a59c8500d4e958089cd1835a6d871
parent  a2147a174b058b9be1b7f3cc7eb1e84050daf5e3
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
0
@@ -1 +1,183 @@
0
+# internal
0
+require 'chassis'
0
+require 'rails_adapter'
0
+
0
+# core
0
+require 'stringio'
0
+require 'logger'
0
+
0
+# gems
0
+require 'rack'
0
+
0
+rails_root = '/Users/tom/dev/mojombo/helloworld'
0
+
0
+require File.join(rails_root, 'config/boot')
0
+require RAILS_ROOT + "/config/environment"
0
+
0
+LOG = true
0
+
0
+$total_avg = [0, 0]
0
+$rails_avg = [0, 0]
0
+$logger = Logger.new(RAILS_ROOT + "/log/fuzed.#{Process.pid}.log")
0
+
0
+def log(msg)
0
+ $logger << msg + "\n"
0
+end
0
+
0
+$app = Rack::Adapter::Rails.new(:root => RAILS_ROOT)
0
+
0
+def service(request)
0
+ method = request[:method]
0
+ version = request[:http_version] # => e.g. [1, 1]
0
+ path = request[:querypath]
0
+ query = request[:querydata] == :undefined ? '' : request[:querydata]
0
+ server = request[:servername]
0
+ headers = request[:headers]
0
+ cookies = request[:cookies]
0
+ postdata = request[:postdata] == :undefined ? '' : request[:postdata]
0
+
0
+ translate = {:content_type => 'CONTENT_TYPE',
0
+ :content_length => 'CONTENT_LENGTH',
0
+ :accept => 'HTTP_ACCEPT',
0
+ :'Accept-Charset' => 'HTTP_ACCEPT_CHARSET',
0
+ :'Accept-Encoding' => 'HTTP_ACCEPT_ENCODING',
0
+ :'Accept-Language' => 'HTTP_ACCEPT_LANGUAGE',
0
+ :connection => 'HTTP_CONNECTION',
0
+ :keep_alive => 'HTTP_KEEP_ALIVE',
0
+ :host => 'HTTP_HOST',
0
+ :referer => 'HTTP_REFERER',
0
+ :user_agent => 'HTTP_USER_AGENT',
0
+ 'X-Prototype-Version' => 'HTTP_X_PROTOTYPE_VERSION',
0
+ 'X-Requested-With' => 'HTTP_X_REQUESTED_WITH'}
0
+
0
+ env = {}
0
+ env['REQUEST_METHOD'] = method.to_s
0
+ env['QUERY_STRING'] = query
0
+ env["PATH_INFO"] = path == '/' ? '' : path
0
+ env = headers.inject(env) { |a, x| a[translate[x[0]] || x[0].to_s] = x[1]; a }
0
+ env.delete_if { |k, v| v.nil? }
0
+
0
+ env.update({"rack.version" => [0, 1],
0
+ "rack.input" => StringIO.new(postdata),
0
+ "rack.errors" => STDERR,
0
+
0
+ "rack.multithread" => true,
0
+ "rack.multiprocess" => false,
0
+ "rack.run_once" => false,
0
+
0
+ "rack.url_scheme" => ["yes", "on", "1"].include?(ENV["HTTPS"]) ? "https" : "http"
0
+ })
0
+
0
+ env['SERVER_NAME'] = server.split(':')[0]
0
+ env['SERVER_PORT'] = server.split(':')[1]
0
+ env['HTTP_VERSION'] = version.join('.')
0
+
0
+ env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
0
+ env["QUERY_STRING"] ||= ""
0
+ env["REQUEST_PATH"] ||= "/"
0
+ env.delete "PATH_INFO" if env["PATH_INFO"] == ""
0
+
0
+ cookies.each do |cookie|
0
+ env["HTTP_COOKIE"] = cookie.to_s
0
+ end
0
+
0
+ log('------------IN------------') if LOG
0
+ log(env.inspect) if LOG
0
+
0
+ begin
0
+ t1 = Time.now
0
+
0
+ # status = '200'
0
+ # headers = {}
0
+ # body = ['foo']
0
+
0
+ status, headers, body = $app.call(env)
0
+
0
+ rails_delta = Time.now - t1
0
+ $rails_avg[0] += rails_delta
0
+ $rails_avg[1] += 1
0
+ log(">> Rails in #{rails_delta} (#{$rails_avg[0] / $rails_avg[1]} avg) sec") if LOG
0
+
0
+ html = ''
0
+ body.each do |part|
0
+ html << part
0
+ end
0
+
0
+ headers['Server'] = 'YAWS + Fuzed 0.0.1'
0
+ headers['Connection'] = 'close'
0
+
0
+ cookies = headers.delete('cookie')
0
+ #cookies.map! {|c| c.include?('path=') ? c : c + "; path=/"}
0
+ headers['Set-Cookie'] = cookies if cookies
0
+
0
+ # p headers
0
+
0
+ status = (headers["Status"].split(" ").first rescue nil) || status
0
+ headers.delete("Status") if headers["Status"]
0
+
0
+ res =
0
+ [:response,
0
+ [[:status, status.to_i],
0
+ [:allheaders, headers.inject([]) { |a, x| a += x[1].map { |y| [:header, x[0], y] } }],
0
+ [:html, html]]]
0
+ rescue => e
0
+ res =
0
+ [:response,
0
+ [[:status, 500],
0
+ [:allheaders, [
0
+ [:header, "Content-Type", "text/plain; charset=utf-8"],
0
+ [:header, "Cache-Control", "no-cache"]]],
0
+ [:html, "500 Internal Error\n\n#{e}\n\n#{e.backtrace}"]]]
0
+ end
0
+
0
+ log('-----------OUT------------') if LOG
0
+ log(res.inspect) if LOG
0
+
0
+ res
0
+end
0
+
0
+class RailsHandler < Chassis
0
+ kind "rails"
0
+
0
+ details("rails" => "default")
0
+
0
+ handle(:handle_request) do |args|
0
+ args[:request]
0
+
0
+ $app.call(env)
0
+ end
0
+end
0
+
0
+if true
0
+ # [[:method, :POST], [:http_version, [1, 1]], [:querypath, "/main/go"], [:querydata, ""], [:servername, "testing:8002"], [:headers, [[:connection, "keep-alive"], [:accept, "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"], [:host, "localhost:8002"], [:referer, "http://localhost:8002/main/ready"], [:user_agent, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"], [:keep_alive, "300"], [:content_length, "7"], [:content_type, "application/x-www-form-urlencoded"], [:"Cache-Control", "max-age=0"], [:"Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"], [:"Accept-Encoding", "gzip,deflate"], [:"Accept-Language", "en-us,en;q=0.5"]]], [:cookies, ["_helloworld_session_id=d3eae987aab3230377abc433b7a8d7c1"]], [:pathinfo, "/Users/tom/dev/fuzed/helloworld/public"], [:postdata, "val=foo"]]
0
+
0
+ req =
0
+ {:method => :POST,
0
+ :http_version => [1, 1],
0
+ :querypath => "/main/go",
0
+ :querydata => "",
0
+ :servername => "testing:8002",
0
+ :headers => {:connection => "keep-alive",
0
+ :accept => "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
0
+ :host => "localhost:8002",
0
+ :referer => "http://localhost:8002/main/ready",
0
+ :user_agent => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3",
0
+ :keep_alive => "300",
0
+ :content_length => "7",
0
+ :content_type => "application/x-www-form-urlencoded",
0
+ :"Cache-Control" => "max-age=0",
0
+ :"Accept-Charset" => "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
0
+ :"Accept-Encoding" => "gzip,deflate",
0
+ :"Accept-Language" => "en-us,en;q=0.5"},
0
+ :cookies => ["_helloworld_session_id=d3eae987aab3230377abc433b7a8d7c1"],
0
+ :pathinfo => "/Users/tom/dev/fuzed/helloworld/public",
0
+ :postdata => "val=foo"}
0
+
0
+ # [[:method, :GET], [:http_version, [1, 1]], [:querypath, "/main/say"], [:querydata, ""], [:servername, "testing:8002"], [:headers, [[:connection, "keep-alive"], [:accept, "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"], [:host, "localhost:8002"], [:user_agent, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"], [:keep_alive, "300"], [:"Cache-Control", "max-age=0"], [:"Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"], [:"Accept-Encoding", "gzip,deflate"], [:"Accept-Language", "en-us,en;q=0.5"]]], [:cookies, ["_helloworld_session_id=166098a3c3f702698d0529c6148c6164"]], [:pathinfo, "/Users/tom/dev/fuzed/helloworld/public"], [:postdata, :undefined]]
0
+
0
+ p service(req)
0
+ p service(req)
0
+ p service(req)
0
+ exit!
0
+end

Comments

    No one has commented yet.