annotate cagou/core/patches.py @ 306:e2afbec1d178

remote: enforce unicode in bridge methods
author Goffi <goffi@goffi.org>
date Sat, 20 Jul 2019 15:53:50 +0200
parents b0461363bc65
children 772c170b47a9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
280
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr//bin/env python2
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2016-2019 Jérôme Poisson (goffi@goffi.org)
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 import urllib2
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 import ssl
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
22
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
23
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 def apply():
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 # allow to disable certificate validation
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 ctx_no_verify = ssl.create_default_context()
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 ctx_no_verify.check_hostname = False
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 ctx_no_verify.verify_mode = ssl.CERT_NONE
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 class HTTPSHandler(urllib2.HTTPSHandler):
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 no_certificate_check = False
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 def __init__(self, *args, **kwargs):
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 urllib2._HTTPSHandler_ori.__init__(self, *args, **kwargs)
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 if self.no_certificate_check:
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 self._context = ctx_no_verify
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 urllib2._HTTPSHandler_ori = urllib2.HTTPSHandler
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 urllib2.HTTPSHandler = HTTPSHandler
b0461363bc65 core: certificate validation can be disabled:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 urllib2.HTTPSHandler.no_certificate_check = True