def read_header_format(self, sz, header_size, data): # clear out any previous transforms self.__read_transforms = [] header_size = header_size * 4 if header_size > sz: raise TTransportException(TTransportException.INVALID_FRAME_SIZE, "Header size is larger than frame") end_header = header_size + data.tell() self.__proto_id = readVarint(data) num_headers = readVarint(data) if self.__proto_id == 1 and self.__client_type != \ CLIENT_TYPE.HTTP_SERVER: raise TTransportException(TTransportException.INVALID_CLIENT_TYPE, "Trying to recv JSON encoding over binary") # Read the headers. Data for each header varies. for _ in range(0, num_headers): trans_id = readVarint(data) if trans_id == TRANSFORM.ZLIB: self.__read_transforms.insert(0, trans_id) elif trans_id == TRANSFORM.SNAPPY: self.__read_transforms.insert(0, trans_id) elif trans_id == TRANSFORM.HMAC: raise TApplicationException( TApplicationException.INVALID_TRANSFORM, "Hmac transform is no longer supported: %i" % trans_id) else: # TApplicationException will be sent back to client raise TApplicationException( TApplicationException.INVALID_TRANSFORM, "Unknown transform in client request: %i" % trans_id) # Clear out previous info headers. self.__read_headers.clear() # Read the info headers. while data.tell() < end_header: info_id = readVarint(data) if info_id == INFO.NORMAL: _read_info_headers( data, end_header, self.__read_headers) elif info_id == INFO.PERSISTENT: _read_info_headers( data, end_header, self.__read_persistent_headers) else: break # Unknown header. Stop info processing. if self.__read_persistent_headers: self.__read_headers.update(self.__read_persistent_headers) # Skip the rest of the header data.seek(end_header) payload = data.read(sz - header_size) # Read the data section. self.__rbuf = StringIO(self.untransform(payload))
def recv_verifyClientVersion(self, ): (fname, mtype, rseqid) = self._iprot.readMessageBegin() if mtype == TMessageType.EXCEPTION: x = TApplicationException() x.read(self._iprot) self._iprot.readMessageEnd() raise x result = verifyClientVersion_result() result.read(self._iprot) self._iprot.readMessageEnd() if result.success != None: return result.success raise TApplicationException(TApplicationException.MISSING_RESULT, "verifyClientVersion failed: unknown result");
def write_results_after_future( result, event_handler, handler_ctx, seqid, oprot, fn_name, known_exceptions, future, ): """Result/exception handler for asyncio futures.""" try: try: result.success = future.result() reply_type = TMessageType.REPLY except TException as e: for exc_name, exc_type in known_exceptions.items(): if isinstance(e, exc_type): setattr(result, exc_name, e) reply_type = TMessageType.REPLY event_handler.handlerException(handler_ctx, fn_name, e) break else: raise except Exception as e: result = TApplicationException(message=str(e)) reply_type = TMessageType.EXCEPTION event_handler.handlerError(handler_ctx, fn_name, e) write_result(result, reply_type, seqid, event_handler, handler_ctx, fn_name, oprot)
def _getProcessor(self, processor): """ Check if a processor is really a processor, or if it is a handler auto create a processor for it """ if isinstance(processor, TProcessor): return processor elif hasattr(processor, "_processor_type"): handler = processor return handler._processor_type(handler) else: raise TApplicationException( message="Could not detect processor type")
def reset_protocol(self): if self.__proto_id == self.trans.get_protocol_id(): return proto_id = self.trans.get_protocol_id() if proto_id == self.T_BINARY_PROTOCOL: self.__proto = TBinaryProtocolAccelerated(self.trans, self.strictRead, True) elif proto_id == self.T_COMPACT_PROTOCOL: self.__proto = TCompactProtocolAccelerated(self.trans) else: raise TApplicationException(TProtocolException.INVALID_PROTOCOL, "Unknown protocol requested") self.__proto_id = proto_id
def nested(self, seqid, iprot, oprot, server_ctx): _mem_before = _process_method_mem_usage() fn_name = get_function_name(func) # self is a TProcessor instance handler_ctx = self._event_handler.getHandlerContext( fn_name, server_ctx, ) set_request_context(self, iprot) try: args = self.readArgs(iprot, handler_ctx, fn_name, argtype) except Exception as e: args = argtype() log.exception( 'Exception thrown while reading arguments: `%s`', str(e), ) result = TApplicationException(message=str(e)) if not oneway: self.writeException(oprot, fn_name, seqid, result) else: if asyncio: return func(self, args, handler_ctx, seqid, oprot, fn_name) result = func(self, args, handler_ctx) if not oneway: self.writeReply(oprot, handler_ctx, fn_name, seqid, result, server_ctx) finally: reset_request_context(self) _mem_after = _process_method_mem_usage() if _mem_after - _mem_before > MEMORY_WARNING_THRESHOLD: log.error( 'Memory usage rose from %d to %d while processing `%s` ' 'with args `%s`', _mem_before, _mem_after, fn_name, str(args), )
def _handle_timeout(self, fname, seqid): exc = TApplicationException(TApplicationException.TIMEOUT, "Call to {} timed out".format(fname)) serialized_exc = self.serialize_texception(fname, seqid, exc) self._handle_message(serialized_exc, clear_timeout=False)
def make_unknown_function_exception(name): return TApplicationException( TApplicationException.UNKNOWN_METHOD, 'Unknown function {!r}'.format(name), )