/[glsr]/trunk/harmonious/_requestfactory.py
Gentoo

Contents of /trunk/harmonious/_requestfactory.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 109 - (show annotations) (download) (as text)
Mon Aug 1 14:40:49 2005 UTC (8 years, 11 months ago) by port001
File MIME type: text/x-python
File size: 6200 byte(s)
Added a 'flush2disk' mode to the SessionStorage class which pickles session data to disk when shut down, and unpickles it on startup.

1 __id__ = '$Id$'
2 __modulename__ = 'RequestFactory'
3
4 import os
5 import cgi
6 import urlparse
7 import Cookie
8 from copy import copy
9 from time import time
10 from StringIO import StringIO
11
12 from harmonious import server
13 from harmonious._sessionutils import Session, gen_hash, gen_session_id
14
15 class RequestFactory:
16
17 def __init__(self, req_obj, host, keep_alive):
18
19 self.__request = req_obj
20 self.__request.host = host
21 self.__request.raw_path = self.__request.path
22 self.__request.fragment = ''
23 self.__request.abs_path = ''
24 self.__request.file = ''
25 self.__request.status = 0
26 self.__request.queries = {}
27 self.__request.cookies = {}
28 self.__request.forms = cgi.FieldStorage()
29 self.__request.keep_alive = keep_alive[0]
30 self.__request.keep_alive_value = keep_alive[1]
31
32 self.__session = Session()
33
34 # Rename the socket object 'request' to 'connection' as having
35 # a reference of request.request is confusing.
36 self.__request.connection = self.__request.request
37 del self.__request.request
38
39 self.__url_elements = []
40
41 def construct(self):
42
43 self.__parse_url()
44 self.__parse_url_elements()
45
46 if self.__request.command == 'POST':
47
48 self.__parse_post()
49
50 if server.config.session['SessionEnable'] == True:
51 self.__load_cookies()
52 self.__load_session()
53
54 return (self.__request, self.__session)
55
56 def __load_cookies(self):
57
58 self.__request.cookies = Cookie.SimpleCookie()
59
60 for cookie in self.__request.headers.getallmatchingheaders('cookie'):
61
62 self.__request.cookies.load(cookie)
63
64 # Remove invalid cookies
65 for cookie in self.__request.cookies.keys():
66
67 if cookie != server.config.session['CookieName']:
68 self.__request.cookies.pop(cookie)
69 continue
70
71 if len(self.__request.cookies[server.config.session['CookieName']].value) < 50:
72 self.__request.cookies.pop(cookie)
73 continue
74
75 if gen_hash(self.__request.cookies[server.config.session['CookieName']].value[:40]) != \
76 self.__request.cookies[server.config.session['CookieName']].value[-10:]:
77 self.__request.cookies.pop(cookie)
78 continue
79
80 if not server._sessionStore.has_session(self.__request.cookies[server.config.session['CookieName']].value):
81 self.__request.cookies.pop(cookie)
82 continue
83
84 def __load_session(self):
85
86 if self.__request.cookies.has_key(server.config.session['CookieName']):
87
88 # Remove the session if it has expired
89 if server._sessionStore.has_expired(self.__request.cookies[server.config.session['CookieName']].value):
90
91 server._sessionStore.remove_session(self.__request.cookies[server.config.session['CookieName']].value)
92 self.__create_session()
93
94 else:
95
96 # Update the expiry date then load it
97 self.__session._set_id(self.__request.cookies[server.config.session['CookieName']].value)
98 server._sessionStore.update_session(self.__session.get_id(),
99 expires = int(time() + (server.config.session['Timeout'] * 60)))
100 session_data = server._sessionStore.load_session(self.__session.get_id())
101 self.__session.expires = session_data['expires']
102
103 self.__session.attrs = session_data['attrs']
104 self.__session.set_persistent(session_data['persistent'])
105
106 self.__session._end_init()
107
108 else:
109
110 self.__create_session()
111
112 def __create_session(self):
113
114 self.__session._end_init()
115
116 id = gen_session_id(self.__request.client_address[1], self.__request.host)
117
118 # Create the session
119 self.__session._set_id(id)
120 self.__session.expires = int(time() + (server.config.session['Timeout'] * 60))
121
122 # Save it
123 server._sessionStore.store_session(sessionId=self.__session.get_id(),
124 expires=server.config.session['Timeout'])
125
126 def __parse_post(self):
127
128 import cgi
129 from StringIO import StringIO
130
131 post_data = self.__request.rfile.read(int(self.__request.headers['Content-Length']))
132 post_file = StringIO(post_data)
133 self.__request.forms = cgi.FieldStorage(fp=post_file, headers=self.__request.headers,
134 environ={'REQUEST_METHOD': 'POST'}, keep_blank_values=1)
135
136 def __parse_url(self):
137
138 new_path = "/%s" % self.__request.raw_path.lstrip('/')
139 self.__url_elements = urlparse.urlsplit(new_path, 'http', True)
140
141 def __parse_url_elements(self):
142
143 # Queries
144 if len(self.__url_elements[3]) != 0:
145
146 queries = self.__url_elements[3].split('&')
147
148 for query in queries:
149
150 try:
151 (key, value) = query.split('=')
152 except ValueError:
153 key = query
154 value = None
155
156 if not self.__request.queries.has_key(key):
157
158 self.__request.queries[key] = u'%s' % value
159
160 # Fragment
161 if len(self.__url_elements[4]) != 0:
162
163 self.__request.fragment = u"%s" % self.__url_elements[4]
164
165 # Path - Including the file, for now
166 self.__request.path = self.__url_elements[2].rstrip('/')
167
168 # File
169 if os.path.exists("%s%s" % (server.config.hosts[self.__request.host]['DocumentRoot'], self.__request.path)) and not \
170 os.path.isdir("%s%s" % (server.config.hosts[self.__request.host]['DocumentRoot'], self.__request.path)):
171
172 # A file has been requested
173 self.__request.file = os.path.basename(self.__request.path.rstrip('/'))
174
175 # Update path
176 self.__request.path = os.path.dirname(self.__request.path)
177
178 # Absolute path
179 self.__request.abs_path = u"%s%s" % (server.config.hosts[self.__request.host]['DocumentRoot'], self.__request.path)

Properties

Name Value
svn:keywords Id

  ViewVC Help
Powered by ViewVC 1.1.20