def make_request(self, method): transaction_id = '%x' % random.getrandbits(64) chunk = protocol.MSRPData(transaction_id=transaction_id, method=method) chunk.add_header( protocol.ToPathHeader(self.local_path + self.remote_path + [self.remote_uri])) chunk.add_header(protocol.FromPathHeader([self.local_uri])) return chunk
def make_report(chunk, code, comment): if chunk.success_report == 'yes' or (chunk.failure_report in ('yes', 'partial') and code != 200): report = protocol.MSRPData(transaction_id='%x' % random.getrandbits(64), method='REPORT') report.add_header(protocol.ToPathHeader(chunk.from_path)) report.add_header(protocol.FromPathHeader([chunk.to_path[0]])) report.add_header(protocol.StatusHeader(protocol.Status(code, comment))) report.add_header(protocol.MessageIDHeader(chunk.message_id)) if chunk.byte_range is None: start = 1 total = chunk.size else: start, end, total = chunk.byte_range report.add_header(protocol.ByteRangeHeader(protocol.ByteRange(start, start+chunk.size-1, total))) return report else: return None
def make_response(chunk, code, comment): """Construct a response to a request as described in RFC4975 Section 7.2. If the response is not needed, return None. If a required header missing, raise ChunkParseError. """ if chunk.failure_report == 'no': return if chunk.failure_report == 'partial' and code == 200: return if chunk.to_path is None: raise ChunkParseError('missing To-Path header: %r' % chunk) if chunk.from_path is None: raise ChunkParseError('missing From-Path header: %r' % chunk) if chunk.method == 'SEND': to_path = [chunk.from_path[0]] else: to_path = chunk.from_path from_path = [chunk.to_path[0]] response = protocol.MSRPData(chunk.transaction_id, code=code, comment=comment) response.add_header(protocol.ToPathHeader(to_path)) response.add_header(protocol.FromPathHeader(from_path)) return response
def _relay_connect(self): try: msrp = self._connect(self.local_uri, self.relay) except Exception: self.logger.info('Could not connect to relay %s', self.relay) raise local_address = msrp.getHost() remote_address = msrp.getPeer() try: self.local_uri.port = local_address.port msrpdata = protocol.MSRPData(method="AUTH", transaction_id='%x' % random.getrandbits(64)) msrpdata.add_header(protocol.ToPathHeader([self.relay.uri_domain])) msrpdata.add_header(protocol.FromPathHeader([self.local_uri])) response = _deliver_chunk(msrp, msrpdata) if response.code == 401: www_authenticate = response.headers["WWW-Authenticate"] auth, rsp_auth = process_www_authenticate( self.relay.username, self.relay.password, "AUTH", str(self.relay.uri_domain), **www_authenticate.decoded) msrpdata.transaction_id = '%x' % random.getrandbits(64) msrpdata.add_header(protocol.AuthorizationHeader(auth)) response = _deliver_chunk(msrp, msrpdata) if response.code != 200: raise MSRPRelayAuthError(comment=response.comment, code=response.code) msrp.set_local_path(list(response.headers["Use-Path"].decoded)) self.logger.info('Reserved session at relay %s:%s', remote_address.host, remote_address.port) self.remote_endpoint = "%s:%s:%s" % (self.local_uri.scheme, remote_address.host, remote_address.port) except: self.logger.info('Could not reserve session at relay %s:%s', remote_address.host, remote_address.port) msrp.loseConnection(wait=False) raise return msrp
def make_report(chunk, code, comment): if chunk.success_report == 'yes' or ( chunk.failure_report in ('yes', 'partial') and code != 200): report = protocol.MSRPData(transaction_id='%x' % random.getrandbits(64), method='REPORT') report.add_header( protocol.ToPathHeader(chunk.headers['From-Path'].decoded)) report.add_header( protocol.FromPathHeader([chunk.headers['To-Path'].decoded[0]])) report.add_header(protocol.StatusHeader('000 %d %s' % (code, comment))) report.add_header(protocol.MessageIDHeader(chunk.message_id)) byterange = chunk.headers.get('Byte-Range') if byterange is None: start = 1 total = chunk.size else: start, end, total = byterange.decoded report.add_header( protocol.ByteRangeHeader((start, start + chunk.size - 1, total))) return report else: return None