merb-core 0.9.7 → 0.9.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (274) hide show
  1. data/CHANGELOG +136 -2
  2. data/CONTRIBUTORS +6 -0
  3. data/PUBLIC_CHANGELOG +38 -0
  4. data/Rakefile +38 -30
  5. data/lib/merb-core.rb +88 -23
  6. data/lib/merb-core/bootloader.rb +235 -51
  7. data/lib/merb-core/config.rb +121 -36
  8. data/lib/merb-core/controller/abstract_controller.rb +59 -36
  9. data/lib/merb-core/controller/exceptions.rb +2 -15
  10. data/lib/merb-core/controller/merb_controller.rb +44 -1
  11. data/lib/merb-core/controller/mime.rb +4 -0
  12. data/lib/merb-core/controller/mixins/controller.rb +38 -21
  13. data/lib/merb-core/controller/mixins/render.rb +44 -29
  14. data/lib/merb-core/controller/mixins/responder.rb +3 -31
  15. data/lib/merb-core/controller/template.rb +45 -21
  16. data/lib/merb-core/core_ext/kernel.rb +60 -32
  17. data/lib/merb-core/dispatch/default_exception/default_exception.rb +2 -2
  18. data/lib/merb-core/dispatch/default_exception/views/_css.html.erb +3 -1
  19. data/lib/merb-core/dispatch/default_exception/views/_javascript.html.erb +71 -67
  20. data/lib/merb-core/dispatch/default_exception/views/index.html.erb +7 -3
  21. data/lib/merb-core/dispatch/dispatcher.rb +3 -3
  22. data/lib/merb-core/dispatch/request.rb +56 -9
  23. data/lib/merb-core/dispatch/router.rb +159 -133
  24. data/lib/merb-core/dispatch/router/behavior.rb +462 -703
  25. data/lib/merb-core/dispatch/router/cached_proc.rb +3 -3
  26. data/lib/merb-core/dispatch/router/resources.rb +289 -0
  27. data/lib/merb-core/dispatch/router/route.rb +514 -294
  28. data/lib/merb-core/dispatch/session.rb +4 -2
  29. data/lib/merb-core/logger.rb +213 -202
  30. data/lib/merb-core/plugins.rb +9 -1
  31. data/lib/merb-core/rack.rb +3 -1
  32. data/lib/merb-core/rack/adapter.rb +7 -4
  33. data/lib/merb-core/rack/adapter/abstract.rb +188 -0
  34. data/lib/merb-core/rack/adapter/ebb.rb +12 -13
  35. data/lib/merb-core/rack/adapter/evented_mongrel.rb +2 -15
  36. data/lib/merb-core/rack/adapter/irb.rb +3 -2
  37. data/lib/merb-core/rack/adapter/mongrel.rb +22 -15
  38. data/lib/merb-core/rack/adapter/swiftiplied_mongrel.rb +4 -16
  39. data/lib/merb-core/rack/adapter/thin.rb +21 -22
  40. data/lib/merb-core/rack/adapter/thin_turbo.rb +4 -11
  41. data/lib/merb-core/rack/adapter/webrick.rb +54 -18
  42. data/lib/merb-core/rack/application.rb +4 -4
  43. data/lib/merb-core/rack/handler/mongrel.rb +12 -13
  44. data/lib/merb-core/rack/middleware/csrf.rb +1 -1
  45. data/lib/merb-core/rack/stream_wrapper.rb +41 -0
  46. data/lib/merb-core/server.rb +157 -90
  47. data/lib/merb-core/tasks/gem_management.rb +267 -0
  48. data/lib/merb-core/tasks/merb.rb +1 -0
  49. data/lib/merb-core/tasks/merb_rake_helper.rb +48 -34
  50. data/lib/merb-core/tasks/stats.rake +1 -1
  51. data/lib/merb-core/test.rb +9 -3
  52. data/lib/merb-core/test/helpers.rb +1 -0
  53. data/lib/merb-core/test/helpers/mock_request_helper.rb +393 -0
  54. data/lib/merb-core/test/helpers/multipart_request_helper.rb +3 -2
  55. data/lib/merb-core/test/helpers/request_helper.rb +40 -372
  56. data/lib/merb-core/test/helpers/route_helper.rb +16 -2
  57. data/lib/merb-core/test/matchers.rb +1 -0
  58. data/lib/merb-core/test/matchers/controller_matchers.rb +4 -247
  59. data/lib/merb-core/test/matchers/request_matchers.rb +140 -0
  60. data/lib/merb-core/test/matchers/view_matchers.rb +22 -4
  61. data/lib/merb-core/test/run_specs.rb +118 -26
  62. data/lib/merb-core/test/test_ext/rspec.rb +181 -0
  63. data/lib/merb-core/version.rb +1 -9
  64. metadata +10 -342
  65. data/docs/bootloading.dox +0 -58
  66. data/docs/documentation_standards +0 -40
  67. data/docs/merb-core-call-stack-diagram.mmap +0 -0
  68. data/docs/merb-core-call-stack-diagram.pdf +0 -0
  69. data/docs/merb-core-call-stack-diagram.png +0 -0
  70. data/docs/new_render_api +0 -51
  71. data/lib/merb-core/vendor/facets.rb +0 -2
  72. data/lib/merb-core/vendor/facets/dictionary.rb +0 -433
  73. data/lib/merb-core/vendor/facets/inflect.rb +0 -342
  74. data/spec/private/config/adapter_spec.rb +0 -32
  75. data/spec/private/config/config_spec.rb +0 -202
  76. data/spec/private/config/environment_spec.rb +0 -13
  77. data/spec/private/config/merb_spec.rb +0 -34
  78. data/spec/private/config/spec_helper.rb +0 -1
  79. data/spec/private/core_ext/kernel_spec.rb +0 -159
  80. data/spec/private/dispatch/bootloader_spec.rb +0 -24
  81. data/spec/private/dispatch/fixture/app/controllers/application.rb +0 -4
  82. data/spec/private/dispatch/fixture/app/controllers/exceptions.rb +0 -25
  83. data/spec/private/dispatch/fixture/app/controllers/foo.rb +0 -19
  84. data/spec/private/dispatch/fixture/app/helpers/global_helpers.rb +0 -8
  85. data/spec/private/dispatch/fixture/app/views/exeptions/client_error.html.erb +0 -37
  86. data/spec/private/dispatch/fixture/app/views/exeptions/internal_server_error.html.erb +0 -216
  87. data/spec/private/dispatch/fixture/app/views/exeptions/not_acceptable.html.erb +0 -38
  88. data/spec/private/dispatch/fixture/app/views/exeptions/not_found.html.erb +0 -40
  89. data/spec/private/dispatch/fixture/app/views/foo/bar.html.erb +0 -0
  90. data/spec/private/dispatch/fixture/app/views/layout/application.html.erb +0 -11
  91. data/spec/private/dispatch/fixture/config/black_hole.rb +0 -12
  92. data/spec/private/dispatch/fixture/config/environments/development.rb +0 -6
  93. data/spec/private/dispatch/fixture/config/environments/production.rb +0 -5
  94. data/spec/private/dispatch/fixture/config/environments/test.rb +0 -6
  95. data/spec/private/dispatch/fixture/config/init.rb +0 -45
  96. data/spec/private/dispatch/fixture/config/rack.rb +0 -11
  97. data/spec/private/dispatch/fixture/config/router.rb +0 -35
  98. data/spec/private/dispatch/fixture/log/merb_test.log +0 -8839
  99. data/spec/private/dispatch/fixture/public/images/merb.jpg +0 -0
  100. data/spec/private/dispatch/fixture/public/merb.fcgi +0 -4
  101. data/spec/private/dispatch/fixture/public/stylesheets/master.css +0 -119
  102. data/spec/private/dispatch/route_params_spec.rb +0 -23
  103. data/spec/private/dispatch/spec_helper.rb +0 -1
  104. data/spec/private/router/behavior_spec.rb +0 -60
  105. data/spec/private/router/fixture/log/merb_test.log +0 -35
  106. data/spec/private/router/route_spec.rb +0 -418
  107. data/spec/private/router/router_spec.rb +0 -183
  108. data/spec/private/vendor/facets/plural_spec.rb +0 -564
  109. data/spec/private/vendor/facets/singular_spec.rb +0 -489
  110. data/spec/public/DEFINITIONS +0 -11
  111. data/spec/public/abstract_controller/controllers/alt_views/layout/application.erb +0 -1
  112. data/spec/public/abstract_controller/controllers/alt_views/layout/merb/test/fixtures/abstract/render_string_controller_layout.erb +0 -1
  113. data/spec/public/abstract_controller/controllers/alt_views/layout/merb/test/fixtures/abstract/render_template_controller_layout.erb +0 -1
  114. data/spec/public/abstract_controller/controllers/alt_views/merb/test/fixtures/abstract/display_object_with_multiple_roots/index.erb +0 -1
  115. data/spec/public/abstract_controller/controllers/alt_views/merb/test/fixtures/abstract/display_object_with_multiple_roots/show.erb +0 -1
  116. data/spec/public/abstract_controller/controllers/alt_views/merb/test/fixtures/abstract/render_template_multiple_roots/index.erb +0 -1
  117. data/spec/public/abstract_controller/controllers/alt_views/partial/basic_partial_with_multiple_roots/_partial.erb +0 -1
  118. data/spec/public/abstract_controller/controllers/alt_views/render_template_multiple_roots_and_custom_location/index.erb +0 -1
  119. data/spec/public/abstract_controller/controllers/alt_views/render_template_multiple_roots_inherited/index.erb +0 -1
  120. data/spec/public/abstract_controller/controllers/cousins.rb +0 -41
  121. data/spec/public/abstract_controller/controllers/display.rb +0 -60
  122. data/spec/public/abstract_controller/controllers/filters.rb +0 -260
  123. data/spec/public/abstract_controller/controllers/helpers.rb +0 -41
  124. data/spec/public/abstract_controller/controllers/partial.rb +0 -121
  125. data/spec/public/abstract_controller/controllers/render.rb +0 -113
  126. data/spec/public/abstract_controller/controllers/views/helpers/capture/index.erb +0 -1
  127. data/spec/public/abstract_controller/controllers/views/helpers/capture_eq/index.erb +0 -1
  128. data/spec/public/abstract_controller/controllers/views/helpers/capture_with_args/index.erb +0 -1
  129. data/spec/public/abstract_controller/controllers/views/helpers/concat/index.erb +0 -1
  130. data/spec/public/abstract_controller/controllers/views/layout/alt.erb +0 -1
  131. data/spec/public/abstract_controller/controllers/views/layout/custom.erb +0 -1
  132. data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/display_object/index.erb +0 -1
  133. data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/display_object_with_action/new.erb +0 -1
  134. data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template/index.erb +0 -1
  135. data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_app_layout/index.erb +0 -0
  136. data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_custom_layout/index.erb +0 -1
  137. data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_multiple_roots/index.erb +0 -1
  138. data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_multiple_roots/show.erb +0 -1
  139. data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_two_throw_contents/index.erb +0 -1
  140. data/spec/public/abstract_controller/controllers/views/partial/another_directory/_partial.erb +0 -1
  141. data/spec/public/abstract_controller/controllers/views/partial/basic_partial/_partial.erb +0 -1
  142. data/spec/public/abstract_controller/controllers/views/partial/basic_partial/index.erb +0 -1
  143. data/spec/public/abstract_controller/controllers/views/partial/basic_partial_with_multiple_roots/index.erb +0 -1
  144. data/spec/public/abstract_controller/controllers/views/partial/nested_partial/_first.erb +0 -1
  145. data/spec/public/abstract_controller/controllers/views/partial/nested_partial/_second.erb +0 -1
  146. data/spec/public/abstract_controller/controllers/views/partial/nested_partial/index.erb +0 -1
  147. data/spec/public/abstract_controller/controllers/views/partial/partial_in_another_directory/index.erb +0 -1
  148. data/spec/public/abstract_controller/controllers/views/partial/partial_with_both/_collection.erb +0 -1
  149. data/spec/public/abstract_controller/controllers/views/partial/partial_with_both/index.erb +0 -1
  150. data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections/_collection.erb +0 -1
  151. data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections/index.erb +0 -1
  152. data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections_and_as/_collection.erb +0 -1
  153. data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections_and_as/index.erb +0 -1
  154. data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections_and_counter/_collection.erb +0 -1
  155. data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections_and_counter/index.erb +0 -1
  156. data/spec/public/abstract_controller/controllers/views/partial/partial_with_locals/_variables.erb +0 -1
  157. data/spec/public/abstract_controller/controllers/views/partial/partial_with_locals/index.erb +0 -1
  158. data/spec/public/abstract_controller/controllers/views/partial/partial_with_with_and_locals/_both.erb +0 -1
  159. data/spec/public/abstract_controller/controllers/views/partial/partial_with_with_and_locals/index.erb +0 -1
  160. data/spec/public/abstract_controller/controllers/views/partial/with_absolute_partial/_partial.erb +0 -1
  161. data/spec/public/abstract_controller/controllers/views/partial/with_absolute_partial/index.erb +0 -1
  162. data/spec/public/abstract_controller/controllers/views/partial/with_as_partial/_with_partial.erb +0 -1
  163. data/spec/public/abstract_controller/controllers/views/partial/with_as_partial/index.erb +0 -1
  164. data/spec/public/abstract_controller/controllers/views/partial/with_nil_partial/_with_partial.erb +0 -1
  165. data/spec/public/abstract_controller/controllers/views/partial/with_nil_partial/index.erb +0 -1
  166. data/spec/public/abstract_controller/controllers/views/partial/with_partial/_with_partial.erb +0 -1
  167. data/spec/public/abstract_controller/controllers/views/partial/with_partial/index.erb +0 -1
  168. data/spec/public/abstract_controller/controllers/views/test_display/foo.html.erb +0 -1
  169. data/spec/public/abstract_controller/controllers/views/test_render/foo.html.erb +0 -0
  170. data/spec/public/abstract_controller/controllers/views/wonderful/index.erb +0 -1
  171. data/spec/public/abstract_controller/display_spec.rb +0 -37
  172. data/spec/public/abstract_controller/filter_spec.rb +0 -136
  173. data/spec/public/abstract_controller/helper_spec.rb +0 -21
  174. data/spec/public/abstract_controller/partial_spec.rb +0 -61
  175. data/spec/public/abstract_controller/render_spec.rb +0 -90
  176. data/spec/public/abstract_controller/spec_helper.rb +0 -31
  177. data/spec/public/boot_loader/boot_loader_spec.rb +0 -33
  178. data/spec/public/boot_loader/spec_helper.rb +0 -1
  179. data/spec/public/controller/authentication_spec.rb +0 -174
  180. data/spec/public/controller/base_spec.rb +0 -88
  181. data/spec/public/controller/conditional_get_spec.rb +0 -100
  182. data/spec/public/controller/config/init.rb +0 -6
  183. data/spec/public/controller/controllers/authentication.rb +0 -74
  184. data/spec/public/controller/controllers/base.rb +0 -65
  185. data/spec/public/controller/controllers/conditional_get.rb +0 -35
  186. data/spec/public/controller/controllers/cookies.rb +0 -36
  187. data/spec/public/controller/controllers/dispatcher.rb +0 -35
  188. data/spec/public/controller/controllers/display.rb +0 -118
  189. data/spec/public/controller/controllers/redirect.rb +0 -36
  190. data/spec/public/controller/controllers/responder.rb +0 -93
  191. data/spec/public/controller/controllers/url.rb +0 -7
  192. data/spec/public/controller/controllers/views/layout/custom.html.erb +0 -1
  193. data/spec/public/controller/controllers/views/layout/custom_arg.html.erb +0 -1
  194. data/spec/public/controller/controllers/views/layout/custom_arg.json.erb +0 -1
  195. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/class_and_local_provides/index.html.erb +0 -1
  196. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/class_and_local_provides/index.xml.erb +0 -1
  197. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/class_provides/index.html.erb +0 -1
  198. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/class_provides/index.xml.erb +0 -1
  199. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/display_with_template/index.html.erb +0 -1
  200. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/display_with_template/no_layout.html.erb +0 -1
  201. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/display_with_template_argument/index.html.erb +0 -1
  202. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/html_default/index.html.erb +0 -1
  203. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/layout/custom.html.erb +0 -1
  204. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/local_provides/index.html.erb +0 -1
  205. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/local_provides/index.xml.erb +0 -1
  206. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/multi_provides/index.html.erb +0 -1
  207. data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/multi_provides/index.js.erb +0 -1
  208. data/spec/public/controller/cookies_spec.rb +0 -95
  209. data/spec/public/controller/dispatcher_spec.rb +0 -410
  210. data/spec/public/controller/display_spec.rb +0 -84
  211. data/spec/public/controller/redirect_spec.rb +0 -33
  212. data/spec/public/controller/responder_spec.rb +0 -169
  213. data/spec/public/controller/spec_helper.rb +0 -13
  214. data/spec/public/controller/url_spec.rb +0 -255
  215. data/spec/public/core/merb_core_spec.rb +0 -45
  216. data/spec/public/core_ext/fixtures/core_ext_dependency.rb +0 -2
  217. data/spec/public/core_ext/kernel_spec.rb +0 -88
  218. data/spec/public/core_ext/spec_helper.rb +0 -1
  219. data/spec/public/directory_structure/directory/app/controllers/application.rb +0 -3
  220. data/spec/public/directory_structure/directory/app/controllers/base.rb +0 -13
  221. data/spec/public/directory_structure/directory/app/controllers/custom.rb +0 -19
  222. data/spec/public/directory_structure/directory/app/views/base/template.html.erb +0 -1
  223. data/spec/public/directory_structure/directory/app/views/wonderful/template.erb +0 -1
  224. data/spec/public/directory_structure/directory/config/router.rb +0 -3
  225. data/spec/public/directory_structure/directory/log/merb_test.log +0 -4588
  226. data/spec/public/directory_structure/directory_spec.rb +0 -43
  227. data/spec/public/logger/logger_spec.rb +0 -181
  228. data/spec/public/logger/spec_helper.rb +0 -1
  229. data/spec/public/rack/conditinal_get_middleware_spec.rb +0 -127
  230. data/spec/public/rack/csrf_middleware_spec.rb +0 -70
  231. data/spec/public/rack/rack_middleware_spec.rb +0 -99
  232. data/spec/public/rack/shared_example_groups.rb +0 -35
  233. data/spec/public/reloading/directory/app/controllers/application.rb +0 -3
  234. data/spec/public/reloading/directory/app/controllers/reload.rb +0 -6
  235. data/spec/public/reloading/directory/config/init.rb +0 -2
  236. data/spec/public/reloading/directory/log/merb_test.log +0 -288231
  237. data/spec/public/reloading/reload_spec.rb +0 -103
  238. data/spec/public/request/multipart_spec.rb +0 -41
  239. data/spec/public/request/request_spec.rb +0 -250
  240. data/spec/public/router/default_spec.rb +0 -21
  241. data/spec/public/router/deferred_spec.rb +0 -22
  242. data/spec/public/router/fixation_spec.rb +0 -49
  243. data/spec/public/router/fixture/log/merb_test.log +0 -524
  244. data/spec/public/router/namespace_spec.rb +0 -113
  245. data/spec/public/router/nested_matches_spec.rb +0 -97
  246. data/spec/public/router/nested_resources_spec.rb +0 -41
  247. data/spec/public/router/resource_spec.rb +0 -37
  248. data/spec/public/router/resources_spec.rb +0 -82
  249. data/spec/public/router/spec_helper.rb +0 -90
  250. data/spec/public/router/special_spec.rb +0 -61
  251. data/spec/public/router/string_spec.rb +0 -61
  252. data/spec/public/session/controllers/sessions.rb +0 -56
  253. data/spec/public/session/cookie_session_spec.rb +0 -73
  254. data/spec/public/session/memcached_session_spec.rb +0 -31
  255. data/spec/public/session/memory_session_spec.rb +0 -28
  256. data/spec/public/session/multiple_sessions_spec.rb +0 -74
  257. data/spec/public/session/no_session_spec.rb +0 -12
  258. data/spec/public/session/session_spec.rb +0 -106
  259. data/spec/public/template/template_spec.rb +0 -104
  260. data/spec/public/template/templates/error.html.erb +0 -2
  261. data/spec/public/template/templates/template.html.erb +0 -1
  262. data/spec/public/template/templates/template.html.myt +0 -1
  263. data/spec/public/test/controller_matchers_spec.rb +0 -412
  264. data/spec/public/test/controllers/controller_assertion_mock.rb +0 -7
  265. data/spec/public/test/controllers/dispatch_controller.rb +0 -11
  266. data/spec/public/test/controllers/spec_helper_controller.rb +0 -39
  267. data/spec/public/test/multipart_request_helper_spec.rb +0 -159
  268. data/spec/public/test/multipart_upload_text_file.txt +0 -1
  269. data/spec/public/test/request_helper_spec.rb +0 -269
  270. data/spec/public/test/route_helper_spec.rb +0 -78
  271. data/spec/public/test/route_matchers_spec.rb +0 -166
  272. data/spec/public/test/view_helper_spec.rb +0 -96
  273. data/spec/public/test/view_matchers_spec.rb +0 -183
  274. data/spec/spec_helper.rb +0 -121
