Exemplo n.º 1
0
 def __init__(self,
              http_request,
              client_address,
              server,
              serve_path=os.getcwd()):
     self.serve_path = serve_path
     SimpleHTTPRequestHandler.__init__(self, http_request, client_address,
                                       server)
Exemplo n.º 2
0
 def do_GET(self):
     if not self._try_handler("GET"):
         res = self._find_path()
         if res:
             self.path, self.disk_root = res
             # don't include query string and fragment, and prepend
             # host directory if required.
             if self.request.netloc and self.proxy_host_dirs:
                 self.path = "/" + self.request.netloc + self.path
             SimpleHTTPRequestHandler.do_GET(self)
         else:
             self.send_response(404)
             self.end_headers()
             self.wfile.write(b"")
Exemplo n.º 3
0
    def do_GET(self, *args, **kwargs):

        # 1. Redirect all requests to `/MAGIC_REDIRECT/`.
        if self.path.startswith('/MAGIC_REDIRECT/'):
            parsed_path = urlparse(self.path)
            qs = parse_qs(parsed_path.query)
            if 'dst' not in qs:
                self.send_error(
                    404,
                    "Requests to the path `/MAGIC_REDIRECT/` must specify "
                    "a destination to redirect to via a `dst` query parameter."
                )
                return
            dst = qs['dst'][0]
            new_qs = list()
            if len(qs['dst']) > 1:
                new_qs.append('dst=' + '&dst='.join(qs['dst'][1:]))
            for key in qs.keys():
                if key == 'dst':
                    continue
                temp = '%s=' + '&%s='.join(qs[key])
                new_qs.append(temp % key)
            if len(new_qs) > 0:
                new_url = "%s?%s" % (dst, '&'.join(new_qs))
            else:
                new_url = dst
            self.send_response(301)
            self.send_header("Location", new_url)
            self.end_headers()
            return

        # Otherwise, return file from disk
        return SimpleHTTPRequestHandler.do_GET(self, *args, **kwargs)
Exemplo n.º 4
0
 def serve_file(self):
     """
     If a file exists corresponding to the request's path, serve it.
     Otherwise, raise IOError.
     """
     path = self.translate_path(self.path)
     try:
         open(path, 'rb')
     except IOError:
         raise
     else:
         return SimpleHTTPRequestHandler.do_GET(self)
Exemplo n.º 5
0
 def serve_file(self):
     """
     If a file exists corresponding to the request's path, serve it.
     Otherwise, raise IOError.
     """
     path = self.translate_path(self.path)
     try:
         open(path, 'rb')
     except IOError:
         raise
     else:
         return SimpleHTTPRequestHandler.do_GET(self)
Exemplo n.º 6
0
    def do_GET(self):  # pylint: disable=invalid-name
        """
        Check parameters to see if a delay was specified.
        If so then wait and then serve the GET request.
        """
        # Parse the url into components
        parsed_url = urlparse(self.path)

        # Determine if delay was passed as a parameter
        delay_time = parse_qs(parsed_url.query).get('delay')

        if delay_time:
            # Values are passed as a list of strings
            # so keep the first value and convert to a float.
            sleep(float(delay_time[0]))

        # Prepend "tests/site" to the path because that
        # is where the test files should be served from.
        self.path = "tests/site{}".format(self.path)

        return SimpleHTTPRequestHandler.do_GET(self)
Exemplo n.º 7
0
 def __init__(self, *args, **kwargs):
     SimpleHTTPRequestHandler.__init__(self, *args, **kwargs)
     self.path = None
Exemplo n.º 8
0
 def setup(self):
     """Set up request handler."""
     SimpleHTTPRequestHandler.setup(self)
     self.request.settimeout(1)
Exemplo n.º 9
0
 def __init__(self, *args, **kwargs):
     SimpleHTTPRequestHandler.__init__(self, *args, **kwargs)
     self.extensions_map['.svg'] = 'image/svg+xml'
Exemplo n.º 10
0
 def parse_request(self):
     retval = SimpleHTTPRequestHandler.parse_request(self)
     self.request = Request(self.path, self.headers, self.rfile)
     return retval
Exemplo n.º 11
0
 def __init__(self, *args, **kwargs):
     self._silent = lgr.getEffectiveLevel() > logging.DEBUG
     SimpleHTTPRequestHandler.__init__(self, *args, **kwargs)
Exemplo n.º 12
0
 def translate_path(self, path):
     path = SimpleHTTPRequestHandler.translate_path(self, path)
     relpath_ = relpath(path, os.getcwd())
     abspath_ = join(self.serve_path, relpath_)
     return abspath_
Exemplo n.º 13
0
 def end_headers(self):
     """
     This is required by hls.js to play hls videos.
     """
     self.send_header('Access-Control-Allow-Origin', '*')
     SimpleHTTPRequestHandler.end_headers(self)
Exemplo n.º 14
0
        def translate_path(self, path):
            t_path = SimpleHTTPRequestHandler.translate_path(self, path)
            short_t_path = t_path[len(os.getcwd()):].lstrip('/')

            return os.path.join(self.__root_dir, short_t_path)
Exemplo n.º 15
0
 def end_headers(self):
     self.send_my_headers()
     SimpleHTTPRequestHandler.end_headers(self)
Exemplo n.º 16
0
 def __init__(self, *args, **kwargs):
     self._silent = lgr.getEffectiveLevel() > logging.DEBUG
     SimpleHTTPRequestHandler.__init__(self, *args, **kwargs)
Exemplo n.º 17
0
 def end_headers(self):
     """
     This is required by hls.js to play hls videos.
     """
     self.send_header('Access-Control-Allow-Origin', '*')
     SimpleHTTPRequestHandler.end_headers(self)