g_thang 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Paul Barry
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,9 @@
1
+ This is a very simple Rack-Compliant HTTP Server based on GServer. See the `examples` directory to see how to run a couple of small rack apps. To run a Rails 2.3 app, in the root of your Rails app, run these commands:
2
+
3
+ $ gem install g_thang
4
+ $ script/generate g_thang
5
+ $ script/g_thang
6
+
7
+ The purpose of this server is just to [illustrate how Rack and GServer work][blog].
8
+
9
+ [blog]: http://paulbarry.com/articles/2009/10/09/aint-nothing-but-a-g-thang
@@ -0,0 +1,4 @@
1
+ require 'g_thang/http_server'
2
+
3
+ require 'rack/handler'
4
+ Rack::Handler.register("g_thang", "GThang::HttpServer")
@@ -0,0 +1,40 @@
1
+ require 'gserver'
2
+ require 'cgi'
3
+ require 'g_thang/rack_handler'
4
+
5
+ module GThang
6
+ class HttpServer < GServer
7
+
8
+ attr_reader :port, :rack_app
9
+
10
+ def initialize(options={})
11
+ @port = options[:Port] || 8080
12
+ @rack_app = options[:rack_app]
13
+ super(@port)
14
+ end
15
+
16
+ def serve(io)
17
+ RackHandler.new(io, rack_app, port).handle_request
18
+ rescue Exception => ex
19
+ response = "HTTP/1.1 500 Internal Server Error\r\n"
20
+ response << "Connection: close\r\n"
21
+ response << "Content-Type: text/html\r\n\r\n"
22
+ response << "<html><head><title>Internal Server Error</title></head><body>\n"
23
+ response << "<h1>Internal Server Error</h1>\n<pre>\n"
24
+ response << h("#{ex.class}: #{ex.message}\n#{ex.backtrace.join("\n ")}\n")
25
+ response << "</pre></body></html>"
26
+ io << response
27
+ end
28
+
29
+ def h(str)
30
+ CGI::escapeHTML(str.to_s)
31
+ end
32
+
33
+ def self.run(app, options={})
34
+ server = new({:rack_app => app}.merge(options))
35
+ server.start
36
+ server.join
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,120 @@
1
+ require 'rubygems'
2
+ require 'rack/rewindable_input'
3
+ require 'socket'
4
+
5
+ module GThang
6
+ class RackHandler
7
+
8
+ StatusCodeMapping = {
9
+ 200 => "OK",
10
+ 400 => "Bad Request",
11
+ 403 => "Forbidden",
12
+ 405 => "Method Not Allowed",
13
+ 411 => "Length Required",
14
+ 500 => "Internal Server Error"
15
+ }
16
+
17
+ DefaultResponseHeaders = {
18
+ "Connection" => "close",
19
+ "Content-Type" => "text/html"
20
+ }
21
+
22
+ attr_reader :socket, :app, :env, :port
23
+
24
+ def initialize(socket, app, port)
25
+ @socket = socket
26
+ @app = app
27
+ @env = {
28
+ "GATEWAY_INTERFACE" => "CGI/1.1",
29
+ "SCRIPT_NAME" => "",
30
+ "SERVER_NAME" => Socket.gethostname,
31
+ "SERVER_PORT" => port.to_s
32
+ }
33
+ end
34
+
35
+ def handle_request
36
+ return unless add_rack_variables_to_env
37
+ return unless add_connection_info_to_env
38
+ return unless add_request_line_info_to_env
39
+ return unless add_headers_to_env
40
+ send_response(app.call(env))
41
+ end
42
+
43
+ protected
44
+ def add_rack_variables_to_env
45
+ env["rack.version"] = [1,0]
46
+ env["rack.url_scheme"] = "http"
47
+ env["rack.input"] = Rack::RewindableInput.new(socket)
48
+ env["rack.errors"] = $stderr
49
+ env["rack.multithread"] = true
50
+ env["rack.multiprocess"] = true
51
+ env["rack.run_once"] = false
52
+ true
53
+ end
54
+
55
+ def add_connection_info_to_env
56
+ env["REMOTE_ADDR"] = socket.addr[3]
57
+ env["REMOTE_HOST"] = socket.addr[2]
58
+ true
59
+ end
60
+
61
+ def add_request_line_info_to_env
62
+ if m = socket.gets.match(/^(\S+)\s+(\S+)\s+(\S+)/)
63
+ env["REQUEST_METHOD"] = m[1].to_s.upcase
64
+ path_info, query_string = m[2].to_s.split('?')
65
+ env["PATH_INFO"] = path_info.to_s
66
+ env["QUERY_STRING"] = query_string.to_s
67
+ env["HTTP_VERSION"] = env["SERVER_PROTOCOL"] = m[3].to_s.upcase
68
+ true
69
+ else
70
+ send_response(400)
71
+ false
72
+ end
73
+ end
74
+
75
+ def add_headers_to_env
76
+ while (line=socket.gets) !~ /^(\n|\r)/
77
+ if m = line.match(/^([\w-]+):\s*(.*)$/)
78
+ env["HTTP_#{m[1].to_s.gsub(/-/,'_').upcase}"] = m[2]
79
+ end
80
+ end
81
+ env["SERVER_NAME"] = env["HTTP_HOST"] if env["HTTP_HOST"]
82
+ true
83
+ end
84
+
85
+ def send_response(response)
86
+ case response
87
+ when Integer
88
+ socket << status_line(response)
89
+ when Array
90
+ socket << http_response(response)
91
+ end
92
+ end
93
+
94
+ def status_line(response_code)
95
+ response_message = StatusCodeMapping[response_code.to_i]
96
+ unless response_message
97
+ response_code = 500
98
+ response_message = StatusCodeMapping[response_code.to_i]
99
+ end
100
+ "HTTP/1.1 #{response_code} #{response_message}\r\n"
101
+ end
102
+
103
+ # Expects response to be an Array that conforms to the Rack Spec
104
+ # [status_code (Integer), headers (Hash), body (Enumerable-ish)]
105
+ # See: http://rack.rubyforge.org/doc/SPEC.html
106
+ def http_response(response)
107
+ status_code, headers, body = response
108
+ http_response = status_line(status_code)
109
+ DefaultResponseHeaders.merge(headers).each do |k,v|
110
+ http_response << "#{k}: #{v}\r\n"
111
+ end
112
+ http_response << "\r\n"
113
+ body.each do |s|
114
+ http_response << s
115
+ end
116
+ http_response
117
+ end
118
+
119
+ end
120
+ end
@@ -0,0 +1,7 @@
1
+ Description:
2
+ Adds the script/g_thang script to your Rails project
3
+ so you can run your Rails app on G-Thang, baby.
4
+ After you run this, you just run script/g_thang to start the app.
5
+
6
+ Examples:
7
+ `./script/generate g_thang`
@@ -0,0 +1,7 @@
1
+ class GThangGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.file 'g_thang.rb', 'script/g_thang', { :chmod => 0755 }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ require File.dirname(__FILE__) + '/../config/boot'
3
+ require 'rubygems'
4
+ require 'g_thang'
5
+ ARGV[0] = 'g_thang'
6
+ require 'commands/server'
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: g_thang
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Paul Barry
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-09 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A Rack-Compliant HTTP Server Implemented with GServer
17
+ email: mail@paulbarry.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.mdown
25
+ files:
26
+ - lib/g_thang.rb
27
+ - lib/g_thang/http_server.rb
28
+ - lib/g_thang/rack_handler.rb
29
+ - rails_generators/g_thang/USAGE
30
+ - rails_generators/g_thang/g_thang_generator.rb
31
+ - rails_generators/g_thang/templates/g_thang.rb
32
+ - LICENSE
33
+ - README.mdown
34
+ has_rdoc: true
35
+ homepage: http://github.com/pjb3/g_thang
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --charset=UTF-8
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.3.5
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: A Rack-Compliant HTTP Server Implemented with GServer
62
+ test_files: []
63
+