Lines 41-53
connections to remote |UMC| servers
|
Link Here
|
---|
|
41 |
import ssl |
41 |
import ssl |
42 |
import json |
42 |
import json |
43 |
import locale |
43 |
import locale |
44 |
from Cookie import SimpleCookie |
|
|
45 |
from httplib import HTTPSConnection, HTTPException |
46 |
|
44 |
|
47 |
from univention.config_registry import ConfigRegistry |
45 |
import six |
48 |
ucr = ConfigRegistry() |
|
|
49 |
ucr.load() |
50 |
|
46 |
|
|
|
47 |
if six.PY3: |
48 |
from http.cookies import SimpleCookie |
49 |
from http.client import HTTPSConnection, HTTPException |
50 |
else: |
51 |
from Cookie import SimpleCookie |
52 |
from httplib import HTTPSConnection, HTTPException |
53 |
|
54 |
def _get_ucr(): |
55 |
from univention.config_registry import ConfigRegistry |
56 |
ucr = ConfigRegistry() |
57 |
ucr.load() |
58 |
return ucr |
59 |
|
60 |
def _get_useragent(): |
61 |
ucr = _get_ucr() |
62 |
return 'UCS/%s (univention.lib.umc/%s-errata%s)' % (ucr.get('version/version', '0.0'), ucr.get('version/patchlevel', '0'), ucr.get('version/erratalevel', '0')), |
63 |
|
64 |
def _get_fqdn(): |
65 |
ucr = _get_ucr() |
66 |
return '%s.%s' % (ucr.get('hostname'), ucr.get('domainname')) |
51 |
|
67 |
|
52 |
class _HTTPType(type): |
68 |
class _HTTPType(type): |
53 |
""" |
69 |
""" |
Lines 340-355
class Client(object):
|
Link Here
|
---|
|
340 |
|
356 |
|
341 |
ConnectionType = HTTPSConnection |
357 |
ConnectionType = HTTPSConnection |
342 |
|
358 |
|
343 |
def __init__(self, hostname=None, username=None, password=None, language=None, timeout=None, automatic_reauthentication=False): |
359 |
def __init__(self, hostname=None, username=None, password=None, language=None, timeout=None, automatic_reauthentication=False, useragent=None): |
344 |
# type: (Optional[str], Optional[str], Optional[str], Optional[str], Optional[float], bool) -> None |
360 |
# type: (Optional[str], Optional[str], Optional[str], Optional[str], Optional[float], bool) -> None |
345 |
self.hostname = hostname or '%s.%s' % (ucr.get('hostname'), ucr.get('domainname')) |
361 |
self.hostname = hostname or _get_fqdn() |
346 |
self._language = language or locale.getdefaultlocale()[0] or '' |
362 |
self._language = language or locale.getdefaultlocale()[0] or '' |
347 |
self._headers = { |
363 |
self._headers = { |
348 |
'Content-Type': 'application/json; charset=UTF-8', |
364 |
'Content-Type': 'application/json; charset=UTF-8', |
349 |
'Accept': 'application/json; q=1, text/html; q=0.5; */*; q=0.1', |
365 |
'Accept': 'application/json; q=1, text/html; q=0.5; */*; q=0.1', |
350 |
'Accept-Language': self._language.replace('_', '-'), |
366 |
'Accept-Language': self._language.replace('_', '-'), |
351 |
'X-Requested-With': 'XMLHttpRequest', |
367 |
'X-Requested-With': 'XMLHttpRequest', |
352 |
'User-Agent': 'UCS/%s (univention.lib.umc/%s-errata%s)' % (ucr.get('version/version', '0.0'), ucr.get('version/patchlevel', '0'), ucr.get('version/erratalevel', '0')), |
368 |
'User-Agent': useragent or _get_useragent() |
353 |
} |
369 |
} |
354 |
self._base_uri = '/univention/' |
370 |
self._base_uri = '/univention/' |
355 |
self._timeout = timeout |
371 |
self._timeout = timeout |
Lines 411-417
class Client(object):
|
Link Here
|
---|
|
411 |
|
427 |
|
412 |
:raises ConnectionError: if :file:`/etc/machine.secret` cannot be read. |
428 |
:raises ConnectionError: if :file:`/etc/machine.secret` cannot be read. |
413 |
""" |
429 |
""" |
414 |
username = '%s$' % ucr.get('hostname') |
430 |
username = '%s$' % _get_ucr().get('hostname') |
415 |
try: |
431 |
try: |
416 |
with open('/etc/machine.secret') as machine_file: |
432 |
with open('/etc/machine.secret') as machine_file: |
417 |
password = machine_file.readline().strip() |
433 |
password = machine_file.readline().strip() |
Lines 530-536
class Client(object):
|
Link Here
|
---|
|
530 |
:raises ConnectionError: if the request cannot be send. |
546 |
:raises ConnectionError: if the request cannot be send. |
531 |
:raises HTTPError: if an |UMC| error occurs. |
547 |
:raises HTTPError: if an |UMC| error occurs. |
532 |
""" |
548 |
""" |
533 |
cookie = '; '.join(['='.join(x) for x in self.cookies.iteritems()]) |
549 |
cookie = '; '.join(['='.join(x) for x in self.cookies.items()]) |
534 |
request.headers = dict(self._headers, Cookie=cookie, **request.headers) |
550 |
request.headers = dict(self._headers, Cookie=cookie, **request.headers) |
535 |
if 'UMCSessionId' in self.cookies: |
551 |
if 'UMCSessionId' in self.cookies: |
536 |
request.headers['X-XSRF-Protection'] = self.cookies['UMCSessionId'] |
552 |
request.headers['X-XSRF-Protection'] = self.cookies['UMCSessionId'] |