def decompose_incoming_envelope(self, prot, ctx, message): """This function is only called by the HttpRpc protocol to have the twisted web's Request object is parsed into ``ctx.in_body_doc`` and ``ctx.in_header_doc``. """ request = ctx.in_document assert isinstance(request, Request) ctx.in_header_doc = dict(request.requestHeaders.getAllRawHeaders()) fi = ctx.transport.file_info if fi is not None and len(request.args) == 1: key, = request.args.keys() if fi.field_name == key and fi.file_name is not None: ctx.in_body_doc = {key: [File.Value(name=fi.file_name, type=fi.file_type, data=request.args[key])]} else: ctx.in_body_doc = request.args else: ctx.in_body_doc = request.args # this is a huge hack because twisted seems to take the slashes in urls # too seriously. postpath = getattr(request, 'realpostpath', None) if postpath is None: postpath = request.path params = self.match_pattern(ctx, request.method, postpath, request.getHeader('Host')) if ctx.method_request_string is None: # no pattern match ctx.method_request_string = '{%s}%s' % (self.app.interface.get_tns(), request.path.rsplit('/', 1)[-1]) logger.debug("%sMethod name: %r%s" % (LIGHT_GREEN, ctx.method_request_string, END_COLOR)) for k, v in params.items(): val = ctx.in_body_doc.get(k, []) val.extend(v) ctx.in_body_doc[k] = val r = {} for k,v in ctx.in_body_doc.items(): l = [] for v2 in v: if isinstance(v2, string_types): l.append(unquote(v2)) else: l.append(v2) r[k] = l ctx.in_body_doc = r # This is consistent with what server.wsgi does. if request.method in ('POST', 'PUT', 'PATCH'): for k, v in ctx.in_body_doc.items(): if v == ['']: ctx.in_body_doc[k] = [None]
def test_file_storage(self): class C(TableModel): __tablename__ = "c" id = Integer32(pk=True) f = File(store_as=HybridFileStore('test_file_storage', 'json')) self.metadata.create_all() c = C(f=File.Value(name=u"name", type=u"type", data=[b"data"])) self.session.add(c) self.session.flush() self.session.commit() c = self.session.query(C).get(1) print(c) assert c.f.name == "name" assert c.f.type == "type" assert c.f.data[0][:] == b"data"
def test_log_repr_complex(self): from spyne.model import ByteArray from spyne.model import File from spyne.model.complex import ComplexModel from spyne.model.primitive import String from spyne.util.web import log_repr class Z(ComplexModel): _type_info = [ ('f', File(logged=False)), ('t', ByteArray(logged=False)), ('z', Array(String)), ] l = MAX_STRING_FIELD_LENGTH + 100 val = Z(z=["abc"] * l, t=['t'], f=File.Value(name='aaa', data=['t'])) print(repr(val)) assert log_repr(val) == "Z(z=['abc', 'abc', (...)])"
def file_from_string(self, 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 file_from_bytes(self, cls, value, suggested_encoding=None): encoding = self.get_cls_attrs(cls).encoding if encoding is BINARY_ENCODING_USE_DEFAULT: encoding = suggested_encoding return File.Value(data=binary_decoding_handlers[encoding](value))