示例#1
0
def search_post():
    """Handle form submit."""
    qry_domains = tools.query_domains(flask.request.form)

    # Handle malformed domains data by redirecting to GET page.
    if qry_domains is None:
        app.logger.info(
            'No valid domains in POST dict {}'.format(flask.request.args)
        )
        return flask.redirect('/error/2')

    # Attempt to create a <= 200 character GET parameter from the domains so
    # we can redirect to that (allows bookmarking). As in '/api/analysis/ip'
    # we use hex to hide the domains from firewalls that already block some of
    # them.
    path = ','.join(map(binascii.hexlify, qry_domains))
    if len(path) <= 200:
        return flask.redirect('/search/{}'.format(path))

    # If there's a ton of domains, just to the report.
    return html_render(qry_domains)
示例#2
0
    def test_query_domains(self):
        """ Test the helper that splits up the domains query into a list of
            domains.
        """
        self.assertIs(
            None, tools.query_domains({}), 'Missing domains key'
        )

        self.assertIs(
            None, tools.query_domains({'blah': 'cat'}), 'Missing domains key'
        )

        inp = 'www.examplE.COM'

        self.assertEqual(
            ['www.example.com'],
            tools.query_domains({'domains': inp}),
            'One domain, made lower-case',
        )

        inp = '\n\n\n\n\n          www.example.com         \n\n\n\n\n'

        self.assertEqual(
            ['www.example.com'],
            tools.query_domains({'domains': inp}),
            'One domain',
        )

        inp = """

            www.example.com

            www.example2.com

            www.example2.com

        """

        self.assertItemsEqual(
            ['www.example.com', 'www.example2.com'],
            tools.query_domains({'domains': inp}),
            'Two domains, duplicates and whitespace ignored',
        )

        inp = """  www.example.com ,   www.example2.com\t, www.example3.com \r\n  \t"""

        self.assertItemsEqual(
            ['www.example.com', 'www.example2.com', 'www.example3.com'],
            tools.query_domains({'domains': inp}),
            'Three domains, commas treated as tabs as whitespace',
        )

        inp = "a.com, b.com c.com"

        self.assertItemsEqual(
            ['a.com', 'b.com', 'c.com'],
            tools.query_domains({'domains': inp}),
        )

        inp = u"""

            www.example.com\r\n

            www.example2.com

            www.example2.com

        """

        self.assertItemsEqual(
            ['www.example.com', 'www.example2.com'],
            tools.query_domains({'domains': inp}),
            'Unicode and Windows newlines not handled',
        )

        # If a URL is entered, an attempt is made to tidy it up.
        inp = ' https://www.example1.com/  https:// https://www.example2.com/ '
        assert tools.query_domains({'domains': inp}) == [
            'www.example1.com', 'www.example2.com'
        ]