Exemplo n.º 1
0
 def test_basic_xhtml_iso8859_15(self):
     initial_code = self.read_file('test-data/xhtml2-iso8859-15-initial.html')
     final_code = self.XML_NORMALIZATION_RE.sub(b'><', fix_widget_code(initial_code, 'http://server.com/widget', 'application/xhtml+xml', None, 'iso-8859-15', False, {}, False, 'classic')) + b'\n'
     expected_code = self.read_file('test-data/xhtml2-iso8859-15-expected.html')
     self.assertEqual(final_code, expected_code)
Exemplo n.º 2
0
 def test_xhtml_without_head_element(self):
     initial_code = self.read_file('test-data/xhtml3-initial.html')
     final_code = self.XML_NORMALIZATION_RE.sub(b'><', fix_widget_code(initial_code, 'http://server.com/widget', 'application/xhtml+xml', None, 'utf-8', False, {}, False, 'classic')) + b'\n'
     expected_code = self.read_file('test-data/xhtml3-expected.html')
     self.assertEqual(final_code, expected_code)
Exemplo n.º 3
0
 def test_xhtml_without_head_element(self):
     initial_code = self.read_file('test-data/xhtml3-initial.html')
     final_code = self.XML_NORMALIZATION_RE.sub('><', fix_widget_code(initial_code, 'http://server.com/widget', 'application/xhtml+xml', None, False)) + '\n'
     expected_code = self.read_file('test-data/xhtml3-expected.html')
     self.assertEqual(final_code, expected_code)
Exemplo n.º 4
0
 def test_html_with_more_than_one_base_element(self):
     initial_code = self.read_file('test-data/xhtml4-extra-base-elements-initial.html')
     final_code = self.XML_NORMALIZATION_RE.sub(b'><', fix_widget_code(initial_code, 'text/html', None, 'utf-8', False, {}, 'classic', 'wirecloud_defaulttheme')) + b'\n'
     expected_code = self.read_file('test-data/xhtml4-expected.html')
     self.assertEqual(final_code, expected_code)
Exemplo n.º 5
0
 def test_basic_xhtml_iso8859_15(self):
     initial_code = self.read_file('test-data/xhtml2-iso8859-15-initial.html')
     final_code = self.XML_NORMALIZATION_RE.sub('><', fix_widget_code(initial_code, 'http://server.com/widget', 'application/xhtml+xml', None, 'iso-8859-15', False)) + '\n'
     expected_code = self.read_file('test-data/xhtml2-iso8859-15-expected.html')
     self.assertEqual(final_code, expected_code)
Exemplo n.º 6
0
def process_widget_code(request, resource):

    mode = request.GET.get('mode', 'classic')
    theme = request.GET.get('theme', get_active_theme_name())
    widget_info = json.loads(resource.json_description)

    # check if the xhtml code has been cached
    if widget_info['contents']['cacheable'] is True:

        cache_key = resource.widget.xhtml.get_cache_key(get_current_domain(request), mode, theme)
        cache_entry = cache.get(cache_key)
        if cache_entry is not None:
            response = HttpResponse(cache_entry['code'], content_type=cache_entry['content_type'])
            patch_cache_headers(response, cache_entry['timestamp'], cache_entry['timeout'])
            return response

    # process xhtml
    xhtml = resource.widget.xhtml
    content_type = widget_info['contents'].get('contenttype', 'text/html')
    charset = widget_info['contents'].get('charset', 'utf-8')

    code = xhtml.code
    if not xhtml.cacheable or code == '':
        try:
            code = download_local_file(os.path.join(showcase_utils.wgt_deployer.root_dir, url2pathname(xhtml.url)))

        except Exception as e:
            if isinstance(e, IOError) and e.errno == errno.ENOENT:
                return build_response(request, 404, {'error_msg': _("Widget code not found"), 'details': "%s" % e}, WIDGET_ERROR_FORMATTERS)
            else:
                return build_response(request, 500, {'error_msg': _("Error reading widget code"), 'details': "%s" % e}, WIDGET_ERROR_FORMATTERS)
    else:
        # Code contents comes as unicode from persistence, we need bytes
        code = code.encode(charset)

    if xhtml.cacheable and (xhtml.code == '' or xhtml.code_timestamp is None):
        try:
            xhtml.code = code.decode(charset)
        except UnicodeDecodeError:
            msg = _('Widget code was not encoded using the specified charset (%(charset)s as stated in the widget description file).') % {'charset': charset}
            return build_response(request, 502, {'error_msg': msg}, WIDGET_ERROR_FORMATTERS)

        xhtml.code_timestamp = time.time() * 1000
        xhtml.save()

    try:
        code = fix_widget_code(code, content_type, request, charset, xhtml.use_platform_style, process_requirements(widget_info['requirements']), mode, theme)
    except UnicodeDecodeError:
        msg = _('Widget code was not encoded using the specified charset (%(charset)s as stated in the widget description file).') % {'charset': charset}
        return build_response(request, 502, {'error_msg': msg}, WIDGET_ERROR_FORMATTERS)
    except Exception as e:
        msg = _('Error processing widget code')
        return build_response(request, 502, {'error_msg': msg, 'details':"%s" % e}, WIDGET_ERROR_FORMATTERS)

    if xhtml.cacheable:
        cache_timeout = 31536000  # 1 year
        cache_entry = {
            'code': code,
            'content_type': '%s; charset=%s' % (content_type, charset),
            'timestamp': xhtml.code_timestamp,
            'timeout': cache_timeout,
        }
        cache.set(cache_key, cache_entry, cache_timeout)
    else:
        cache_timeout = 0

    response = HttpResponse(code, content_type='%s; charset=%s' % (content_type, charset))
    patch_cache_headers(response, xhtml.code_timestamp, cache_timeout)
    return response
