ry / ebb fork watch download tarball
public
Description: web server
Homepage: http://ebb.rubyforge.org
Clone URL: git://github.com/ry/ebb.git
Search Repo:
Ryan Dahl (author)
Sat Feb 23 10:28:25 -0800 2008
commit  3d4a4f70abc7eb097d4f5b8c6a4e4cdd52ae08f6
tree    92329005f483d4214efbe843c1d53c4bc43f722e
parent  489e487914c2127720f7d47d240dfaaeec9e402b
ebb / ruby_lib / ebb.rb
100644 174 lines (154 sloc) 4.698 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# A ruby binding to the ebb web server
# Copyright (c) 2007 Ry Dahl <ry.d4hl@gmail.com>
# This software is released under the "MIT License". See README file for details.
module Ebb
  LIBDIR = File.expand_path(File.dirname(__FILE__))
  VERSION = File.read(LIBDIR + "/../VERSION").gsub(/\s/,'')
end
 
$: << Ebb::LIBDIR
require Ebb::LIBDIR + '/../src/ebb_ext'
require 'daemonizable'
 
module Ebb
  class Client
    BASE_ENV = {
      'SERVER_SOFTWARE' => "Ebb #{Ebb::VERSION}",
      'SERVER_PROTOCOL' => 'HTTP/1.1',
      'GATEWAY_INTERFACE' => 'CGI/1.2',
      'rack.version' => [0, 1],
      'rack.errors' => STDERR,
      'rack.multithread' => false,
      'rack.multiprocess' => false,
      'rack.run_once' => false
    }.freeze
    
    def env
      @env ||= begin
        env = @ebb_env.update(BASE_ENV)
        env['rack.input'] = Input.new(self)
        env
      end
    end
  end
  
  class Input
    CHUNKSIZE = 4*1024
    def initialize(client)
      @client = client
    end
    
    def read(len = 1)
      @client.read_input(len)
    end
    
    def gets
      raise NotImplementedError, "Fix me, please. Yes, you!"
    end
    
    def each
      raise NotImplementedError, "Fix me, please Yes, you!"
    end
  end
  
  class Server
    include Daemonizable
    def self.run(app, options={})
      # port must be an integer
      server = self.new(app, options)
      yield server if block_given?
      server.start
    end
    
    def initialize(app, options={})
      #@host = options[:host] || '0.0.0.0'
      @socket = options[:socket]
      @port = (options[:port] || 4001).to_i
      pid_file = options[:pid_file]
      log_file = options[:log_file]
      @timeout = options[:timeout]
      
      if options[:daemonize]
        change_privilege options[:user], options[:group] if options[:user] && options[:group]
        daemonize
      end
      @app = app
      init
    end
    
    def process_client(client)
      status, headers, body = @app.call(client.env)
      
      client.write "HTTP/1.1 %d %s\r\n" % [status, HTTP_STATUS_CODES[status]]
      
      if body.respond_to? :length and status != 304
        client.write "Connection: close\r\n"
        headers['Content-Length'] = body.length
      end
      
      headers.each { |k, v| client.write "#{k}: #{v}\r\n" }
      client.write "\r\n"
      
      # Not many apps use streaming yet so i'll hold off on that feature
      # until the rest of ebb is more developed
      # Yes, I know streaming responses are very cool.
      if body.kind_of?(String)
        client.write body
      else
        body.each { |p| client.write p }
      end
      client.finished
    end
    
    def start
      trap('INT') { @running = false }
      
      if @socket
        listen_on_socket(@socket) or raise "Problem listening on socket #{@socket}"
        puts "Listening on socket #{@socket}"
      else
        listen_on_port(@port) or raise "Problem listening on port #{@port}"
        puts "Listening on port #{@port}"
      end
      @waiting_clients = []
      
      @running = true
      while process_connections and @running
        unless @waiting_clients.empty?
          if $debug and @waiting_clients.length > 1
            puts "#{@waiting_clients.length} waiting clients"
          end
          client = @waiting_clients.shift
          process_client(client)
        end
      end
      unlisten
    end
  end
  
  HTTP_STATUS_CODES = {
    100 => 'Continue',
    101 => 'Switching Protocols',
    200 => 'OK',
    201 => 'Created',
    202 => 'Accepted',
    203 => 'Non-Authoritative Information',
    204 => 'No Content',
    205 => 'Reset Content',
    206 => 'Partial Content',
    300 => 'Multiple Choices',
    301 => 'Moved Permanently',
    302 => 'Moved Temporarily',
    303 => 'See Other',
    304 => 'Not Modified',
    305 => 'Use Proxy',
    400 => 'Bad Request',
    401 => 'Unauthorized',
    402 => 'Payment Required',
    403 => 'Forbidden',
    404 => 'Not Found',
    405 => 'Method Not Allowed',
    406 => 'Not Acceptable',
    407 => 'Proxy Authentication Required',
    408 => 'Request Time-out',
    409 => 'Conflict',
    410 => 'Gone',
    411 => 'Length Required',
    412 => 'Precondition Failed',
    413 => 'Request Entity Too Large',
    414 => 'Request-URI Too Large',
    415 => 'Unsupported Media Type',
    500 => 'Internal Server Error',
    501 => 'Not Implemented',
    502 => 'Bad Gateway',
    503 => 'Service Unavailable',
    504 => 'Gateway Time-out',
    505 => 'HTTP Version not supported'
  }
end
 
module Rack
  module Adapter
    autoload :Rails, 'rails_adapter'
  end
end