@@ -1,36 +1,72 @@
1
1
  require 'webrick'
2
+ require 'webrick/utils'
2
3
  require 'rack/handler/webrick'
3
4
  module Merb
4
5
  module Rack
5
6
 
6
- class WEBrick
7
- # start WEBrick server on given host and port.
7
+ class WEBrick < Merb::Rack::AbstractAdapter
8
8
 
9
- # ==== Parameters
10
- # opts<Hash>:: Options for WEBrick (see below).
11
- #
12
- # ==== Options (opts)
13
- # :host<String>:: The hostname that WEBrick should serve.
14
- # :port<Fixnum>:: The port WEBrick should bind to.
15
- # :app<String>>:: The application name.
16
- def self.start(opts={})
17
- Merb.logger.warn!("Using Webrick adapter")
9
+ class << self
10
+ attr_accessor :server
11
+ end
18
12
 
13
+ def self.new_server(port)
19
14
  options = {
20
- :Port => opts[:port],
21
- :BindAddress => opts[:host],
15
+ :Port => port,
16
+ :BindAddress => @opts[:host],
22
17
  :Logger => Merb.logger,
23
18
  :AccessLog => [
24
19
  [Merb.logger, ::WEBrick::AccessLog::COMMON_LOG_FORMAT],
25
20
  [Merb.logger, ::WEBrick::AccessLog::REFERER_LOG_FORMAT]
26
21
  ]
27
22
  }
