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

funilrys / PyFunceble / 9637396124

23 Jun 2024 08:22PM UTC coverage: 94.723%. Remained the same
9637396124

push

github

funilrys
Bump version to v4.2.24.dev

1 of 1 new or added line in 1 file covered. (100.0%)

11326 of 11957 relevant lines covered (94.72%)

14.17 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, 2024 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
        https://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
15✔
55

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

61

62
class SubjectSwitchRulesHandler(ExtraRuleHandlerBase):
15✔
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
15✔
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":
15✔
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

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

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

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

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

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

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

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

125
            self.switch_to_down()
×
126
            break
×
127

128
        return self
×
129

130
    @ExtraRuleHandlerBase.ensure_status_is_given
15✔
131
    @ExtraRuleHandlerBase.setup_status_before
15✔
132
    @ExtraRuleHandlerBase.setup_status_after
15✔
133
    def start(self) -> "SubjectSwitchRulesHandler":
15✔
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

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

147
                if not self.status.status_after_extra_rules:
×
148
                    self._switch_down_by_history()
×
149
        except PyFunceble.factory.Requester.exceptions.RequestException:
×
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

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