/usr/share/pyshared/pyweblib/helper.py is in python-weblib 1.3.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | """
pyweblib.helper - Misc. stuff useful in CGI-BINs
(c) by Michael Stroeder <michael@stroeder.com>
This module is distributed under the terms of the
GPL (GNU GENERAL PUBLIC LICENSE) Version 2
(see http://www.gnu.org/copyleft/gpl.html)
$Id: helper.py,v 1.15 2011/01/16 09:04:10 michael Exp $
"""
__version__ = '0.3.1'
import os,re,UserDict
known_browsers = {
'MSIE':'Microsoft Internet Explorer',
'Mozilla':'Netscape Navigator',
'Lynx':'Lynx',
'Opera':'Opera',
'StarOffice':'StarOffice',
'NCSA_Mosaic':'NCSA Mosaic',
'NetPositive':'Net Positive',
'Mozilla':'Firefox',
'Mozilla':'Seamonkey',
}
known_browsers_rev = {}
for b in known_browsers.keys():
known_browsers_rev[known_browsers[b]]=b
compatible_browsers = known_browsers.keys()
compatible_browsers.remove('Mozilla')
compatible_browsers_re = re.compile('(%s)[/ ]+([0-9.]*)' % '|'.join(compatible_browsers))
mozilla_re = re.compile('(Mozilla)[/ ]+([0-9.]*)')
def BrowserType(http_user_agent):
"""
Parse the HTTP_USER_AGENT environment variable and return the
tuple (Browser,Version).
Not sure if this succeeds in every situation since most
browsers have very obscure HTTP_USER_AGENT entries for compability reasons.
The following browsers are known by name:
Netscape Netscape Navigator, Netscape Communicator)
MSIE MS Internet Explorer
Opera Opera browser from http://www.operasoftware.com/
StarOffice built-in browser of Star Office
Lynx the text-based browser Lynx
NetPositive Net Positive (BeOS)
"""
if not http_user_agent:
return ('','')
else:
browserrm = compatible_browsers_re.search(http_user_agent)
if browserrm:
return browserrm.groups()
else:
browserrm = mozilla_re.search(http_user_agent)
if browserrm:
return browserrm.groups()
else:
return ('','')
def guessClientAddr(env=None):
"""
Guesses the host name or IP address of the HTTP client by looking
at various HTTP headers mapped to CGI-BIN environment.
env
dictionary containing environment vars (default os.env)
"""
env = env or os.environ
return env.get('FORWARDED_FOR',
env.get('HTTP_X_FORWARDED_FOR',
env.get('REMOTE_HOST',
env.get('REMOTE_ADDR',None))))
class AcceptHeaderDict(UserDict.UserDict):
"""
This dictionary class is used to parse
Accept-header lines with quality weights.
It's a base class for all Accept-* headers described
in sections 14.1 to 14.5 of RFC2616.
"""
def __init__(self,envKey,env=None,defaultValue=None):
"""
Parse the Accept-* header line.
httpHeader
string with value of Accept-* header line
"""
env = env or os.environ
UserDict.UserDict.__init__(self)
self.defaultValue = defaultValue
self.preferred_value = []
try:
http_accept_value = [
s
for s in env[envKey].strip().split(',')
if len(s)
]
except KeyError:
self.data = {'*':1.0}
else:
if not http_accept_value:
self.data = {'*':1.0}
else:
self.data = {}
for i in http_accept_value:
try:
c,w=i.split(';')
except ValueError:
c,w = i,''
# Normalize charset name
c=c.strip().lower()
try:
q,qvalue_str=w.split('=',1)
qvalue = float(qvalue_str)
except ValueError:
qvalue = 1.0
# Add to capability dictionary
if c:
self.data[c] = qvalue
return # AcceptHeaderDict.__init__()
def __getitem__(self,value):
"""
value
String representing the value for which to return
the floating point capability weight.
"""
return self.data.get(
value.lower(),
self.data.get('*',0)
)
def items(self):
"""
Return the accepted values as tuples (value,weigth)
in descending order of capability weight
"""
l = self.data.items()
l.sort(lambda x,y:cmp(y[1],x[1]))
return l
def keys(self):
"""
Return the accepted values in descending order of capability weight
"""
l = self.items()
return [ k for k,v in l ]
class AcceptCharsetDict(AcceptHeaderDict):
"""
Special class for Accept-Charset header
"""
def __init__(self,envKey='HTTP_ACCEPT_CHARSET',env=None,defaultValue='utf-8'):
AcceptHeaderDict.__init__(self,envKey,env,defaultValue)
# Special treating of ISO-8859-1 charset to be compliant to RFC2616
self.data['iso-8859-1'] = self.data.get('iso-8859-1',self.data.get('*',1.0))
return # AcceptCharsetDict.__init__()
def preferred(self):
"""
Return the value name with highest capability weigth
"""
l = self.items()
while l and l[0][0]!='*':
try:
u''.encode(l[0][0])
except LookupError:
l.pop(0)
else:
break
if l:
if self.defaultValue and l[0][0]=='*':
return self.defaultValue
else:
return l[0][0]
else:
return self.defaultValue
|