ebb 0.3.1 → 0.3.2

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.
Files changed (3) hide show
  1. data/lib/ebb.rb +52 -20
  2. data/lib/ebb/version.rb +1 -1
  3. metadata +2 -2
data/lib/ebb.rb CHANGED
@@ -10,12 +10,16 @@ module Ebb
10
10
  def self.running?
11
11
  FFI::server_open?
12
12
  end
13
+
14
+ def self.ssl?
15
+ @@secure
16
+ end
13
17
 
14
18
  def self.stop_server()
15
19
  @running = false
16
20
  end
17
21
 
18
- def self.start_server(app, options={})
22
+ def self.set_options(options)
19
23
  if options.has_key?(:fileno)
20
24
  fd = options[:fileno].to_i
21
25
  FFI::server_listen_on_fd(fd)
@@ -31,6 +35,7 @@ module Ebb
31
35
  log.puts "Ebb is listening at http://0.0.0.0:#{port}/"
32
36
  end
33
37
 
38
+ @@secure = false
34
39
  if options.has_key?(:ssl_cert) and options.has_key?(:ssl_key)
35
40
  unless FFI.respond_to?(:server_set_secure)
36
41
  log.puts "ebb compiled without ssl support. get gnutls"
@@ -39,16 +44,25 @@ module Ebb
39
44
  key_file = options[:ssl_key]
40
45
  if FileTest.readable?(cert_file) and FileTest.readable?(cert_file)
41
46
  FFI::server_set_secure(cert_file, key_file);
47
+ @@secure = true
42
48
  else
43
49
  log.puts "error opening certificate or key file"
44
50
  end
45
51
  end
46
52
  end
47
53
 
54
+ unless options[:docroot].nil?
55
+ @@docroot = options[:docroot]
56
+ raise "#{@@docroot} not a directory"unless File.directory?(@@docroot)
57
+ end
58
+ end
59
+
60
+ def self.start_server(app, options={})
61
+ set_options(options)
48
62
  log.puts "Ebb PID #{Process.pid}"
49
63
 
50
- @running = true
51
64
  Connection.reset_responses
65
+ @running = true
52
66
  trap('INT') { stop_server }
53
67
 
54
68
  while @running
@@ -68,7 +82,6 @@ module Ebb
68
82
  res = req.response
69
83
  req.connection.responses << res
70
84
 
71
- # p req.env
72
85
  status = headers = body = nil
73
86
  catch(:async) do
74
87
  status, headers, body = app.call(req.env)
@@ -157,16 +170,26 @@ module Ebb
157
170
 
158
171
  def call(status, headers, body)
159
172
  @head = "HTTP/1.1 #{status} #{HTTP_STATUS_CODES[status.to_i]}\r\n"
160
- headers.each { |field, value| @head << "#{field}: #{value}\r\n" }
161
- @head << "\r\n"
173
+
174
+ # Hack to be compatible with frameworks who return Strings.
175
+ if body.kind_of?(String)
176
+ headers["Content-Length"] = body.length.to_s
177
+ body = [body]
178
+ end
162
179
 
163
180
  # XXX i would prefer to do
164
181
  # @chunked = true unless body.respond_to?(:length)
165
- @chunked = true if headers["Transfer-Encoding"] == "chunked"
182
+ if headers["Transfer-Encoding"] == "chunked" or
183
+ !headers.has_key?("Content-Length")
184
+ then
185
+ headers["Transfer-Encoding"] = "chunked"
186
+ @chunked = true
187
+ end
166
188
  # I also don't like this
167
189
  @last = true if headers["Connection"] == "close"
168
190
 
169
- # Note: not setting Content-Length. do it yourself.
191
+ headers.each { |field, value| @head << "#{field}: #{value}\r\n" }
192
+ @head << "\r\n"
170
193
 
171
194
  body.each do |chunk|
172
195
  write(chunk)
@@ -257,25 +280,34 @@ module Ebb
257
280
 
258
281
  class Request
259
282
  attr_accessor :connection
283
+
284
+ BASE_ENV = {
285
+ 'SERVER_NAME' => '0.0.0.0',
286
+ 'SCRIPT_NAME' => '',
287
+ 'SERVER_SOFTWARE' => Ebb::VERSION_STRING,
288
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
289
+ 'rack.version' => [0, 1],
290
+ 'rack.errors' => STDERR,
291
+ 'rack.multiprocess' => false,
292
+ 'rack.run_once' => false
293
+ }
260
294
 
261
295
  def env
262
296
  @env ||= begin
263
- @env_ffi.update(
264
- 'SERVER_NAME' => '0.0.0.0',
265
- 'SCRIPT_NAME' => '',
266
- 'SERVER_SOFTWARE' => Ebb::VERSION_STRING,
267
- 'SERVER_PROTOCOL' => 'HTTP/1.1',
268
- 'rack.version' => [0, 1],
269
- 'rack.errors' => STDERR,
270
- 'rack.url_scheme' => 'http',
271
- 'rack.multiprocess' => false,
272
- 'rack.run_once' => false,
273
-
297
+ env = BASE_ENV.merge(@env_ffi)
298
+ env.update(
274
299
  'rack.input' => self,
275
300
  'async.callback' => response,
276
- 'CONTENT_LENGTH' => @env_ffi.delete('HTTP_CONTENT_LENGTH'),
277
- 'CONTENT_TYPE' => @env_ffi.delete('HTTP_CONTENT_TYPE')
301
+ 'rack.url_scheme' => Ebb.ssl? ? 'https' : 'http'
278
302
  )
303
+ env["HTTP_HOST"] ||= BASE_ENV["SERVER_NAME"]
304
+ if env.has_key?('HTTP_CONTENT_LENGTH')
305
+ env['CONTENT_LENGTH'] = env.delete('HTTP_CONTENT_LENGTH')
306
+ end
307
+ if env.has_key?('HTTP_CONTENT_TYPE')
308
+ env['CONTENT_TYPE'] = env.delete('HTTP_CONTENT_TYPE')
309
+ end
310
+ env
279
311
  end
280
312
  end
281
313
 
@@ -1,4 +1,4 @@
1
1
  module Ebb
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  VERSION_STRING = "Ebb #{VERSION}"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ebb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ry dahl
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-19 00:00:00 +02:00
12
+ date: 2008-08-20 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15