@@ -766,7 +766,7 @@ internal static void ParseBase(RDFTurtleContext turtleContext, RDFGraph result)
766766 }
767767
768768 /// <summary>
769- /// Parses the Turtle data in order to detect a valid Uri
769+ /// Parses an IRIREF ("<...>"), resolving it against the current base Uri if relative
770770 /// </summary>
771771 internal static Uri ParseURI ( RDFTurtleContext turtleContext )
772772 {
@@ -783,13 +783,12 @@ internal static Uri ParseURI(RDFTurtleContext turtleContext)
783783 if ( bufChar == '>' )
784784 break ;
785785
786- switch ( bufChar )
787- {
788- case - 1 :
789- throw new RDFModelException ( "Unexpected end of Turtle file" + GetTurtleContextCoordinates ( turtleContext ) ) ;
790- case ' ' :
791- throw new RDFModelException ( "Uri included an unencoded space: '" + bufChar + "'" + GetTurtleContextCoordinates ( turtleContext ) ) ;
792- }
786+ if ( bufChar == - 1 )
787+ throw new RDFModelException ( "Unexpected end of Turtle file" + GetTurtleContextCoordinates ( turtleContext ) ) ;
788+
789+ //Control chars (space included) and '<"{}|^`' are illegal unescaped inside an IRIREF
790+ if ( bufChar <= 0x20 || bufChar == '<' || bufChar == '"' || bufChar == '{' || bufChar == '}' || bufChar == '|' || bufChar == '^' || bufChar == '`' )
791+ throw new RDFModelException ( "Uri included an illegal character: '" + char . ConvertFromUtf32 ( bufChar ) + "'" + GetTurtleContextCoordinates ( turtleContext ) ) ;
793792
794793 AppendCodePoint ( uriBuf , bufChar ) ;
795794
@@ -807,17 +806,52 @@ internal static Uri ParseURI(RDFTurtleContext turtleContext)
807806 }
808807
809808 string uriString = DecodeString ( turtleContext , uriBuf . ToString ( ) ) ;
810- //Absolute: use as found
811- if ( Uri . IsWellFormedUriString ( uriString , UriKind . Absolute ) )
812- return new Uri ( uriString ) ;
813- //Relative: append to base IRI
814- if ( Uri . IsWellFormedUriString ( uriString , UriKind . Relative ) )
815- return new Uri ( string . Concat ( turtleContext . Resolver . BaseUri , uriString ) ) ;
816- //PureFragment: append to base IRI
817- return uriString . Equals ( "#" )
818- ? new Uri ( string . Concat ( turtleContext . Resolver . BaseUri . TrimEnd ( '#' ) , uriString ) )
819- //Error: not well-formed, so throw exception
820- : throw new RDFModelException ( "Uri is not well-formed" + GetTurtleContextCoordinates ( turtleContext ) ) ;
809+
810+ //An IRI reference is absolute per RFC 3986 iff it opens with a genuine "scheme:" prefix
811+ if ( HasSchemePrefix ( uriString ) )
812+ {
813+ //Uri.TryCreate(..., UriKind.Absolute) alone is not enough here: it also accepts scheme-less
814+ //OS-style paths like "/x" or "//host/x" by silently assigning them an implicit "file" scheme
815+ if ( ! Uri . TryCreate ( uriString , UriKind . Absolute , out Uri absoluteUri ) )
816+ throw new RDFModelException ( "Uri is not well-formed" + GetTurtleContextCoordinates ( turtleContext ) ) ;
817+ return absoluteUri ;
818+ }
819+
820+ //No scheme: it is a relative reference, resolvable only against a base Uri
821+ if ( string . IsNullOrEmpty ( turtleContext . Resolver . BaseUri )
822+ || ! Uri . TryCreate ( turtleContext . Resolver . BaseUri , UriKind . Absolute , out Uri baseUri ) )
823+ throw new RDFModelException ( "Found relative Uri reference '" + uriString + "' with no usable base Uri to resolve it against" + GetTurtleContextCoordinates ( turtleContext ) ) ;
824+
825+ //The two-argument Uri resolution already implements RFC 3986 §5.3 (dot-segment removal, '#'/'?'/'/'/'//' references, ...)
826+ if ( ! Uri . TryCreate ( baseUri , uriString , out Uri resolvedUri ) )
827+ throw new RDFModelException ( "Uri is not well-formed" + GetTurtleContextCoordinates ( turtleContext ) ) ;
828+ return resolvedUri ;
829+
830+ #region Utilities
831+ //True iff the string opens with an RFC 3986 scheme ("ALPHA *(ALPHA / DIGIT / '+' / '-' / '.') ':'")
832+ bool HasSchemePrefix ( string candidateUriString )
833+ {
834+ int colonIndex = candidateUriString . IndexOf ( ':' ) ;
835+ if ( colonIndex <= 0 )
836+ return false ;
837+
838+ //First scheme character must be a letter
839+ char firstChar = candidateUriString [ 0 ] ;
840+ if ( ( firstChar < 'a' || firstChar > 'z' ) && ( firstChar < 'A' || firstChar > 'Z' ) )
841+ return false ;
842+
843+ //The rest may also contain digits, '+', '-', '.'
844+ for ( int i = 1 ; i < colonIndex ; i ++ )
845+ {
846+ char c = candidateUriString [ i ] ;
847+ bool isSchemeChar = ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) || ( c >= '0' && c <= '9' )
848+ || c == '+' || c == '-' || c == '.' ;
849+ if ( ! isSchemeChar )
850+ return false ;
851+ }
852+ return true ;
853+ }
854+ #endregion
821855 }
822856
823857 /// <summary>
0 commit comments