thin 1.8.0 → 2.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of thin might be problematic. Click here for more details.

Files changed (108) hide show
  1. data/.gitignore +9 -0
  2. data/CHANGELOG +29 -107
  3. data/Gemfile +8 -0
  4. data/README.md +44 -78
  5. data/Rakefile +28 -18
  6. data/bin/thin +4 -4
  7. data/examples/async.ru +21 -0
  8. data/examples/thin.conf.rb +39 -0
  9. data/lib/thin.rb +2 -44
  10. data/lib/thin/async.rb +108 -0
  11. data/lib/thin/backends/prefork.rb +44 -0
  12. data/lib/thin/backends/single_process.rb +28 -0
  13. data/lib/thin/chunked_body.rb +28 -0
  14. data/lib/thin/configurator.rb +118 -0
  15. data/lib/thin/connection.rb +246 -172
  16. data/lib/thin/listener.rb +114 -0
  17. data/lib/thin/request.rb +94 -76
  18. data/lib/thin/response.rb +112 -45
  19. data/lib/thin/runner.rb +134 -197
  20. data/lib/thin/server.rb +203 -252
  21. data/lib/thin/system.rb +49 -0
  22. data/lib/thin/version.rb +11 -26
  23. data/man/index.txt +3 -0
  24. data/man/thin-conf.5.ronn +121 -0
  25. data/man/thin.1.ronn +105 -0
  26. data/site/.gitignore +2 -0
  27. data/site/README.md +21 -0
  28. data/site/Rakefile +20 -0
  29. data/site/config.ru +4 -0
  30. data/site/public/images/grid.png +0 -0
  31. data/site/public/javascripts/dd_belatedpng.js +13 -0
  32. data/site/public/javascripts/modernizr-1.6.min.js +30 -0
  33. data/site/public/man/thin-conf.5.html +220 -0
  34. data/site/public/man/thin.1.html +177 -0
  35. data/site/site/assets/javascripts/main.coffee +2 -0
  36. data/site/site/assets/stylesheets/_config.scss +55 -0
  37. data/site/site/assets/stylesheets/main.scss +24 -0
  38. data/site/site/helpers.rb +17 -0
  39. data/site/site/layouts/base.erb +55 -0
  40. data/site/site/layouts/default.erb +17 -0
  41. data/site/site/pages/about.md +5 -0
  42. data/site/site/pages/index.erb +10 -0
  43. data/site/site/partials/.gitkeep +0 -0
  44. data/test/fixtures/big.txt +1 -0
  45. data/test/fixtures/small.txt +1 -0
  46. data/test/fixtures/thin.conf.rb +15 -0
  47. data/test/integration/async_test.rb +35 -0
  48. data/test/integration/big_request_test.rb +30 -0
  49. data/test/integration/config.ru +57 -0
  50. data/test/integration/daemonize_test.rb +26 -0
  51. data/test/integration/env_test.rb +44 -0
  52. data/test/integration/error_test.rb +37 -0
  53. data/test/integration/file_sending_test.rb +24 -0
  54. data/test/integration/keep_alive_test.rb +35 -0
  55. data/test/integration/robustness_test.rb +37 -0
  56. data/test/integration/single_process_test.rb +15 -0
  57. data/test/integration/socket_family_test.rb +38 -0
  58. data/test/integration/worker_test.rb +22 -0
  59. data/test/test_helper.rb +195 -0
  60. data/test/unit/configurator_test.rb +43 -0
  61. data/test/unit/connection_test.rb +94 -0
  62. data/test/unit/listener_test.rb +74 -0
  63. data/test/unit/request_test.rb +74 -0
  64. data/test/unit/response_test.rb +90 -0
  65. data/test/unit/server_test.rb +29 -0
  66. data/test/unit/system_test.rb +17 -0
  67. data/thin.gemspec +26 -0
  68. data/v2.todo +21 -0
  69. metadata +138 -93
  70. checksums.yaml +0 -7
  71. data/example/adapter.rb +0 -32
  72. data/example/async_app.ru +0 -126
  73. data/example/async_chat.ru +0 -247
  74. data/example/async_tailer.ru +0 -100
  75. data/example/config.ru +0 -22
  76. data/example/monit_sockets +0 -20
  77. data/example/monit_unixsock +0 -20
  78. data/example/myapp.rb +0 -1
  79. data/example/ramaze.ru +0 -12
  80. data/example/thin.god +0 -80
  81. data/example/thin_solaris_smf.erb +0 -36
  82. data/example/thin_solaris_smf.readme.txt +0 -150
  83. data/example/vlad.rake +0 -72
  84. data/ext/thin_parser/common.rl +0 -59
  85. data/ext/thin_parser/ext_help.h +0 -14
  86. data/ext/thin_parser/extconf.rb +0 -6
  87. data/ext/thin_parser/parser.c +0 -1447
  88. data/ext/thin_parser/parser.h +0 -49
  89. data/ext/thin_parser/parser.rl +0 -152
  90. data/ext/thin_parser/thin.c +0 -435
  91. data/lib/rack/adapter/loader.rb +0 -75
  92. data/lib/rack/adapter/rails.rb +0 -178
  93. data/lib/rack/handler/thin.rb +0 -38
  94. data/lib/thin/backends/base.rb +0 -169
  95. data/lib/thin/backends/swiftiply_client.rb +0 -56
  96. data/lib/thin/backends/tcp_server.rb +0 -34
  97. data/lib/thin/backends/unix_server.rb +0 -56
  98. data/lib/thin/command.rb +0 -53
  99. data/lib/thin/controllers/cluster.rb +0 -178
  100. data/lib/thin/controllers/controller.rb +0 -189
  101. data/lib/thin/controllers/service.rb +0 -76
  102. data/lib/thin/controllers/service.sh.erb +0 -39
  103. data/lib/thin/daemonizing.rb +0 -199
  104. data/lib/thin/headers.rb +0 -40
  105. data/lib/thin/logging.rb +0 -174
  106. data/lib/thin/stats.html.erb +0 -216
  107. data/lib/thin/stats.rb +0 -52
  108. data/lib/thin/statuses.rb +0 -48
