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:
ryah (author)
Tue Apr 08 04:40:03 -0700 2008
commit  0861921e11e6cc5211d59ad13fd3471e85270b7a
tree    4fa60ceb6ee48ad12e0851b8fabb88bc0a1f277a
parent  cbef11050d5cde7478f45abe5f5bd3f97923f933
ebb / python_lib / __init__.py
100644 87 lines (71 sloc) 2.332 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
"""ebb
 
A WSGI web server
"""
import ebb_ffi
import re
from headers import *
from signal import *
 
# TODO fix me
def interupt_handler(signum, frame):
  ebb_ffi.server_stop()
  
signal(SIGINT, interupt_handler)
signal(SIGTERM, interupt_handler)
 
def body_length(body):
  if len(body) == 1:
    return len(body[0])
  else:
    # TODO
    raise Exception, "Not implemented"
    
def should_keep_alive(env):
  if env['HTTP_VERSION'] == 'HTTP/1.0':
    if env.has_key('HTTP_CONNECTION') and env['HTTP_CONNECTION'].upper() == 'KEEP-ALIVE':
      return True
  else:
    if env.has_key('HTTP_CONNECTION'):
      if env['HTTP_CONNECTION'].upper() != 'CLOSE':
        return True
    else:
      return True
  return False
 
# For WSGI 2.0
def request_cb(app, client):
  status_string, header_list, body = app.__call__(client.env())
  
  # status_string should be something like "200 OK" or "404 Not Found"
  # need to split this into an integer and human readable string for ebb
  match = re.search('(\d+) (.*)', status_string)
  status = int(match.group(1))
  status_human = match.group(2)
  
  # write the status
  client.write_status(status, status_human)
  
  headers = Headers(header_list)
  if not headers.has_key('Content-Length'):
    headers['Content-Length'] = str(body_length(body))
  
  # Decide if we should keep the connection alive or not
  if not headers.has_key('Connection'):
    if headers.has_key('Content-Length') and should_keep_alive(client.env()):
      headers['Connection'] = 'Keep-Alive'
    else:
      headers['Connection'] = 'close'
  
  # write the headers
  for field, value in headers.items():
    client.write_header(field, value)
    
  for part in body:
    client.write_body(part)
  
  client.release()
  
  
def start_server(app, args = {}):
  if args.has_key('unix_socket'):
    socketfile = args['unix_socket']
    ebb_ffi.listen_on_unix_socket(socketfile)
    print "Ebb is listening on unix socket %s" % socketfile
  elif args.has_key('fileno'):
    fileno = int(args['fileno'])
    ebb_ffi.listen_on_fd(fileno)
    print "Ebb is listening on fd %d" % fileno
  else:
    if args.has_key('port'):
      port = int(args['port'])
    else:
      port = 4001
    ebb_ffi.listen_on_port(port)
    print "Ebb is listening at http://0.0.0.0:%d/" % port
  
  ebb_ffi.process_connections(app, request_cb)