Example #1
0
 def test_file(self):
     v = b'aaaa'
     f = BytesIO(v)
     elt = get_object_as_xml(File.Value(handle=f), File, 'B')
     eltstr = etree.tostring(elt)
     print(eltstr)
     assert elt.text == b64encode(v).decode('ascii')
Example #2
0
def _create_binaire_inhoud(binary_data, filename=None):
    encoded = base64.b64encode(binary_data)
    inhoud = BinaireInhoud(
        data=File.Value(data=BytesIO(encoded)),
        bestandsnaam=filename
    )
    return inhoud
Example #3
0
    def get_file(ctx, path):
        # protect against file name injection attacks
        # note that this doesn't protect against symlinks. depending on the kind
        # of write access your clients have, this may or may not be a problem.
        if not os.path.abspath(path).startswith(FILE_REPO):
            raise ValidationError(path)

        return File.Value(path=path)
    def say_hello_as_binary_file(ctx, name, times):
        # WARNING!: the native value for data is an iterable of bytes, not just
        # bytes! If you forget this you may return data using 1-byte http chunks
        # which is incredibly inefficient.

        # WARNING!: don't forget to encode your data! This is the binary
        # output mode! You can't just write unicode data to socket!

        mime_type = HttpTransportContext.gen_header("text/plain",
                                                    charset="utf8")

        return File.Value(type=mime_type,
                          data=[
                              '\n'.join(
                                  s.encode('utf8')
                                  for s in _say_hello(ctx, name, times, 'txt'))
                          ])
def _create_binaire_inhoud(binary_data, filename=None):
    inhoud = BinaireInhoud(
        data=File.Value(data=BytesIO(binary_data)),
        bestandsnaam=filename
    )
    return inhoud
Example #6
0
    def decompose_incoming_envelope(self, prot, ctx, message):
        """This function is only called by the HttpRpc protocol to have the wsgi
        environment parsed into ``ctx.in_body_doc`` and ``ctx.in_header_doc``.
        """

        params = {}
        wsgi_env = ctx.in_document

        if self.has_patterns:
            # http://legacy.python.org/dev/peps/pep-0333/#url-reconstruction
            domain = wsgi_env.get('HTTP_HOST', None)
            if domain is None:
                domain = wsgi_env['SERVER_NAME']
            else:
                domain = domain.partition(':')[0] # strip port info

            params = self.match_pattern(ctx,
                    wsgi_env.get('REQUEST_METHOD', ''),
                    wsgi_env.get('PATH_INFO', ''),
                    domain,
                )

        if ctx.method_request_string is None:
            ctx.method_request_string = '{%s}%s' % (
                                    prot.app.interface.get_tns(),
                                    wsgi_env['PATH_INFO'].split('/')[-1])

        logger.debug("%sMethod name: %r%s" % (LIGHT_GREEN,
                                          ctx.method_request_string, END_COLOR))

        ctx.in_header_doc = ctx.transport.headers
        ctx.in_body_doc = _parse_qs(wsgi_env['QUERY_STRING'])

        for k, v in params.items():
             if k in ctx.in_body_doc:
                 ctx.in_body_doc[k].extend(v)
             else:
                 ctx.in_body_doc[k] = list(v)

        verb = wsgi_env['REQUEST_METHOD'].upper()
        if verb in ('POST', 'PUT', 'PATCH'):
            stream, form, files = parse_form_data(wsgi_env,
                                             stream_factory=prot.stream_factory)

            for k, v in form.lists():
                val = ctx.in_body_doc.get(k, [])
                val.extend(v)
                ctx.in_body_doc[k] = val

            for k, v in files.items():
                val = ctx.in_body_doc.get(k, [])

                mime_type = v.headers.get('Content-Type',
                                                     'application/octet-stream')

                path = getattr(v.stream, 'name', None)
                if path is None:
                    val.append(File.Value(name=v.filename, type=mime_type,
                                                    data=[v.stream.getvalue()]))
                else:
                    v.stream.seek(0)
                    val.append(File.Value(name=v.filename, type=mime_type,
                                                    path=path, handle=v.stream))

                ctx.in_body_doc[k] = val

            for k, v in ctx.in_body_doc.items():
                if v == ['']:
                    ctx.in_body_doc[k] = [None]