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