def kXR_attn_asynresp(self, streamid=None, status=None, dlen=None, actnum=None, reserved=None, rstreamid=None, rstatus=None, rlen=None, rdata=None): """Return a packed representation of a kXR_attn_asynresp response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Attn_asynresp') if not rdata: rdata = '' params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_attn'), 'dlen' : dlen if dlen else len(rdata) + 16, 'actnum' : actnum if actnum else get_attncode('kXR_asynresp'), 'reserved': reserved if reserved else (4 * '\0'), 'rsid' : rstreamid if rstreamid else 0, 'rstatus' : rstatus if rstatus else get_responseid('kXR_ok'), 'rlen' : rlen if rlen else len(rdata), 'rdata' : rdata} return self.mh.build_message(response_struct, params)
def kXR_login(self, streamid=None, status=None, dlen=None, sessid=None, sec=None, verify_auth=False): """Return a packed representation of a kXR_login response. Pass verify_auth=True to enable authentication.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Login') # Check if client needs to authenticate if verify_auth and not sec: auth = AuthHelper.AuthHelper(self.context) sec = auth.getsectoken() else: sec = '' params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_ok'), 'dlen' : dlen if dlen else len(sec) + 16, 'sessid' : sessid if sessid else gen_sessid(), 'sec' : sec} return self.mh.build_message(response_struct, params)
def kXR_open(self, streamid=None, status=None, dlen=None, fhandle=None, cpsize=None, cptype=None, data=None): """Return a packed representation of a kXR_open response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Open') params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_ok'), 'dlen' : 0, 'fhandle' : fhandle if fhandle else (4 * '\0'), 'cpsize' : cpsize if cpsize else 0, 'cptype' : cptype if cptype else (4 * '\0'), 'data' : data if data else ''} if not cpsize: del response_struct[4] del params['cpsize'] if not cptype: del response_struct[4] del params['cptype'] if not data: del response_struct[4] del params['data'] if not dlen: dlen = 4 + (len(cpsize) if cpsize else 0) \ + (len(cptype) if cptype else 0) \ + (len(data) if data else 0) params['dlen'] = dlen return self.mh.build_message(response_struct, params)
def kXR_bind(self, streamid=None, status=None, dlen=None, pathid=None): """Return a packed representation of a kXR_bind response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Bind') params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_ok'), 'dlen' : dlen if dlen else 1, 'pathid' : pathid if pathid else '\0'} return self.mh.build_message(response_struct, params)
def kXR_waitresp(self, streamid=None, status=None, dlen=None, seconds=None): """Return a packed representation of a kXR_waitresp response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Waitresp') params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_waitresp'), 'dlen' : dlen if dlen else 4, 'seconds' : seconds if seconds else 0} return self.mh.build_message(response_struct, params)
def kXR_ok(self, streamid=None, status=None, dlen=None, data=None): """Return a packed representation of a kXR_ok response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Buffer') if not data: data = '' params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_ok'), 'dlen' : dlen if dlen else len(data), 'data' : data} return self.mh.build_message(response_struct, params)
def kXR_attn_asyncwt(self, streamid=None, status=None, dlen=None, actnum=None, wsec=None): """Return a packed representation of a kXR_attn_asyncwt response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Attn_asyncwt') params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_attn'), 'dlen' : dlen if dlen else 8, 'actnum' : actnum if actnum else get_attncode('kXR_asyncwt'), 'wsec' : wsec if wsec else 0} return self.mh.build_message(response_struct, params)
def kXR_protocol(self, streamid=None, status=None, dlen=None, pval=None, flags=None): """Return a packed representation of a kXR_protocol response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Protocol') params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_ok'), 'dlen' : dlen if dlen else 8, 'pval' : pval if pval else XProtocol.kXR_PROTOCOLVERSION, 'flags' : flags if flags else XProtocol.kXR_isServer} return self.mh.build_message(response_struct, params)
def kXR_attn_asyncab(self, streamid=None, status=None, dlen=None, actnum=None, msg=None): """Return a packed representation of a kXR_attn_asyncab response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Attn') if not msg: msg = '' params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_attn'), 'dlen' : dlen if dlen else len(msg) + 4, 'actnum' : actnum if actnum else get_attncode('kXR_asyncab'), 'parms' : msg} return self.mh.build_message(response_struct, params)
def kXR_redirect(self, streamid=None, status=None, dlen=None, port=None, host=None, opaque=None, token=None): """Return a packed representation of a kXR_redirect response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Redirect') if not host: host = '' else: host += (opaque if opaque else '') + (token if token else '') params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_redirect'), 'dlen' : dlen if dlen else len(host) + 4, 'port' : port if port else 0, 'host' : host if host else r''} return self.mh.build_message(response_struct, params)
def kXR_error(self, streamid=None, status=None, dlen=None, errnum=None, errmsg=None): """Return a packed representation of a kXR_error response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Error') if not errmsg: errmsg = '' if not errnum: errnum = XProtocol.XErrorCode.kXR_ArgInvalid params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_error'), 'dlen' : dlen if dlen else len(errmsg + str(errnum)), 'errnum' : errnum, 'errmsg' : errmsg} return self.mh.build_message(response_struct, params)
def kXR_attn_asyncrd(self, streamid=None, status=None, dlen=None, actnum=None, port=None, host=None, token=None): """Return a packed representation of a kXR_attn_asyncrd response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Attn_asyncrd') if not host: host = '' else: host += (token if token else '') params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_attn'), 'dlen' : dlen if dlen else len(host), 'actnum' : actnum if actnum else get_attncode('kXR_asyncrd'), 'port' : port if port else 0, 'host' : host} return self.mh.build_message(response_struct, params)
def kXR_open(self, streamid=None, status=None, dlen=None, fhandle=None, cpsize=None, cptype=None, data=None): """Return a packed representation of a kXR_open response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Open') params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_ok'), 'dlen' : 0, 'fhandle' : fhandle if fhandle else (4 * '\0'), 'cpsize' : cpsize if cpsize else 0, 'cptype' : cptype if cptype else (4 * '\0'), 'data' : data if data else ''} if not cpsize: del response_struct[4]; del params['cpsize'] if not cptype: del response_struct[4]; del params['cptype'] if not data: del response_struct[4]; del params['data'] if not dlen: dlen = 4 + (len(cpsize) if cpsize else 0) \ + (len(cptype) if cptype else 0) \ + (len(data) if data else 0) params['dlen'] = dlen return self.mh.build_message(response_struct, params)
def kXR_oksofar(self, streamid=None, status=None, dlen=None, data=None): """Return a packed representation of a kXR_oksofar response.""" status = get_responseid('kXR_oksofar') return self.kXR_ok(streamid, status, dlen, data)
def kXR_authmore(self, streamid=None, status=None, dlen=None, data=None): """Return a packed representation of a kXR_authmore response.""" if not status: status = get_responseid('kXR_authmore') return self.kXR_ok(streamid, status, dlen, data)
def unpack_response(self, response_raw, request_raw): """Return an unpacked named tuple representation of a server response.""" if not len(response_raw): return '' # Unpack the request that generated this response for reference request = self.unpack_request(request_raw)[0] requestid = get_requestid(request.type) # Unpack the response header to find the status and data length header_struct = get_struct('ServerResponseHeader') format = '>' for member in header_struct: format += member['type'] header = self.unpack(format + (str(len(response_raw) - 8) + 's'), response_raw) streamid = header[0] status = header[1] dlen = header[2] body = header[-1] # Check if this is a handshake response if requestid == XProtocol.XRequestTypes.handshake: body_struct = get_struct('ServerInitHandShake') # Check if this is an asynchronous response elif status == XProtocol.XResponseType.kXR_attn: # Extract the attn code attncode = self.unpack('>H', body[:2])[0] body_struct = get_struct('ServerResponseBody_Attn_' \ + get_attncode(attncode)[4:]) if not body_struct: body_struct = body_struct = get_struct('ServerResponseBody_Attn') # Check if this is more than a simple kXR_ok response elif status != XProtocol.XResponseType.kXR_ok: body_struct = get_struct('ServerResponseBody_' \ + get_responseid(status)[4:].title()) else: body_struct = get_struct('ServerResponseBody_' \ + request.type[4:].title()) if not body_struct: body_struct = list() response_struct = header_struct + body_struct # The number of params in a kXR_open response depends on the options that # were passed in the request. if requestid == XProtocol.XRequestTypes.kXR_open: # Remove members from the response struct if the option was not given in # the request. response_struct[:] = [m for m in response_struct if self.option_included(m, request, response_raw)] # Build complete format string format = '>' for member in response_struct: if member.has_key('size'): if member['size'] == 'dlen': if member.has_key('offset'): format += str(dlen - member['offset']) + member['type'] else: format += str(dlen) + member['type'] else: format += str(member['size']) + member['type'] else: format += member['type'] if len(body_struct) == 0 and dlen > 0: format += (str(dlen) + 's') # Unpack to regular tuple response_tuple = self.unpack(format, response_raw) # Convert to named tuple response_struct.insert(0, {'name': 'type'}) type = get_responseid(status) response = namedtuple('response', ' '.join([m['name'] for m in response_struct])) return response(type, *response_tuple)
def unpack_response(self, response_raw, request_raw): """Return an unpacked named tuple representation of a server response.""" if not len(response_raw): return '' # Unpack the request that generated this response for reference request = self.unpack_request(request_raw)[0] requestid = get_requestid(request.type) # Unpack the response header to find the status and data length header_struct = get_struct('ServerResponseHeader') format = '>' for member in header_struct: format += member['type'] header = self.unpack(format + (str(len(response_raw) - 8) + 's'), response_raw) streamid = header[0] status = header[1] dlen = header[2] body = header[-1] # Check if this is a handshake response if requestid == XProtocol.XRequestTypes.handshake: body_struct = get_struct('ServerInitHandShake') # Check if this is an asynchronous response elif status == XProtocol.XResponseType.kXR_attn: # Extract the attn code attncode = self.unpack('>H', body[:2])[0] body_struct = get_struct('ServerResponseBody_Attn_' \ + get_attncode(attncode)[4:]) if not body_struct: body_struct = body_struct = get_struct( 'ServerResponseBody_Attn') # Check if this is more than a simple kXR_ok response elif status != XProtocol.XResponseType.kXR_ok: body_struct = get_struct('ServerResponseBody_' \ + get_responseid(status)[4:].title()) else: body_struct = get_struct('ServerResponseBody_' \ + request.type[4:].title()) if not body_struct: body_struct = list() response_struct = header_struct + body_struct # The number of params in a kXR_open response depends on the options that # were passed in the request. if requestid == XProtocol.XRequestTypes.kXR_open: # Remove members from the response struct if the option was not given in # the request. response_struct[:] = [ m for m in response_struct if self.option_included(m, request, response_raw) ] # Build complete format string format = '>' for member in response_struct: if member.has_key('size'): if member['size'] == 'dlen': if member.has_key('offset'): format += str(dlen - member['offset']) + member['type'] else: format += str(dlen) + member['type'] else: format += str(member['size']) + member['type'] else: format += member['type'] if len(body_struct) == 0 and dlen > 0: format += (str(dlen) + 's') # Unpack to regular tuple response_tuple = self.unpack(format, response_raw) # Convert to named tuple response_struct.insert(0, {'name': 'type'}) type = get_responseid(status) response = namedtuple('response', ' '.join([m['name'] for m in response_struct])) return response(type, *response_tuple)