1
2
3 """
4 @copyright:
5 Copyright (c) 2014 by mTLD Top Level Domain Limited. All rights reserved.\n
6 Portions copyright (c) 2008 by Argo Interactive Limited.\n
7 Portions copyright (c) 2008 by Nokia Inc.\n
8 Portions copyright (c) 2008 by Telecom Italia Mobile S.p.A.\n
9 Portions copyright (c) 2008 by Volantis Systems Limited.\n
10 Portions copyright (c) 2002-2008 by Andreas Staeding.\n
11 Portions copyright (c) 2008 by Zandan.\n
12 @author: dotMobi
13 """
14
15 from mobi.mtld.da.device.client_props_rule_set import ClientPropsRuleSet
16 from mobi.mtld.da.device.post_walk_rules import PostWalkRules
17 from mobi.mtld.da.property import Property
18 from mobi.mtld.da.exception.client_properties_exception import ClientPropertiesException
19
21
22
23 KEY_CP_RULES = 'cpr'
24 KEY_USER_AGENT = 'ua'
25
28
80
81
83 """
84 Parse the client side properties and if typed values is set convert the
85 values to the appropriate type.
86
87 The first character of the property name is the type of the value.
88
89 @param client_side_properties: Cookie content with the form:
90 bjs.webGl:1|sdeviceAspectRatio:16/10|iusableDisplayHeight:1050
91 """
92
93
94
95 html_escape_characters = {'&': '&', '"': '"', '<': '<',
96 '>': '>'}
97
98 props = {}
99 client_side_properties = client_side_properties.strip()
100
101 try:
102
103 name = ''
104 value = ''
105 is_key = True
106 is_type = True
107 key_ok = True
108 type = 0
109
110 num_chars = len(client_side_properties) - 1
111
112
113 max_i = num_chars + 1
114 for i in range(0,max_i):
115
116 c = client_side_properties[i]
117
118 if c == '|':
119
120
121 if key_ok:
122 type = name[0]
123 props[name] = value
124 self.tree_provider.properties[name[1:]] = Property(value, type)
125
126
127 name = ''
128 value = ''
129 is_key = True
130 key_ok = True
131 is_type = True
132
133 continue
134
135 elif c == ':':
136
137 is_key = False
138 continue
139
140 elif c == '"':
141
142 if i < num_chars:
143 next_c = client_side_properties[i+1]
144 else:
145 next_c = 0
146
147 if (i == 0 or i == num_chars or is_key or len(value) == 0 or
148 next_c == '|' or next_c == '"'):
149 continue
150
151
152
153 if is_key:
154
155 if is_type:
156 if c != 'b' and c != 'i' and c != 's' and c != 'd':
157 key_ok = False
158 is_type = False
159 else:
160 c_ord = ord(c)
161 if ((c_ord < 48 and c_ord != 46) or c_ord > 122 or
162 (c_ord > 57 and c_ord < 65) or (c_ord > 90 and c_ord < 97)):
163 key_ok = False
164 name += c
165 else:
166 if c in html_escape_characters:
167 value += html_escape_characters[c]
168 else:
169 value += c
170
171
172
173 if key_ok:
174 type = name[0]
175 props[name] = value
176 self.tree_provider.properties[name[1:]] = Property(value, type)
177
178 except Exception as e:
179 raise ClientPropertiesException(
180 "Could not decode client properties: %s" % e.message)
181
182 return props
183
184
185
187 """
188 Find all the properties that are used for matching.
189
190 @param group: The rule group that can contain a property matcher.
191 @param prop_ids: The set of found property IDs.
192 It returns an updated set of property IDs.
193 """
194 if group[self.KEY_PROPERTY_MATCHER]:
195 property_matchers = group[self.KEY_PROPERTY_MATCHER]
196 for property_matcher in property_matchers:
197 prop_id = property_matcher[self.KEY_PROPERTY_MATCHER]
198 if prop_id not in prop_ids:
199 prop_ids.append(prop_id)
200 return prop_ids
201
203 """
204 Prepare the rule set by extracting it from the current group and wrapping
205 it in a list.
206
207 @param group: The current parent group.
208 It returns a list of all rule sets.
209 """
210
211 return [{ self.KEY_RULE_ARR: group[self.KEY_RULE_ARR] }]
212
213
214
234
266
267 - def __compare_values(self, detected_value, expected_value, operator,
268 type_prop_name):
269 result = False
270 operator_equals = '='
271 operator_not_equals = '!='
272 operator_less_than = '<'
273 operator_less_than_equals = '<='
274 operator_greater_than = '>'
275 operator_greater_than_equals = '>='
276
277 t = type_prop_name[0]
278
279 if t in ('s', 'b'):
280 if operator == operator_equals:
281 result = (detected_value == expected_value)
282 elif operator == operator_not_equals:
283 result = (not detected_value == expected_value)
284 elif t == 'i':
285 d_val = int(detectedValue)
286 e_val = int(expectedValue)
287
288 if d_val == e_val and (operator in(operator_equals,
289 operator_less_than_equals, operator_greater_than_equals)):
290 result = True
291 elif d_val > e_val and (operator in(operator_greater_than,
292 operator_greater_than_equals)):
293 result = True
294 elif d_val < e_val and (operator in(operator_less_than,
295 operator_less_than_equals)):
296 result = True
297 elif d_val != e_val and operator == operator_not_equals:
298 result = True
299
300 return result
301