28
-
29
- server = ::WEBrick::HTTPServer.new(options)
30
- Merb::Server.change_privilege
31
- server.mount("/", ::Rack::Handler::WEBrick, opts[:app])
32
- server.start
23
+
24
+ sockets = ::WEBrick::Utils.create_listeners nil, port
25
+ @server = ::WEBrick::HTTPServer.new(options.merge(:DoNotListen => true))
26
+ @server.listeners.replace sockets
33
27
  end
28
+
29
+ def self.start_server
30
+ @server.mount("/", ::Rack::Handler::WEBrick, @opts[:app])
31
+ @server.start
32
+ exit(@status)
33
+ end
34
+
35
+ def self.stop(status = 0)
36
+ @status = status
37
+ @server.shutdown
38
+ end
39
+
40
+ def self.exit_process(status = 0)
41
+ end
42
+
43
+ # start WEBrick server on given host and port.
44
+
45
+ # ==== Parameters
46
+ # opts<Hash>:: Options for WEBrick (see below).
47
+ #
48
+ # ==== Options (opts)
49
+ # :host<String>:: The hostname that WEBrick should serve.
50
+ # :port<Fixnum>:: The port WEBrick should bind to.
51
+ # :app<String>>:: The application name.
52
+ # def self.start(opts={})
53
+ # Merb.logger.warn!("Using Webrick adapter")
54
+ #
55
+ # options = {
56
+ # :Port => opts[:port],
57
+ # :BindAddress => opts[:host],
58
+ # :Logger => Merb.logger,
59
+ # :AccessLog => [
60
+ # [Merb.logger, ::WEBrick::AccessLog::COMMON_LOG_FORMAT],
61
+ # [Merb.logger, ::WEBrick::AccessLog::REFERER_LOG_FORMAT]
62
+ # ]
63
+ # }
64
+ #
65
+ # server = ::WEBrick::HTTPServer.new(options)
66
+ # Merb::Server.change_privilege
67
+ # server.mount("/", ::Rack::Handler::WEBrick, opts[:app])
68
+ # server.start
69
+ # end
34
70
  end
