def download_file(ctx, project_name, version, file_name): repository_path = os.path.abspath(os.path.join(FILES_PATH)) file_path = os.path.join(repository_path, project_name, version, file_name) file_path = os.path.abspath(file_path) if not file_path.startswith(repository_path): # This request tried to read data from where it's not supposed to raise RequestNotAllowed(repr([project_name, version, file_name])) return File.Value(name=file_name, path=file_path)
def test_file_value(self): dbe = _DictDocumentChild.default_binary_encoding beh = binary_encoding_handlers[dbe] # Prepare data v = File.Value( name='some_file.bin', type='application/octet-stream', ) file_data = bytes(bytearray(range(0xff))) v.data = (file_data, ) beh([file_data]) if _DictDocumentChild.text_based: test_data = beh(v.data).decode('latin1') else: test_data = beh(v.data) print(repr(v.data)) class SomeService(Service): @srpc(File, _returns=File) def some_call(p): print(p) print(type(p)) assert isinstance(p, File.Value) assert p.data == (file_data, ) assert p.type == v.type assert p.name == v.name return p d = get_object_as_dict(v, File, protocol=_DictDocumentChild, ignore_wrappers=False) ctx = _dry_me([SomeService], {"some_call": {'p': d}}) s = b''.join(ctx.out_string) d = self.dumps({ "some_callResponse": { "some_callResult": { 'name': v.name, 'type': v.type, 'data': test_data, } } }) print(self.loads(s)) print(self.loads(d)) print(v) assert self.loads(s) == self.loads(d)
def test_file_value(self): dbe = _DictDocumentChild.default_binary_encoding beh = binary_encoding_handlers[dbe] # Prepare data v = File.Value( name='some_file.bin', type='application/octet-stream', ) file_data = ''.join([chr(x) for x in range(0xff)]) v.data = beh(file_data) class SomeService(ServiceBase): @srpc(File, _returns=File) def some_call(p): print(p) print(type(p)) assert isinstance(p, File.Value) assert p.data == [file_data] assert p.type == v.type assert p.name == v.name return p d = get_object_as_dict(v, File, ignore_wrappers=False) assert d[File.get_type_name()]['name'] == v.name assert d[File.get_type_name()]['type'] == v.type assert d[File.get_type_name()]['data'] == v.data ctx = _dry_me([SomeService], {"some_call": {'p': d}}) s = ''.join(ctx.out_string) d = serializer.dumps( { "some_callResponse": { "some_callResult": { 'name': v.name, 'type': v.type, 'data': v.data, } } }, **dumps_kwargs) print(serializer.loads(s)) print(serializer.loads(d)) print(v) assert serializer.loads(s) == serializer.loads(d)
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 = _get_http_headers(wsgi_env) 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]
def file_from_string(cls, value, suggested_encoding=None): encoding = cls.Attributes.encoding if encoding is None: encoding = suggested_encoding return File.Value(data=binary_decoding_handlers[encoding](value))
def file_from_string(cls, value, suggested_encoding=None): encoding = cls.Attributes.encoding if encoding is BINARY_ENCODING_USE_DEFAULT: encoding = suggested_encoding return File.Value(data=binary_decoding_handlers[encoding](value))
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``. """ if self.has_patterns: from werkzeug.exceptions import NotFound if self._map_adapter is None: self.generate_map_adapter(ctx) try: #If PATH_INFO matches a url, Set method_request_string to mrs mrs, params = self._map_adapter.match( ctx.in_document["PATH_INFO"], ctx.in_document["REQUEST_METHOD"]) ctx.method_request_string = mrs except NotFound: # Else set method_request_string normally params = {} ctx.method_request_string = '{%s}%s' % ( prot.app.interface.get_tns(), ctx.in_document['PATH_INFO'].split('/')[-1]) else: params = {} ctx.method_request_string = '{%s}%s' % (prot.app.interface.get_tns( ), ctx.in_document['PATH_INFO'].split('/')[-1]) logger.debug("%sMethod name: %r%s" % (LIGHT_GREEN, ctx.method_request_string, END_COLOR)) ctx.in_header_doc = _get_http_headers(ctx.in_document) ctx.in_body_doc = parse_qs(ctx.in_document['QUERY_STRING']) for k, v in params.items(): if k in ctx.in_body_doc: ctx.in_body_doc[k].append(v) else: ctx.in_body_doc[k] = [v] if ctx.in_document['REQUEST_METHOD'].upper() in ('POST', 'PUT', 'PATCH'): stream, form, files = parse_form_data( ctx.in_document, 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