// port = *DIGIT private static bool ParsePort(ParserState state) { string part = state.remaining; if (part.Length == 0 || part[0] != ':') { return(part.Length > 0); } StringBuilder sb = new StringBuilder(); int index; for (index = 1; index < part.Length; index++) { char ch = part[index]; if (!CharUtilities.IsDigit(ch)) { if (ch == '/' || ch == '#' || ch == '?') { break; } state.error = "Invalid URI: Invalid port specified."; return(false); } sb.Append(ch); } if (index <= part.Length) { state.remaining = part.Substring(index); } if (sb.Length == 0) { return(state.remaining.Length > 0); } int port; try { port = Int32.Parse(sb.ToString()); } catch (Exception) { state.error = "Invalid URI: Invalid port number"; return(false); } if (port < 0 || port > UInt16.MaxValue) { state.error = "Invalid URI: Invalid port number"; return(false); } state.elements.port = port; return(state.remaining.Length > 0); }
// 3.1) scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) private static bool ParseScheme(ParserState state) { string part = state.remaining; StringBuilder sb = new StringBuilder(); sb.Append(part[0]); int index; for (index = 1; index < part.Length; index++) { char ch = part[index]; if (ch != '.' && ch != '-' && ch != '+' && !IsAlpha(ch) && !CharUtilities.IsDigit(ch)) { break; } sb.Append(ch); } if (index == 0 || index >= part.Length) { if (state.kind == UriKind.Absolute) { state.error = "Invalid URI: The format of the URI could not be determined."; return(false); } state.elements.isAbsoluteUri = false; return(state.remaining.Length > 0); } if (part[index] != ':') { if (state.kind == UriKind.Absolute) { state.error = "Invalid URI: The URI scheme is not valid."; return(false); } state.elements.isAbsoluteUri = false; return(state.remaining.Length > 0); } state.elements.scheme = sb.ToString().ToLower(); state.remaining = part.Substring(index); // Check scheme name characters as specified in RFC2396. // Note: different checks in 1.x and 2.0 if (!Uri.CheckSchemeName(state.elements.scheme)) { if (state.kind == UriKind.Absolute) { state.error = "Invalid URI: The URI scheme is not valid."; return(false); } state.elements.isAbsoluteUri = false; return(state.remaining.Length > 0); } return(ParseDelimiter(state)); }