Exemplo n.º 7
0
 def test_basic_html(self):
     initial_code = self.read_file('test-data/xhtml1-initial.html')
     final_code = self.XML_NORMALIZATION_RE.sub(b'><', fix_widget_code(initial_code, 'text/html', None, 'utf-8', False, {}, 'classic', 'wirecloud_defaulttheme')) + b'\n'
     expected_code = self.read_file('test-data/xhtml1-expected.html')
     self.assertEqual(final_code, expected_code)
Exemplo n.º 8
0
 def test_basic_xhtml(self):
     initial_code = self.read_file('test-data/xhtml2-initial.html')
     final_code = fix_widget_code(initial_code, 'http://server.com/widget', 'application/xhtml+xml', None, False) + '\n'
     expected_code = self.read_file('test-data/xhtml2-expected.html')
     self.assertEqual(final_code, expected_code)
Exemplo n.º 9
0
    def test_unhandled_content_type(self):

        initial_code = b'plain text'
        final_code = fix_widget_code(initial_code, 'text/plain', None, 'utf-8', False, {}, 'classic', 'wirecloud.defaulttheme')
        self.assertEqual(final_code, initial_code)
Exemplo n.º 10
0
    def test_empty_html(self):

        initial_code = b''
        final_code = self.XML_NORMALIZATION_RE.sub(b'><', fix_widget_code(initial_code, 'text/html', None, 'utf-8', False, {}, 'classic', 'wirecloud_defaulttheme')) + b'\n'
        expected_code = self.read_file('test-data/html-empty-expected.html')
        self.assertEqual(final_code, expected_code)
Exemplo n.º 11
0
 def test_xhtml_without_head_element(self):
     initial_code = self.read_file('test-data/xhtml3-initial.html')
     final_code = self.XML_NORMALIZATION_RE.sub(b'><', fix_widget_code(initial_code, 'application/xhtml+xml', None, 'utf-8', False, {}, 'classic', 'wirecloud_defaulttheme')) + b'\n'
     expected_code = self.read_file('test-data/xhtml3-expected.html')
     self.assertEqual(final_code, expected_code)
Exemplo n.º 12
0
 def test_basic_xhtml_iso8859_15(self):
     initial_code = self.read_file('test-data/xhtml2-iso8859-15-initial.html')
     final_code = self.XML_NORMALIZATION_RE.sub(b'><', fix_widget_code(initial_code, 'application/xhtml+xml', None, 'iso-8859-15', False, {}, 'classic', 'wirecloud_defaulttheme')) + b'\n'
     expected_code = self.read_file('test-data/xhtml2-iso8859-15-expected.html')
     self.assertEqual(final_code, expected_code)