35
71
  end
36
72
  end
@@ -11,10 +11,10 @@ module Merb
11
11
  Merb.logger.info "\n\n"
12
12
  Merb.logger.flush
13
13
 
14
- unless controller.headers[Merb::Const::DATE]
15
- require "time"
16
- controller.headers[Merb::Const::DATE] = Time.now.rfc2822.to_s
17
- end
14
+ # unless controller.headers[Merb::Const::DATE]
15
+ # require "time"
16
+ # controller.headers[Merb::Const::DATE] = Time.now.rfc2822.to_s
17
+ # end
18
18
  controller.rack_response
19
19
  end
20
20
 
@@ -32,11 +32,15 @@ module Merb
32
32
  # The hostname on which the app should run. Defaults to "0.0.0.0"
33
33
  # :Port<Fixnum>:: The port for the app. Defaults to 8080.
34
34
  def self.run(app, options={})
35
- server = ::Mongrel::HttpServer.new(options[:Host] || '0.0.0.0',
35
+ @server = ::Mongrel::HttpServer.new(options[:Host] || '0.0.0.0',
36
36
  options[:Port] || 8080)
37
- server.register('/', ::Merb::Rack::Handler::Mongrel.new(app))
38
- yield server if block_given?
39
- server.run.join
37
+ @server.register('/', ::Merb::Rack::Handler::Mongrel.new(app))
38
+ yield @server if block_given?
39
+ @server.run.join
40
+ end
41
+
42
+ def self.stop(block = true)
43
+ @server.stop
40
44
  end
41
45
 
42
46
  # ==== Parameters
@@ -63,8 +67,7 @@ module Merb
63
67
  "rack.multiprocess" => false, # ???
64
68
  "rack.run_once" => false,
65
69
 
66
- "rack.url_scheme" => "http",
67
- "rack.streaming" => true
70
+ "rack.url_scheme" => "http"
68
71
  })
