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

funilrys / PyFunceble / 3607917966

pending completion
3607917966

push

github-actions

funilrys
fixup! Introduction of the changeip SPECIAL rule.

11094 of 11508 relevant lines covered (96.4%)

11.55 hits per line

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

30.77
/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 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

85
        variations = set(
×
86
            [
87
                self.status.netloc.replace("www.", "", 1),
88
                self.status.netloc.replace("m.", "", 1),
89
            ]
90
        )
91
        # The current netloc should be included in the variations
92
        variations.remove(self.status.netloc)
×
93

94
        start_path = (
×
95
            self.url2netloc.set_data_to_convert(self.req_url)
96
            .parse_url()
97
            .parsed_url.path
98
        )
99

100
        for response in self.req.history:
×
101
            if (
×
102
                not str(response.status_code).startswith("3")
103
                or "location" not in response.headers
104
            ):
105
                continue
×
106

107
            redirect_url = response.headers["location"]
×
108

109
            netloc = self.url2netloc.set_data_to_convert(redirect_url).get_converted()
×
110
            local_path = self.url2netloc.parsed_url.path
×
111

112
            if netloc == self.status.idna_subject and netloc not in variations:
×
113
                continue
×
114

115
            if not start_path:
×
116
                if local_path != "/":
×
117
                    continue
×
118
            elif start_path != local_path:
×
119
                continue
×
120

121
            self.switch_to_down()
×
122
            break
×
123

124
        return self
×
125

126
    @ExtraRuleHandlerBase.ensure_status_is_given
12✔
127
    @ExtraRuleHandlerBase.setup_status_before
12✔
128
    @ExtraRuleHandlerBase.setup_status_after
12✔
129
    def start(self) -> "SubjectSwitchRulesHandler":
12✔
130
        """
131
        Process the check and handling of the current subject.
132
        """
133

134
        PyFunceble.facility.Logger.info(
135
            "Started to check %r against our subject switcher rules.",
136
            self.status.idna_subject,
137
        )
138

139
        try:
×
140
            if any(self.status.netloc.startswith(x) for x in ("www.", "m.")):
×
141
                self.do_request()
×
142

143
                if not self.status.status_after_extra_rules:
×
144
                    self._switch_down_by_history()
×
145
        except PyFunceble.factory.Requester.exceptions.RequestException:
×
146
            pass
×
147

148
        PyFunceble.facility.Logger.info(
149
            "Finished to check %r against our subject switcher rules.",
150
            self.status.idna_subject,
151
        )
152

153
        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