Exemplo n.º 13
0
 def test_basic_xhtml_compressed(self):
     initial_code = self.read_file('test-data/xhtml2-initial.html')
     final_code = self.XML_NORMALIZATION_RE.sub(b'><', fix_widget_code(initial_code, 'application/xhtml+xml', None, 'utf-8', False, {}, 'classic', 'wirecloud_defaulttheme')) + b'\n'
     final_code = self.COMPRESS_HASH_RE.sub(b'/widgetapi.js', final_code)
     expected_code = self.read_file('test-data/xhtml2-compressed-expected.html')
     self.assertEqual(final_code, expected_code)
Exemplo n.º 14
0
    def test_unhandled_content_type(self):

        initial_code = b'plain text'
        final_code = fix_widget_code(initial_code, 'http://server.com/widget', 'text/plain', None, 'utf-8', False, {}, False, 'classic')
        self.assertEqual(final_code, initial_code)
Exemplo n.º 15
0
 def test_basic_html_iso8859_15(self):
     initial_code = self.read_file('test-data/xhtml1-iso8859-15-initial.html')
     final_code = self.XML_NORMALIZATION_RE.sub(b'><', fix_widget_code(initial_code, 'text/html', None, 'iso-8859-15', False, {}, 'classic', 'wirecloud_defaulttheme')) + b'\n'
     expected_code = self.read_file('test-data/xhtml1-iso8859-15-expected.html')
     self.assertEqual(final_code, expected_code)
Exemplo n.º 16
0
    def test_html_unclosed_tags(self):

        initial_code = self.read_file('test-data/html-unclosed-tags-initial.html')
        final_code = self.XML_NORMALIZATION_RE.sub(b'><', fix_widget_code(initial_code, 'http://server.com/widget', 'text/html', None, 'utf-8', False, {}, False, 'classic')) + b'\n'
        expected_code = self.read_file('test-data/html-unclosed-tags-expected.html')
        self.assertEqual(final_code, expected_code)
Exemplo n.º 17
0
                return build_error_response(request, 502, msg)
        else:
            # Code contents comes as unicode from persistence, we need bytes
            code = code.encode(charset)

        if xhtml.cacheable and (xhtml.code == '' or xhtml.code_timestamp is None):
            xhtml.code = code.decode(charset)
            xhtml.code_timestamp = time.time() * 1000
            xhtml.save()
        elif not xhtml.cacheable and xhtml.code != '':
            xhtml.code = ''
            xhtml.code_timestamp = None
            xhtml.save()

        try:
            code = fix_widget_code(code, base_url, content_type, request, charset, xhtml.use_platform_style, force_base=force_base)
        except UnicodeEncodeError:
            msg = _('Widget code was not encoded using the specified charset (%(charset)s according to the widget descriptor file).')
            return build_error_response(request, 502, msg % {'charset': charset})

        if xhtml.cacheable:
            cache_timeout = 31536000  # 1 year
            cache_entry = {
                'code': code,
                'mimetype': '%s; charset=%s' % (content_type, charset),
                'timestamp': xhtml.code_timestamp,
                'timeout': cache_timeout,
            }
            cache.set(cache_key, cache_entry, cache_timeout)
        else:
            cache_timeout = 0
