C# Class Microsoft.CodeAnalysis.Sarif.UriHelper

Mostra file Open project: Microsoft/sarif-sdk Class Usage Examples

Public Methods

Method Description
MakeValidUri ( string path ) : string

Create a syntactically valid URI from a path that might be absolute or relative, and that might require percent-encoding.

In general, path might be: 1. Possible to interpret as an absolute path / absolute URI 2. Possible to interpret as a relative path / relative URI 3. Neither We must create a valid URI to persist in the SARIF log. We proceed as follows: 1. Try to create an absolute System.Uri. If that succeeds, take its AbsoluteUri, which (unlike Uri.ToString()) will be properly percent-encoded. 2. Try to create a relative System.Uri. If that succeeds, we want to write it out, but since this is a relative URI, we can't access its AbsoluteUri or AbsolutePath property -- and again, Uri.ToString() does not perform percent encoding. We use this workaround: a. Combine the relative path with an arbitrary scheme and host to form an absolute URI. b. Extract the AbsolutePath property, which will be percent encoded. 3. If all else fails, we have a string that we can't convert to a System.Uri, so just percent encode the whole thing. This should be extremely rare in practice. Thanks and a tip o' the hat to @nguerrera for this code (and for the comment).

Private Methods

Method Description
GetLeadingDotSegments ( string path ) : string

Method Details

MakeValidUri() public static method

Create a syntactically valid URI from a path that might be absolute or relative, and that might require percent-encoding.
In general, path might be: 1. Possible to interpret as an absolute path / absolute URI 2. Possible to interpret as a relative path / relative URI 3. Neither We must create a valid URI to persist in the SARIF log. We proceed as follows: 1. Try to create an absolute System.Uri. If that succeeds, take its AbsoluteUri, which (unlike Uri.ToString()) will be properly percent-encoded. 2. Try to create a relative System.Uri. If that succeeds, we want to write it out, but since this is a relative URI, we can't access its AbsoluteUri or AbsolutePath property -- and again, Uri.ToString() does not perform percent encoding. We use this workaround: a. Combine the relative path with an arbitrary scheme and host to form an absolute URI. b. Extract the AbsolutePath property, which will be percent encoded. 3. If all else fails, we have a string that we can't convert to a System.Uri, so just percent encode the whole thing. This should be extremely rare in practice. Thanks and a tip o' the hat to @nguerrera for this code (and for the comment).
public static MakeValidUri ( string path ) : string
path string /// The path to be transformed into a syntactically valid URI. ///
return string