Example #1
0
def test_replace_caps_url_wms_130():
    doc = etree.parse(WMS_CAPS_NCWMS2_130_XML)
    xml = etree.tostring(doc)
    assert b'http://localhost:8080/ncWMS2/wms' in xml
    xml = utils.replace_caps_url(xml, "https://localhost/ows/proxy/wms")
    # assert 'http://localhost:8080/ncWMS2/wms' not in xml
    assert b'https://localhost/ows/proxy/wms' in xml
Example #2
0
def test_replace_caps_url_wps():
    doc = etree.parse(WPS_CAPS_EMU_XML)
    xml = etree.tostring(doc)
    assert b'http://localhost:8094/wps' in xml
    xml = utils.replace_caps_url(xml, "https://localhost/ows/proxy/emu")
    assert b'http://localhost:8094/wps' not in xml
    assert b'https://localhost/ows/proxy/emu' in xml
Example #3
0
def test_replace_caps_url_wms_130():
    doc = etree.parse(WMS_CAPS_NCWMS2_130_XML)
    xml = etree.tostring(doc)
    assert 'http://localhost:8080/ncWMS2/wms' in xml
    xml = utils.replace_caps_url(xml, "https://localhost/ows/proxy/wms")
    # assert 'http://localhost:8080/ncWMS2/wms' not in xml
    assert 'https://localhost/ows/proxy/wms' in xml
Example #4
0
def test_replace_caps_url_wps():
    doc = etree.parse(WPS_CAPS_EMU_XML)
    xml = etree.tostring(doc)
    assert 'http://localhost:8094/wps' in xml
    xml = utils.replace_caps_url(xml, "https://localhost/ows/proxy/emu")
    assert 'http://localhost:8094/wps' not in xml
    assert 'https://localhost/ows/proxy/emu' in xml
Example #5
0
    if "Content-Type" in resp.headers:
        ct = resp.headers["Content-Type"]
        if not ct.split(";")[0] in allowed_content_types:
            msg = "Content type is not allowed: %s." % (ct)
            logger.error(msg)
            return OWSAccessForbidden(msg)
    else:
        # return HTTPNotAcceptable("Could not get content type from response.")
        logger.warn("Could not get content type from response")

    try:
        if ct in ['text/xml', 'application/xml', 'text/xml;charset=ISO-8859-1']:
                # replace urls in xml content
                proxy_url = request.route_url('owsproxy', service_name=service['name'])
                # TODO: where do i need to replace urls?
                content = replace_caps_url(resp.content, proxy_url, service.get('url'))
        else:
            # raw content
            content = resp.content
    except:
        return HTTPNotAcceptable("Could not decode content.")

    headers = {}
    if ct:
        headers["Content-Type"] = ct
    return Response(content, status=resp.status_code, headers=headers)


def owsproxy_url(request):
    url = request.params.get("url")
    if url is None:
Example #6
0
def _send_request(request, service, extra_path=None, request_params=None):

    # TODO: fix way to build url
    url = service['url']
    if extra_path:
        url += '/' + extra_path
    if request_params:
        url += '?' + request_params
    LOGGER.debug('url = %s', url)

    # forward request to target (without Host Header)
    h = dict(request.headers)
    h.pop("Host", h)
    h['Accept-Encoding'] = None
    #
    service_type = service['type']
    if service_type and (service_type.lower() != 'wps'):
        try:
            resp_iter = requests.request(method=request.method.upper(),
                                         url=url,
                                         data=request.body,
                                         headers=h,
                                         stream=True,
                                         verify=service.verify)
        except Exception as e:
            return OWSAccessFailed("Request failed: {}".format(e))

        # Headers meaningful only for a single transport-level connection
        HopbyHop = [
            'Connection', 'Keep-Alive', 'Public', 'Proxy-Authenticate',
            'Transfer-Encoding', 'Upgrade'
        ]
        return Response(app_iter=BufferedResponse(resp_iter),
                        headers={
                            k: v
                            for k, v in list(resp_iter.headers.items())
                            if k not in HopbyHop
                        })
    else:
        try:
            resp = requests.request(method=request.method.upper(),
                                    url=url,
                                    data=request.body,
                                    headers=h,
                                    verify=service.verify)
        except Exception as e:
            return OWSAccessFailed("Request failed: {}".format(e))

        if resp.ok is False:
            if 'ExceptionReport' in resp.text:
                pass
            else:
                return OWSAccessFailed("Response is not ok: {}".format(
                    resp.reason))

        # check for allowed content types
        ct = None
        # LOGGER.debug("headers=", resp.headers)
        if "Content-Type" in resp.headers:
            ct = resp.headers["Content-Type"]
            if not ct.split(";")[0] in allowed_content_types:
                msg = "Content type is not allowed: {}.".format(ct)
                LOGGER.error(msg)
                return OWSAccessForbidden(msg)
        else:
            # return OWSAccessFailed("Could not get content type from response.")
            LOGGER.warn("Could not get content type from response")

        try:
            if ct in [
                    'text/xml', 'application/xml',
                    'text/xml;charset=ISO-8859-1'
            ]:
                # replace urls in xml content
                # ... if public URL is not configured use proxy url.
                if service.has_purl():
                    public_url = service.get('purl')
                else:
                    public_url = request.route_url(
                        'owsproxy', service_name=service['name'])
                # TODO: where do i need to replace urls?
                content = replace_caps_url(resp.content, public_url,
                                           service.get('url'))
            else:
                # raw content
                content = resp.content
        except Exception:
            return OWSAccessFailed("Could not decode content.")

        headers = {}
        if ct:
            headers["Content-Type"] = ct
        return Response(content, status=resp.status_code, headers=headers)