Esempio n. 1
0
    def follow_html_redirect(self, response):
        """Repeatedly follow HTML redirects in the page.

        If the given response has no HTML redirect, return it unaltered.
        Otherwise, return a new response by following the redirects.
        """

        js_redirect_pattern = (
            r'(?:<!--)?\s*' +
            r'document\.location\.replace\(\'' +
            r'(?P<url>(?:\\.|[^\'])+)' +
            r'\'\);\s*' +
            r'(?:(?://)?-->)?\s*$')
        real_login_url = (
            'https://bb.au.dk/webapps/' +
            'bb-auth-provider-shibboleth-BBLEARN/execute/shibbolethLogin')
        history = list(response.history) + [response]

        while True:
            document = html5lib.parse(
                response.content, encoding=response.encoding)
            scripts = document.findall('.//h:script', NS)

            next_url = None
            for s in scripts:
                t = ''.join(s.itertext())
                mo = re.match(js_redirect_pattern, t)
                if mo:
                    next_url = mo.group('url')
                    break
            if next_url is not None:
                o = urlparse(next_url)
                p = o.netloc + o.path
                if p == 'bb.au.dk/webapps/login/':
                    qs = parse_qs(o.query)
                    try:
                        return_url = qs['new_loc'][0]
                    except KeyError:
                        print("We are being redirected to %r" % (next_url,))
                        return_url = ''
                    # It seems that making a GET request to this page
                    # logs you out?
                    if return_url == '/webapps/login/?action=relogin':
                        logger.debug(
                            "Not setting returnUrl to %r", return_url)
                        return_url = ''
                    new_qs = urlencode(
                        dict(returnUrl=return_url,
                             authProviderId='_102_1'))
                    next_url = '%s?%s' % (real_login_url, new_qs)
                response = self.session.get(next_url)
                history += list(response.history) + [response]
                continue
            break
        response.history = history[:-1]
        return response
Esempio n. 2
0
 def ensure_edit_mode(self, response):
     if self.get_edit_mode(response) is False:
         url = ('https://bb.au.dk/webapps/blackboard/execute/' +
                'doCourseMenuAction?cmd=setDesignerParticipantViewMode' +
                '&courseId=' + self.course_id + '&mode=designer')
         logger.debug("Switch to edit mode")
         r = self.get(url)
         history = (list(response.history) + [response] + list(r.history) +
                    [r])
         response = self.get(history[0].url)
         response.history = history + list(response.history)
     return response
Esempio n. 3
0
    def follow_html_redirect(self, response):
        """Repeatedly follow HTML redirects in the page.

        If the given response has no HTML redirect, return it unaltered.
        Otherwise, return a new response by following the redirects.
        """

        js_redirect_pattern = (r'(?:<!--)?\s*' +
                               r'document\.location\.replace\(\'' +
                               r'(?P<url>(?:\\.|[^\'])+)' + r'\'\);\s*' +
                               r'(?:(?://)?-->)?\s*$')
        real_login_url = (
            'https://bb.au.dk/webapps/' +
            'bb-auth-provider-shibboleth-BBLEARN/execute/shibbolethLogin')
        history = list(response.history) + [response]

        while True:
            document = html5lib.parse(response.content,
                                      encoding=response.encoding)
            scripts = document.findall('.//h:script', NS)

            next_url = None
            for s in scripts:
                t = ''.join(s.itertext())
                mo = re.match(js_redirect_pattern, t)
                if mo:
                    next_url = mo.group('url')
                    break
            if next_url is not None:
                o = urlparse(next_url)
                p = o.netloc + o.path
                if p == 'bb.au.dk/webapps/login/':
                    qs = parse_qs(o.query)
                    try:
                        return_url = qs['new_loc'][0]
                    except KeyError:
                        print("We are being redirected to %r" % (next_url, ))
                        return_url = ''
                    # It seems that making a GET request to this page
                    # logs you out?
                    if return_url == '/webapps/login/?action=relogin':
                        logger.debug("Not setting returnUrl to %r", return_url)
                        return_url = ''
                    new_qs = urlencode(
                        dict(returnUrl=return_url, authProviderId='_102_1'))
                    next_url = '%s?%s' % (real_login_url, new_qs)
                response = self.session.get(next_url)
                history += list(response.history) + [response]
                continue
            break
        response.history = history[:-1]
        return response
Esempio n. 4
0
 def ensure_edit_mode(self, response):
     if self.get_edit_mode(response) is False:
         url = ('https://bb.au.dk/webapps/blackboard/execute/' +
                'doCourseMenuAction?cmd=setDesignerParticipantViewMode' +
                '&courseId=' + self.course_id +
                '&mode=designer')
         logger.debug("Switch to edit mode")
         r = self.get(url)
         history = (list(response.history) + [response] +
                    list(r.history) + [r])
         response = self.get(history[0].url)
         response.history = history + list(response.history)
     return response