Exemplo n.º 18
0
def process_widget_code(request, resource):

    mode = request.GET.get('mode', 'classic')
    theme = request.GET.get('theme', get_active_theme_name())
    widget_info = json.loads(resource.json_description)

    # check if the xhtml code has been cached
    if widget_info['contents']['cacheable'] is True:

        cache_key = resource.widget.xhtml.get_cache_key(
            get_current_domain(request), mode, theme)
        cache_entry = cache.get(cache_key)
        if cache_entry is not None:
            response = HttpResponse(cache_entry['code'],
                                    content_type=cache_entry['content_type'])
            patch_cache_headers(response, cache_entry['timestamp'],
                                cache_entry['timeout'])
            return response

    # process xhtml
    xhtml = resource.widget.xhtml
    content_type = widget_info['contents'].get('contenttype', 'text/html')
    charset = widget_info['contents'].get('charset', 'utf-8')

    code = xhtml.code
    if not xhtml.cacheable or code == '':
        try:
            code = download_local_file(
                os.path.join(showcase_utils.wgt_deployer.root_dir,
                             url2pathname(xhtml.url)))

        except Exception as e:
            if isinstance(e, IOError) and e.errno == errno.ENOENT:
                return build_response(request, 404, {
                    'error_msg': _("Widget code not found"),
                    'details': "%s" % e
                }, WIDGET_ERROR_FORMATTERS)
            else:
                return build_response(
                    request, 500, {
                        'error_msg': _("Error reading widget code"),
                        'details': "%s" % e
                    }, WIDGET_ERROR_FORMATTERS)
    else:
        # Code contents comes as unicode from persistence, we need bytes
        code = code.encode(charset)

    if xhtml.cacheable and (xhtml.code == '' or xhtml.code_timestamp is None):
        try:
            xhtml.code = code.decode(charset)
        except UnicodeDecodeError:
            msg = _(
                'Widget code was not encoded using the specified charset (%(charset)s as stated in the widget description file).'
            ) % {
                'charset': charset
            }
            return build_response(request, 502, {'error_msg': msg},
                                  WIDGET_ERROR_FORMATTERS)

        xhtml.code_timestamp = time.time() * 1000
        xhtml.save()

    try:
        code = fix_widget_code(
            code, content_type, request, charset, xhtml.use_platform_style,
            process_requirements(widget_info['requirements']), mode, theme)
    except UnicodeDecodeError:
        msg = _(
            'Widget code was not encoded using the specified charset (%(charset)s as stated in the widget description file).'
        ) % {
            'charset': charset
        }
        return build_response(request, 502, {'error_msg': msg},
                              WIDGET_ERROR_FORMATTERS)
    except Exception as e:
        msg = _('Error processing widget code')
        return build_response(request, 502, {
            'error_msg': msg,
            'details': "%s" % e
        }, WIDGET_ERROR_FORMATTERS)

    if xhtml.cacheable:
        cache_timeout = 31536000  # 1 year
        cache_entry = {
            'code': code,
            'content_type': '%s; charset=%s' % (content_type, charset),
            'timestamp': xhtml.code_timestamp,
            'timeout': cache_timeout,
        }
        cache.set(cache_key, cache_entry, cache_timeout)
    else:
        cache_timeout = 0

    response = HttpResponse(code,
                            content_type='%s; charset=%s' %
                            (content_type, charset))
    patch_cache_headers(response, xhtml.code_timestamp, cache_timeout)
    return response
Exemplo n.º 19
0
 def test_basic_html_iso8859_15(self):
     initial_code = self.read_file('test-data/xhtml1-iso8859-15-initial.html')
     final_code = self.XML_NORMALIZATION_RE.sub(b'><', fix_widget_code(initial_code, 'http://server.com/widget', 'text/html', None, 'iso-8859-15', False, {}, False, 'classic')) + b'\n'
     expected_code = self.read_file('test-data/xhtml1-iso8859-15-expected.html')
     self.assertEqual(final_code, expected_code)
