1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import socket
19 try:
20
21 import httplib
22 except ImportError:
23
24 import http.client as httplib
25
26
28
29
30 - def __init__(self, env, start_response):
31 self.method = env['REQUEST_METHOD']
32 self.headers_in = env
33 self.path_info = env['PATH_INFO']
34 self.start_response = start_response
35 self.uri = self.unparsed_uri = env['REQUEST_URI']
36 self.server = WsgiServer(env['SERVER_NAME'], env['SERVER_PORT'])
37 self.connection = WsgiConnection(env)
38 self.options = {}
39 self.main = 0
40 self.proto_num = float(env['SERVER_PROTOCOL'].split('/')[1])
41 self.headers_out = WsgiMPtable()
42 self.sent_header = 0
43 self.content_type = ""
44 self.the_request = env['REQUEST_METHOD'] + " " + env['SCRIPT_NAME'] + " " + env['SERVER_PROTOCOL']
45 self.output = []
46 self.err_headers_out = WsgiMPtable()
47 self.status = ""
48 self.sent_bodyct = 0
49 self.sent_header = 0
50
53
56
58 return repr(self.__dict__)
59
62
64 self.sent_header = 1
65 self.status = str(self.status)
66 if status is not None:
67 self.status = str(status)
68 if len(self.status) == 0 or self.status is None:
69 self.status = "200"
70 elif self.status.startswith("500"):
71 for i in self.err_headers_out.items():
72 self.headers_out.add(i[0], i[1])
73
74 if hasattr(httplib, "responses"):
75 self.status = self.status + " " + httplib.responses[int(self.status)]
76 else:
77
78
79 self.status = self.status + " Status " + self.status
80
81 if len(self.content_type) > 0:
82 self.headers_out['Content-Type'] = self.content_type
83
84 if not self.headers_out.has_key('Content-Type'):
85 self.headers_out['Content-Type'] = 'text/xml'
86
87 self.start_response(self.status, list(self.headers_out.items()))
88 return
89
91 host = self.headers_in['REMOTE_ADDR']
92 try:
93 host = socket.gethostbyaddr(host)[0]
94 except:
95
96 pass
97 return host
98
99 - def read(self, buf=-1):
100 return self.headers_in['wsgi.input'].read(buf)
101
102
104
105
107 self.server_hostname = hostname
108 self.port = int(port)
109
110
112
113
115 self.remote_ip = env['REMOTE_ADDR']
116 self.local_addr = (env['SERVER_NAME'], env['SERVER_PORT'])
117
118
120
121 """ This class emulates mod_python's mp_table. See
122 http://www.modpython.org/live/current/doc-html/pyapi-mptable.html
123
124 The table object is a wrapper around the Apache APR table. The table
125 object behaves very much like a dictionary (including the Python 2.2
126 features such as support of the in operator, etc.), with the following
127 differences:
128
129 ...
130 - Duplicate keys are allowed (see add() below). When there is more
131 than one value for a key, a subscript operation returns a list.
132
133 Much of the information that Apache uses is stored in tables.
134 For example, req.headers_in and req.headers_out.
135 """
136
139
140 - def add(self, key, value):
145
147 if len(self.dict[key]) == 1:
148 return self.dict[key][0]
149 return self.dict[key]
150
153
155 ilist = []
156 for k, v in self.dict.items():
157 for vi in v:
158 ilist.append((k, vi))
159 return ilist
160
162 return key in self.dict
163
166
169