@@ -1,76 +0,0 @@
1
- require 'erb'
2
-
3
- module Thin
4
- module Controllers
5
- # System service controller to launch all servers which
6
- # config files are in a directory.
7
- class Service < Controller
8
- INITD_PATH = File.directory?('/etc/rc.d') ? '/etc/rc.d/thin' : '/etc/init.d/thin'
9
- DEFAULT_CONFIG_PATH = '/etc/thin'
10
- TEMPLATE = File.dirname(__FILE__) + '/service.sh.erb'
11
-
12
- def initialize(options)
13
- super
14
-
15
- raise PlatformNotSupported, 'Running as a service only supported on Linux' unless Thin.linux?
16
- end
17
-
18
- def config_path
19
- @options[:all] || DEFAULT_CONFIG_PATH
20
- end
21
-
22
- def start
23
- run :start
24
- end
25
-
26
- def stop
27
- run :stop
28
- end
29
-
30
- def restart
31
- run :restart
32
- end
33
-
34
- def install(config_files_path=DEFAULT_CONFIG_PATH)
35
- if File.exist?(INITD_PATH)
36
- log_info "Thin service already installed at #{INITD_PATH}"
37
- else
38
- log_info "Installing thin service at #{INITD_PATH} ..."
39
- sh "mkdir -p #{File.dirname(INITD_PATH)}"
40
- log_info "writing #{INITD_PATH}"
41
- File.open(INITD_PATH, 'w') do |f|
42
- f << ERB.new(File.read(TEMPLATE)).result(binding)
43
- end
44
- sh "chmod +x #{INITD_PATH}" # Make executable
45
- end
46
-
47
- sh "mkdir -p #{config_files_path}"
48
-
49
- log_info ''
50
- log_info "To configure thin to start at system boot:"
51
- log_info "on RedHat like systems:"
52
- log_info " sudo /sbin/chkconfig --level 345 #{NAME} on"
53
- log_info "on Debian-like systems (Ubuntu):"
54
- log_info " sudo /usr/sbin/update-rc.d -f #{NAME} defaults"
55
- log_info "on Gentoo:"
56
- log_info " sudo rc-update add #{NAME} default"
57
- log_info ''
58
- log_info "Then put your config files in #{config_files_path}"
59
- end
60
-
61
- private
62
- def run(command)
63
- Dir[config_path + '/*'].each do |config|
64
- next if config.end_with?("~")
65
- log_info "[#{command}] #{config} ..."
66
- Command.run(command, :config => config, :daemonize => true)
67
- end
68
- end
69
-
70
- def sh(cmd)
71
- log_info cmd
72
- system(cmd)
73
- end
74
- end
75
- end
76
- end
@@ -1,39 +0,0 @@
1
- #!/bin/sh
2
- ### BEGIN INIT INFO
3
- # Provides: thin
4
- # Required-Start: $local_fs $remote_fs
5
- # Required-Stop: $local_fs $remote_fs
6
- # Default-Start: 2 3 4 5
7
- # Default-Stop: S 0 1 6
8
- # Short-Description: thin initscript
9
- # Description: thin
10
- ### END INIT INFO
11
-
12
- # Original author: Forrest Robertson
13
-
14
- # Do NOT "set -e"
15
-
16
- DAEMON=<%= Command.script %>
17
- SCRIPT_NAME=<%= INITD_PATH %>
18
- CONFIG_PATH=<%= config_files_path %>
19
-
20
- # Exit if the package is not installed
21
- [ -x "$DAEMON" ] || exit 0
22
-
23
- case "$1" in
24
- start)
25
- $DAEMON start --all $CONFIG_PATH
26
- ;;
27
- stop)
28
- $DAEMON stop --all $CONFIG_PATH
29
- ;;
30
- restart)
31
- $DAEMON restart --all $CONFIG_PATH
32
- ;;
33
- *)
34
- echo "Usage: $SCRIPT_NAME {start|stop|restart}" >&2
35
- exit 3
36
- ;;
37
- esac
38
-
39
- :
@@ -1,199 +0,0 @@
1
- require 'etc'
2
- require 'daemons' unless Thin.win?
3
-
4
- module Process
5
- # Returns +true+ the process identied by +pid+ is running.
6
- def running?(pid)
7
- Process.getpgid(pid) != -1
8
- rescue Errno::EPERM
9
- true
10
- rescue Errno::ESRCH
11
- false
12
- end
13
- module_function :running?
14
- end
15
-
16
- module Thin
17
- # Raised when the pid file already exist starting as a daemon.
18
- class PidFileExist < RuntimeError; end
19
- class PidFileNotFound < RuntimeError; end
20
-
21
- # Module included in classes that can be turned into a daemon.
22
- # Handle stuff like:
23
- # * storing the PID in a file
24
- # * redirecting output to the log file
25
- # * changing process privileges
26
- # * killing the process gracefully
27
- module Daemonizable
28
- attr_accessor :pid_file, :log_file
29
-
30
- def self.included(base)
31
- base.extend ClassMethods
32
- end
33
-
34
- def pid
35
- File.exist?(pid_file) && !File.zero?(pid_file) ? open(pid_file).read.to_i : nil
36
- end
37
-
38
- def kill(timeout = 60)
39
- if File.exist?(@pid_file)
40
- self.class.kill(@pid_file, timeout)
41
- end
42
- end
43
-
44
- # Turns the current script into a daemon process that detaches from the console.
45
- def daemonize
46
- raise PlatformNotSupported, 'Daemonizing is not supported on Windows' if Thin.win?
47
- raise ArgumentError, 'You must specify a pid_file to daemonize' unless @pid_file
48
-
49
- remove_stale_pid_file
50
-
51
- pwd = Dir.pwd # Current directory is changed during daemonization, so store it
52
-
53
- # HACK we need to create the directory before daemonization to prevent a bug under 1.9
54
- # ignoring all signals when the directory is created after daemonization.
55
- FileUtils.mkdir_p File.dirname(@pid_file)
56
- FileUtils.mkdir_p File.dirname(@log_file)
57
-
58
- Daemonize.daemonize(File.expand_path(@log_file), name)
59
-
60
- Dir.chdir(pwd)
61
-
62
- write_pid_file
63
-
64
- at_exit do
65
- log_info "Exiting!"
66
- remove_pid_file
67
- end
68
- end
69
-
70
- # Change privileges of the process
71
- # to the specified user and group.
72
- def change_privilege(user, group=user)
73
- log_info "Changing process privilege to #{user}:#{group}"
74
-
75
- uid, gid = Process.euid, Process.egid
76
- target_uid = Etc.getpwnam(user).uid
77
- target_gid = Etc.getgrnam(group).gid
78
-
79
- if uid != target_uid || gid != target_gid
80
- # Change PID file ownership
81
- File.chown(target_uid, target_gid, @pid_file) if File.exists?(@pid_file)
82
-
83
- # Change process ownership
84
- Process.initgroups(user, target_gid)
85
- Process::GID.change_privilege(target_gid)
86
- Process::UID.change_privilege(target_uid)
87
-
88
- # Correct environment variables
89
- ENV.store('USER', user)
90
- ENV.store('HOME', File.expand_path("~#{user}"))
91
- end
92
- rescue Errno::EPERM => e
93
- log_info "Couldn't change user and group to #{user}:#{group}: #{e}"
94
- end
95
-
96
- # Register a proc to be called to restart the server.
97
- def on_restart(&block)
98
- @on_restart = block
99
- end
100
-
101
- # Restart the server.
102
- def restart
103
- if @on_restart
104
- log_info 'Restarting ...'
105
- stop
106
- remove_pid_file
107
- @on_restart.call
108
- EM.next_tick { exit! }
109
- end
110
- end
111
-
112
- module ClassMethods
113
- # Send a QUIT or INT (if timeout is +0+) signal the process which
114
- # PID is stored in +pid_file+.
115
- # If the process is still running after +timeout+, KILL signal is
116
- # sent.
117
- def kill(pid_file, timeout=60)
118
- if timeout == 0
119
- send_signal('INT', pid_file, timeout)
120
- else
121
- send_signal('QUIT', pid_file, timeout)
122
- end
123
- end
124
-
125
- # Restart the server by sending HUP signal.
126
- def restart(pid_file)
127
- send_signal('HUP', pid_file)
128
- end
129
-
130
- def monotonic_time
131
- Process.clock_gettime(Process::CLOCK_MONOTONIC)
132
- end
133
-
134
- # Send a +signal+ to the process which PID is stored in +pid_file+.
135
- def send_signal(signal, pid_file, timeout=60)
136
- if pid = read_pid_file(pid_file)
137
- Logging.log_info "Sending #{signal} signal to process #{pid} ... "
138
-
139
- Process.kill(signal, pid)
140
-
141
- # This loop seems kind of racy to me...
142
- started_at = monotonic_time
143
- while Process.running?(pid)
144
- sleep 0.1
145
- raise Timeout::Error if (monotonic_time - started_at) > timeout
146
- end
147
- else
148
- raise PidFileNotFound, "Can't stop process, no PID found in #{pid_file}"
149
- end
150
- rescue Timeout::Error
151
- Logging.log_info "Timeout!"
152
- force_kill(pid, pid_file)
153
- rescue Interrupt
154
- force_kill(pid, pid_file)
155
- rescue Errno::ESRCH # No such process
156
- Logging.log_info "process not found!"
157
- force_kill(pid, pid_file)
158
- end
159
-
160
- def force_kill(pid, pid_file)
161
- Logging.log_info "Sending KILL signal to process #{pid} ... "
162
- Process.kill("KILL", pid)
163
- File.delete(pid_file) if File.exist?(pid_file)
164
- end
165
-
166
- def read_pid_file(file)
167
- if File.file?(file) && pid = File.read(file)
168
- pid.to_i
169
- else
170
- nil
171
- end
172
- end
173
- end
174
-
175
- protected
176
- def remove_pid_file
177
- File.delete(@pid_file) if @pid_file && File.exists?(@pid_file)
178
- end
179
-
180
- def write_pid_file
181
- log_info "Writing PID to #{@pid_file}"
182
- open(@pid_file,"w") { |f| f.write(Process.pid) }
183
- File.chmod(0644, @pid_file)
184
- end
185
-
186
- # If PID file is stale, remove it.
187
- def remove_stale_pid_file
188
- if File.exist?(@pid_file)
189
- if pid && Process.running?(pid)
190
- raise PidFileExist, "#{@pid_file} already exists, seems like it's already running (process ID: #{pid}). " +
191
- "Stop the process or delete #{@pid_file}."
192
- else
193
- log_info "Deleting stale PID file #{@pid_file}"
194
- remove_pid_file
195
- end
196
- end
197
- end
198
- end
199
- end
@@ -1,40 +0,0 @@
1
- module Thin
2
- # Store HTTP header name-value pairs direcly to a string
3
- # and allow duplicated entries on some names.
4
- class Headers
5
- HEADER_FORMAT = "%s: %s\r\n".freeze
6
- ALLOWED_DUPLICATES = %w(set-cookie set-cookie2 warning www-authenticate).freeze
7
-
8
- def initialize
9
- @sent = {}
10
- @out = []
11
- end
12
-
13
- # Add <tt>key: value</tt> pair to the headers.
14
- # Ignore if already sent and no duplicates are allowed
15
- # for this +key+.
16
- def []=(key, value)
17
- downcase_key = key.downcase
18
- if !@sent.has_key?(downcase_key) || ALLOWED_DUPLICATES.include?(downcase_key)
19
- @sent[downcase_key] = true
20
- value = case value
21
- when Time
22
- value.httpdate
23
- when NilClass
24
- return
25
- else
26
- value.to_s
27
- end
28
- @out << HEADER_FORMAT % [key, value]
29
- end
30
- end
31
-
32
- def has_key?(key)
33
- @sent[key.downcase]
34
- end
35
-
36
- def to_s
37
- @out.join
38
- end
39
- end
40
- end
@@ -1,174 +0,0 @@
1
- require 'logger'
2
-
3
- module Thin
4
- # To be included in classes to allow some basic logging
5
- # that can be silenced (<tt>Logging.silent=</tt>) or made
6
- # more verbose.
7
- # <tt>Logging.trace=</tt>: log all raw request and response and
8
- # messages logged with +trace+.
9
- # <tt>Logging.silent=</tt>: silence all log all log messages
10
- # altogether.
11
- module Logging
12
- # Simple formatter which only displays the message.
13
- # Taken from ActiveSupport
14
- class SimpleFormatter < Logger::Formatter
15
- def call(severity, timestamp, progname, msg)
16
- "#{timestamp} #{String === msg ? msg : msg.inspect}\n"
17
- end
18
- end
19
-
20
- @trace_logger = nil
21
-
22
- class << self
23
- attr_reader :logger
24
- attr_reader :trace_logger
25
-
26
- def trace=(enabled)
27
- if enabled
28
- @trace_logger ||= Logger.new(STDOUT)
29
- else
30
- @trace_logger = nil
31
- end
32
- end
33
-
34
- def trace?
35
- !@trace_logger.nil?
36
- end
37
-
38
- def silent=(shh)
39
- if shh
40
- @logger = nil
41
- else
42
- @logger ||= Logger.new(STDOUT)
43
- end
44
- end
45
-
46
- def silent?
47
- !@logger.nil?
48
- end
49
-
50
- def level
51
- @logger ? @logger.level : nil # or 'silent'
52
- end
53
-
54
- def level=(value)
55
- # If logging has been silenced, then re-enable logging
56
- @logger = Logger.new(STDOUT) if @logger.nil?
57
- @logger.level = value
58
- end
59
-
60
- # Allow user to specify a custom logger to use.
61
- # This object must respond to:
62
- # +level+, +level=+ and +debug+, +info+, +warn+, +error+, +fatal+
63
- def logger=(custom_logger)
64
- [ :level ,
65
- :level= ,
66
- :debug ,
67
- :info ,
68
- :warn ,
69
- :error ,
70
- :fatal ,
71
- :unknown ,
72
- ].each do |method|
73
- if not custom_logger.respond_to?(method)
74
- raise ArgumentError, "logger must respond to #{method}"
75
- end
76
- end
77
-
78
- @logger = custom_logger
79
- end
80
-
81
- def trace_logger=(custom_tracer)
82
- [ :level ,
83
- :level= ,
84
- :debug ,
85
- :info ,
86
- :warn ,
87
- :error ,
88
- :fatal ,
89
- :unknown ,
90
- ].each do |method|
91
- if not custom_tracer.respond_to?(method)
92
- raise ArgumentError, "trace logger must respond to #{method}"
93
- end
94
- end
95
-
96
- @trace_logger = custom_tracer
97
- end
98
-
99
- def log_msg(msg, level=Logger::INFO)
100
- return unless @logger
101
- @logger.add(level, msg)
102
- end
103
-
104
- def trace_msg(msg)
105
- return unless @trace_logger
106
- @trace_logger.info(msg)
107
- end
108
-
109
- # Provided for backwards compatibility.
110
- # Callers should be using the +level+ (on the +Logging+ module
111
- # or on the instance) to figure out what the log level is.
112
- def debug?
113
- self.level == Logger::DEBUG
114
- end
115
- def debug=(val)
116
- self.level = (val ? Logger::DEBUG : Logger::INFO)
117
- end
118
-
119
- end # module methods
120
-
121
- # Default logger to stdout.
122
- self.logger = Logger.new(STDOUT)
123
- self.logger.level = Logger::INFO
124
- self.logger.formatter = Logging::SimpleFormatter.new
125
-
126
- def silent
127
- Logging.silent?
128
- end
129
-
130
- def silent=(value)
131
- Logging.silent = value
132
- end
133
-
134
- # Log a message if tracing is activated
135
- def trace(msg=nil)
136
- Logging.trace_msg(msg) if msg
137
- end
138
- module_function :trace
139
- public :trace
140
-
141
- # Log a message at DEBUG level
142
- def log_debug(msg=nil)
143
- Logging.log_msg(msg || yield, Logger::DEBUG)
144
- end
145
- module_function :log_debug
146
- public :log_debug
147
-
148
- # Log a message at INFO level
149
- def log_info(msg)
150
- Logging.log_msg(msg || yield, Logger::INFO)
151
- end
152
- module_function :log_info
153
- public :log_info
154
-
155
- # Log a message at ERROR level (and maybe a backtrace)
156
- def log_error(msg, e=nil)
157
- log_msg = msg
158
- if e
159
- log_msg += ": #{e}\n\t" + e.backtrace.join("\n\t") + "\n"
160
- end
161
- Logging.log_msg(log_msg, Logger::ERROR)
162
- end
163
- module_function :log_error
164
- public :log_error
165
-
166
- # For backwards compatibility
167
- def log msg
168
- STDERR.puts('#log has been deprecated, please use the ' \
169
- 'log_level function instead (e.g. - log_info).')
170
- log_info(msg)
171
- end
172
-
173
- end
174
- end