Example #1
0
 def validate_json(self, contents):
     """
     Verify that the enclosed manifest.json is a valid and parsable JSON
     file.
     """
     try:
         # We support only UTF-8 encoded manifests.
         decoded_data = smart_unicode(strip_bom(contents))
     except UnicodeDecodeError:
         self.error("INVALID_JSON_ENCODING")
     try:
         return json.loads(decoded_data)
     except ValueError:
         self.error("INVALID_JSON")
Example #2
0
 def validate_json(self, contents):
     """
     Verify that the enclosed manifest.json is a valid and parsable JSON
     file.
     """
     try:
         # We support only UTF-8 encoded manifests.
         decoded_data = smart_unicode(strip_bom(contents))
     except UnicodeDecodeError:
         self.error('INVALID_JSON_ENCODING')
     try:
         return json.loads(decoded_data)
     except ValueError:
         self.error('INVALID_JSON')
Example #3
0
    def decode_manifest(cls, manifest):
        """
        Returns manifest, stripped of BOMs and UTF-8 decoded, as Python dict.
        """
        try:
            data = strip_bom(manifest)
            # Marketplace only supports UTF-8 encoded manifests.
            decoded_data = data.decode('utf-8')
        except (ValueError, UnicodeDecodeError) as exc:
            msg = 'Error parsing manifest (encoding: utf-8): %s: %s'
            log.error(msg % (exc.__class__.__name__, exc))
            raise forms.ValidationError(
                _('Could not decode the webapp manifest file. '
                  'Check your manifest file for special non-utf-8 '
                  'characters.'))

        try:
            return json.loads(decoded_data)
        except Exception:
            raise forms.ValidationError(
                _('The webapp manifest is not valid JSON.'))
Example #4
0
    def decode_manifest(cls, manifest):
        """
        Returns manifest, stripped of BOMs and UTF-8 decoded, as Python dict.
        """
        try:
            data = strip_bom(manifest)
            # Marketplace only supports UTF-8 encoded manifests.
            decoded_data = data.decode('utf-8')
        except (ValueError, UnicodeDecodeError) as exc:
            msg = 'Error parsing manifest (encoding: utf-8): %s: %s'
            log.error(msg % (exc.__class__.__name__, exc))
            raise forms.ValidationError(
                _('Could not decode the webapp manifest file. '
                  'Check your manifest file for special non-utf-8 '
                  'characters.'))

        try:
            return json.loads(decoded_data)
        except Exception:
            raise forms.ValidationError(
                _('The webapp manifest is not valid JSON.'))
Example #5
0
        fail(_('Your manifest file was not encoded as valid UTF-8.'),
             upload=upload)
        return

    # Get the individual parts of the content type.
    ct_split = map(str.strip, ct.split(';'))
    if len(ct_split) > 1:
        # Figure out if we've got a charset specified.
        kv_pairs = dict(tuple(p.split('=', 1)) for p in ct_split[1:] if
                        '=' in p)
        if 'charset' in kv_pairs and kv_pairs['charset'].lower() != 'utf-8':
            fail(_("The manifest's encoding does not match the charset "
                   'provided in the HTTP Content-Type.'),
                 upload=upload)

    content = strip_bom(content)
    return content


@post_request_task
@use_master
def fetch_manifest(url, upload_pk=None, **kw):
    log.info(u'[1@None] Fetching manifest: %s.' % url)
    upload = FileUpload.objects.get(pk=upload_pk)

    content = _fetch_manifest(url, upload)
    if content is None:
        return

    upload.add_file([content], url, len(content))
    # Send the upload to the validator.
Example #6
0
        fail(_('Your manifest file was not encoded as valid UTF-8.'),
             upload=upload)
        return

    # Get the individual parts of the content type.
    ct_split = map(str.strip, ct.split(';'))
    if len(ct_split) > 1:
        # Figure out if we've got a charset specified.
        kv_pairs = dict(tuple(p.split('=', 1)) for p in ct_split[1:] if
                        '=' in p)
        if 'charset' in kv_pairs and kv_pairs['charset'].lower() != 'utf-8':
            fail(_("The manifest's encoding does not match the charset "
                   'provided in the HTTP Content-Type.'),
                 upload=upload)

    content = strip_bom(content)
    return content


@task
@use_master
def fetch_manifest(url, upload_pk=None, **kw):
    log.info(u'[1@None] Fetching manifest: %s.' % url)
    upload = FileUpload.objects.get(pk=upload_pk)

    content = _fetch_manifest(url, upload)
    if content is None:
        return

    upload.add_file([content], url, len(content))
    # Send the upload to the validator.