69
72
  env["QUERY_STRING"] ||= ""
70
73
  env.delete "PATH_INFO" if env["PATH_INFO"] == ""
@@ -79,13 +82,9 @@ module Merb
79
82
  }
80
83
  }
81
84
 
82
- if Proc === body
83
- body.call(response)
84
- else
85
- body.each { |part|
86
- response.body << part
87
- }
88
- end
85
+ body.each { |part|
86
+ response.body << part
87
+ }
89
88
  response.finished
90
89
  ensure
91
90
  body.close if body.respond_to? :close
@@ -10,7 +10,7 @@ module Merb
10
10
 
11
11
  def call(env)
12
12
  status, header, body = @app.call(env)
13
-
13
+ body = body.to_s
14
14
  if env[Merb::Const::REQUEST_METHOD] == Merb::Const::GET
15
15
  body = process_response(body) if valid_content_type?(header[Merb::Const::CONTENT_TYPE])
16
16
  elsif env[Merb::Const::REQUEST_METHOD] == Merb::Const::POST
@@ -0,0 +1,41 @@
1
+ module Merb
2
+ module Rack
3
+
4
+ class StreamWrapper
5
+ def initialize(body)
6
+ @body = body
7
+ end
8
+
9
+ def each(&callback)
10
+ if Proc === @body
11
+ @writer = lambda { |x| callback.call(x) }
12
+ @body.call(self)
13
+ elsif @body.is_a?(String)
14
+ @body.each_line(&callback)
15
+ else
16
+ @body.each(&callback)
17
+ end
18
+ end
19
+
20
+ def write(str)
21
+ @writer.call str.to_s
22
+ str
23
+ end
24
+
25
+ def to_s
26
+ @body.to_s
27
+ end
28
+
29
+ def ==(other)
30
+ @body == other
31
+ end
32
+
33
+ def method_missing(sym, *args, &blk)
34
+ @body.send(sym, *args, &blk)
35
+ end
36
+
37
+
38
+ end
39
+
40
+ end
41
+ end
@@ -1,4 +1,5 @@
1
1
  require 'etc'
