Mercurial > kallithea
annotate rhodecode/lib/middleware/https_fixup.py @ 1217:a3b2b4b4e440
fixes for issue #149
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Tue, 05 Apr 2011 18:04:06 +0200 |
parents | 136af52f374b |
children | bf263968da47 |
rev | line source |
---|---|
921
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
1 # -*- coding: utf-8 -*- |
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
2 """ |
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
3 rhodecode.lib.middleware.https_fixup |
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
5 |
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
6 middleware to handle https correctly |
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
7 |
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
8 :created_on: May 23, 2010 |
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
9 :author: marcink |
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
10 :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> |
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
11 :license: GPLv3, see COPYING for more details. |
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
12 """ |
1217
a3b2b4b4e440
fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
921
diff
changeset
|
13 # This program is free software: you can redistribute it and/or modify |
a3b2b4b4e440
fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
921
diff
changeset
|
14 # it under the terms of the GNU General Public License as published by |
a3b2b4b4e440
fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
921
diff
changeset
|
15 # the Free Software Foundation, either version 3 of the License, or |
a3b2b4b4e440
fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
921
diff
changeset
|
16 # (at your option) any later version. |
a3b2b4b4e440
fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
921
diff
changeset
|
17 # |
252
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
204
diff
changeset
|
18 # This program is distributed in the hope that it will be useful, |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
204
diff
changeset
|
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
204
diff
changeset
|
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
204
diff
changeset
|
21 # GNU General Public License for more details. |
1217
a3b2b4b4e440
fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
921
diff
changeset
|
22 # |
252
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
204
diff
changeset
|
23 # You should have received a copy of the GNU General Public License |
1217
a3b2b4b4e440
fixes for issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
921
diff
changeset
|
24 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
252
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
204
diff
changeset
|
25 |
921
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
26 from rhodecode.lib import str2bool |
252
3782a6d698af
licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
204
diff
changeset
|
27 |
204
a8ea3ce3cdc4
Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
28 class HttpsFixup(object): |
921
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
29 def __init__(self, app, config): |
204
a8ea3ce3cdc4
Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
30 self.application = app |
921
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
31 self.config = config |
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
32 |
204
a8ea3ce3cdc4
Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
33 def __call__(self, environ, start_response): |
a8ea3ce3cdc4
Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
34 self.__fixup(environ) |
a8ea3ce3cdc4
Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
35 return self.application(environ, start_response) |
921
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
36 |
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
37 |
204
a8ea3ce3cdc4
Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
38 def __fixup(self, environ): |
a8ea3ce3cdc4
Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
39 """Function to fixup the environ as needed. In order to use this |
a8ea3ce3cdc4
Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
40 middleware you should set this header inside your |
a8ea3ce3cdc4
Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
41 proxy ie. nginx, apache etc. |
a8ea3ce3cdc4
Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
42 """ |
a8ea3ce3cdc4
Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
43 proto = environ.get('HTTP_X_URL_SCHEME') |
921
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
44 |
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
45 if str2bool(self.config.get('force_https')): |
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
46 proto = 'https' |
136af52f374b
merged found bugs and fixed for stable release:
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
47 |
204
a8ea3ce3cdc4
Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
48 if proto == 'https': |
a8ea3ce3cdc4
Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
49 environ['wsgi.url_scheme'] = proto |
a8ea3ce3cdc4
Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
50 else: |
a8ea3ce3cdc4
Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
51 environ['wsgi.url_scheme'] = 'http' |
a8ea3ce3cdc4
Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
52 return None |