Exemplo n.º 20
0
    def read(self, request, vendor, name, version):

        resource = get_object_or_404(CatalogueResource.objects.select_related('widget__xhtml'), vendor=vendor, short_name=name, version=version)
        # For now, all widgets are freely accessible/distributable
        #if not resource.is_available_for(request.user):
        #    return build_error_response(request, 403, "Forbidden")

        if resource.resource_type() != 'widget':
            raise Http404()

        mode = request.GET.get('mode', 'classic')
        widget_info = json.loads(resource.json_description)

        # check if the xhtml code has been cached
        if widget_info['contents']['cacheable'] is True:

            cache_key = resource.widget.xhtml.get_cache_key(get_current_domain(request), mode)
            cache_entry = cache.get(cache_key)
            if cache_entry is not None:
                response = HttpResponse(cache_entry['code'], content_type=cache_entry['content_type'])
                patch_cache_headers(response, cache_entry['timestamp'], cache_entry['timeout'])
                return response

        # process xhtml
        xhtml = resource.widget.xhtml
        content_type = widget_info['contents'].get('contenttype', 'text/html')
        charset = widget_info['contents'].get('charset', 'utf-8')

        force_base = False
        base_url = xhtml.url
        if not base_url.startswith(('http://', 'https://')):
            # Newer versions of Django urlencode urls created using reverse
            # Fix double encoding
            base_url = urlunquote(base_url)
            base_url = get_absolute_reverse_url('wirecloud.showcase_media', args=(base_url.split('/', 3)), request=request)
            force_base = True

        code = xhtml.code
        if not xhtml.cacheable or code == '':
            try:
                if xhtml.url.startswith(('http://', 'https://')):
                    code = download_http_content(urljoin(base_url, xhtml.url), user=request.user)
                else:
                    code = download_local_file(os.path.join(showcase_utils.wgt_deployer.root_dir, url2pathname(xhtml.url)))

            except Exception as e:
                return build_response(request, 502, {'error_msg': _("(X)HTML code is not accessible"), 'details': "%s" % e}, WIDGET_ERROR_FORMATTERS)
        else:
            # Code contents comes as unicode from persistence, we need bytes
            code = code.encode(charset)

        if xhtml.cacheable and (xhtml.code == '' or xhtml.code_timestamp is None):
            try:
                xhtml.code = code.decode(charset)
            except UnicodeDecodeError:
                msg = _('Widget code was not encoded using the specified charset (%(charset)s as stated in the widget description file).') % {'charset': charset}
                return build_response(request, 502, {'error_msg': msg}, WIDGET_ERROR_FORMATTERS)

            xhtml.code_timestamp = time.time() * 1000
            xhtml.save()
        elif not xhtml.cacheable and xhtml.code != '':
            xhtml.code = ''
            xhtml.code_timestamp = None
            xhtml.save()

        try:
            code = fix_widget_code(code, base_url, content_type, request, charset, xhtml.use_platform_style, process_requirements(widget_info['requirements']), force_base, mode)
        except UnicodeDecodeError:
            msg = _('Widget code was not encoded using the specified charset (%(charset)s as stated in the widget description file).') % {'charset': charset}
            return build_response(request, 502, {'error_msg': msg}, WIDGET_ERROR_FORMATTERS)
        except Exception as e:
            msg = _('Error processing widget code')
            return build_response(request, 502, {'error_msg': msg, 'details':"%s" % e}, WIDGET_ERROR_FORMATTERS)

        if xhtml.cacheable:
            cache_timeout = 31536000  # 1 year
            cache_entry = {
                'code': code,
                'content_type': '%s; charset=%s' % (content_type, charset),
                'timestamp': xhtml.code_timestamp,
                'timeout': cache_timeout,
            }
            cache.set(cache_key, cache_entry, cache_timeout)
        else:
            cache_timeout = 0

        response = HttpResponse(code, content_type='%s; charset=%s' % (content_type, charset))
        patch_cache_headers(response, xhtml.code_timestamp, cache_timeout)
        return response
Exemplo n.º 21
0
 def test_html_with_more_than_one_base_element_force_base(self):
     initial_code = self.read_file('test-data/xhtml4-extra-base-elements-initial.html')
     final_code = self.XML_NORMALIZATION_RE.sub(b'><', fix_widget_code(initial_code, 'http://server.com/widget', 'text/html', None, 'utf-8', False, {}, True, 'classic')) + b'\n'
     expected_code = self.read_file('test-data/xhtml4-forced-base-expected.html')
     self.assertEqual(final_code, expected_code)
Exemplo n.º 22
0
 def test_basic_html(self):
     initial_code = self.read_file('test-data/xhtml1-initial.html')
     final_code = self.XML_NORMALIZATION_RE.sub('><', fix_widget_code(initial_code, 'http://server.com/widget', 'text/html', None, False)) + '\n'
     expected_code = self.read_file('test-data/xhtml1-expected.html')
     self.assertEqual(final_code, expected_code)
Exemplo n.º 23
0
 def test_basic_xhtml(self):
     initial_code = self.read_file('test-data/xhtml2-initial.html')
     final_code = self.XML_NORMALIZATION_RE.sub(b'><', fix_widget_code(initial_code, 'http://server.com/widget', 'application/xhtml+xml', None, 'utf-8', False, {}, False, 'classic')) + b'\n'
     expected_code = self.read_file('test-data/xhtml2-expected.html')
     self.assertEqual(final_code, expected_code)
