<s> import logging <EOL> import re <EOL> from urllib2 import urlopen <EOL> from urlparse import urlparse , urlunparse <EOL> from django . core import urlresolvers <EOL> from django . http import HttpResponse <EOL> from desktop . lib . exceptions import MessageException <EOL> from proxy import conf <EOL> LOGGER = logging . getLogger ( __name__ ) <EOL> def check_host_port ( host , port ) : <EOL> """<STR_LIT>""" <EOL> hostport = "<STR_LIT>" % ( host , port ) <EOL> for regexp in conf . WHITELIST . get ( ) : <EOL> if regexp . match ( hostport ) : <EOL> return True <EOL> return False <EOL> def check_blacklist ( host , port , path ) : <EOL> """<STR_LIT>""" <EOL> blacklist = conf . BLACKLIST . get ( ) <EOL> if not blacklist : <EOL> return True <EOL> has_trailing_slash = path . endswith ( '<STR_LIT:/>' ) <EOL> path_elems = path . split ( '<STR_LIT:/>' ) <EOL> path_elems = [ p for p in path_elems if p ] <EOL> canon_url = "<STR_LIT>" % ( host , port , '<STR_LIT:/>' . join ( path_elems ) ) <EOL> if has_trailing_slash : <EOL> canon_url += '<STR_LIT:/>' <EOL> for regexp in blacklist : <EOL> if regexp . match ( canon_url ) : <EOL> return False <EOL> return True <EOL> def proxy ( request , host , port , path ) : <EOL> """<STR_LIT>""" <EOL> port = int ( port ) <EOL> if not check_host_port ( host , port ) : <EOL> raise MessageException ( <EOL> ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) % ( host , port ) ) <EOL> if not check_blacklist ( host , port , path ) : <EOL> raise MessageException ( <EOL> "<STR_LIT>" % ( host , port , path ) ) <EOL> url = urlunparse ( ( "<STR_LIT:http>" , "<STR_LIT>" % ( host , port ) , <EOL> path , <EOL> None , <EOL> request . META . get ( "<STR_LIT>" ) , <EOL> None ) ) <EOL> LOGGER . info ( "<STR_LIT>" % url ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> post_data = request . POST . urlencode ( ) <EOL> else : <EOL> post_data = None <EOL> data = urlopen ( url , data = post_data ) <EOL> content_type = data . headers . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if not re . match ( r'<STR_LIT>' , content_type ) : <EOL> resp_text = data . read ( <NUM_LIT> * <NUM_LIT> ) <EOL> else : <EOL> resp_text = _rewrite_links ( data ) <EOL> request . path = _reverse ( host , port , path ) <EOL> return HttpResponse ( resp_text , content_type = data . headers . get ( "<STR_LIT>" ) ) <EOL> def _reverse ( host , port , path ) : <EOL> return urlresolvers . reverse ( "<STR_LIT>" , <EOL> kwargs = dict ( host = host , port = port , path = path ) ) <EOL> def _rewrite_url ( url ) : <EOL> """<STR_LIT>""" <EOL> scheme , netloc , path , params , query , fragment = urlparse ( url ) <EOL> if scheme != "<STR_LIT:http>" : <EOL> return url <EOL> if "<STR_LIT::>" in netloc : <EOL> host , port = netloc . rsplit ( "<STR_LIT::>" , <NUM_LIT:1> ) <EOL> else : <EOL> host , port = netloc , str ( <NUM_LIT> ) <EOL> path = path or "<STR_LIT:/>" <EOL> try : <EOL> out = _reverse ( host , port , path ) <EOL> except urlresolvers . NoReverseMatch , ex : <EOL> LOGGER . error ( "<STR_LIT>" % ( url , ) ) <EOL> return None <EOL> if query : <EOL> out = out + "<STR_LIT:?>" + query <EOL> return out <EOL> def _rewrite_links ( data ) : <EOL> import lxml . html <EOL> html = lxml . html . parse ( data ) <EOL> html . getroot ( ) . rewrite_links ( _rewrite_url , resolve_base_href = True , base_href = data . geturl ( ) ) <EOL> return lxml . html . tostring ( html ) </s>
