Advertisement
Guest User

mirror.py

a guest
Jan 18th, 2014
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.20 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Copyright 2010 Paulo Jerônimo
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. #     http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17.  
  18. __author__='Paulo Jerônimo (paulojeronimo@gmail.com)'
  19.  
  20. # This code is a reduction / adaptation of mirrorrr project
  21. # (http://code.google.com/p/mirrorrr/ by Brett Slatkin)
  22. # held specifically to achieve the goals to build a proxy for files in an
  23. # account published in the DropBox.
  24. # If you want a full proxy to run on GAE, use the mirrorr.
  25.  
  26. # Set up your Dropbox user number here:
  27. DROPBOX_USER = '138965110/SITE'
  28.  
  29. DROPBOX_PREFIX ='/dl.dropbox.com/u/'
  30. DEBUG = False
  31. HTTP_PREFIX = "http://"
  32. IGNORE_HEADERS = frozenset([
  33.   'set-cookie',
  34.   'expires',
  35.   'cache-control',
  36.   # Ignore hop-by-hop headers
  37.   'connection',
  38.   'keep-alive',
  39.   'proxy-authenticate',
  40.   'proxy-authorization',
  41.   'te',
  42.   'trailers',
  43.   'transfer-encoding',
  44.   'upgrade',
  45. ])
  46.  
  47. import logging
  48. import wsgiref.handlers
  49.  
  50. from google.appengine.api import urlfetch
  51. from google.appengine.ext import webapp
  52. from google.appengine.runtime import apiproxy_errors
  53.  
  54. class MirroredContent(object):
  55.   def __init__(self, original_address, translated_address,
  56.                status, headers, data, base_url):
  57.     self.original_address = original_address
  58.     self.translated_address = translated_address
  59.     self.status = status
  60.     self.headers = headers
  61.     self.data = data
  62.     self.base_url = base_url
  63.  
  64.   @staticmethod
  65.   def fetch_and_store(base_url, translated_address, mirrored_url):
  66.     """Fetch a page.
  67.    
  68.    Args:
  69.      base_url: The hostname of the page that's being mirrored.
  70.      translated_address: The URL of the mirrored page on this site.
  71.      mirrored_url: The URL of the original page. Hostname should match
  72.        the base_url.
  73.    
  74.    Returns:
  75.      A new MirroredContent object, if the page was successfully retrieved.
  76.      None if any errors occurred or the content could not be retrieved.
  77.    """
  78.     logging.debug("Fetching '%s'", mirrored_url)
  79.     try:
  80.       response = urlfetch.fetch(mirrored_url)
  81.     except (urlfetch.Error, apiproxy_errors.Error):
  82.       logging.exception("Could not fetch URL")
  83.       return None
  84.  
  85.     adjusted_headers = {}
  86.     for key, value in response.headers.iteritems():
  87.       adjusted_key = key.lower()
  88.       if adjusted_key not in IGNORE_HEADERS:
  89.         adjusted_headers[adjusted_key] = value
  90.  
  91.     return MirroredContent(
  92.       base_url=base_url,
  93.       original_address=mirrored_url,
  94.       translated_address=translated_address,
  95.       status=response.status_code,
  96.       headers=adjusted_headers,
  97.       data=response.content)
  98.      
  99.  
  100. class MirrorHandler(webapp.RequestHandler):
  101.   def get_relative_url(self):
  102.     slash = self.request.url.find("/", len(self.request.scheme + "://"))
  103.     if slash == -1:
  104.       return "/"
  105.     return DROPBOX_PREFIX + DROPBOX_USER + self.request.url[slash:]
  106.  
  107.   def get(self, base_url):
  108.     assert base_url
  109.     logging.debug('User-Agent = "%s", Referrer = "%s"',
  110.                   self.request.user_agent,
  111.                   self.request.referer)
  112.     logging.debug('Base_url = "%s", url = "%s"', base_url, self.request.url)
  113.     translated_address = self.get_relative_url()[1:]  # remove leading /
  114.     content = MirroredContent.fetch_and_store(base_url, translated_address,
  115.       HTTP_PREFIX + translated_address)
  116.     if content is None:
  117.       return self.error(404)
  118.     for key, value in content.headers.iteritems():
  119.       self.response.headers[key] = value
  120.     self.response.out.write(content.data)
  121.  
  122.  
  123. app = webapp.WSGIApplication([
  124.   (r"/", MirrorHandler),
  125.   (r"/([^/]+).*", MirrorHandler)
  126. ], debug=DEBUG)
  127.  
  128.  
  129. def main():
  130.   wsgiref.handlers.CGIHandler().run(app)
  131.  
  132.  
  133. if __name__ == "__main__":
  134.   main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement