• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

funilrys / PyFunceble / 5109997145

pending completion
5109997145

push

github-actions

funilrys
Merge remote-tracking branch 'origin/dev' into mtest

296 of 461 new or added lines in 39 files covered. (64.21%)

11297 of 11853 relevant lines covered (95.31%)

11.42 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

28.57
/PyFunceble/checker/availability/extras/subject_switch.py
1
"""
2
The tool to check the availability or syntax of domain, IP or URL.
3

4
::
5

6

7
    ██████╗ ██╗   ██╗███████╗██╗   ██╗███╗   ██╗ ██████╗███████╗██████╗ ██╗     ███████╗
8
    ██╔══██╗╚██╗ ██╔╝██╔════╝██║   ██║████╗  ██║██╔════╝██╔════╝██╔══██╗██║     ██╔════╝
9
    ██████╔╝ ╚████╔╝ █████╗  ██║   ██║██╔██╗ ██║██║     █████╗  ██████╔╝██║     █████╗
10
    ██╔═══╝   ╚██╔╝  ██╔══╝  ██║   ██║██║╚██╗██║██║     ██╔══╝  ██╔══██╗██║     ██╔══╝
11
    ██║        ██║   ██║     ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
12
    ╚═╝        ╚═╝   ╚═╝      ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝
13

14
Provides the extra rules handler based on the "switching" domain behavior of some
15
subjects.
16

17
Author:
18
    Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom
19

20
Special thanks:
21
    https://pyfunceble.github.io/#/special-thanks
22

23
Contributors:
24
    https://pyfunceble.github.io/#/contributors
25

26
Project link:
27
    https://github.com/funilrys/PyFunceble
28

29
Project documentation:
30
    https://pyfunceble.readthedocs.io/en/dev/
31

32
Project homepage:
33
    https://pyfunceble.github.io/
34

35
License:
36
::
37

38

39
    Copyright 2017, 2018, 2019, 2020, 2022, 2023 Nissar Chababy
40

41
    Licensed under the Apache License, Version 2.0 (the "License");
42
    you may not use this file except in compliance with the License.
43
    You may obtain a copy of the License at
44

45
        http://www.apache.org/licenses/LICENSE-2.0
46

47
    Unless required by applicable law or agreed to in writing, software
48
    distributed under the License is distributed on an "AS IS" BASIS,
49
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
50
    See the License for the specific language governing permissions and
51
    limitations under the License.
52
"""
53

54
from typing import Optional
12✔
55

56
import PyFunceble.facility
12✔
57
from PyFunceble.checker.availability.extras.base import ExtraRuleHandlerBase
12✔
58
from PyFunceble.checker.availability.status import AvailabilityCheckerStatus
12✔
59
from PyFunceble.converter.url2netloc import Url2Netloc
12✔
60

61

62
class SubjectSwitchRulesHandler(ExtraRuleHandlerBase):
12✔
63
    """
64
    Provides our very own "subject switch" handler.
65

66
    This handler will be used to detects a subject switch behavior from a server.
67
    In other words, this handler should be able to detect the following scenario
68
    and switch the original subject to :code:`INACTIVE`.
69

70
        1. https://www.example.org/hello/world -> https://example.org/hello/world
71
        2. https://m.example.org/hello/world -> https://example.org/hello/world
72
    """
73

74
    url2netloc: Optional[Url2Netloc] = None
12✔
75

76
    def __init__(self, status: Optional[AvailabilityCheckerStatus] = None) -> None:
77
        self.url2netloc = Url2Netloc()
78
        super().__init__(status)
79

80
    def _switch_down_by_history(self) -> "SubjectSwitchRulesHandler":
12✔
81
        """
82
        Tries to switch the status to :code:`INACTIVE` by following hte history.
83
        """
84

NEW
85
        variations = set(
×
86
            [
87
                self.status.netloc.replace("www.", "", 1),
88
                self.status.netloc.replace("m.", "", 1),
89
            ]
90
        )
91

NEW
92
        try:
×
93
            # The current netloc should be included in the variations
NEW
94
            variations.remove(self.status.netloc)
×
NEW
95
        except KeyError:
×
NEW
96
            pass
×
97

NEW
98
        start_path = (
×
99
            self.url2netloc.set_data_to_convert(self.req_url)
100
            .parse_url()
101
            .parsed_url.path
102
        )
103

NEW
104
        for response in self.req.history:
×
NEW
105
            if (
×
106
                not str(response.status_code).startswith("3")
107
                or "location" not in response.headers
108
            ):
NEW
109
                continue
×
110

NEW
111
            redirect_url = response.headers["location"]
×
112

NEW
113
            netloc = self.url2netloc.set_data_to_convert(redirect_url).get_converted()
×
NEW
114
            local_path = self.url2netloc.parsed_url.path
×
115

NEW
116
            if netloc == self.status.idna_subject and netloc not in variations:
×
NEW
117
                continue
×
118

NEW
119
            if not start_path:
×
NEW
120
                if local_path != "/":
×
NEW
121
                    continue
×
NEW
122
            elif start_path != local_path:
×
NEW
123
                continue
×
124

NEW
125
            self.switch_to_down()
×
NEW
126
            break
×
127

NEW
128
        return self
×
129

130
    @ExtraRuleHandlerBase.ensure_status_is_given
12✔
131
    @ExtraRuleHandlerBase.setup_status_before
12✔
132
    @ExtraRuleHandlerBase.setup_status_after
12✔
133
    def start(self) -> "SubjectSwitchRulesHandler":
12✔
134
        """
135
        Process the check and handling of the current subject.
136
        """
137

138
        PyFunceble.facility.Logger.info(
139
            "Started to check %r against our subject switcher rules.",
140
            self.status.idna_subject,
141
        )
142

NEW
143
        try:
×
NEW
144
            if any(self.status.netloc.startswith(x) for x in ("www.", "m.")):
×
NEW
145
                self.do_request()
×
146

NEW
147
                if not self.status.status_after_extra_rules:
×
NEW
148
                    self._switch_down_by_history()
×
NEW
149
        except PyFunceble.factory.Requester.exceptions.RequestException:
×
NEW
150
            pass
×
151

152
        PyFunceble.facility.Logger.info(
153
            "Finished to check %r against our subject switcher rules.",
154
            self.status.idna_subject,
155
        )
156

NEW
157
        return self
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc