def test_simple_rule_matching(self): # Test simple matching matcher = progressive.from_rules("b501-1024~e404") self.assertIsNotNone(matcher) action = matcher.get_action(0, 500) self.assertIsNone(action, "Byte range before the rule start") action = matcher.get_action(600, 1000) self.assertEqual("e404", action, "Byte range in the middle of the rule start and end") action = matcher.get_action(1050, 2000) self.assertIsNone(action, "Byte range after the rule end") action = matcher.get_action(200, 600) self.assertEqual("e404", action, "Byte range contains the rule start") action = matcher.get_action(800, 1400) self.assertEqual("e404", action, "Byte range contains the rule end") action = matcher.get_action(200, 1400) self.assertEqual("e404", action, "Byte range encloses the entire rule") action = matcher.get_action(501, 1024) self.assertEqual("e404", action, "Byte range exactly the same as rule")
def test_rule_validation(self): self.assertIsNotNone(progressive.from_rules("b1-2~e404")) self.assertIsNotNone(progressive.from_rules("b1-*~e404")) self.assertIsNotNone(progressive.from_rules("b1-1000~net20")) self.assertIsNotNone(progressive.from_rules("b1-1000~net20,b1100-*~e404")) self.assertRaises(ValueError, progressive.from_rules, "foo") self.assertRaises(ValueError, progressive.from_rules, ",") self.assertRaises(ValueError, progressive.from_rules, "~") self.assertRaises(ValueError, progressive.from_rules, "~e404") self.assertRaises(ValueError, progressive.from_rules, "b1-2~") self.assertRaises(ValueError, progressive.from_rules, "b1-2~e404,") self.assertRaises(ValueError, progressive.from_rules, "b1-2~e404,~") self.assertRaises(ValueError, progressive.from_rules, "b1-2~e404,~") self.assertRaises(ValueError, progressive.from_rules, "1-2~e404") self.assertRaises(ValueError, progressive.from_rules, "b1-~e404") self.assertRaises(ValueError, progressive.from_rules, "b-1000~e404") self.assertRaises(ValueError, progressive.from_rules, "b1-1000~e404,b999-2000~e404") self.assertRaises(ValueError, progressive.from_rules, "b1000-2000~e404,b0-1000~e404")
def test_rule_collision_matching(self): matcher = progressive.from_rules("b500-1000~e404,b2000-2500~net20") self.assertIsNotNone(matcher) action = matcher.get_action(0,501) self.assertEqual("e404", action, "Only matches the first") action = matcher.get_action(1500, 2100) self.assertEqual("net20", action, "Only matches the second") action = matcher.get_action(500, 2100) self.assertEqual("e404", action, "Matches more of the first than the second") action = matcher.get_action(900, 2500) self.assertEqual("net20", action, "Matches more of the second than the first") action = matcher.get_action(500, 2500) self.assertEqual("e404", action, "Matches equally, so we choose the earliest match")
def _handle_progressive_rules(self, url, rules, request, mock_shape_segment=True): stream_url = url matcher = progressive.from_rules(rules) if 'Range' in request.headers: ranges = httputil.get_ranges(request.headers.get('Range'), sys.maxint) if ranges: # can return multiple ranges # assume 1 for now action = matcher.get_action(ranges[0][0], ranges[0][1] -1) # make range inclusive else: # this is fetching an entire file, so this should be the same as matching 0-* action = matcher.get_action(0, sys.maxint) if action: if action.startswith("e"): self.ostatus(action[1:]) else: (traffic_limit, traffic_loss, cache) = shaper.parse_net_rule_action(action) port = shaper.get_shape_port_for(traffic_limit, traffic_loss, {}, mock_shape_segment) stream_url=conf.common.get_final_url("/s/{0}/progressive?url={1}&from_dripls=1".format(port, urllib.quote_plus(url)),"") return stream_url
def parse_prog_rules(rule_string): return progressive.from_rules(rule_string)