ry / ebb fork watch download tarball
public this repo is viewable by everyone
Description: web server
Homepage: http://ebb.rubyforge.org
Clone URL: git://github.com/ry/ebb.git
ryah (author)
about 1 month ago
commit  cbef11050d5cde7478f45abe5f5bd3f97923f933
tree    fd39e021622be2d9823ae00a30bd878d79348df3
parent  dc4a7e28337306378a008aa9d7e205a306e79934
ebb / python_lib / __init__.py
100644 78 lines (62 sloc) 1.985 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
"""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['port']:
    ebb_ffi.listen_on_port(int(args['port']))
  else:
    print "no port given!"
    exit(1)
  
  print "Ebb listening on port %d" % args['port']
  ebb_ffi.process_connections(app, request_cb)