2
+
2
3
  module Merb
3
4
 
4
5
  # Server encapsulates the management of Merb daemons.
@@ -18,40 +19,25 @@ module Merb
18
19
  # If cluster is left out, then one process will be started. This process
19
20
  # will be daemonized if Merb::Config[:daemonize] is true.
20
21
  def start(port, cluster=nil)
22
+
21
23
  @port = port
22
- @cluster = cluster
23
- if @cluster
24
- @port.to_i.upto(@port.to_i + @cluster.to_i-1) do |port|
25
- pidfile = pid_file(port)
26
- pid = IO.read(pidfile).chomp.to_i if File.exist?(pidfile)
27
-
28
- unless alive?(port)
29
- remove_pid_file(port)
30
- puts "Starting merb server on port #{port}, pid file: #{pidfile} and process id is #{pid}" if Merb::Config[:verbose]
31
- daemonize(port)
32
- else
33
- raise "Merb is already running: port is #{port}, pid file: #{pidfile}, process id is #{pid}"
34
- end
35
- end
36
- elsif Merb::Config[:daemonize]
24
+ @cluster = cluster
25
+
26
+ if Merb::Config[:daemonize]
37
27
  pidfile = pid_file(port)
38
- pid = IO.read(pidfile).chomp.to_i if File.exist?(pidfile)
28
+ pid = File.read(pidfile).chomp.to_i if File.exist?(pidfile)
39
29
 
40
30
  unless alive?(@port)
41
31
  remove_pid_file(@port)
42
32
  puts "Daemonizing..." if Merb::Config[:verbose]
43
33
  daemonize(@port)
44
34
  else
45
- raise "Merb is already running: port is #{port}, pid file: #{pidfile}, process id is #{pid}"
35
+ Merb.fatal! "Merb is already running on port #{port}.\n" \
36
+ "\e[0m \e[1;31;47mpid file: \e[34;47m#{pidfile}" \
37
+ "\e[1;31;47m, process id is \e[34;47m#{pid}."
46
38
  end
47
39
  else
48
- trap('TERM') { exit }
49
- trap('INT') { puts "\nExiting"; exit }
50
- puts "Running bootloaders..." if Merb::Config[:verbose]
51
- BootLoader.run
52
- puts "Starting Rack adapter..." if Merb::Config[:verbose]
53
- Merb.logger.info! "Starting Merb server listening at #{Merb::Config[:host]}:#{port}"
54
- Merb.adapter.start(Merb::Config.to_hash)
40
+ bootup
55
41
  end
56
42
  end
57
43
 
@@ -65,12 +51,14 @@ module Merb
65
51
  puts "About to check if port #{port} is alive..." if Merb::Config[:verbose]
66
52
  pidfile = pid_file(port)
67
53
  puts "Pidfile is #{pidfile}..." if Merb::Config[:verbose]
68
- pid = IO.read(pidfile).chomp.to_i
54
+ pid = File.read(pidfile).chomp.to_i
69
55
  puts "Process id is #{pid}" if Merb::Config[:verbose]
70
56
  Process.kill(0, pid)
71
57
  true
72
- rescue
58
+ rescue Errno::ESRCH, Errno::ENOENT
73
59
  false
60
+ rescue Errno::EACCES => e
61
+ Merb.fatal!("You don't have access to the PID file at #{pidfile}.", e)
74
62
  end
75
63
 
76
64
  # ==== Parameters
@@ -80,32 +68,51 @@ module Merb
80
68
  # ==== Alternatives
81
69
  # If you pass "all" as the port, the signal will be sent to all Merb
82
70
  # processes.
83
- def kill(port, sig=9)
71
+ def kill(port, sig="INT")
84
72
  Merb::BootLoader::BuildFramework.run
73
+ if sig == 9 && port == "main"
74
+ kill_pid("INT", pid_file("main"))
75
+ Dir["#{Merb.log_path}" / "*.pid"].each do |file|
76
+ kill_pid(9, file)
77
+ end
78
+ else
79
+ kill_pid(sig, pid_file(port))
80
+ end
81
+
82
+ if sig.is_a?(Integer)
83
+ sig = Signal.list.invert[sig]
84
+ end
85
+
86
+ if sig == "KILL" && port == "main"
87
+ Merb.fatal! "Killed all PIDs with signal KILL"
88
+ else
89
+ Merb.fatal! "Killed #{port} with signal #{sig}"
90
+ end
91
+ end
92
+
93
+ def kill_pid(sig, file)
85
94
  begin
