diff --git a/owslib/csw.py b/owslib/csw.py index 1fa7da638..8df0fba3d 100644 --- a/owslib/csw.py +++ b/owslib/csw.py @@ -68,7 +68,7 @@ def __init__(self, url, lang='en-US', version='2.0.2', timeout=10, skip_caps=Fal """ - self.url = url + self.url = util.clean_ows_url(url) self.lang = lang self.version = version self.timeout = timeout diff --git a/owslib/sos.py b/owslib/sos.py index 15a496d68..15989a64c 100644 --- a/owslib/sos.py +++ b/owslib/sos.py @@ -14,6 +14,7 @@ from __future__ import (absolute_import, division, print_function) from .swe.observation import sos100, sos200 +from .util import clean_ows_url def SensorObservationService(url, @@ -30,11 +31,14 @@ def SensorObservationService(url, :param password: password for the username :return: a version specific SensorObservationService object """ + + clean_url = clean_ows_url(url) + if version in ['1.0', '1.0.0']: return sos100.SensorObservationService_1_0_0.__new__( - sos100.SensorObservationService_1_0_0, url, version, + sos100.SensorObservationService_1_0_0, clean_url, version, xml, username, password) elif version in ['2.0', '2.0.0']: return sos200.SensorObservationService_2_0_0.__new__( - sos200.SensorObservationService_2_0_0, url, version, + sos200.SensorObservationService_2_0_0, clean_url, version, xml, username, password) diff --git a/owslib/util.py b/owslib/util.py index 4d5d988cc..d08d35709 100644 --- a/owslib/util.py +++ b/owslib/util.py @@ -15,11 +15,7 @@ import pytz from owslib.etree import etree, ParseError from owslib.namespaces import Namespaces -try: # Python 3 - from urllib.parse import urlsplit, urlencode -except ImportError: # Python 2 - from urlparse import urlsplit - from urllib import urlencode +from six.moves.urllib.parse import urlsplit, urlencode, urlparse, parse_qs, urlunparse try: from StringIO import StringIO # Python 2 @@ -573,6 +569,35 @@ def strip_bom(raw_text): return raw_text +def clean_ows_url(url): + """ + clean an OWS URL of basic service elements + + source: https://stackoverflow.com/a/11640565 + """ + + filtered_kvp = {} + basic_service_elements = ('service', 'version', 'request') + + parsed = urlparse(url) + qd = parse_qs(parsed.query, keep_blank_values=True) + + for key, value in qd.items(): + if key.lower() not in basic_service_elements: + filtered_kvp[key] = value + + newurl = urlunparse([ + parsed.scheme, + parsed.netloc, + parsed.path, + parsed.params, + urlencode(filtered_kvp, doseq=True), + parsed.fragment + ]) + + return newurl + + def bind_url(url): """binds an HTTP GET query string endpiont""" if url.find('?') == -1: # like http://host/wms diff --git a/owslib/wcs.py b/owslib/wcs.py index b67e77186..1be086338 100644 --- a/owslib/wcs.py +++ b/owslib/wcs.py @@ -17,7 +17,7 @@ from . import etree from .coverage import wcs100, wcs110, wcs111, wcsBase -from owslib.util import openURL +from owslib.util import clean_ows_url, openURL def WebCoverageService(url, version=None, xml=None, cookies=None, timeout=30): @@ -33,9 +33,11 @@ def WebCoverageService(url, version=None, xml=None, cookies=None, timeout=30): version = capabilities.get('version') del capabilities + clean_url = clean_ows_url(url) + if version == '1.0.0': - return wcs100.WebCoverageService_1_0_0.__new__(wcs100.WebCoverageService_1_0_0, url, xml, cookies) + return wcs100.WebCoverageService_1_0_0.__new__(wcs100.WebCoverageService_1_0_0, clean_url, xml, cookies) elif version == '1.1.0': - return wcs110.WebCoverageService_1_1_0.__new__(wcs110.WebCoverageService_1_1_0,url, xml, cookies) + return wcs110.WebCoverageService_1_1_0.__new__(wcs110.WebCoverageService_1_1_0, clean_url, xml, cookies) elif version == '1.1.1': - return wcs111.WebCoverageService_1_1_1.__new__(wcs111.WebCoverageService_1_1_1,url, xml, cookies) + return wcs111.WebCoverageService_1_1_1.__new__(wcs111.WebCoverageService_1_1_1, clean_url, xml, cookies) diff --git a/owslib/wfs.py b/owslib/wfs.py index 8550bbb2e..e27ca4396 100644 --- a/owslib/wfs.py +++ b/owslib/wfs.py @@ -16,6 +16,7 @@ from __future__ import (absolute_import, division, print_function) from .feature import wfs100, wfs110, wfs200 +from .util import clean_ows_url def WebFeatureService(url, version='1.0.0', xml=None, parse_remote_metadata=False, @@ -33,18 +34,21 @@ def WebFeatureService(url, version='1.0.0', xml=None, parse_remote_metadata=Fals @param password: service authentication password @return: initialized WebFeatureService_2_0_0 object ''' + + clean_url = clean_ows_url(url) + if version in ['1.0', '1.0.0']: - return wfs100.WebFeatureService_1_0_0(url, version, xml, parse_remote_metadata, + return wfs100.WebFeatureService_1_0_0(clean_url, version, xml, parse_remote_metadata, timeout=timeout, username=username, password=password) elif version in ['1.1', '1.1.0']: - return wfs110.WebFeatureService_1_1_0(url, version, xml, parse_remote_metadata, + return wfs110.WebFeatureService_1_1_0(clean_url, version, xml, parse_remote_metadata, timeout=timeout, username=username, password=password) elif version in ['2.0', '2.0.0']: - return wfs200.WebFeatureService_2_0_0(url, version, xml, parse_remote_metadata, + return wfs200.WebFeatureService_2_0_0(clean_url, version, xml, parse_remote_metadata, timeout=timeout, username=username, password=password) diff --git a/owslib/wms.py b/owslib/wms.py index 10c63940b..1697879e6 100644 --- a/owslib/wms.py +++ b/owslib/wms.py @@ -17,6 +17,7 @@ from __future__ import (absolute_import, division, print_function) from .map import wms111, wms130 +from .util import clean_ows_url def WebMapService(url, @@ -39,13 +40,16 @@ def WebMapService(url, @param timeout: time (in seconds) after which requests should timeout @return: initialized WebFeatureService_2_0_0 object ''' + + clean_url = clean_ows_url(url) + if version in ['1.1.1']: - return wms111.WebMapService_1_1_1(url, version=version, xml=xml, + return wms111.WebMapService_1_1_1(clean_url, version=version, xml=xml, parse_remote_metadata=parse_remote_metadata, username=username, password=password, timeout=timeout, headers=headers) elif version in ['1.3.0']: - return wms130.WebMapService_1_3_0(url, version=version, xml=xml, + return wms130.WebMapService_1_3_0(clean_url, version=version, xml=xml, parse_remote_metadata=parse_remote_metadata, username=username, password=password, timeout=timeout, headers=headers) diff --git a/owslib/wmts.py b/owslib/wmts.py index e672c54d4..ec7a5c2ef 100644 --- a/owslib/wmts.py +++ b/owslib/wmts.py @@ -42,7 +42,7 @@ from urllib import urlencode from urlparse import urlparse, urlunparse, parse_qs, ParseResult from .etree import etree -from .util import openURL, testXMLValue, getXMLInteger +from .util import clean_ows_url, openURL, testXMLValue, getXMLInteger from .fgdc import Metadata from .iso import MD_Metadata from .ows import ServiceProvider, ServiceIdentification, OperationsMetadata @@ -159,7 +159,7 @@ def __init__(self, url, version='1.0.0', xml=None, username=None, requests. """ - self.url = url + self.url = clean_ows_url(url) self.username = username self.password = password self.version = version diff --git a/owslib/wps.py b/owslib/wps.py index 849d340b0..ee4029ed0 100644 --- a/owslib/wps.py +++ b/owslib/wps.py @@ -89,7 +89,7 @@ from owslib.etree import etree from owslib.ows import DEFAULT_OWS_NAMESPACE, ServiceIdentification, ServiceProvider, OperationsMetadata, BoundingBox from time import sleep -from owslib.util import (testXMLValue, build_get_url, dump, getTypedValue, +from owslib.util import (testXMLValue, build_get_url, clean_ows_url, dump, getTypedValue, getNamespace, element_to_string, nspath, openURL, nspath_eval, log) from xml.dom.minidom import parseString from owslib.namespaces import Namespaces @@ -188,7 +188,7 @@ def __init__(self, url, version=WPS_DEFAULT_VERSION, username=None, password=Non """ # fields passed in from object initializer - self.url = url + self.url = clean_ows_url(url) self.username = username self.password = password self.version = version diff --git a/tests/doctests/util.txt b/tests/doctests/util.txt new file mode 100644 index 000000000..636086a24 --- /dev/null +++ b/tests/doctests/util.txt @@ -0,0 +1,24 @@ + +Imports + + >>> from __future__ import (absolute_import, division, print_function) + >>> from owslib.util import clean_ows_url + +Tests + + >>> clean_ows_url('http//example.org/wms') + 'http//example.org/wms' + >>> clean_ows_url('http//example.org/wms?service=WMS') + 'http//example.org/wms' + >>> clean_ows_url('http//example.org/wms?SERVICE=WMS') + 'http//example.org/wms' + >>> clean_ows_url('http//example.org/wms?SeRvIcE=WMS') + 'http//example.org/wms' + >>> clean_ows_url('http//example.org/wms?SeRvIcE=WMS&version=1.3.0&request=GetCapabilities') + 'http//example.org/wms' + >>> clean_ows_url('http//example.org/wms?foo=bar&SeRvIcE=WMS&version=1.3.0&request=GetCapabilities') + 'http//example.org/wms?foo=bar' + >>> clean_ows_url('http://example.org/wms?map=/path/to/foo.map&SERVICE=WMS&version=1.3.0&request=GetCapabilities') + 'http://example.org/wms?map=%2Fpath%2Fto%2Ffoo.map' + >>> clean_ows_url('http://example.org/wms?map=/path/to/foo.map&foo=bar&&SERVICE=WMS&version=1.3.0&request=GetCapabilities') + 'http://example.org/wms?map=%2Fpath%2Fto%2Ffoo.map&foo=bar'