unicorn-simon 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. checksums.yaml +7 -0
  2. data/.CHANGELOG.old +25 -0
  3. data/.document +28 -0
  4. data/.gitattributes +5 -0
  5. data/.gitignore +25 -0
  6. data/.mailmap +26 -0
  7. data/.manifest +156 -0
  8. data/.olddoc.yml +18 -0
  9. data/Application_Timeouts +77 -0
  10. data/CONTRIBUTORS +35 -0
  11. data/COPYING +674 -0
  12. data/DESIGN +95 -0
  13. data/Documentation/.gitignore +5 -0
  14. data/Documentation/GNUmakefile +30 -0
  15. data/Documentation/unicorn.1.txt +187 -0
  16. data/Documentation/unicorn_rails.1.txt +175 -0
  17. data/FAQ +70 -0
  18. data/GIT-VERSION-FILE +1 -0
  19. data/GIT-VERSION-GEN +39 -0
  20. data/GNUmakefile +253 -0
  21. data/HACKING +120 -0
  22. data/ISSUES +90 -0
  23. data/KNOWN_ISSUES +79 -0
  24. data/LATEST +30 -0
  25. data/LICENSE +67 -0
  26. data/Links +56 -0
  27. data/NEWS +2465 -0
  28. data/PHILOSOPHY +139 -0
  29. data/README +138 -0
  30. data/Rakefile +16 -0
  31. data/SIGNALS +123 -0
  32. data/Sandbox +104 -0
  33. data/TODO +3 -0
  34. data/TUNING +119 -0
  35. data/archive/.gitignore +3 -0
  36. data/archive/slrnpull.conf +4 -0
  37. data/bin/unicorn +126 -0
  38. data/bin/unicorn_rails +209 -0
  39. data/examples/big_app_gc.rb +2 -0
  40. data/examples/echo.ru +27 -0
  41. data/examples/init.sh +102 -0
  42. data/examples/logger_mp_safe.rb +25 -0
  43. data/examples/logrotate.conf +44 -0
  44. data/examples/nginx.conf +155 -0
  45. data/examples/unicorn.conf.minimal.rb +13 -0
  46. data/examples/unicorn.conf.rb +110 -0
  47. data/examples/unicorn.socket +11 -0
  48. data/examples/unicorn@.service +33 -0
  49. data/ext/unicorn_http/CFLAGS +13 -0
  50. data/ext/unicorn_http/c_util.h +124 -0
  51. data/ext/unicorn_http/common_field_optimization.h +111 -0
  52. data/ext/unicorn_http/ext_help.h +62 -0
  53. data/ext/unicorn_http/extconf.rb +11 -0
  54. data/ext/unicorn_http/global_variables.h +97 -0
  55. data/ext/unicorn_http/httpdate.c +78 -0
  56. data/ext/unicorn_http/unicorn_http.c +4274 -0
  57. data/ext/unicorn_http/unicorn_http.rl +980 -0
  58. data/ext/unicorn_http/unicorn_http_common.rl +76 -0
  59. data/lib/unicorn/app/old_rails/static.rb +59 -0
  60. data/lib/unicorn/app/old_rails.rb +35 -0
  61. data/lib/unicorn/cgi_wrapper.rb +147 -0
  62. data/lib/unicorn/configurator.rb +664 -0
  63. data/lib/unicorn/const.rb +21 -0
  64. data/lib/unicorn/http_request.rb +122 -0
  65. data/lib/unicorn/http_response.rb +60 -0
  66. data/lib/unicorn/http_server.rb +824 -0
  67. data/lib/unicorn/launcher.rb +62 -0
  68. data/lib/unicorn/oob_gc.rb +82 -0
  69. data/lib/unicorn/preread_input.rb +33 -0
  70. data/lib/unicorn/socket_helper.rb +195 -0
  71. data/lib/unicorn/stream_input.rb +146 -0
  72. data/lib/unicorn/tee_input.rb +133 -0
  73. data/lib/unicorn/tmpio.rb +27 -0
  74. data/lib/unicorn/util.rb +90 -0
  75. data/lib/unicorn/version.rb +1 -0
  76. data/lib/unicorn/worker.rb +140 -0
  77. data/lib/unicorn.rb +123 -0
  78. data/man/man1/unicorn.1 +221 -0
  79. data/man/man1/unicorn_rails.1 +212 -0
  80. data/setup.rb +1586 -0
  81. data/t/.gitignore +4 -0
  82. data/t/GNUmakefile +74 -0
  83. data/t/README +42 -0
  84. data/t/bin/content-md5-put +36 -0
  85. data/t/bin/sha1sum.rb +17 -0
  86. data/t/bin/unused_listen +40 -0
  87. data/t/broken-app.ru +12 -0
  88. data/t/detach.ru +11 -0
  89. data/t/env.ru +3 -0
  90. data/t/fails-rack-lint.ru +5 -0
  91. data/t/heartbeat-timeout.ru +12 -0
  92. data/t/hijack.ru +43 -0
  93. data/t/listener_names.ru +4 -0
  94. data/t/my-tap-lib.sh +201 -0
  95. data/t/oob_gc.ru +20 -0
  96. data/t/oob_gc_path.ru +20 -0
  97. data/t/pid.ru +3 -0
  98. data/t/preread_input.ru +17 -0
  99. data/t/rack-input-tests.ru +21 -0
  100. data/t/t0000-http-basic.sh +50 -0
  101. data/t/t0001-reload-bad-config.sh +53 -0
  102. data/t/t0002-config-conflict.sh +49 -0
  103. data/t/t0002-parser-error.sh +94 -0
  104. data/t/t0003-working_directory.sh +51 -0
  105. data/t/t0004-heartbeat-timeout.sh +69 -0
  106. data/t/t0004-working_directory_broken.sh +24 -0
  107. data/t/t0005-working_directory_app.rb.sh +40 -0
  108. data/t/t0006-reopen-logs.sh +83 -0
  109. data/t/t0006.ru +13 -0
  110. data/t/t0007-working_directory_no_embed_cli.sh +44 -0
  111. data/t/t0008-back_out_of_upgrade.sh +110 -0
  112. data/t/t0009-broken-app.sh +56 -0
  113. data/t/t0009-winch_ttin.sh +59 -0
  114. data/t/t0010-reap-logging.sh +55 -0
  115. data/t/t0011-active-unix-socket.sh +79 -0
  116. data/t/t0012-reload-empty-config.sh +85 -0
  117. data/t/t0013-rewindable-input-false.sh +24 -0
  118. data/t/t0013.ru +12 -0
  119. data/t/t0014-rewindable-input-true.sh +24 -0
  120. data/t/t0014.ru +12 -0
  121. data/t/t0015-configurator-internals.sh +25 -0
  122. data/t/t0018-write-on-close.sh +23 -0
  123. data/t/t0019-max_header_len.sh +49 -0
  124. data/t/t0020-at_exit-handler.sh +49 -0
  125. data/t/t0021-process_detach.sh +29 -0
  126. data/t/t0022-listener_names-preload_app.sh +32 -0
  127. data/t/t0100-rack-input-tests.sh +124 -0
  128. data/t/t0116-client_body_buffer_size.sh +80 -0
  129. data/t/t0116.ru +16 -0
  130. data/t/t0200-rack-hijack.sh +30 -0
  131. data/t/t0300-no-default-middleware.sh +20 -0
  132. data/t/t9000-preread-input.sh +48 -0
  133. data/t/t9001-oob_gc.sh +47 -0
  134. data/t/t9002-oob_gc-path.sh +75 -0
  135. data/t/test-lib.sh +128 -0
  136. data/t/write-on-close.ru +11 -0
  137. data/test/aggregate.rb +15 -0
  138. data/test/benchmark/README +50 -0
  139. data/test/benchmark/dd.ru +18 -0
  140. data/test/benchmark/stack.ru +8 -0
  141. data/test/exec/README +5 -0
  142. data/test/exec/test_exec.rb +1099 -0
  143. data/test/test_helper.rb +298 -0
  144. data/test/unit/test_configurator.rb +175 -0
  145. data/test/unit/test_droplet.rb +28 -0
  146. data/test/unit/test_http_parser.rb +886 -0
  147. data/test/unit/test_http_parser_ng.rb +633 -0
  148. data/test/unit/test_request.rb +182 -0
  149. data/test/unit/test_response.rb +111 -0
  150. data/test/unit/test_server.rb +268 -0
  151. data/test/unit/test_signals.rb +188 -0
  152. data/test/unit/test_socket_helper.rb +197 -0
  153. data/test/unit/test_stream_input.rb +203 -0
  154. data/test/unit/test_tee_input.rb +304 -0
  155. data/test/unit/test_upload.rb +306 -0
  156. data/test/unit/test_util.rb +105 -0
  157. data/unicorn.gemspec +50 -0
  158. metadata +310 -0
