Mercurial > libervia-web
comparison src/server/server.py @ 972:c4e58c4dba75
server: getURL + minor improvments:
- new getURL method allow to retrieve URL (same as "url" property) with possibility to specify arguments which will be quoted and added to the path
- added quote function which encode unicode value and do a urllib.quote_plus
- use of new getURL in _on_data_post_redirect, this simplify a bit the code
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 10 Nov 2017 11:10:15 +0100 |
parents | 4d37b23777c3 |
children | 4aa38c49bff7 |
comparison
equal
deleted
inserted
replaced
971:6e966ef8d69f | 972:c4e58c4dba75 |
---|---|
66 | 66 |
67 # following value are set from twisted.plugins.libervia_server initialise (see the comment there) | 67 # following value are set from twisted.plugins.libervia_server initialise (see the comment there) |
68 DATA_DIR_DEFAULT = OPT_PARAMETERS_BOTH = OPT_PARAMETERS_CFG = coerceDataDir = None | 68 DATA_DIR_DEFAULT = OPT_PARAMETERS_BOTH = OPT_PARAMETERS_CFG = coerceDataDir = None |
69 | 69 |
70 | 70 |
71 def quote(value): | |
72 """shortcut to quote an unicode value for URL""" | |
73 return urllib.quote_plus(value.encode('utf-8')).replace('%40','@') | |
71 | 74 |
72 | 75 |
73 class LiberviaSession(server.Session): | 76 class LiberviaSession(server.Session): |
74 sessionTimeout = C.SESSION_TIMEOUT | 77 sessionTimeout = C.SESSION_TIMEOUT |
75 | 78 |
156 if not item: | 159 if not item: |
157 raise KeyError | 160 raise KeyError |
158 except (IndexError, KeyError): | 161 except (IndexError, KeyError): |
159 raise NotImplementedError(u"only item for PubSub URI is handled for the moment for url_redirections_dict") | 162 raise NotImplementedError(u"only item for PubSub URI is handled for the moment for url_redirections_dict") |
160 location = "/blog/{profile}/{item}".format( | 163 location = "/blog/{profile}/{item}".format( |
161 profile=urllib.quote(options['url_redirections_profile'].encode('utf-8')), | 164 profile=quote(options['url_redirections_profile']), |
162 item = urllib.quote_plus(item), | 165 item = urllib.quote_plus(item), |
163 ).decode('utf-8') | 166 ).decode('utf-8') |
164 request_data = self._getRequestData(location) | 167 request_data = self._getRequestData(location) |
165 | 168 |
166 elif new_url.scheme in ('', 'http', 'https'): | 169 elif new_url.scheme in ('', 'http', 'https'): |
1497 """ | 1500 """ |
1498 return u'{root_url}?redirect_url={redirect_url}'.format( | 1501 return u'{root_url}?redirect_url={redirect_url}'.format( |
1499 root_url = self.getPageByName(page_name).url, | 1502 root_url = self.getPageByName(page_name).url, |
1500 redirect_url=urllib.quote_plus(request.uri) if url is None else url.encode('utf-8')) | 1503 redirect_url=urllib.quote_plus(request.uri) if url is None else url.encode('utf-8')) |
1501 | 1504 |
1505 def getURL(self, *args): | |
1506 """retrieve URL of the page set arguments | |
1507 | |
1508 *args(list[unicode]): argument to add to the URL as path elements | |
1509 """ | |
1510 url_args = [quote(a) for a in args] | |
1511 return os.path.join(self.url, *url_args) | |
1512 | |
1502 def getChildWithDefault(self, path, request): | 1513 def getChildWithDefault(self, path, request): |
1503 # we handle children ourselves | 1514 # we handle children ourselves |
1504 raise exceptions.InternalError(u"this method should not be used with LiberviaPage") | 1515 raise exceptions.InternalError(u"this method should not be used with LiberviaPage") |
1505 | 1516 |
1506 def nextPath(self, request): | 1517 def nextPath(self, request): |
1678 session_data = self.host.getSessionData(request, session_iface.ISATSession) | 1689 session_data = self.host.getSessionData(request, session_iface.ISATSession) |
1679 if 'post_redirect_page' in request.data: | 1690 if 'post_redirect_page' in request.data: |
1680 redirect_page_data = request.data['post_redirect_page'] | 1691 redirect_page_data = request.data['post_redirect_page'] |
1681 if isinstance(redirect_page_data, tuple): | 1692 if isinstance(redirect_page_data, tuple): |
1682 redirect_page = redirect_page_data[0] | 1693 redirect_page = redirect_page_data[0] |
1683 redirect_page_args = [urllib.quote_plus(a.encode('utf-8')).replace('%40','@') | 1694 redirect_page_args = redirect_page_data[1:] |
1684 for a in redirect_page_data[1:]] | 1695 redirect_uri = redirect_page.getURL(*redirect_page_args) |
1685 else: | 1696 else: |
1686 redirect_page = redirect_page_data | 1697 redirect_page = redirect_page_data |
1687 redirect_page_args = [] | 1698 redirect_uri = redirect_page.url |
1688 redirect_uri = os.path.join(redirect_page.url, *redirect_page_args) | |
1689 else: | 1699 else: |
1690 redirect_page = self | 1700 redirect_page = self |
1691 redirect_uri = request.uri | 1701 redirect_uri = request.uri |
1692 | 1702 |
1693 if not C.POST_NO_CONFIRM in ret: | 1703 if not C.POST_NO_CONFIRM in ret: |