File: httpheader.py

package info (click to toggle)
python-nvchecker 2.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 736 kB
  • sloc: python: 4,801; makefile: 25
file content (42 lines) | stat: -rw-r--r-- 1,047 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# MIT licensed
# Copyright (c) 2021 lilydjwg <lilydjwg@gmail.com>, et al.

import re

from nvchecker.api import session, GetVersionError

async def get_version(name, conf, *, cache, **kwargs):
  key = tuple(sorted(conf.items()))
  return await cache.get(key, get_version_impl)

async def get_version_impl(info):
  conf = dict(info)
  url = conf['url']
  header = conf.get('header', 'Location')
  follow_redirects = conf.get('follow_redirects', False)
  method = conf.get('method', 'HEAD')

  try:
    regex = re.compile(conf['regex'])
  except re.error as e:
    raise GetVersionError('bad regex', exc_info=e)

  res = await session.request(
    url,
    method = method,
    follow_redirects = follow_redirects,
  )

  header_value = res.headers.get(header)
  if not header_value:
    raise GetVersionError(
      'header not found or is empty',
      header = header,
      value = header_value,
    )

  try:
    version = regex.findall(header_value)
  except ValueError:
    raise GetVersionError('version string not found.')
  return version