@@ -0,0 +1,62 @@
1
+ # -*- encoding: binary -*-
2
+
3
+ # :enddoc:
4
+ $stdout.sync = $stderr.sync = true
5
+ $stdin.binmode
6
+ $stdout.binmode
7
+ $stderr.binmode
8
+
9
+ require 'unicorn'
10
+
11
+ module Unicorn::Launcher
12
+
13
+ # We don't do a lot of standard daemonization stuff:
14
+ # * umask is whatever was set by the parent process at startup
15
+ # and can be set in config.ru and config_file, so making it
16
+ # 0000 and potentially exposing sensitive log data can be bad
17
+ # policy.
18
+ # * don't bother to chdir("/") here since unicorn is designed to
19
+ # run inside APP_ROOT. Unicorn will also re-chdir() to
20
+ # the directory it was started in when being re-executed
21
+ # to pickup code changes if the original deployment directory
22
+ # is a symlink or otherwise got replaced.
23
+ def self.daemonize!(options)
24
+ cfg = Unicorn::Configurator
25
+ $stdin.reopen("/dev/null")
26
+
27
+ # We only start a new process group if we're not being reexecuted
28
+ # and inheriting file descriptors from our parent
29
+ unless ENV['UNICORN_FD']
30
+ # grandparent - reads pipe, exits when master is ready
31
+ # \_ parent - exits immediately ASAP
32
+ # \_ unicorn master - writes to pipe when ready
33
+
34
+ rd, wr = IO.pipe
35
+ grandparent = $$
36
+ if fork
37
+ wr.close # grandparent does not write
38
+ else
39
+ rd.close # unicorn master does not read
40
+ Process.setsid
41
+ exit if fork # parent dies now
42
+ end
43
+
44
+ if grandparent == $$
45
+ # this will block until HttpServer#join runs (or it dies)
46
+ master_pid = (rd.readpartial(16) rescue nil).to_i
47
+ unless master_pid > 1
48
+ warn "master failed to start, check stderr log for details"
49
+ exit!(1)
50
+ end
51
+ exit 0
52
+ else # unicorn master process
53
+ options[:ready_pipe] = wr
54
+ end
55
+ end
56
+ # $stderr/$stderr can/will be redirected separately in the Unicorn config
57
+ cfg::DEFAULTS[:stderr_path] ||= "/dev/null"
58
+ cfg::DEFAULTS[:stdout_path] ||= "/dev/null"
59
+ cfg::RACKUP[:daemonized] = true
60
+ end
61
+
62
+ end
@@ -0,0 +1,82 @@
1
+ # -*- encoding: binary -*-
2
+
3
+ # Strongly consider https://github.com/tmm1/gctools if using Ruby 2.1+
4
+ # It is built on new APIs in Ruby 2.1, so it is more intelligent than
5
+ # this historical implementation.
6
+ #
7
+ # Users on Ruby 2.0 (not 2.1+) may also want to check out
8
+ # lib/middleware/unicorn_oobgc.rb from the Discourse project
9
+ # (https://github.com/discourse/discourse)
10
+ #
11
+ # The following information is only for historical versions of Ruby.
12
+ #
13
+ # Runs GC after requests, after closing the client socket and
14
+ # before attempting to accept more connections.
15
+ #
16
+ # This shouldn't hurt overall performance as long as the server cluster
17
+ # is at <50% CPU capacity, and improves the performance of most memory
18
+ # intensive requests. This serves to improve _client-visible_
19
+ # performance (possibly at the cost of overall performance).
20
+ #
21
+ # Increasing the number of +worker_processes+ may be necessary to
22
+ # improve average client response times because some of your workers
23
+ # will be busy doing GC and unable to service clients. Think of
24
+ # using more workers with this module as a poor man's concurrent GC.
25
+ #
26
+ # We'll call GC after each request is been written out to the socket, so
27
+ # the client never sees the extra GC hit it.
28
+ #
29
+ # This middleware is _only_ effective for applications that use a lot
30
+ # of memory, and will hurt simpler apps/endpoints that can process
31
+ # multiple requests before incurring GC.
32
+ #
33
+ # This middleware is only designed to work with unicorn, as it harms
34
+ # performance with keepalive-enabled servers.
35
+ #
36
+ # Example (in config.ru):
37
+ #
38
+ # require 'unicorn/oob_gc'
39
+ #
40
+ # # GC ever two requests that hit /expensive/foo or /more_expensive/foo
41
+ # # in your app. By default, this will GC once every 5 requests
42
+ # # for all endpoints in your app
43
+ # use Unicorn::OobGC, 2, %r{\A/(?:expensive/foo|more_expensive/foo)}
44
+ #
45
+ # Feedback from users of early implementations of this module:
46
+ # * https://bogomips.org/unicorn-public/0BFC98E9-072B-47EE-9A70-05478C20141B@lukemelia.com/
47
+ # * https://bogomips.org/unicorn-public/AANLkTilUbgdyDv9W1bi-s_W6kq9sOhWfmuYkKLoKGOLj@mail.gmail.com/
48
+
49
+ module Unicorn::OobGC
50
+
51
+ # this pretends to be Rack middleware because it used to be
52
+ # But we need to hook into unicorn internals so we need to close
53
+ # the socket before clearing the request env.
54
+ #
55
+ # +interval+ is the number of requests matching the +path+ regular
56
+ # expression before invoking GC.
57
+ def self.new(app, interval = 5, path = %r{\A/})
58
+ @@nr = interval
59
+ self.const_set :OOBGC_PATH, path
60
+ self.const_set :OOBGC_INTERVAL, interval
61
+ ObjectSpace.each_object(Unicorn::HttpServer) do |s|
62
+ s.extend(self)
63
+ self.const_set :OOBGC_ENV, s.instance_variable_get(:@request).env
64
+ end
65
+ app # pretend to be Rack middleware since it was in the past
66
+ end
67
+
68
+ #:stopdoc:
69
+ PATH_INFO = "PATH_INFO"
70
+ def process_client(client)
71
+ super(client) # Unicorn::HttpServer#process_client
72
+ if OOBGC_PATH =~ OOBGC_ENV[PATH_INFO] && ((@@nr -= 1) <= 0)
73
+ @@nr = OOBGC_INTERVAL
74
+ OOBGC_ENV.clear
75
+ disabled = GC.enable
76
+ GC.start
77
+ GC.disable if disabled
78
+ end
79
+ end
80
+
81
+ # :startdoc:
82
+ end
@@ -0,0 +1,33 @@
1
+ # -*- encoding: binary -*-
2
+
3
+ module Unicorn
4
+ # This middleware is used to ensure input is buffered to memory
5
+ # or disk (depending on size) before the application is dispatched
6
+ # by entirely consuming it (from TeeInput) beforehand.
7
+ #
8
+ # Usage (in config.ru):
9
+ #
10
+ # require 'unicorn/preread_input'
11
+ # if defined?(Unicorn)
12
+ # use Unicorn::PrereadInput
13
+ # end
14
+ # run YourApp.new
15
+ class PrereadInput
16
+
17
+ # :stopdoc:
18
+ def initialize(app)
19
+ @app = app
20
+ end
21
+
22
+ def call(env)
23
+ buf = ""
24
+ input = env["rack.input"]
25
+ if input.respond_to?(:rewind)
26
+ true while input.read(16384, buf)
27
+ input.rewind
28
+ end
29
+ @app.call(env)
30
+ end
31
+ # :startdoc:
32
+ end
33
+ end
@@ -0,0 +1,195 @@
1
+ # -*- encoding: binary -*-
2
+ # :enddoc:
3
+ require 'socket'
4
+
5
+ module Unicorn
6
+ module SocketHelper
7
+
8
+ # internal interface
9
+ DEFAULTS = {
10
+ # The semantics for TCP_DEFER_ACCEPT changed in Linux 2.6.32+
11
+ # with commit d1b99ba41d6c5aa1ed2fc634323449dd656899e9
12
+ # This change shouldn't affect unicorn users behind nginx (a
13
+ # value of 1 remains an optimization).
14
+ :tcp_defer_accept => 1,
15
+
16
+ # FreeBSD, we need to override this to 'dataready' if we
17
+ # eventually support non-HTTP/1.x
18
+ :accept_filter => 'httpready',
19
+
20
+ # same default value as Mongrel
21
+ :backlog => 1024,
22
+
23
+ # favor latency over bandwidth savings
24
+ :tcp_nopush => nil,
25
+ :tcp_nodelay => true,
26
+ }
27
+
28
+ # configure platform-specific options (only tested on Linux 2.6 so far)
29
+ def accf_arg(af_name)
30
+ [ af_name, nil ].pack('a16a240')
31
+ end if RUBY_PLATFORM =~ /freebsd/ && Socket.const_defined?(:SO_ACCEPTFILTER)
32
+
33
+ def set_tcp_sockopt(sock, opt)
34
+ # just in case, even LANs can break sometimes. Linux sysadmins
35
+ # can lower net.ipv4.tcp_keepalive_* sysctl knobs to very low values.
36
+ Socket.const_defined?(:SO_KEEPALIVE) and
37
+ sock.setsockopt(:SOL_SOCKET, :SO_KEEPALIVE, 1)
38
+
39
+ if Socket.const_defined?(:TCP_NODELAY)
40
+ val = opt[:tcp_nodelay]
41
+ val = DEFAULTS[:tcp_nodelay] if val.nil?
42
+ sock.setsockopt(:IPPROTO_TCP, :TCP_NODELAY, val ? 1 : 0)
43
+ end
44
+
45
+ val = opt[:tcp_nopush]
46
+ unless val.nil?
47
+ if Socket.const_defined?(:TCP_CORK) # Linux
48
+ sock.setsockopt(:IPPROTO_TCP, :TCP_CORK, val)
49
+ elsif Socket.const_defined?(:TCP_NOPUSH) # FreeBSD
50
+ sock.setsockopt(:IPPROTO_TCP, :TCP_NOPUSH, val)
51
+ end
52
+ end
53
+
54
+ # No good reason to ever have deferred accepts off in single-threaded
55
+ # servers (except maybe benchmarking)
56
+ if Socket.const_defined?(:TCP_DEFER_ACCEPT)
57
+ # this differs from nginx, since nginx doesn't allow us to
58
+ # configure the the timeout...
59
+ seconds = opt[:tcp_defer_accept]
60
+ seconds = DEFAULTS[:tcp_defer_accept] if [true,nil].include?(seconds)
61
+ seconds = 0 unless seconds # nil/false means disable this
62
+ sock.setsockopt(:IPPROTO_TCP, :TCP_DEFER_ACCEPT, seconds)
63
+ elsif respond_to?(:accf_arg)
64
+ name = opt[:accept_filter]
65
+ name = DEFAULTS[:accept_filter] if name.nil?
66
+ begin
67
+ sock.setsockopt(:SOL_SOCKET, :SO_ACCEPTFILTER, accf_arg(name))
68
+ rescue => e
69
+ logger.error("#{sock_name(sock)} " \
70
+ "failed to set accept_filter=#{name} (#{e.inspect})")
71
+ end
72
+ end
73
+ end
74
+
75
+ def set_server_sockopt(sock, opt)
76
+ opt = DEFAULTS.merge(opt || {})
77
+
78
+ TCPSocket === sock and set_tcp_sockopt(sock, opt)
79
+
80
+ rcvbuf, sndbuf = opt.values_at(:rcvbuf, :sndbuf)
81
+ if rcvbuf || sndbuf
82
+ log_buffer_sizes(sock, "before: ")
83
+ sock.setsockopt(:SOL_SOCKET, :SO_RCVBUF, rcvbuf) if rcvbuf
84
+ sock.setsockopt(:SOL_SOCKET, :SO_SNDBUF, sndbuf) if sndbuf
85
+ log_buffer_sizes(sock, " after: ")
86
+ end
87
+ sock.listen(opt[:backlog])
88
+ rescue => e
89
+ Unicorn.log_error(logger, "#{sock_name(sock)} #{opt.inspect}", e)
90
+ end
91
+
92
+ def log_buffer_sizes(sock, pfx = '')
93
+ rcvbuf = sock.getsockopt(:SOL_SOCKET, :SO_RCVBUF).int
94
+ sndbuf = sock.getsockopt(:SOL_SOCKET, :SO_SNDBUF).int
95
+ logger.info "#{pfx}#{sock_name(sock)} rcvbuf=#{rcvbuf} sndbuf=#{sndbuf}"
96
+ end
97
+
98
+ # creates a new server, socket. address may be a HOST:PORT or
99
+ # an absolute path to a UNIX socket. address can even be a Socket
100
+ # object in which case it is immediately returned
101
+ def bind_listen(address = '0.0.0.0:8080', opt = {})
102
+ return address unless String === address
103
+
104
+ sock = if address[0] == ?/
105
+ if File.exist?(address)
106
+ if File.socket?(address)
107
+ begin
108
+ UNIXSocket.new(address).close
109
+ # fall through, try to bind(2) and fail with EADDRINUSE
110
+ # (or succeed from a small race condition we can't sanely avoid).
111
+ rescue Errno::ECONNREFUSED
112
+ logger.info "unlinking existing socket=#{address}"
113
+ File.unlink(address)
114
+ end
115
+ else
116
+ raise ArgumentError,
117
+ "socket=#{address} specified but it is not a socket!"
118
+ end
119
+ end
120
+ old_umask = File.umask(opt[:umask] || 0)
121
+ begin
122
+ Kgio::UNIXServer.new(address)
123
+ ensure
124
+ File.umask(old_umask)
125
+ end
126
+ elsif /\A\[([a-fA-F0-9:]+)\]:(\d+)\z/ =~ address
127
+ new_tcp_server($1, $2.to_i, opt.merge(:ipv6=>true))
128
+ elsif /\A(\d+\.\d+\.\d+\.\d+):(\d+)\z/ =~ address
129
+ new_tcp_server($1, $2.to_i, opt)
130
+ else
131
+ raise ArgumentError, "Don't know how to bind: #{address}"
132
+ end
133
+ set_server_sockopt(sock, opt)
134
+ sock
135
+ end
136
+
137
+ def new_tcp_server(addr, port, opt)
138
+ # n.b. we set FD_CLOEXEC in the workers
139
+ sock = Socket.new(opt[:ipv6] ? :AF_INET6 : :AF_INET, :SOCK_STREAM)
140
+ if opt.key?(:ipv6only)
141
+ Socket.const_defined?(:IPV6_V6ONLY) or
142
+ abort "Socket::IPV6_V6ONLY not defined, upgrade Ruby and/or your OS"
143
+ sock.setsockopt(:IPPROTO_IPV6, :IPV6_V6ONLY, opt[:ipv6only] ? 1 : 0)
144
+ end
145
+ sock.setsockopt(:SOL_SOCKET, :SO_REUSEADDR, 1)
146
+ if Socket.const_defined?(:SO_REUSEPORT) && opt[:reuseport]
147
+ sock.setsockopt(:SOL_SOCKET, :SO_REUSEPORT, 1)
148
+ end
149
+ sock.bind(Socket.pack_sockaddr_in(port, addr))
150
+ sock.autoclose = false
151
+ Kgio::TCPServer.for_fd(sock.fileno)
152
+ end
153
+
154
+ # returns rfc2732-style (e.g. "[::1]:666") addresses for IPv6
155
+ def tcp_name(sock)
156
+ port, addr = Socket.unpack_sockaddr_in(sock.getsockname)
157
+ addr.include?(':') ? "[#{addr}]:#{port}" : "#{addr}:#{port}"
158
+ end
159
+ module_function :tcp_name
160
+
161
+ # Returns the configuration name of a socket as a string. sock may
162
+ # be a string value, in which case it is returned as-is
163
+ # Warning: TCP sockets may not always return the name given to it.
164
+ def sock_name(sock)
165
+ case sock
166
+ when String then sock
167
+ when UNIXServer
168
+ Socket.unpack_sockaddr_un(sock.getsockname)
169
+ when TCPServer
170
+ tcp_name(sock)
171
+ when Socket
172
+ begin
173
+ tcp_name(sock)
174
+ rescue ArgumentError
175
+ Socket.unpack_sockaddr_un(sock.getsockname)
176
+ end
177
+ else
178
+ raise ArgumentError, "Unhandled class #{sock.class}: #{sock.inspect}"
179
+ end
180
+ end
181
+
182
+ module_function :sock_name
183
+
184
+ # casts a given Socket to be a TCPServer or UNIXServer
185
+ def server_cast(sock)
186
+ begin
187
+ Socket.unpack_sockaddr_in(sock.getsockname)
188
+ Kgio::TCPServer.for_fd(sock.fileno)
189
+ rescue ArgumentError
190
+ Kgio::UNIXServer.for_fd(sock.fileno)
191
+ end
192
+ end
193
+
194
+ end # module SocketHelper
195
+ end # module Unicorn
@@ -0,0 +1,146 @@
1
+ # -*- encoding: binary -*-
2
+
3
+ # When processing uploads, Unicorn may expose a StreamInput object under
4
+ # "rack.input" of the (future) Rack (2.x) environment.
5
+ class Unicorn::StreamInput
6
+ # The I/O chunk size (in +bytes+) for I/O operations where
7
+ # the size cannot be user-specified when a method is called.
8
+ # The default is 16 kilobytes.
9
+ @@io_chunk_size = Unicorn::Const::CHUNK_SIZE
10
+
11
+ # Initializes a new StreamInput object. You normally do not have to call
12
+ # this unless you are writing an HTTP server.
13
+ def initialize(socket, request)
14
+ @chunked = request.content_length.nil?
15
+ @socket = socket
16
+ @parser = request
17
+ @buf = request.buf
18
+ @rbuf = ''
19
+ @bytes_read = 0
20
+ filter_body(@rbuf, @buf) unless @buf.empty?
21
+ end
22
+
23
+ # :call-seq:
24
+ # ios.read([length [, buffer ]]) => string, buffer, or nil
25
+ #
26
+ # Reads at most length bytes from the I/O stream, or to the end of
27
+ # file if length is omitted or is nil. length must be a non-negative
28
+ # integer or nil. If the optional buffer argument is present, it
29
+ # must reference a String, which will receive the data.
30
+ #
31
+ # At end of file, it returns nil or '' depend on length.
32
+ # ios.read() and ios.read(nil) returns ''.
33
+ # ios.read(length [, buffer]) returns nil.
34
+ #
35
+ # If the Content-Length of the HTTP request is known (as is the common
36
+ # case for POST requests), then ios.read(length [, buffer]) will block
37
+ # until the specified length is read (or it is the last chunk).
38
+ # Otherwise, for uncommon "Transfer-Encoding: chunked" requests,
39
+ # ios.read(length [, buffer]) will return immediately if there is
40
+ # any data and only block when nothing is available (providing
41
+ # IO#readpartial semantics).
42
+ def read(length = nil, rv = '')
43
+ if length
44
+ if length <= @rbuf.size
45
+ length < 0 and raise ArgumentError, "negative length #{length} given"
46
+ rv.replace(@rbuf.slice!(0, length))
47
+ else
48
+ to_read = length - @rbuf.size
49
+ rv.replace(@rbuf.slice!(0, @rbuf.size))
50
+ until to_read == 0 || eof? || (rv.size > 0 && @chunked)
51
+ @socket.kgio_read(to_read, @buf) or eof!
52
+ filter_body(@rbuf, @buf)
53
+ rv << @rbuf
54
+ to_read -= @rbuf.size
55
+ end
56
+ @rbuf.clear
57
+ end
58
+ rv = nil if rv.empty? && length != 0
59
+ else
60
+ read_all(rv)
61
+ end
62
+ rv
63
+ end
64
+
65
+ # :call-seq:
66
+ # ios.gets => string or nil
67
+ #
68
+ # Reads the next ``line'' from the I/O stream; lines are separated
69
+ # by the global record separator ($/, typically "\n"). A global
70
+ # record separator of nil reads the entire unread contents of ios.
71
+ # Returns nil if called at the end of file.
72
+ # This takes zero arguments for strict Rack::Lint compatibility,
73
+ # unlike IO#gets.
74
+ def gets
75
+ sep = $/
76
+ if sep.nil?
77
+ read_all(rv = '')
78
+ return rv.empty? ? nil : rv
79
+ end
80
+ re = /\A(.*?#{Regexp.escape(sep)})/
81
+
82
+ begin
83
+ @rbuf.sub!(re, '') and return $1
84
+ return @rbuf.empty? ? nil : @rbuf.slice!(0, @rbuf.size) if eof?
85
+ @socket.kgio_read(@@io_chunk_size, @buf) or eof!
86
+ filter_body(once = '', @buf)
87
+ @rbuf << once
88
+ end while true
89
+ end
90
+
91
+ # :call-seq:
92
+ # ios.each { |line| block } => ios
93
+ #
94
+ # Executes the block for every ``line'' in *ios*, where lines are
95
+ # separated by the global record separator ($/, typically "\n").
96
+ def each
97
+ while line = gets
98
+ yield line
99
+ end
100
+
101
+ self # Rack does not specify what the return value is here
102
+ end
103
+
104
+ private
105
+
106
+ def eof?
107
+ if @parser.body_eof?
108
+ while @chunked && ! @parser.parse
109
+ once = @socket.kgio_read(@@io_chunk_size) or eof!
110
+ @buf << once
111
+ end
112
+ @socket = nil
113
+ true
114
+ else
115
+ false
116
+ end
117
+ end
118
+
119
+ def filter_body(dst, src)
120
+ rv = @parser.filter_body(dst, src)
121
+ @bytes_read += dst.size
122
+ rv
123
+ end
124
+
125
+ def read_all(dst)
126
+ dst.replace(@rbuf)
127
+ @socket or return
128
+ until eof?
129
+ @socket.kgio_read(@@io_chunk_size, @buf) or eof!
130
+ filter_body(@rbuf, @buf)
131
+ dst << @rbuf
132
+ end
133
+ ensure
134
+ @rbuf.clear
135
+ end
136
+
137
+ def eof!
138
+ # in case client only did a premature shutdown(SHUT_WR)
139
+ # we do support clients that shutdown(SHUT_WR) after the
140
+ # _entire_ request has been sent, and those will not have
141
+ # raised EOFError on us.
142
+ @socket.shutdown if @socket
143
+ ensure
144
+ raise Unicorn::ClientShutdown, "bytes_read=#{@bytes_read}", []
145
+ end
146
+ end
@@ -0,0 +1,133 @@
1
+ # -*- encoding: binary -*-
2
+
3
+ # acts like tee(1) on an input input to provide a input-like stream
4
+ # while providing rewindable semantics through a File/StringIO backing
5
+ # store. On the first pass, the input is only read on demand so your
6
+ # Rack application can use input notification (upload progress and
7
+ # like). This should fully conform to the Rack::Lint::InputWrapper
8
+ # specification on the public API. This class is intended to be a
9
+ # strict interpretation of Rack::Lint::InputWrapper functionality and
10
+ # will not support any deviations from it.
11
+ #
12
+ # When processing uploads, Unicorn exposes a TeeInput object under
13
+ # "rack.input" of the Rack environment.
14
+ class Unicorn::TeeInput < Unicorn::StreamInput
15
+ # The maximum size (in +bytes+) to buffer in memory before
16
+ # resorting to a temporary file. Default is 112 kilobytes.
17
+ @@client_body_buffer_size = Unicorn::Const::MAX_BODY
18
+
19
+ # sets the maximum size of request bodies to buffer in memory,
20
+ # amounts larger than this are buffered to the filesystem
21
+ def self.client_body_buffer_size=(bytes)
22
+ @@client_body_buffer_size = bytes
23
+ end
24
+
25
+ # returns the maximum size of request bodies to buffer in memory,
26
+ # amounts larger than this are buffered to the filesystem
27
+ def self.client_body_buffer_size
28
+ @@client_body_buffer_size
29
+ end
30
+
31
+ # for Rack::TempfileReaper in rack 1.6+
32
+ def new_tmpio # :nodoc:
33
+ tmpio = Unicorn::TmpIO.new
34
+ (@parser.env['rack.tempfiles'] ||= []) << tmpio
35
+ tmpio
36
+ end
37
+
38
+ # Initializes a new TeeInput object. You normally do not have to call
39
+ # this unless you are writing an HTTP server.
40
+ def initialize(socket, request)
41
+ @len = request.content_length
42
+ super
43
+ @tmp = @len && @len <= @@client_body_buffer_size ?
44
+ StringIO.new("") : new_tmpio
45
+ end
46
+
47
+ # :call-seq:
48
+ # ios.size => Integer
49
+ #
50
+ # Returns the size of the input. For requests with a Content-Length
51
+ # header value, this will not read data off the socket and just return
52
+ # the value of the Content-Length header as an Integer.
53
+ #
54
+ # For Transfer-Encoding:chunked requests, this requires consuming
55
+ # all of the input stream before returning since there's no other
56
+ # way to determine the size of the request body beforehand.
57
+ #
58
+ # This method is no longer part of the Rack specification as of
59
+ # Rack 1.2, so its use is not recommended. This method only exists
60
+ # for compatibility with Rack applications designed for Rack 1.1 and
61
+ # earlier. Most applications should only need to call +read+ with a
62
+ # specified +length+ in a loop until it returns +nil+.
63
+ def size
64
+ @len and return @len
65
+ pos = @tmp.pos
66
+ consume!
67
+ @tmp.pos = pos
68
+ @len = @tmp.size
69
+ end
70
+
71
+ # :call-seq:
72
+ # ios.read([length [, buffer ]]) => string, buffer, or nil
73
+ #
74
+ # Reads at most length bytes from the I/O stream, or to the end of
75
+ # file if length is omitted or is nil. length must be a non-negative
76
+ # integer or nil. If the optional buffer argument is present, it
77
+ # must reference a String, which will receive the data.
78
+ #
79
+ # At end of file, it returns nil or "" depend on length.
80
+ # ios.read() and ios.read(nil) returns "".
81
+ # ios.read(length [, buffer]) returns nil.
82
+ #
83
+ # If the Content-Length of the HTTP request is known (as is the common
84
+ # case for POST requests), then ios.read(length [, buffer]) will block
85
+ # until the specified length is read (or it is the last chunk).
86
+ # Otherwise, for uncommon "Transfer-Encoding: chunked" requests,
87
+ # ios.read(length [, buffer]) will return immediately if there is
88
+ # any data and only block when nothing is available (providing
89
+ # IO#readpartial semantics).
90
+ def read(*args)
91
+ @socket ? tee(super) : @tmp.read(*args)
92
+ end
93
+
94
+ # :call-seq:
95
+ # ios.gets => string or nil
96
+ #
97
+ # Reads the next ``line'' from the I/O stream; lines are separated
98
+ # by the global record separator ($/, typically "\n"). A global
99
+ # record separator of nil reads the entire unread contents of ios.
100
+ # Returns nil if called at the end of file.
101
+ # This takes zero arguments for strict Rack::Lint compatibility,
102
+ # unlike IO#gets.
103
+ def gets
104
+ @socket ? tee(super) : @tmp.gets
105
+ end
106
+
107
+ # :call-seq:
108
+ # ios.rewind => 0
109
+ #
110
+ # Positions the *ios* pointer to the beginning of input, returns
111
+ # the offset (zero) of the +ios+ pointer. Subsequent reads will
112
+ # start from the beginning of the previously-buffered input.
113
+ def rewind
114
+ return 0 if 0 == @tmp.size
115
+ consume! if @socket
116
+ @tmp.rewind # Rack does not specify what the return value is here
117
+ end
118
+
119
+ private
120
+
121
+ # consumes the stream of the socket
122
+ def consume!
123
+ junk = ""
124
+ nil while read(@@io_chunk_size, junk)
125
+ end
126
+
127
+ def tee(buffer)
128
+ if buffer && buffer.size > 0
129
+ @tmp.write(buffer)
130
+ end
131
+ buffer
132
+ end
133
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: binary -*-
2
+ # :stopdoc:
3
+ require 'tmpdir'
4
+
5
+ # some versions of Ruby had a broken Tempfile which didn't work
6
+ # well with unlinked files. This one is much shorter, easier
7
+ # to understand, and slightly faster.
8
+ class Unicorn::TmpIO < File
9
+
10
+ # creates and returns a new File object. The File is unlinked
11
+ # immediately, switched to binary mode, and userspace output
12
+ # buffering is disabled
13
+ def self.new
14
+ fp = begin
15
+ super("#{Dir::tmpdir}/#{rand}", RDWR|CREAT|EXCL, 0600)
16
+ rescue Errno::EEXIST
17
+ retry
18
+ end
19
+ unlink(fp.path)
20
+ fp.binmode
21
+ fp.sync = true
22
+ fp
23
+ end
24
+
25
+ # pretend we're Tempfile for Rack::TempfileReaper
26
+ alias close! close
27
+ end