Package flumotion :: Package component :: Package misc :: Package httpserver :: Package httpcached :: Module http_utils
[hide private]

Source Code for Module flumotion.component.misc.httpserver.httpcached.http_utils

  1  # -*- Mode: Python; test-case-name: flumotion.test.test_component_providers -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3   
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007,2008,2009 Fluendo, S.L. 
  6  # Copyright (C) 2010,2011 Flumotion Services, S.A. 
  7  # All rights reserved. 
  8  # 
  9  # This file may be distributed and/or modified under the terms of 
 10  # the GNU Lesser General Public License version 2.1 as published by 
 11  # the Free Software Foundation. 
 12  # This file is distributed without any warranty; without even the implied 
 13  # warranty of merchantability or fitness for a particular purpose. 
 14  # See "LICENSE.LGPL" in the source distribution for more information. 
 15  # 
 16  # Headers in this file shall remain intact. 
 17   
 18  import urlparse 
 19  import urllib 
 20   
 21  from twisted.web import http 
 22   
 23  DEFAULT_PORTS = {'http': 80, 
 24                   'https': 443} 
 25   
 26  DEFAULT_SCHEME = 'http' 
27 28 29 -def unparse_qs(query):
30 result = [] 31 for name, values in query.items(): 32 qname = urllib.quote(name) 33 for value in values: 34 result.append(qname + "=" + urllib.quote(value)) 35 return "&".join(result)
36
37 38 -class _Dummy(object):
39 pass
40
41 42 -class Url(object):
43 """ 44 Represents an HTTP URL. 45 Can parse and can be serialized to string. 46 """ 47 48 @classmethod
49 - def fromString(cls, url):
50 url = url.strip() 51 parsed = urlparse.urlparse(url) 52 53 scheme = parsed[0] 54 path = parsed[2] 55 56 location = urlparse.urlunparse(('', '')+parsed[2:]) 57 58 if path == "": 59 path = "/" 60 location = "/" + location 61 62 hostname = parsed[1] 63 username = None 64 password = None 65 port = None 66 67 if '@' in hostname: 68 username, hostname = hostname.split('@', 1) 69 if ':' in username: 70 username, password = username.split(':', 1) 71 72 host = hostname 73 74 if ':' in hostname: 75 hostname, portstr = hostname.rsplit(':', 1) 76 port = int(portstr) 77 else: 78 port = DEFAULT_PORTS.get(scheme, None) 79 80 81 obj = _Dummy() 82 83 obj.url = url 84 obj.scheme = scheme 85 obj.netloc = parsed[1] 86 obj.host = host 87 obj.path = path 88 obj.params = parsed[3] 89 obj.query = http.parse_qs(parsed[4], 1) 90 obj.fragment = parsed[5] 91 obj.location = location 92 obj.hostname = hostname 93 obj.username = username 94 obj.password = password 95 obj.port = port 96 97 obj.__class__ = cls 98 99 return obj
100
101 - def __init__(self, scheme=None, hostname=None, path="/", 102 params="", query={}, fragment="", 103 username=None, password=None, port=None):
104 105 self.path = path 106 self.params = params 107 self.query = query 108 self.fragment = fragment 109 110 if hostname: 111 # Absolute URL 112 if username: 113 if password: 114 netloc = username + ':' + password + '@' + hostname 115 else: 116 netloc = username + '@' + hostname 117 else: 118 netloc = hostname 119 120 if not scheme: 121 scheme = DEFAULT_SCHEME 122 123 host = hostname 124 125 defport = DEFAULT_PORTS.get(scheme, None) 126 127 if port: 128 if port != defport: 129 netloc = netloc + ':' + str(port) 130 host = host + ':' + str(port) 131 else: 132 port = defport 133 134 self.scheme = scheme 135 self.netloc = netloc 136 self.host = host 137 self.hostname = hostname 138 self.username = username 139 self.password = password 140 self.port = port 141 142 else: 143 # Relative URL 144 self.scheme = "" 145 self.netloc = "" 146 self.host = "" 147 self.hostname = "" 148 self.username = None 149 self.password = None 150 self.port = None 151 152 query_string = unparse_qs(self.query) 153 quoted_path = urllib.quote(self.path) 154 155 self.location = urlparse.urlunparse(('', '', quoted_path, self.params, 156 query_string, self.fragment)) 157 158 self.url = urlparse.urlunparse((self.scheme, self.netloc, quoted_path, 159 self.params, query_string, 160 self.fragment))
161
162 - def toString(self):
163 return self.url
164
165 - def __repr__(self):
166 return self.url
167 168 if __name__ == "__main__": 169 import sys 170 171 url = Url.fromString(sys.argv[1]) 172 for a, v in url.__dict__.items(): 173 print a, ":", v 174