You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

URIParser.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.util;
  18. import java.net.URI;
  19. import java.net.URISyntaxException;
  20. import java.util.regex.Matcher;
  21. import java.util.regex.Pattern;
  22. import javax.inject.Inject;
  23. import javax.inject.Singleton;
  24. /**
  25. * Utility class for parsing IRC URIs from user input.
  26. */
  27. @Singleton
  28. public class URIParser {
  29. /**
  30. * Pattern used to breakdown a URI authority.
  31. */
  32. private final Pattern authorityBreakdown = Pattern.compile(
  33. "(?:(?<auth>[^@]*)@)?(?<host>[^:]+)(?::(?<secure>\\+)?(?<port>[0-9]*))?");
  34. /**
  35. * Pattern used to breakdown a URI. From RFC3986 appendix-B.
  36. */
  37. private final Pattern uriBreakdown = Pattern.compile(
  38. "((?<scheme>[^:/?#]+):)?(//(?<authority>[^/?#]*))?(?<path>[^?#]*)(\\?(?<query>[^#]*))?(#(?<fragment>.*))?");
  39. /**
  40. * Creates a new instance of {@link URIParser}.
  41. */
  42. @Inject
  43. public URIParser() {
  44. }
  45. /**
  46. * Parses the given string as a URI, allowing for a '+' symbol at the start of the port to
  47. * indicate a secure connection.
  48. *
  49. * @param input The string to be parsed.
  50. *
  51. * @return An equivalent URI, if one can be parsed.
  52. *
  53. * @throws InvalidURIException If the string cannot be parsed as a URI.
  54. */
  55. public URI parseFromURI(final String input) throws InvalidURIException {
  56. String scheme;
  57. final String userInfo;
  58. final String authority;
  59. final String host;
  60. final String portString;
  61. final int port;
  62. final String path;
  63. final String query;
  64. final String fragment;
  65. final Matcher uriMatcher = uriBreakdown.matcher(input);
  66. if (!uriMatcher.matches() || uriMatcher.group("scheme") == null || uriMatcher.group(
  67. "authority") == null) {
  68. throw new InvalidURIException("Invalid address specified");
  69. }
  70. scheme = uriMatcher.group("scheme");
  71. authority = uriMatcher.group("authority");
  72. path = uriMatcher.group("path");
  73. query = uriMatcher.group("query");
  74. fragment = uriMatcher.group("fragment");
  75. final Matcher authorityMatcher = authorityBreakdown.matcher(authority);
  76. if (!authorityMatcher.matches()) {
  77. throw new InvalidURIException("Invalid address specified");
  78. }
  79. userInfo = authorityMatcher.group("auth");
  80. host = authorityMatcher.group("host");
  81. if (authorityMatcher.group("secure") != null && scheme.charAt(scheme.length() - 1) != 's') {
  82. scheme += "s";
  83. }
  84. portString = authorityMatcher.group("port");
  85. if (portString != null) {
  86. try {
  87. port = Integer.parseInt(authorityMatcher.group(4));
  88. } catch (NumberFormatException ex) {
  89. throw new InvalidURIException("Invalid port specified", ex);
  90. }
  91. if (port <= 0 || port > 65535) {
  92. throw new InvalidURIException("Invalid port specified",
  93. new IllegalArgumentException("Port must be between 1 and 65535"));
  94. }
  95. } else {
  96. port = -1;
  97. }
  98. try {
  99. return new URI(scheme, userInfo, host, port, path, query, fragment);
  100. } catch (URISyntaxException ex) {
  101. throw new InvalidURIException("Invalid address specified", ex);
  102. }
  103. }
  104. /**
  105. * Parses the given string as a free-form address. This takes the form of a hostname and
  106. * optional port, followed by an optional password. If the input appears to contain a full URI
  107. * already, it is parsed by {@link #parseFromURI(java.lang.String)}.
  108. *
  109. * @param input The string to be parsed.
  110. *
  111. * @return An equivalent URI, if one can be parsed.
  112. *
  113. * @throws InvalidURIException If the string cannot be parsed as a URI, or an invalid component
  114. * is specified.
  115. */
  116. public URI parseFromText(final String input) throws InvalidURIException {
  117. if (input.indexOf(' ') == -1 && input.contains("://")) {
  118. // Looks like a full URI, parse it as such.
  119. return parseFromURI(input);
  120. }
  121. boolean ssl = false;
  122. final String host;
  123. String pass = null;
  124. int port = -1;
  125. final String[] parts = input.split(" ", 2);
  126. // Check for port
  127. if (parts[0].indexOf(':') > -1) {
  128. final String[] portParts = parts[0].split(":");
  129. if (portParts.length < 2) {
  130. throw new InvalidURIException("Invalid port specified");
  131. }
  132. host = portParts[0];
  133. if (!portParts[1].isEmpty() && portParts[1].charAt(0) == '+') {
  134. ssl = true;
  135. portParts[1] = portParts[1].substring(1);
  136. }
  137. try {
  138. port = Integer.parseInt(portParts[1]);
  139. } catch (NumberFormatException ex) {
  140. throw new InvalidURIException("Invalid port specified", ex);
  141. }
  142. if (port <= 0 || port > 65535) {
  143. throw new InvalidURIException("Invalid port specified",
  144. new IllegalArgumentException("Port must be between 1 and 65535"));
  145. }
  146. } else {
  147. host = parts[0];
  148. }
  149. // Check for password
  150. if (parts.length > 1) {
  151. pass = parts[1];
  152. }
  153. try {
  154. return new URI("irc" + (ssl ? "s" : ""), pass, host, port, null, null, null);
  155. } catch (URISyntaxException ex) {
  156. throw new InvalidURIException("Invalid address specified", ex);
  157. }
  158. }
  159. }