86
- pidfiles = port == "all" ?
87
- pid_files : [ pid_file(port) ]
88
-
89
- pidfiles.each do |f|
90
- pid = IO.read(f).chomp.to_i
91
- begin
92
- Process.kill(sig, pid)
93
- FileUtils.rm(f) if File.exist?(f)
94
- puts "killed PID #{pid} with signal #{sig}"
95
- rescue Errno::EINVAL
96
- puts "Failed to kill PID #{pid}: '#{sig}' is an invalid or unsupported signal number."
97
- rescue Errno::EPERM
98
- puts "Failed to kill PID #{pid}: Insufficient permissions."
99
- rescue Errno::ESRCH
100
- puts "Failed to kill PID #{pid}: Process is deceased or zombie."
101
- FileUtils.rm f
102
- rescue Exception => e
103
- puts "Failed to kill PID #{pid}: #{e.message}"
104
- end
95
+ pid = File.read(file).chomp.to_i
96
+ Merb.logger.warn! "Killing pid #{pid}"
97
+ Process.kill(sig, pid)
98
+ FileUtils.rm(file) if File.exist?(file)
99
+ rescue Errno::EINVAL
100
+ Merb.fatal! "Failed to kill PID #{pid}: '#{sig}' is an invalid " \
101
+ "or unsupported signal number."
102
+ rescue Errno::EPERM
103
+ Merb.fatal! "Failed to kill PID #{pid}: Insufficient permissions."
104
+ rescue Errno::ESRCH
105
+ FileUtils.rm file
106
+ Merb.fatal! "Failed to kill PID #{pid}: Process is " \
107
+ "deceased or zombie."
108
+ rescue Errno::EACCES => e
109
+ Merb.fatal! e.message, e
110
+ rescue Errno::ENOENT => e
111
+ Merb.fatal! "Could not find a PID file at #{file}", e
112
+ rescue Exception => e
113
+ if !e.is_a?(SystemExit)
114
+ Merb.fatal! "Failed to kill PID #{pid}", e
105
115
  end
106
- ensure
107
- Merb.started = false
108
- exit
109
116
  end
110
117
  end
111
118
 
@@ -116,28 +123,45 @@ module Merb
116
123
  fork do
117
124
  Process.setsid
118
125
  exit if fork
126
+ Merb.logger.warn! "In #{Process.pid}" if Merb.logger
119
127
  File.umask 0000
120
128
  STDIN.reopen "/dev/null"
121
129
  STDOUT.reopen "/dev/null", "a"
122
130
  STDERR.reopen STDOUT
123
- trap("TERM") { exit }
124
- Dir.chdir Merb::Config[:merb_root]
131
+ begin
132
+ Dir.chdir Merb::Config[:merb_root]
133
+ rescue Errno::EACCES => e
134
+ Merb.fatal! "You specified #{Merb::Config[:merb_root]} " \
135
+ "as the Merb root, but you did not have access to it.", e
136
+ end
125
137
  at_exit { remove_pid_file(port) }
126
138
  Merb::Config[:port] = port
127
- BootLoader.run
128
- Merb.adapter.start(Merb::Config.to_hash)
139
+ bootup
129
140
  end
141
+ rescue NotImplementedError => e
142
+ Merb.fatal! "Daemonized mode is not supported on your platform", e
143
+ end
144
+
145
+ def bootup
146
+ Merb.trap('TERM') { exit }
147
+
148
+ puts "Running bootloaders..." if Merb::Config[:verbose]
149
+ BootLoader.run
150
+ puts "Starting Rack adapter..." if Merb::Config[:verbose]
151
+ Merb.adapter.start(Merb::Config.to_hash)
130
152
  end
131
153
 
132
154
  def change_privilege
133
- if Merb::Config[:user]
134
- if Merb::Config[:group]
135
- puts "About to change privilege to group #{Merb::Config[:group]} and user #{Merb::Config[:user]}" if Merb::Config[:verbose]
136
- _change_privilege(Merb::Config[:user], Merb::Config[:group])
137
- else
138
- puts "About to change privilege to user #{Merb::Config[:user]}" if Merb::Config[:verbose]
139
- _change_privilege(Merb::Config[:user])
140
- end
155
+ if Merb::Config[:user] && Merb::Config[:group]
156
+ Merb.logger.verbose! "About to change privilege to group " \
157
+ "#{Merb::Config[:group]} and user #{Merb::Config[:user]}"
158
+ _change_privilege(Merb::Config[:user], Merb::Config[:group])
159
+ elsif Merb::Config[:user]
160
+ Merb.logger.verbose! "About to change privilege to user " \
161
+ "#{Merb::Config[:user]}"
162
+ _change_privilege(Merb::Config[:user])
163
+ else
164
+ return true
141
165
  end
142
166
  end
143
167
 
@@ -154,8 +178,10 @@ module Merb
154
178
  # instead of the port based PID file.
155
179
  def remove_pid_file(port)
156
180
  pidfile = pid_file(port)
157
- puts "Removing pid file #{pidfile} (port is #{port})..."
158
- FileUtils.rm(pidfile) if File.exist?(pidfile)
181
+ if File.exist?(pidfile)
182
+ puts "Removing pid file #{pidfile} (port is #{port})..."
183
+ FileUtils.rm(pidfile)
184
+ end
159
185
  end
160
186
 
161
187
  # Stores a PID file on the filesystem.
