def get_handlers(self): handlers = [ (r'/healthcheck', HealthcheckHandler), ] if self.context.config.UPLOAD_ENABLED: # Handler to upload images (POST). handlers.append( (r'/image', ImageUploadHandler, {'context': self.context}) ) # Handler to retrieve or modify existing images (GET, PUT, DELETE) handlers.append( (r'/image/(.*)', ImageResourceHandler, {'context': self.context}) ) if self.context.config.USE_BLACKLIST: handlers.append( (r'/blacklist', BlacklistHandler, {'context': self.context}) ) # Imaging handler (GET) handlers.append( (Url.regex(), ImagingHandler, {'context': self.context}) ) return handlers
def path_to_parameters(cls, path): ''' :param path: url path :return: A dictionary of parameters to be used with ImagingHandler instances ''' if not cls._url_regex: cls._url_regex = re.compile(Url.regex()) if cls._url_regex.groups: match = cls._url_regex.match(path) # See https://github.com/tornadoweb/tornado/blob/01c78ebfcc993ff4f1d8336c2c45844fe9dab60e/tornado/web.py#L1951 # Pass matched groups to the handler. Since # match.groups() includes both named and # unnamed groups, we want to use either groups # or groupdict but not both. if cls._url_regex.groupindex: parameters = dict( (str(k), tornado.web._unquote_or_none(v)) for (k, v) in match.groupdict().items()) else: parameters = [ tornado.web._unquote_or_none(s) for s in match.groups() ] else: parameters = dict() return parameters
def post(self, **kw): paths = self.get_arguments('paths[]') if len(paths) > MultiHandler.paths_limit: self.set_status(400) super(MultiHandler, self).write( 'Too many paths: %d max' % MultiHandler.paths_limit ) super(MultiHandler, self).finish() return for path in paths: request = HTTPServerRequest( method='GET', uri=path, host=self.request.host, connection=self.request.connection ) handler = MultiHandler( self.application, request, context=self.context ) # Copy over the storage as-is, which allows those requests to # share storage if needed (useful for request-storage) handler.context.modules.storage = self.context.modules.storage m = re.match(Url.regex(), path) yield handler.check_image(m.groupdict()) # Close the request ASAP, the work is to be done async self.set_status(200) super(MultiHandler, self).finish()
def get_handlers(self): handlers = [ (self.context.config.HEALTHCHECK_ROUTE, HealthcheckHandler), ] if self.context.config.UPLOAD_ENABLED: # Handler to upload images (POST). handlers.append((r"/image", ImageUploadHandler, { "context": self.context })) # Handler to retrieve or modify existing images (GET, PUT, DELETE) handlers.append(( r"/image/(.*)", ImageResourceHandler, { "context": self.context }, )) if self.context.config.USE_BLACKLIST: handlers.append((r"/blacklist", BlacklistHandler, { "context": self.context })) # Imaging handler (GET) handlers.append((Url.regex(), ImagingHandler, { "context": self.context })) return handlers
def path_to_parameters(cls, path): ''' :param path: url path :return: A dictionary of parameters to be used with ImagingHandler instances ''' if not cls._url_regex: cls._url_regex = re.compile(Url.regex()) if cls._url_regex.groups: match = cls._url_regex.match(path) # See https://github.com/tornadoweb/tornado/blob/01c78ebfcc993ff4f1d8336c2c45844fe9dab60e/tornado/web.py#L1951 # Pass matched groups to the handler. Since # match.groups() includes both named and # unnamed groups, we want to use either groups # or groupdict but not both. if cls._url_regex.groupindex: parameters = dict((str(k), _unquote_or_none(v)) for (k, v) in match.groupdict().items()) else: parameters = [_unquote_or_none(s) for s in match.groups()] else: parameters = dict() return parameters
class Filter(BaseFilter): phase = PHASE_PRE_LOAD domain_regex = re.compile(r"^(https?://)?.*?/") url_regex = re.compile(Url.regex()) def parse_url(self, url): level = 0 while level < MAX_LEVEL: url = self.domain_regex.sub("", url) result = self.url_regex.match(url) if not result: return None parts = result.groupdict() image = parts.get("image", None) if not (image and (parts.get("hash", None) or parts.get("unsafe", None))): return None top, right, left, bottom = ( parts.get("crop_top", None), parts.get("crop_right", None), parts.get("crop_left", None), parts.get("crop_bottom", None), ) if top and right and left and bottom: return (image, top, right, left, bottom) url = image level += 1 return None @filter_method() async def extract_focal(self): parts = self.parse_url(self.context.request.image_url) if parts: image, top, right, left, bottom = parts top, right, left, bottom = ( int(top), int(right), int(left), int(bottom), ) width = right - left height = bottom - top self.context.request.focal_points.append( FocalPoint.from_square(left, top, width, height, origin="Original Extraction")) self.context.request.image_url = image
def test_can_get_handlers(self): ctx = self.get_context() ctx.config.UPLOAD_ENABLED = False ctx.config.USE_BLACKLIST = False ctx.config.HEALTHCHECK_ROUTE = "/health" app = ThumborServiceApp(ctx) handlers = app.get_handlers() expect(handlers).to_length(2) expect(handlers[0][0]).to_equal(r"/health") expect(handlers[1][0]).to_equal(Url.regex())
def test_can_get_handlers(self): ctx = mock.Mock(config=mock.Mock( UPLOAD_ENABLED=False, USE_BLACKLIST=False, )) app = ThumborServiceApp(ctx) handlers = app.get_handlers() expect(handlers).to_length(2) expect(handlers[0][0]).to_equal(r'/healthcheck') expect(handlers[1][0]).to_equal(Url.regex())
def get_handlers(self): handlers = [] for handler_list in self.context.modules.importer.handler_lists: get_handlers = getattr(handler_list, 'get_handlers', None) if get_handlers is None: continue handlers.extend(get_handlers(self.context)) # Imaging handler (GET) handlers.append((Url.regex(), ImagingHandler, {"context": self.context})) return handlers
def test_can_get_regex_without_unsafe(self): regex = Url.regex(False) expect(regex).to_equal( '/?(?:(?P<debug>debug)/)?(?:(?P<meta>meta)/)?' '(?:(?P<trim>trim(?::(?:top-left|bottom-right))?(?::\\d+)?)/)?' '(?:(?P<crop_left>\\d+)x(?P<crop_top>\\d+):(?P<crop_right>\\d+)x(?P<crop_bottom>\\d+)/)?' '(?:(?P<adaptive>adaptive-)?(?P<full>full-)?(?P<fit_in>fit-in)/)?(?:(?P<horizontal_flip>-)?' '(?P<width>(?:\\d+|orig))?x(?P<vertical_flip>-)?(?P<height>(?:\\d+|orig))?/)?' '(?:(?P<halign>left|right|center)/)?(?:(?P<valign>top|bottom|middle)/)?' '(?:(?P<smart>smart)/)?(?:filters:(?P<filters>.+?\\))/)?(?P<image>.+)' )
def test_can_get_regex_without_unsafe(self): regex = Url.regex(False) expect(regex).to_equal( "/?(?:(?P<debug>debug)/)?(?:(?P<meta>meta)/)?" "(?:(?P<trim>trim(?::(?:top-left|bottom-right))?(?::\\d+)?)/)?" "(?:(?P<crop_left>\\d+)x(?P<crop_top>\\d+):(?P<crop_right>\\d+)x(?P<crop_bottom>\\d+)/)?" "(?:(?P<adaptive>adaptive-)?(?P<full>full-)?(?P<fit_in>fit-in)/)?(?:(?P<horizontal_flip>-)?" "(?P<width>(?:\\d+|orig))?x(?P<vertical_flip>-)?(?P<height>(?:\\d+|orig))?/)?" "(?:(?P<halign>left|right|center)/)?(?:(?P<valign>top|bottom|middle)/)?" "(?:(?P<smart>smart)/)?(?:filters:(?P<filters>.+?\\))/)?(?P<image>.+)" )
def test_can_get_handlers(self): ctx = mock.Mock( config=mock.Mock( UPLOAD_ENABLED=False, USE_BLACKLIST=False, ) ) app = ThumborServiceApp(ctx) handlers = app.get_handlers() expect(handlers).to_length(2) expect(handlers[0][0]).to_equal(r'/healthcheck') expect(handlers[1][0]).to_equal(Url.regex())
def check_image(self, kw): result = re.match(Url.regex(), kw['request']) return super(CoreHandler, self).check_image(result.groupdict())