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

Contents of /trunk/harmonious/_requestfactory.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 26 - (show annotations) (download) (as text)
Thu Jun 30 10:21:39 2005 UTC (9 years, 2 months ago) by port001
File MIME type: text/x-python
File size: 4949 byte(s)
Use the host string when generating a session ID
1 __id__ = '$Id$'
2 __modulename__ = 'RequestFactory'
3
4 import os
5 import urlparse
6 import Cookie
7 from copy import copy
8 from time import time
9
10 from harmonious import harm
11 from harmonious._request import Request
12 from harmonious._sessionutils import Session, gen_session_id
13
14 class RequestFactory:
15
16 def __init__(self, req_obj, host, sessionStore):
17
18 self.__sessionStore = sessionStore
19 self.__request = Request()
20 self.__request.session = Session()
21 self.__request._baseRequest = req_obj
22 self.__request.method = self.__request._baseRequest.command
23 self.__request.headers = copy(self.__request._baseRequest.headers)
24 self.__request.host = host
25 self.__url_elements = []
26
27 def construct(self):
28
29 self.__parse_url()
30 self.__parse_url_elements()
31
32 if self.__request.method == 'POST':
33
34 self.__parse_post()
35
36 if harm.config.session['SessionEnable'] == True:
37 self.__load_cookies()
38 self.__load_session()
39
40 return self.__request
41
42 def __load_cookies(self):
43
44 self.__request.cookies = Cookie.SimpleCookie()
45
46 for cookie in self.__request.headers.getallmatchingheaders('cookie'):
47
48 self.__request.cookies.load(cookie)
49
50 def __load_session(self):
51
52 if self.__request.cookies.has_key(harm.config.session['CookieName']):
53
54 # We have a session cookie, is it in our store?
55 if self.__sessionStore.has_session(self.__request.cookies[harm.config.session['CookieName']].value):
56
57 # Remove the session if it has expired
58 if self.__sessionStore.has_expired(self.__request.cookies[harm.config.session['CookieName']].value):
59
60 self.__sessionStore.remove_session(self.__request.cookies[harm.config.session['CookieName']].value)
61 self.__create_session()
62
63 else:
64
65 # Yes, update the expiry date then load it
66 self.__request.session._set_id(self.__request.cookies[harm.config.session['CookieName']].value)
67 self.__sessionStore.update_session(self.__request.session.get_id(),
68 expirationDate = int(time() + (harm.config.session['Timeout'] * 60)))
69 session_data = self.__sessionStore.load_session(self.__request.session.get_id())
70 self.__request.session.expires = session_data['expirationDate']
71
72 self.__request.session.attrs = session_data['attrs']
73 self.__request.session.persistent = session_data['persistent']
74
75 else:
76
77 # No, create a new one
78 self.__create_session()
79
80 else:
81
82 self.__create_session()
83
84 def __create_session(self):
85
86 # Create the session
87 self.__request.session._set_id(gen_session_id(self.__request._baseRequest.client_address[1],
88 self.__request.host))
89 self.__request.session.expires = int(time() + (harm.config.session['Timeout'] * 60))
90
91 # Save it
92 self.__sessionStore.store_session(sessionId=self.__request.session.get_id(),
93 expirationDate=harm.config.session['Timeout'])
94
95 def __parse_post(self):
96
97 import cgi
98 from StringIO import StringIO
99
100 post_data = self.__request._baseRequest.rfile.read(int(self.__request.headers['Content-Length']))
101 post_file = StringIO(post_data)
102 self.__request.forms = cgi.FieldStorage(fp=post_file, headers=self.__request.headers,
103 environ={'REQUEST_METHOD': 'POST'}, keep_blank_values=1)
104
105 def __parse_url(self):
106
107 self.__url_elements = urlparse.urlsplit(self.__request._baseRequest.path,
108 'http', True)
109
110 def __parse_url_elements(self):
111
112 # Queries
113 if len(self.__url_elements[3]) != 0:
114
115 queries = self.__url_elements[3].split('&')
116
117 for query in queries:
118
119 try:
120 (key, value) = query.split('=')
121 except ValueError:
122 key = query
123 value = None
124
125 if not self.__request.queries.has_key(key):
126
127 self.__request.queries[key] = u'%s' % value
128
129 # Fragment
130 if len(self.__url_elements[4]) != 0:
131
132 self.__request.fragment = u'%s' % self.__url_elements[4]
133
134 # Path - Including the file, PathFilter will split these
135 self.__request.path = self.__url_elements[2]
136
137 # File
138 if self.__request.path[-1] != '/':
139
140 self.__request.file = os.path.basename(self.__request.path)
141
142 # Absolute path
143 self.__request.abs_path = "%s%s" % (harm.config.hosts[self.__request.host]['DocumentRoot'], self.__request.path)

Properties

Name Value
svn:keywords Id

  ViewVC Help
Powered by ViewVC 1.1.20