2 HCE project, Python bindings, Distributed Tasks Manager application. 3 HTTPCookieResolver Class content main functional for collect and resolve cookies 6 @file HTTPCookieResolver.py 7 @author Alexander Vybornyh <alexander.hce.cluster@gmail.com> 8 @link: http://hierarchical-cluster-engine.com/ 9 @copyright: Copyright © 2013-2016 IOIX Ukraine 10 @license: http://hierarchical-cluster-engine.com/license/ 32 STAGE_DEFAULT = STAGE_REGULAR | STAGE_REDIRECT | STAGE_ROBOTS | STAGE_RSS
36 ERROR_BAD_TYPE_INPUT_PROPERTY =
"Bad type (%s) of input property: %s" 37 ERROR_BAD_TYPE_FOUND_PROPERTY =
"Bad type (%s) of found property: %s" 38 ERROR_INPUT_PROPERTY =
"Input wrong properties: %s" 39 ERROR_INITIALIZATION =
"Initialization class '%s' was failed. Error: '%s'" 43 supportNames = [
'expires',
'domain',
'path']
58 if isinstance(self.
name, basestring)
and isinstance(self.
value, basestring)
and \
60 ret = str(self.
name) +
'=' + str(self.
value) +
'; ' 78 if propertyString
is not None and isinstance(propertyString, basestring):
90 ret = json.loads(propertyString)
91 except Exception, err:
108 localUrl = self.
__stripUrl(url)
if strip
else url
111 if isinstance(cookie, basestring):
114 elif isinstance(cookie, dict):
115 self.
cookiesDict[localUrl] =
';'.
join([
'%s=%s' % (k, v)
for k, v
in cookie.iteritems()])
125 if isinstance(url, basestring)
and url.count(
'?') > 0:
126 url = url[:url.find(
'?')]
138 pairElem = element.split(
'=')
139 if len(pairElem) > 0:
142 if len(pairElem) > 1:
156 elementsList = cookieString.split(
';')
158 for element
in elementsList:
159 if element.count(
'=') > 1:
160 endPos = element.rfind(
'=')
161 begPos = element.rfind(
' ', 0, endPos)
163 first = element[:begPos]
164 second = element[begPos:]
165 first = first.strip(
',')
167 elements.append(first.strip())
168 elements.append(second.strip())
170 elements.append(element.strip())
174 for element
in elements:
178 if name.lower()
not in HTTPCookieResolver.Cookie.supportNames
and \
179 cookieObj.name
is not None and cookieObj.value
is not None:
180 cookies.append(cookieObj)
183 if name.lower()
in HTTPCookieResolver.Cookie.supportNames:
184 if hasattr(cookieObj, name.lower()):
185 setattr(cookieObj, name.lower(), value)
186 elif cookieObj.name
is None and cookieObj.value
is None:
187 cookieObj.name = name
188 cookieObj.value = value
190 if cookieObj.name
is not None and cookieObj.value
is not None:
191 cookies.append(cookieObj)
193 logger.debug(
"!!! cookies:")
194 for cookie
in cookies:
195 logger.debug(
"%s",
varDump(cookie))
209 isAllowedPath = isAllowedExpires =
True 212 if cookieObj.path
is not None and re.search(
'.*' + cookieObj.path +
'.*', url, re.UNICODE)
is None:
213 isAllowedPath =
False 216 if cookieObj.expires
is not None:
217 expiresDatetime = DateTimeType.parse(cookieObj.expires,
True, logger,
False)
218 if expiresDatetime
is not None:
219 expiresDatetime = DateTimeType.toUTC(expiresDatetime)
220 currentDatetime = datetime.datetime.utcnow()
221 currentDatetime = currentDatetime.replace(tzinfo=
None)
222 if currentDatetime > expiresDatetime:
223 isAllowedExpires =
False 225 logger.debug(
"Is allowed = %s for path '%s'", str(isAllowedPath), str(cookieObj.path))
226 logger.debug(
"Is allowed = %s for expired '%s'", str(isAllowedExpires), str(cookieObj.expires))
228 if isAllowedPath
and isAllowedExpires:
240 propertyStage = HTTPCookieResolver.STAGE_DEFAULT
241 propertyCookie =
None 246 if propertyObj
is not None:
247 if propertyObj.stage
is not None:
248 propertyStage = propertyObj.stage
249 propertyCookie = propertyObj.cookie
251 return propertyStage, propertyCookie
262 if stage & int(propertyStage):
277 if not isinstance(properties, dict):
281 if domain
in properties:
282 foundProperty = properties[domain]
288 if foundProperty
is not None:
289 if not isinstance(foundProperty, dict):
293 for name, value
in foundProperty.items():
294 if hasattr(domainPropertyObj, name):
295 setattr(domainPropertyObj, name, value)
296 ret = domainPropertyObj
298 except Exception, err:
299 logger.error(str(err))
313 logger.debug(
'!!! getCookie ENTER !!! url: ' + str(url))
316 logger.debug(
'!!! getCookie LEAVE !!! return: ' + str(
';'.
join(self.
cookiesDict[url].split())))
320 logger.debug(
"!!! localUrl = %s, cookieString: %s", str(localUrl), str(cookieString))
323 logger.debug(
"!!! propertyStage = %s, propertyCookie: %s", str(propertyStage),
varDump(propertyCookie))
324 logger.debug(
'is allowed stage: ' + str(self.
__isAllowedStage(stage, propertyStage)))
327 if propertyCookie
is None:
328 logger.debug(
'cookieString: ' + str(cookieString))
331 logger.debug(
'cookies: ' +
varDump(cookies))
332 logger.debug(
'localUrl: ' + str(localUrl))
333 logger.debug(
'url: ' + str(url))
337 for cookie
in cookies:
338 if cookie.domain
is None or (cookie.domain
is not None and \
339 re.search(
'.*' + cookie.domain +
'.*', url, re.UNICODE)
is not None):
343 resList.append(cookie.getData())
346 resList = list(set(resList))
347 resStr =
''.
join(resList)
355 logger.debug(
'return cookie: ' + str(ret))
def __splitCookieString(self, cookieString)
def getCookie(self, url, stage=STAGE_DEFAULT)
def __isAllowedCookie(self, url, cookieObj)
def __extractDomainProperty(self, url)
def __loadProperty(self, propertyString)
string ERROR_BAD_TYPE_INPUT_PROPERTY
string ERROR_INITIALIZATION
string ERROR_BAD_TYPE_FOUND_PROPERTY
def __getDomainProperty(self, properties, domain)
def __init__(self, propertyString=None)
def addCookie(self, url, cookie, strip=True)
def __extractPair(self, element)
def varDump(obj, stringify=True, strTypeMaxLen=256, strTypeCutSuffix='...', stringifyType=1, ignoreErrors=False, objectsHash=None, depth=0, indent=2, ensure_ascii=False, maxDepth=10)
string ERROR_INPUT_PROPERTY
def __isAllowedStage(self, stage, propertyStage)
def __stripUrl(self, url)
def __init__(self, stage=None, cookie=None)