Exemplo n.º 24
0
    def read(self, request, vendor, name, version):

        resource = get_object_or_404(CatalogueResource.objects.select_related('widget__xhtml'), vendor=vendor, short_name=name, version=version)
        # For now, all widgets are freely accessible/distributable
        #if not resource.is_available_for(request.user):
        #    return build_error_response(request, 403, "Forbidden")

        if resource.resource_type() != 'widget':
            raise Http404()

        mode = request.GET.get('mode', 'classic')
        widget_info = json.loads(resource.json_description)

        # check if the xhtml code has been cached
        if widget_info['contents']['cacheable'] is True:

            cache_key = resource.widget.xhtml.get_cache_key(get_current_domain(request), mode)
            cache_entry = cache.get(cache_key)
            if cache_entry is not None:
                response = HttpResponse(cache_entry['code'], content_type=cache_entry['content_type'])
                patch_cache_headers(response, cache_entry['timestamp'], cache_entry['timeout'])
                return response

        # process xhtml
        xhtml = resource.widget.xhtml
        content_type = widget_info['contents'].get('contenttype', 'text/html')
        charset = widget_info['contents'].get('charset', 'utf-8')

        force_base = False
        base_url = xhtml.url
        if not base_url.startswith(('http://', 'https://')):
            # Newer versions of Django urlencode urls created using reverse
            # Fix double encoding
            base_url = urlunquote(base_url)
            base_url = get_absolute_reverse_url('wirecloud.showcase_media', args=(base_url.split('/', 4)), request=request)
            force_base = True

        code = xhtml.code
        if not xhtml.cacheable or code == '':
            try:
                if xhtml.url.startswith(('http://', 'https://')):
                    code = download_http_content(urljoin(base_url, xhtml.url), user=request.user)
                else:
                    code = download_local_file(os.path.join(showcase_utils.wgt_deployer.root_dir, url2pathname(xhtml.url)))

            except Exception as e:
                return build_response(request, 502, {'error_msg': _("(X)HTML code is not accessible"), 'details': "%s" % e}, WIDGET_ERROR_FORMATTERS)
        else:
            # Code contents comes as unicode from persistence, we need bytes
            code = code.encode(charset)

        if xhtml.cacheable and (xhtml.code == '' or xhtml.code_timestamp is None):
            try:
                xhtml.code = code.decode(charset)
            except UnicodeDecodeError:
                msg = _('Widget code was not encoded using the specified charset (%(charset)s as stated in the widget description file).') % {'charset': charset}
                return build_response(request, 502, {'error_msg': msg}, WIDGET_ERROR_FORMATTERS)

            xhtml.code_timestamp = time.time() * 1000
            xhtml.save()
        elif not xhtml.cacheable and xhtml.code != '':
            xhtml.code = ''
            xhtml.code_timestamp = None
            xhtml.save()

        try:
            code = fix_widget_code(code, base_url, content_type, request, charset, xhtml.use_platform_style, process_requirements(widget_info['requirements']), force_base, mode)
        except UnicodeDecodeError:
            msg = _('Widget code was not encoded using the specified charset (%(charset)s as stated in the widget description file).') % {'charset': charset}
            return build_response(request, 502, {'error_msg': msg}, WIDGET_ERROR_FORMATTERS)
        except Exception as e:
            msg = _('Error processing widget code')
            return build_response(request, 502, {'error_msg': msg, 'details':"%s" % e}, WIDGET_ERROR_FORMATTERS)

        if xhtml.cacheable:
            cache_timeout = 31536000  # 1 year
            cache_entry = {
                'code': code,
                'content_type': '%s; charset=%s' % (content_type, charset),
                'timestamp': xhtml.code_timestamp,
                'timeout': cache_timeout,
            }
            cache.set(cache_key, cache_entry, cache_timeout)
        else:
            cache_timeout = 0

        response = HttpResponse(code, content_type='%s; charset=%s' % (content_type, charset))
        patch_cache_headers(response, xhtml.code_timestamp, cache_timeout)
        return response
Exemplo n.º 25
0
    def test_empty_html(self):

        initial_code = b''
        final_code = self.XML_NORMALIZATION_RE.sub(b'><', fix_widget_code(initial_code, 'http://server.com/widget', 'text/html', None, 'utf-8', False, {}, False, 'classic', 'wirecloud_defaulttheme')) + b'\n'
        expected_code = self.read_file('test-data/html-empty-expected.html')
        self.assertEqual(final_code, expected_code)