Package backend :: Package wsgi :: Module wsgiRequest
[hide private]
[frames] | no frames]

Source Code for Module backend.wsgi.wsgiRequest

  1  # 
  2  # Copyright (c) 2010--2016 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15  # 
 16   
 17   
 18  import socket 
 19  try: 
 20      #  python 2 
 21      import httplib 
 22  except ImportError: 
 23      #  python3 
 24      import http.client as httplib 
 25   
 26   
27 -class WsgiRequest:
28 # pylint: disable=R0902 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
51 - def set_option(self, key, value):
52 self.options[key] = value
53
54 - def get_options(self):
55 return self.options
56
57 - def get_config(self):
58 return repr(self.__dict__)
59
60 - def write(self, msg):
61 self.output.append(msg)
62
63 - def send_http_header(self, status=None):
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 # httplib in 2.4 did not have the responses dictionary 78 # and mod_wsgi insists on having something after the numeric value 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 # default to text/xml 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
90 - def get_remote_host(self, _rev=""):
91 host = self.headers_in['REMOTE_ADDR'] 92 try: 93 host = socket.gethostbyaddr(host)[0] 94 except: 95 # pylint: disable=W0702 96 pass 97 return host
98
99 - def read(self, buf=-1):
100 return self.headers_in['wsgi.input'].read(buf)
101 102
103 -class WsgiServer:
104 # pylint: disable=R0903 105
106 - def __init__(self, hostname, port):
107 self.server_hostname = hostname 108 self.port = int(port)
109 110
111 -class WsgiConnection:
112 # pylint: disable=R0903 113
114 - def __init__(self, env):
115 self.remote_ip = env['REMOTE_ADDR'] 116 self.local_addr = (env['SERVER_NAME'], env['SERVER_PORT'])
117 118
119 -class WsgiMPtable:
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
137 - def __init__(self):
138 self.dict = {}
139
140 - def add(self, key, value):
141 if key in self.dict: 142 self.dict[key].append(str(value)) 143 else: 144 self.dict[key] = [str(value)]
145
146 - def __getitem__(self, key):
147 if len(self.dict[key]) == 1: 148 return self.dict[key][0] 149 return self.dict[key]
150
151 - def __setitem__(self, key, value):
152 self.dict[key] = [str(value)]
153
154 - def items(self):
155 ilist = [] 156 for k, v in self.dict.items(): 157 for vi in v: 158 ilist.append((k, vi)) 159 return ilist
160
161 - def has_key(self, key):
162 return key in self.dict
163
164 - def keys(self):
165 return list(self.dict.keys())
166
167 - def __str__(self):
168 return str(self.items())
169