83 |
83 |
84 |
84 |
85 # TODO(tlarsen): write getMultipleIndexParamValues() that returns a |
85 # TODO(tlarsen): write getMultipleIndexParamValues() that returns a |
86 # list of values if present, omitting those values that are |
86 # list of values if present, omitting those values that are |
87 # out of range |
87 # out of range |
|
88 |
|
89 |
|
90 def isReferrerSelf(request, |
|
91 expected_prefix=None, suffix=None): |
|
92 """Returns True if HTTP referrer path starts with the HTTP request path. |
|
93 |
|
94 Args: |
|
95 request: the Django HTTP request object; request.path is used if |
|
96 expected_path is not supplied (the most common usage) |
|
97 expected_prefix: optional HTTP path to use instead of the one in |
|
98 request.path; default is None (use request.path) |
|
99 suffix: suffix to remove from the HTTP request path before comparing |
|
100 it to the HTTP referrer path in the HTTP request object headers |
|
101 (this is often an link name, for example, that may be changing from |
|
102 a POST referrer to a GET redirect target) |
|
103 |
|
104 Returns: |
|
105 True if HTTP referrer path begins with the HTTP request path (either |
|
106 request.path or expected_prefix instead if it was supplied), after |
|
107 any suffix was removed from that request path |
|
108 False otherwise |
|
109 |
|
110 """ |
|
111 http_from = request.META.get('HTTP_REFERER') |
|
112 |
|
113 if not http_from: |
|
114 # no HTTP referrer, so cannot possibly start with expected prefix |
|
115 return False |
|
116 |
|
117 from_path = urlparse.urlparse(http_from).path |
|
118 |
|
119 if not expected_prefix: |
|
120 # use HTTP request path, since expected_prefix was not supplied |
|
121 expected_prefix = request.path |
|
122 |
|
123 if suffix: |
|
124 # remove suffix (such as a link name) before comparison |
|
125 chars_to_remove = len(suffix) |
|
126 |
|
127 if not suffix.startswith('/'): |
|
128 chars_to_remove = chars_to_remove + 1 |
|
129 |
|
130 expected_prefix = expected_prefix[:-chars_to_remove] |
|
131 |
|
132 if not from_path.startswith(expected_prefix): |
|
133 # expected prefix did not match first part of HTTP referrer path |
|
134 return False |
|
135 |
|
136 # HTTP referrer started with (possibly truncated) expected prefix |
|
137 return True |
|
138 |
|
139 |
|
140 def replaceSuffix(path, old_suffix, new_suffix=None, params=None): |
|
141 """Replace the last part of a URL path with something else. |
|
142 |
|
143 Also appends an optional list of query parameters. Used for |
|
144 replacing, for example, one link name at the end of a relative |
|
145 URL path with another. |
|
146 |
|
147 Args: |
|
148 path: HTTP request relative URL path (with no query arguments) |
|
149 old_suffix: expected suffix at the end of request.path component; |
|
150 if any False value (such as None), the empty string '' is used |
|
151 new_suffix: if non-False, appended to request.path along with a |
|
152 '/' separator (after removing old_suffix if necessary) |
|
153 params: an optional dictionary of query parameters to append to |
|
154 the redirect target; appended as ?<key1>=<value1>&<key2>=... |
|
155 |
|
156 Returns: |
|
157 /path/with/new_suffix?a=1&b=2 |
|
158 """ |
|
159 if not old_suffix: |
|
160 old_suffix = '' |
|
161 |
|
162 old_suffix = '/' + old_suffix |
|
163 |
|
164 if path.endswith(old_suffix): |
|
165 # also removes any trailing '/' if old_suffix was empty |
|
166 path = path[:-len(old_suffix)] |
|
167 |
|
168 if new_suffix: |
|
169 # if present, appends new_suffix, after '/' separator |
|
170 path = '%s/%s' % (path, new_suffix) |
|
171 |
|
172 if params: |
|
173 # appends any query parameters, after a '?' and separated by '&' |
|
174 path = '%s?%s' % (path, '&'.join( |
|
175 ['%s=%s' % (p,v) for p,v in params.iteritems()])) |
|
176 |
|
177 return path |