def handle(self, cid, extra): """ Dispatch a request from /containers/x/... to its proper destination. For example: /containers/x/files --> x is a project ID --> /projects/x/files """ results = container_search({'_id': cid}, projection={'_id': 1}) if not results: raise APINotFoundException('No container {} found'.format(cid)) # Create new request instance using destination URI (eg. replace containers with cont_name) cont_name, _ = results[0] destination_environ = self.request.environ for key in 'PATH_INFO', 'REQUEST_URI': if key in destination_environ: destination_environ[key] = destination_environ[key].replace('containers', cont_name, 1) destination_environ['fw_container_type'] = singularize(cont_name) destination_request = Request(destination_environ) # Apply SciTranRequest attrs destination_request.id = self.request.id destination_request.logger = self.request.logger # Dispatch the destination request self.app.router.dispatch(destination_request, self.response)
def lookup(self): """Locate a node by path, and re-route to the endpoint for that node""" result = self._resolve_and_check_permissions(True) # If we resolved a file, we can just return that file node path = result.get('path', []) if not path: raise APINotFoundException('No node matched that path') # In the event that we resolved a file, just return the file node dest = path[-1] if dest.get('container_type') == 'file': return dest # Reroute to the actual path that will log access, resolve analyses, etc path = self._get_node_path(dest) # Create new request instance using destination URI (eg. replace containers with cont_name) destination_environ = self.request.environ for key in 'PATH_INFO', 'REQUEST_URI': if key in destination_environ: destination_environ[key] = destination_environ[key].replace('lookup', path, 1) # We also must update the method, and indicate that we want the container_type included # The client will depend on container_type being set so that it can map to the correct type destination_environ['REQUEST_METHOD'] = 'GET' destination_environ['fw_container_type'] = dest['container_type'] destination_request = Request(destination_environ) # Apply SciTranRequest attrs destination_request.id = self.request.id destination_request.logger = self.request.logger # Dispatch the destination request self.app.router.dispatch(destination_request, self.response)
def test_userdata_add(self): remote_addr = '127.0.0.1' http_user_agent = 'foo agent' domain = 'foobar' referer = None class Request(object): pass request = Request() request.headers = {'HTTP_USER_AGENT': http_user_agent} request.referer = referer os.environ['REMOTE_ADDR'] = remote_addr UserData.add(request, domain) user_data = UserData.query(UserData.domain == domain).get() self.assertEqual(user_data.remote_addr, remote_addr) self.assertEqual(user_data.http_user_agent, http_user_agent) self.assertEqual(user_data.referer, referer)
def request_handler_wrapper(request, *args, **kwargs): from webapp2 import Request, Response, WSGIApplication from django.http import HttpResponse class Route: handler_method = request.method.lower() req = Request(request.environ) req.route = Route() req.route_args = args req.route_kwargs = kwargs req.app = WSGIApplication() response = Response() view_func = request_handler(req, response) view_func.dispatch() django_response = HttpResponse(response.body, status=int(str(response.status).split(" ")[0])) for header, value in response.headers.iteritems(): django_response[header] = value return django_response