1 # A ruby binding to the ebb web server
2 # Copyright (c) 2007 Ry Dahl <ry.d4hl@gmail.com>
3 # This software is released under the "MIT License". See README file for details.
9 def self.run(app, options={})
10 # port must be an integer
11 server = self.new(app, options)
12 yield server if block_given?
16 def initialize(app, options={})
17 @host = options[:Host] || '0.0.0.0'
18 @port = (options[:Port] || 4001).to_i
22 # Called by the C library on each request.
23 # env is a hash containing all the variables of the request
24 # client is a TCPSocket
25 # XXX: push this code to C?
26 def process_client(client)
27 status, headers, body = @app.call(client.env)
29 client.write("HTTP/1.1 %d %s\r\n" % [status, HTTP_STATUS_CODES[status]])
31 if body.respond_to? :length and status != 304
32 client.write("Connection: close\r\n")
33 headers['Content-Length'] = body.length
36 headers.each { |k, v| client.write("#{k}: #{v}\r\n") }
38 body.each { |part| client.write(part) }
40 body.close if body and body.respond_to? :close
45 # problem here. this should be a nonblocking call that starts the server
46 # in a seperate thread. We don't want to sit in libev's loop and ignore
47 # all the ruby related things going on.
48 # Better to start a new thread (do i do this in ebb_ext.c?)
50 trap('INT') { puts "got INT"; stop }
51 trap('TERM') { puts "got TERM"; stop }
54 while process_connections
55 unless @waiting_clients.empty?
56 client = @waiting_clients.shift
57 process_client(client)
65 101 => 'Switching Protocols',
69 203 => 'Non-Authoritative Information',
71 205 => 'Reset Content',
72 206 => 'Partial Content',
73 300 => 'Multiple Choices',
74 301 => 'Moved Permanently',
75 302 => 'Moved Temporarily',
77 304 => 'Not Modified',
80 401 => 'Unauthorized',
81 402 => 'Payment Required',
84 405 => 'Method Not Allowed',
85 406 => 'Not Acceptable',
86 407 => 'Proxy Authentication Required',
87 408 => 'Request Time-out',
90 411 => 'Length Required',
91 412 => 'Precondition Failed',
92 413 => 'Request Entity Too Large',
93 414 => 'Request-URI Too Large',
94 415 => 'Unsupported Media Type',
95 500 => 'Internal Server Error',
96 501 => 'Not Implemented',
98 503 => 'Service Unavailable',
99 504 => 'Gateway Time-out',
100 505 => 'HTTP Version not supported'