ry / ebb fork watch download tarball
public
Description: web server
Homepage: http://ebb.rubyforge.org
Clone URL: git://github.com/ry/ebb.git
ryah (author)
Sat Mar 29 09:57:55 -0700 2008
commit  3c8fdda73cd2e571c3395c425737e439c1a8bf99
tree    a5ae8a6e2bd3f7be68aeaebbd2e3824ad3d5b58e
parent  c124a82348ab9278d4e48408033fc88fcbe3209e
ebb / benchmark / application.rb
100644 85 lines (70 sloc) 2.037 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
DIR = File.dirname(__FILE__)
 
def fib(n)
  return 1 if n <= 1
  fib(n-1) + fib(n-2)
end
 
def wait(seconds)
  n = (seconds / 0.01).to_i
  n.times do
    sleep(0.01)
    #File.read(DIR + '/yahoo.html')
  end
end
 
class SimpleApp
  @@responses = {}
  
  def initialize
    @count = 0
  end
  
  def call(env)
    path = env['PATH_INFO'] || env['REQUEST_URI']
    commands = path.split('/')
    
    @count += 1
    if commands.include?('periodical_activity') and @count % 10 != 1
      return [200, {'Content-Type'=>'text/plain'}, "quick response!\r\n"]
    end
    
    if commands.include?('fibonacci')
      n = commands.last.to_i
      raise "fibonacci called with n <= 0" if n <= 0
      body = (1..n).to_a.map { |i| fib(i).to_s }.join(' ')
      status = 200
    
    elsif commands.include?('wait')
      n = commands.last.to_f
      raise "wait called with n <= 0" if n <= 0
      wait(n)
      body = "waited about #{n} seconds"
      status = 200
    
    elsif commands.include?('bytes')
      n = commands.last.to_i
      raise "bytes called with n <= 0" if n <= 0
      body = @@responses[n] || "C"*n
      status = 200
      
    elsif commands.include?('test_post_length')
      input_body = ""
      while chunk = env['rack.input'].read(512)
        input_body << chunk
      end
      if env['CONTENT_LENGTH'].to_i == input_body.length
        body = "Content-Length matches input length"
        status = 200
      else
        body = "Content-Length doesn't matches input length!
content_length = #{env['CONTENT_LENGTH'].to_i}
input_body.length = #{input_body.length}"
        status = 500
      end
    
    else
      status = 404
      body = "Undefined url"
    end
    
    body += "\r\n"
    headers = {'Content-Type' => 'text/plain', 'Content-Length' => body.length.to_s }
    [status, headers, body]
  end
end
 
 
if $0 == __FILE__
  require 'rubygems'
  require 'ruby-debug'
  Debugger.start
  require DIR + '/../ruby_lib/ebb'
  server = Ebb::start_server(SimpleApp.new, :port => 4001)
end