Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hitting limitation of Uri.TryCreate #70

Open
apolloLegends opened this issue Aug 25, 2020 · 2 comments
Open

Hitting limitation of Uri.TryCreate #70

apolloLegends opened this issue Aug 25, 2020 · 2 comments

Comments

@apolloLegends
Copy link

Hi,

I have ran into an interesting problem when querying an ArcGIS server with a complex geometry...

                Query query = new Query(service.AsEndpoint())
                {
                    SpatialRelationship = SpatialRelationshipTypes.Intersects,
                    Geometry = parcel.Geometry,
                    ReturnGeometry = true,
                    OutputSpatialReference = new Anywhere.ArcGIS.Common.SpatialReference { Wkid = wkid },
                };

In my case the text representation of this query is 85,249 characters but fails not due to a server issue but Uri.TryCreate reporting its too long to be valid at line 706 of PortalGatewayBase.cs. After some quick testing the limit seems to be 65,519 characters.

To workaround this problem i have simply switched some code around, from this

            **bool validUrl = Uri.TryCreate(url, UriKind.Absolute, out Uri uri);**
            if (!validUrl)
            {
                throw new HttpRequestException(string.Format("Not a valid url: {0}", url));
            }

            if (url.Length > MaximumGetRequestLength)
            {                
                _logger.DebugFormat("Url length {0} is greater than maximum configured {1}, switching to POST.", url.Length, MaximumGetRequestLength);
                return await Post<T, TRequest>(requestObject, ct).ConfigureAwait(false);
            }

To

            if (url.Length > MaximumGetRequestLength)
            {                
                _logger.DebugFormat("Url length {0} is greater than maximum configured {1}, switching to POST.", url.Length, MaximumGetRequestLength);
                return await Post<T, TRequest>(requestObject, ct).ConfigureAwait(false);
            }

            **bool validUrl = Uri.TryCreate(url, UriKind.Absolute, out Uri uri);**
            if (!validUrl)
            {
                throw new HttpRequestException(string.Format("Not a valid url: {0}", url));
            }

I haven't found a way to increase the maximum length the .NET framework will support. I'm not if you'd even want to drop the URI validation. I will submit a pull request if you approve of this change.

Cheers.

@ryanwilliams83
Copy link

I just ran into a similar problem when submitting a large whereClause in a Query operation.

PortalGatewayBase.Query()
calls PortalGatewayBase.Get<>()
calls PortalGatewayBase.AsRequestQueryString() to build the URL
https://github.com/davetimmins/Anywhere.ArcGIS/blob/master/src/Anywhere.ArcGIS/PortalGatewayBase.cs#L709

calls StringExtensions.UrlCode() which doesn't support data values longer than 65520 characters https://github.com/davetimmins/Anywhere.ArcGIS/blob/master/src/Anywhere.ArcGIS/Extensions/StringExtensions.cs#L63

causing Uri.TryCreate() to fail due to the invalid url (containing unescaped data strings).
https://github.com/davetimmins/Anywhere.ArcGIS/blob/master/src/Anywhere.ArcGIS/PortalGatewayBase.cs#L733

resulting in error Not a valid url
https://github.com/davetimmins/Anywhere.ArcGIS/blob/master/src/Anywhere.ArcGIS/PortalGatewayBase.cs#L736

right before the fallback to POST
https://github.com/davetimmins/Anywhere.ArcGIS/blob/master/src/Anywhere.ArcGIS/PortalGatewayBase.cs#L739

@davetimmins
Copy link
Owner

@apolloLegends yes I'd be happy for you to submit a pr with that change, cheers, Dave

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants