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.
4 $: << File.expand_path(File.dirname(__FILE__))
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 out = "HTTP/1.1 %d %s\r\n" % [status, HTTP_STATUS_CODES[status]]
31 if body.respond_to? :length and status != 304
32 out += "Connection: close\r\n"
33 headers['Content-Length'] = body.length
36 headers.each { |k, v| out += "#{k}: #{v}\r\n" }
38 body.each { |part| out += part }
41 body.close if body and body.respond_to? :close
46 # problem here. this should be a nonblocking call that starts the server
47 # in a seperate thread. We don't want to sit in libev's loop and ignore
48 # all the ruby related things going on.
49 # Better to start a new thread (do i do this in ebb_ext.c?)
51 trap('INT') { puts "got INT"; stop }
52 trap('TERM') { puts "got TERM"; stop }
55 while process_connections
56 unless @waiting_clients.empty?
57 client = @waiting_clients.shift
58 process_client(client)
66 101 => 'Switching Protocols',
70 203 => 'Non-Authoritative Information',
72 205 => 'Reset Content',
73 206 => 'Partial Content',
74 300 => 'Multiple Choices',
75 301 => 'Moved Permanently',
76 302 => 'Moved Temporarily',
78 304 => 'Not Modified',
81 401 => 'Unauthorized',
82 402 => 'Payment Required',
85 405 => 'Method Not Allowed',
86 406 => 'Not Acceptable',
87 407 => 'Proxy Authentication Required',
88 408 => 'Request Time-out',
91 411 => 'Length Required',
92 412 => 'Precondition Failed',
93 413 => 'Request Entity Too Large',
94 414 => 'Request-URI Too Large',
95 415 => 'Unsupported Media Type',
96 500 => 'Internal Server Error',
97 501 => 'Not Implemented',
99 503 => 'Service Unavailable',
100 504 => 'Gateway Time-out',
101 505 => 'HTTP Version not supported'