Projet

Général

Profil

0001-utils-add-origin-module-53879.patch

Benjamin Dauvergne, 18 mai 2021 10:48

Télécharger (2,12 ko)

Voir les différences:

Subject: [PATCH 1/2] utils: add origin module (#53879)

 passerelle/utils/origin.py | 47 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
 create mode 100644 passerelle/utils/origin.py
passerelle/utils/origin.py
1
# passerelle - uniform access to multiple data sources and services
2
# Copyright (C) 2021 Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
import urllib.parse
18

  
19

  
20
def get_url_origin(url):
21
    '''Build origin of an URL'''
22
    parsed = urllib.parse.urlparse(url)
23
    if ':' in parsed.netloc:
24
        host, port = parsed.netloc.split(':', 1)
25
        if parsed.scheme == 'http' and port == 80:
26
            port = None
27
        if parsed.scheme == 'https' and port == 443:
28
            port = None
29
    else:
30
        host, port = parsed.netloc, None
31
    result = '%s://%s' % (parsed.scheme, host)
32
    if port:
33
        result += ':%s' % port
34
    return result
35

  
36

  
37
def is_same_origin(url1, url2):
38
    if 'null' in [url1, url2] or not url1 or not url2:
39
        return False
40

  
41
    try:
42
        origin1 = get_url_origin(url1)
43
        origin2 = get_url_origin(url2)
44
    except (ValueError, TypeError):
45
        return False
46

  
47
    return origin1 == origin2
0
-