Changeset 201 for trunk/examples
- Timestamp:
- 09/06/08 10:47:31 (18 months ago)
- Location:
- trunk/examples/divan/divan
- Files:
-
- 1 added
- 2 modified
-
comments.py (modified) (4 diffs)
-
tests/__init__.py (modified) (1 diff)
-
tests/comments.py (added)
Legend:
- Unmodified
- Added
- Removed
-
trunk/examples/divan/divan/comments.py
r200 r201 98 98 'Host': get_hostname(comment.openid_server) 99 99 }) 100 result = parse_kv_encoding(body.strip())100 result = decode_kvform(body.strip()) 101 101 if result['is_valid'] == 'true': 102 102 log.info('OpenID authentication for %r validated by %r', … … 201 201 202 202 def get_hostname(url): 203 """Extract the host name from the given URL, which does not include the 204 port for HTTP and HTTPS URLs using the respective default port. 205 206 >>> get_hostname('http://me.yahoo.com/') 207 'me.yahoo.com' 208 >>> get_hostname('http://me.yahoo.com:80/') 209 'me.yahoo.com' 210 >>> get_hostname('https://me.yahoo.com:443/') 211 'me.yahoo.com' 212 213 :param url: the URL from which to extract the host name 214 :return: the extracted host name 215 """ 203 216 scheme, netloc, path, query, fragment = list(urlsplit(url)) 204 217 if ':' in netloc: 205 218 hostname, port = netloc.split(':') 206 if port == '80' and scheme == 'http s':219 if port == '80' and scheme == 'http': 207 220 port = None 208 221 elif port == '443' and scheme == 'https': … … 213 226 214 227 def normalize_openid(url): 228 """Normalize the given OpenID URL. 229 230 >>> normalize_openid('me.yahoo.com') 231 'http://me.yahoo.com/' 232 >>> normalize_openid('https://me.yahoo.com') 233 'https://me.yahoo.com/' 234 >>> normalize_openid('https://me.yahoo.com/joe') 235 'https://me.yahoo.com/joe' 236 237 :param url: the URL to normalize 238 :return: the normalized URL 239 """ 215 240 if '://' not in url: 216 241 url = 'http://%s' % url … … 221 246 222 247 223 def parse_kv_encoding(string): 224 lines = string.splitlines() 248 def decode_kvform(string): 249 """Parse a key-value form encoded string as defined in the OpenID 250 specification, and return a dictionary mapping values to keys. 251 252 >>> encoded = 'ns:http://specs.openid.net/auth/2.0\\nis_valid:true' 253 >>> parsed = decode_kvform(encoded) 254 >>> parsed['ns'] 255 u'http://specs.openid.net/auth/2.0' 256 >>> parsed['is_valid'] 257 u'true' 258 259 :param string: the key-value form encoded string 260 :return: dictionary mapping values to keys 261 """ 262 if not isinstance(string, unicode): 263 string = string.decode('utf-8') 264 string = string.strip() 225 265 retval = {} 226 for line in lines:266 for line in string.split('\n'): 227 267 key, value = line.split(':', 1) 228 retval[key ] = value268 retval[key.strip()] = value.strip() 229 269 return retval 230 270 -
trunk/examples/divan/divan/tests/__init__.py
r135 r201 4 4 5 5 def suite(): 6 from divan.tests import model, publish6 from divan.tests import comments, model, publish 7 7 8 8 suite = unittest.TestSuite() 9 suite.addTest(comments.suite()) 9 10 suite.addTest(model.suite()) 10 11 suite.addTest(publish.suite())