@@ -170,11 +196,23 @@ module Merb
170
196
  # If Merb::Config[:pid_file] has been specified, that will be used
171
197
  # instead of the port based PID file.
172
198
  def store_pid(port)
173
- pidfile = pid_file(port)
174
- puts "Storing pid file to #{pidfile}..."
175
- FileUtils.mkdir_p(File.dirname(pidfile)) unless File.directory?(File.dirname(pidfile))
176
- puts "Created directory, writing process id..." if Merb::Config[:verbose]
177
- File.open(pidfile, 'w'){ |f| f.write("#{Process.pid}") }
199
+ store_details(port)
200
+ end
201
+
202
+ def remove_pid(port)
203
+ FileUtils.rm(pid_file(port)) if File.file?(pid_file(port))
204
+ end
205
+
206
+ def store_details(port = nil)
207
+ file = pid_file(port)
208
+ begin
209
+ FileUtils.mkdir_p(File.dirname(file))
210
+ rescue Errno::EACCES => e
211
+ Merb.fatal! "You tried to store Merb logs in #{File.dirname(file)}, " \
212
+ "but you did not have access.", e
213
+ end
214
+ Merb.logger.warn! "Storing #{type} file to #{file}..." if Merb::Config[:verbose]
215
+ File.open(file, 'w'){ |f| f.write(Process.pid.to_s) }
178
216
  end
179
217
 
180
218
  # Gets the pid file for the specified port.
@@ -188,20 +226,8 @@ module Merb
188
226
  # Location of pid file for specified port. If clustered and pid_file option
189
227
  # is specified, it adds the port value to the path.
190
228
  def pid_file(port)
191
- if Merb::Config[:pid_file]
192
- pidfile = Merb::Config[:pid_file]
193
- if Merb::Config[:cluster]
194
- ext = File.extname(Merb::Config[:pid_file])
195
- base = File.basename(Merb::Config[:pid_file], ext)
196
- dir = File.dirname(Merb::Config[:pid_file])
197
- File.join(dir, "#{base}.#{port}#{ext}")
198
- else
199
- Merb::Config[:pid_file]
200
- end
201
- else
202
- pidfile = Merb.log_path / "merb.#{port}.pid"
203
- Merb.log_path / "merb.#{port}.pid"
204
- end
229
+ pidfile = Merb::Config[:pid_file] || (Merb.log_path / "merb.%s.pid")
230
+ pidfile % port
205
231
  end
206
232
 
207
233
  # Get a list of the pid files.
@@ -212,10 +238,7 @@ module Merb
212
238
  def pid_files
213
239
  if Merb::Config[:pid_file]
214
240
  if Merb::Config[:cluster]
215
- ext = File.extname(Merb::Config[:pid_file])
216
- base = File.basename(Merb::Config[:pid_file], ext)
217
- dir = File.dirname(Merb::Config[:pid_file])
218
- Dir[dir / "#{base}.*#{ext}"]
241
+ Dir[Merb::Config[:pid_file] % "*"]
219
242
  else
220
243
  [ Merb::Config[:pid_file] ]
221
244
  end
@@ -234,11 +257,25 @@ module Merb
234
257
  # If group is left out, the user will be used as the group.
235
258
  def _change_privilege(user, group=user)
236
259
 
237
- puts "Changing privileges to #{user}:#{group}"
260
+ Merb.logger.warn! "Changing privileges to #{user}:#{group}"
238
261
 
239
262
  uid, gid = Process.euid, Process.egid
240
- target_uid = Etc.getpwnam(user).uid
241
- target_gid = Etc.getgrnam(group).gid
263
+
264
+ begin
265
+ target_uid = Etc.getpwnam(user).uid
266
+ rescue ArgumentError => e
267
+ Merb.fatal!(
268
+ "You tried to use user #{user}, but no such user was found", e)
269
+ return false
270
+ end
271
+
272
+ begin
273
+ target_gid = Etc.getgrnam(group).gid
274
+ rescue ArgumentError => e
275
+ Merb.fatal!(
276
+ "You tried to use group #{group}, but no such group was found", e)
277
+ return false
278
+ end
242
279
 
243
280
  if uid != target_uid || gid != target_gid
244
281
  # Change process ownership
@@ -246,8 +283,38 @@ module Merb
246
283
  Process::GID.change_privilege(target_gid)
247
284
  Process::UID.change_privilege(target_uid)
248
285
  end
286
+ true
249
287
  rescue Errno::EPERM => e
250
- puts "Couldn't change user and group to #{user}:#{group}: #{e}"
288
+ Merb.fatal! "Couldn't change user and group to #{user}:#{group}", e
289
+ false
290
+ end
291
+
292
+ def add_irb_trap
293
+ Merb.trap('INT') do
294
+ if @interrupted
295
+ puts "Exiting\n"
296
+ exit
297
+ end
298
+
299
+ @interrupted = true
300
+ puts "Interrupt a second time to quit"
301
+ Kernel.sleep 1.5
302
+ ARGV.clear # Avoid passing args to IRB
303
+
304
+ if @irb.nil?
305
+ require 'irb'
306
+ IRB.setup(nil)
307
+ @irb = IRB::Irb.new(nil)
308
+ IRB.conf[:MAIN_CONTEXT] = @irb.context
309
+ end
310
+
311
+ Merb.trap(:INT) { @irb.signal_handle }
312
+ catch(:IRB_EXIT) { @irb.eval_input }
313
+
314
+ puts "Exiting IRB mode, back in server mode"
315
+ @interrupted = false
316
+ add_irb_trap
317
+ end
251
318
  end
252
319
  end
253
320
  end