Exemplo n.º 1
0
def proxy_tms(request, origin=None, slug=None, z=None, x=None, y=None, u=None, ext=None):

    #starttime = time.clock()
    # Check Existing Tile Sourcesi
    match_tilesource = None
    tilesources = getTileSources(proxy=True)
    for tilesource in tilesources:
        if tilesource['name'] == slug:
            match_tilesource = tilesource
            break

    if match_tilesource:
        if match_tilesource['origin'] != origin:
            print "Origin is not correct.  Tilesource is unique, but origin need to match too."
            print tilesource['origin']
            return None
        else:
            tile = _requestTile(
                request,
                tileservice=None,
                tilesource=match_tilesource,
                tileorigin=match_tilesource['origin'],
                z=z,x=x,y=y,u=u,ext=ext)
            #print "Time Elapsed: "+str(time.clock()-starttime)
            return tile


    # Check Existing Tile Origins to see if we need to create a new tile source
    match_tileorigin = None
    if origin:
        tileorigins = getTileOrigins(proxy=True)
        for tileorigin in tileorigins:
            if tileorigin.name == origin:
                match_tileorigin = tileorigin
                break

    if match_tileorigin:
        to = match_tileorigin
        if to.multiple:
            ts_url = to.url.replace('{slug}', slug)
            if TileSource.objects.filter(url=ts_url).count() > 0:
                print "Error: This souldn't happen.  You should have matched the tilesource earlier so you don't duplicate"
                return None
            exts = string_to_list(to.extensions)
            ts_pattern = url_to_pattern(ts_url, extensions=exts)
            ts = TileSource(auto=True,url=ts_url,pattern=ts_pattern,name=slug,type=to.type,extensions=exts,origin=to)
            ts.save()
            reloadTileSources(proxy=False)
            reloadTileSources(proxy=True)
            return _requestTile(request,tileservice=None,tilesource=tilesource,z=z,x=x,y=y,u=u,ext=ext)
        else:
            ts = TileSource(auto=True,url=to.url,pattern=to.pattern,name=to.name,type=to.type,extensions=to.extensions)
            ts.save()
            reloadTileSources(proxy=False)
            reloadTileSources(proxy=True)
            return _requestTile(request,tileservice=None,tilesource=tilesource,z=z,x=x,y=y,u=u,ext=ext)
    else:
        return None
Exemplo n.º 2
0
def proxy(request):
    PROXY_ALLOWED_HOSTS = getattr(settings, 'PROXY_ALLOWED_HOSTS', ())

    host = None

    if 'url' not in request.GET:
        return HttpResponse("The proxy service requires a URL-encoded URL as a parameter.",
                            status=400,
                            content_type="text/plain"
                            )

    raw_url = request.GET['url']
    url = urlsplit(raw_url)
    locator = url.path
    if url.query != "":
        locator += '?' + url.query
    if url.fragment != "":
        locator += '#' + url.fragment

    if not settings.DEBUG:
        if not validate_host(url.hostname, PROXY_ALLOWED_HOSTS):
            return HttpResponse("DEBUG is set to False but the host of the path provided to the proxy service"
                                " is not in the PROXY_ALLOWED_HOSTS setting.",
                                status=403,
                                content_type="text/plain"
                                )
    headers = {}

    if settings.SESSION_COOKIE_NAME in request.COOKIES and is_safe_url(url=raw_url, host=host):
        headers["Cookie"] = request.META["HTTP_COOKIE"]

    if request.method in ("POST", "PUT") and "CONTENT_TYPE" in request.META:
        headers["Content-Type"] = request.META["CONTENT_TYPE"]

    print "Raw URL: "+ raw_url
    match_regex = None
    match_tilesource = None

    # Try to match against existing tile sources
    #tilesources = TileSource.objects.exclude(pattern__isnull=True).exclude(pattern__exact='')
    tilesources = getTileSources(proxy=True)
    for tilesource in tilesources:
        match = match_pattern_url(tilesource.pattern, raw_url)
        if match:
            match_regex = match
            match_tilesource = tilesource
            break

    if match_tilesource and match_regex:
        return proxy_tilesource(request, match_tilesource, match_regex)
    #else:
    #    return HttpResponse('No matching tilesource found.',RequestContext(request, {}), status=404)

    # Try to match against existing origins that can automatically create tile sources (auto=true)
    match_tileorigin = None
    #tileorigins = TileOrigin.objects.exclude(pattern__isnull=True).exclude(pattern__exact='').filter(auto=True)
    tileorigins = getTileOrigins(proxy=True)
    for tileorigin in tileorigins:
        match = match_pattern_url(tileorigin.pattern, raw_url)
        if match:
            match_regex = match
            match_tileorigin = tileorigin
            break

    if match_tileorigin and match_regex:
        to = match_tileorigin
        if to.multiple:
            slug = getRegexValue(match_regex, 'slug')
            ts_url = to.url.replace('{slug}', slug)
            #print "ts_url: "+ts_url
            if TileSource.objects.filter(url=ts_url).count() > 0:
                print "Error: This souldn't happen.  You should have matched the tilesource earlier so you don't duplicate"
                return None
            exts = string_to_list(to.extensions)
            ts_pattern = url_to_pattern(ts_url, extensions=exts)
            ts = TileSource(auto=True,url=ts_url,pattern=ts_pattern,name=slug,type=to.type,extensions=exts,origin=to)
            ts.save()
            reloadTileSources(proxy=False)
            reloadTileSources(proxy=True)
            return proxy_tilesource(request, ts, match_regex)
        else:
            ts = TileSource(auto=True,url=to.url,pattern=to.pattern,name=to.name,type=to.type,extensions=to.extensions)
            ts.save()
            reloadTileSources(proxy=False)
            reloadTileSources(proxy=True)
            return proxy_tilesource(request, ts, match_regex)
    else:
        return HttpResponse('No matching tile origin or tile source found.',RequestContext(request, {}), status=404)